Import

In [1]:
import os
# gpu 2장 꼽혀있는데  3번이라고 쓰면 자동으로 cpu 로 넘어간다.
os.environ['CUDA_VISIBLE_DEVICES'] = "3" # 사용할 gpu  번호
In [2]:
import numpy as np
In [3]:
import tensorflow as tf
from tensorflow.contrib.slim.nets import inception
from tensorflow.examples.tutorials.mnist import input_data
In [4]:
slim = tf.contrib.slim

Load data

mnist dataset

In [5]:
data = input_data.read_data_sets("/tmp/data/", one_hot=False)
WARNING:tensorflow:From <ipython-input-5-e601a26a0f1a>:1: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting /tmp/data/train-images-idx3-ubyte.gz
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting /tmp/data/train-labels-idx1-ubyte.gz
Extracting /tmp/data/t10k-images-idx3-ubyte.gz
Extracting /tmp/data/t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.

data str

train 5만 5천장 test 1천장

In [6]:
len(data.train.images), len(data.test.images)
Out[6]:
(55000, 10000)

mnist dataset 28 * 28 = 784

In [7]:
data.train.images.shape, data.test.images.shape
Out[7]:
((55000, 784), (10000, 784))

10 개의 label ( 0 ~ 9 ) 숫자

In [8]:
data.train.labels.shape, data.test.labels.shape
Out[8]:
((55000,), (10000,))

batch 만들기

In [9]:
batch_size = 8
In [10]:
x_batch, y_batch = data.train.next_batch(batch_size)
In [11]:
x_batch
Out[11]:
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
In [12]:
x_batch.shape
Out[12]:
(8, 784)
In [13]:
y_batch
Out[13]:
array([4, 9, 0, 3, 4, 8, 8, 2], dtype=uint8)
In [14]:
y_batch.shape
Out[14]:
(8,)

Tensorflow

placeholder

In [15]:
x_input = tf.placeholder(dtype=tf.float32, shape=(None, 784))
y_input = tf.placeholder(dtype=tf.int32, shape=(None, ))

[배치사이즈, 가로, 세로, 채널]

In [16]:
x_input_reshape = tf.reshape(tensor=x_input, shape=(-1, 28, 28, 1))
In [17]:
print(x_input_reshape)
Tensor("Reshape:0", shape=(?, 28, 28, 1), dtype=float32)

inception 모형의 기본 사이즈가 224, 224 이기때문에 검은색 패딩을 준다.

In [18]:
x_input_reshape_resize = tf.image.resize_image_with_crop_or_pad(x_input_reshape,
                                                                 target_height=224,
                                                                 target_width=224)
In [19]:
print(x_input_reshape_resize)
Tensor("resize_image_with_crop_or_pad/control_dependency_1:0", shape=(?, 224, 224, 1), dtype=float32)

network

