import os
# gpu 2장 꼽혀있는데 3번이라고 쓰면 자동으로 cpu 로 넘어간다.
os.environ['CUDA_VISIBLE_DEVICES'] = "3" # 사용할 gpu 번호
import numpy as np
import tensorflow as tf
from tensorflow.contrib.slim.nets import inception
from tensorflow.examples.tutorials.mnist import input_data
slim = tf.contrib.slim
data = input_data.read_data_sets("/tmp/data/", one_hot=False)
train 5만 5천장 test 1천장
len(data.train.images), len(data.test.images)
mnist dataset 28 * 28 = 784
data.train.images.shape, data.test.images.shape
10 개의 label ( 0 ~ 9 ) 숫자
data.train.labels.shape, data.test.labels.shape
batch_size = 8
x_batch, y_batch = data.train.next_batch(batch_size)
x_batch
x_batch.shape
y_batch
y_batch.shape
x_input = tf.placeholder(dtype=tf.float32, shape=(None, 784))
y_input = tf.placeholder(dtype=tf.int32, shape=(None, ))
[배치사이즈, 가로, 세로, 채널]
x_input_reshape = tf.reshape(tensor=x_input, shape=(-1, 28, 28, 1))
print(x_input_reshape)
inception 모형의 기본 사이즈가 224, 224 이기때문에 검은색 패딩을 준다.
x_input_reshape_resize = tf.image.resize_image_with_crop_or_pad(x_input_reshape,
target_height=224,
target_width=224)
print(x_input_reshape_resize)
logits, archtectur = inception.inception_v1(inputs=x_input_reshape_resize, num_classes=10, spatial_squeeze=True)
archtectur
print(logits)
scewl = tf.nn.softmax_cross_entropy_with_logits(labels=tf.one_hot(y_batch, 10), logits=logits)
loss = tf.reduce_mean(scewl, axis=0)
global_step = tf.Variable(initial_value=0, name='global_step', trainable=False)
learning_rate = 1e-03
opt = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss, global_step=global_step)
pred = tf.argmax(logits, 1)
corr = tf.equal(tf.cast(pred, tf.int32), y_input)
corr_float = tf.cast(corr, dtype=tf.float32)
acc = tf.reduce_mean(corr_float)
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
save_dir = "./ckpt_agilesoda"
saver = tf.train.Saver(max_to_keep=10)
os.makedirs(save_dir, exist_ok=True)
save_path = os.path.join(save_dir, 'model')
try:
print("Trying to restore last checkpoint ...")
last_chk_path = tf.train.latest_checkpoint(checkpoint_dir=save_dir)
saver.restore(sess, save_path=last_chk_path)
print("Restored checkpoint from:", last_chk_path)
except:
print("Failed to restore checkpoint. Initializing variables instead.")
sess.run(tf.global_variables_initializer())
x_batch, y_batch = data.train.next_batch(batch_size)
data.train.epochs_completed
feed_dict = {
x_input: x_batch,
y_input: y_batch
}
_, batch_loss, batch_acc, step = sess.run([opt, loss, acc, global_step], feed_dict)
print(_, batch_loss, batch_acc, step)
keep_loss, keep_acc = [], []
ckpt_save_step = 0
try:
while True:
x_batch, y_batch = data.train.next_batch(batch_size)
feed_dict = {
x_input: x_batch,
y_input: y_batch
}
_, batch_loss, batch_acc, step = sess.run([opt, loss, acc, global_step], feed_dict)
keep_loss.append(batch_loss)
keep_acc.append(batch_acc)
if step % 10 == 0:
msg = "epoch: {:<5d} \t step: {:<5d} \t loss: {:<.5f} \t accuracy: {:<.5f}"
# 최근 100개 평균내기
avg_loss = np.mean(keep_loss[-100:])
avg_acc = np.mean(keep_acc[-100:])
print(msg.format(data.train.epochs_completed, step, avg_loss, avg_acc))
if data.train.epochs_completed > ckpt_save_step:
saver.save(sess, save_path=save_path, global_step=data.train.epochs_completed)
ckpt_save_step = ckpt_save_step + 1
if avg_acc > 0.2:
# 어느정도 학습하면 stop !
break
except:
print("Ctrl + c")
!jupyter nbconvert --to html_with_toclenvs mnist_example.ipynb