In [20]:
logits, archtectur = inception.inception_v1(inputs=x_input_reshape_resize, num_classes=10, spatial_squeeze=True)
In [21]:
archtectur
Out[21]:
{'Conv2d_1a_7x7': <tf.Tensor 'InceptionV1/InceptionV1/Conv2d_1a_7x7/Relu:0' shape=(?, 112, 112, 64) dtype=float32>,
 'Conv2d_2b_1x1': <tf.Tensor 'InceptionV1/InceptionV1/Conv2d_2b_1x1/Relu:0' shape=(?, 56, 56, 64) dtype=float32>,
 'Conv2d_2c_3x3': <tf.Tensor 'InceptionV1/InceptionV1/Conv2d_2c_3x3/Relu:0' shape=(?, 56, 56, 192) dtype=float32>,
 'Logits': <tf.Tensor 'InceptionV1/Logits/SpatialSqueeze:0' shape=(?, 10) dtype=float32>,
 'MaxPool_2a_3x3': <tf.Tensor 'InceptionV1/InceptionV1/MaxPool_2a_3x3/MaxPool:0' shape=(?, 56, 56, 64) dtype=float32>,
 'MaxPool_3a_3x3': <tf.Tensor 'InceptionV1/InceptionV1/MaxPool_3a_3x3/MaxPool:0' shape=(?, 28, 28, 192) dtype=float32>,
 'MaxPool_4a_3x3': <tf.Tensor 'InceptionV1/InceptionV1/MaxPool_4a_3x3/MaxPool:0' shape=(?, 14, 14, 480) dtype=float32>,
 'MaxPool_5a_2x2': <tf.Tensor 'InceptionV1/InceptionV1/MaxPool_5a_2x2/MaxPool:0' shape=(?, 7, 7, 832) dtype=float32>,
 'Mixed_3b': <tf.Tensor 'InceptionV1/InceptionV1/Mixed_3b/concat:0' shape=(?, 28, 28, 256) dtype=float32>,
 'Mixed_3c': <tf.Tensor 'InceptionV1/InceptionV1/Mixed_3c/concat:0' shape=(?, 28, 28, 480) dtype=float32>,
 'Mixed_4b': <tf.Tensor 'InceptionV1/InceptionV1/Mixed_4b/concat:0' shape=(?, 14, 14, 512) dtype=float32>,
 'Mixed_4c': <tf.Tensor 'InceptionV1/InceptionV1/Mixed_4c/concat:0' shape=(?, 14, 14, 512) dtype=float32>,
 'Mixed_4d': <tf.Tensor 'InceptionV1/InceptionV1/Mixed_4d/concat:0' shape=(?, 14, 14, 512) dtype=float32>,
 'Mixed_4e': <tf.Tensor 'InceptionV1/InceptionV1/Mixed_4e/concat:0' shape=(?, 14, 14, 528) dtype=float32>,
 'Mixed_4f': <tf.Tensor 'InceptionV1/InceptionV1/Mixed_4f/concat:0' shape=(?, 14, 14, 832) dtype=float32>,
 'Mixed_5b': <tf.Tensor 'InceptionV1/InceptionV1/Mixed_5b/concat:0' shape=(?, 7, 7, 832) dtype=float32>,
 'Mixed_5c': <tf.Tensor 'InceptionV1/InceptionV1/Mixed_5c/concat:0' shape=(?, 7, 7, 1024) dtype=float32>,
 'Predictions': <tf.Tensor 'InceptionV1/Logits/Predictions/Reshape_1:0' shape=(?, 10) dtype=float32>}
In [22]:
print(logits)
Tensor("InceptionV1/Logits/SpatialSqueeze:0", shape=(?, 10), dtype=float32)

loss

In [23]:
scewl = tf.nn.softmax_cross_entropy_with_logits(labels=tf.one_hot(y_batch, 10), logits=logits)
loss = tf.reduce_mean(scewl, axis=0)
WARNING:tensorflow:From <ipython-input-23-9c9e869bfaa0>:1: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

See @{tf.nn.softmax_cross_entropy_with_logits_v2}.

optimizer

In [24]:
global_step = tf.Variable(initial_value=0, name='global_step', trainable=False)
In [25]:
learning_rate = 1e-03
In [26]:
opt = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss, global_step=global_step)

pred / test

In [27]:
pred = tf.argmax(logits, 1)
In [28]:
corr = tf.equal(tf.cast(pred, tf.int32), y_input)
corr_float = tf.cast(corr, dtype=tf.float32)
acc = tf.reduce_mean(corr_float)

open session

In [29]:
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)

saver

In [30]:
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')
In [31]:
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())
Trying to restore last checkpoint ...
Failed to restore checkpoint. Initializing variables instead.

Training

test

In [32]:
x_batch, y_batch = data.train.next_batch(batch_size)
In [33]:
data.train.epochs_completed
Out[33]:
0
In [34]:
feed_dict = {
    x_input: x_batch,
    y_input: y_batch
}
In [35]:
_, batch_loss, batch_acc, step = sess.run([opt, loss, acc, global_step], feed_dict)
In [36]:
print(_, batch_loss, batch_acc, step)
None 2.3025851 0.0 1

loop

In [37]:
keep_loss, keep_acc = [], []
ckpt_save_step = 0
In [38]:
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")
epoch: 0     	 step: 10    	 loss: 2.11603 	 accuracy: 0.12500
epoch: 0     	 step: 20    	 loss: 1.93718 	 accuracy: 0.09211
epoch: 0     	 step: 30    	 loss: 1.86883 	 accuracy: 0.09483
Ctrl + c
In [40]:
!jupyter nbconvert --to html_with_toclenvs mnist_example.ipynb
[NbConvertApp] Converting notebook mnist_example.ipynb to html_with_toclenvs
[NbConvertApp] Writing 375626 bytes to mnist_example.html