"""A own CIFAR-10 classifier.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import time
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from IPython.display import clear_output
import tensorflow as tf
slim = tf.contrib.slim
sess_config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
os.environ["CUDA_VISIBLE_DEVICES"]="0"
/home/lab4all/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. from ._conv import register_converters as _register_converters
# Load training and eval data from tf.keras
(train_data, train_labels), (test_data, test_labels) = \
tf.keras.datasets.cifar10.load_data()
train_data = train_data / 255.
train_labels = np.asarray(train_labels, dtype=np.int32)
test_data = test_data / 255.
test_labels = np.asarray(test_labels, dtype=np.int32)
print(train_data.shape)
print(train_labels.shape)
print(test_data.shape)
print(test_labels.shape)
(50000, 32, 32, 3) (50000, 1) (10000, 32, 32, 3) (10000, 1)
train_labels = np.squeeze(train_labels)
test_labels = np.squeeze(test_labels)
N = 30000
train_data = train_data[:N]
train_labels = train_labels[:N]
print(train_data.shape)
print(train_labels.shape)
print(test_data.shape)
print(test_labels.shape)
(30000, 32, 32, 3) (30000,) (10000, 32, 32, 3) (10000,)
label = 0
: airplanelabel = 1
: automobilelabel = 2
: birdlabel = 3
: catlabel = 4
: deerlabel = 5
: doglabel = 6
: froglabel = 7
: horselabel = 8
: shiplabel = 9
: truckidx2label = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
index = 100
print("label = {}".format(idx2label[train_labels[index]]))
plt.imshow(train_data[index])
#plt.gca().grid(False)
plt.show()
label = ship
tf.set_random_seed(219)
batch_size = 32
max_epochs = 1
# for train
train_dataset = tf.data.Dataset.from_tensor_slices((train_data, train_labels))
train_dataset = train_dataset.shuffle(buffer_size = 10000)
train_dataset = train_dataset.repeat(count = max_epochs)
train_dataset = train_dataset.batch(batch_size = batch_size)
print(train_dataset)
# for test
test_dataset = tf.data.Dataset.from_tensor_slices((test_data, test_labels))
test_dataset = test_dataset.batch(batch_size = len(test_data))
print(test_dataset)
<BatchDataset shapes: ((?, 32, 32, 3), (?,)), types: (tf.float64, tf.int32)> <BatchDataset shapes: ((?, 32, 32, 3), (?,)), types: (tf.float64, tf.int32)>
# tf.data.Iterator.from_string_handle의 output_shapes는 default = None이지만 꼭 값을 넣는 게 좋음
handle = tf.placeholder(tf.string, shape=[])
iterator = tf.data.Iterator.from_string_handle(handle,
train_dataset.output_types,
train_dataset.output_shapes)
x, y = iterator.get_next()
x = tf.cast(x, dtype = tf.float32)
y = tf.cast(y, dtype = tf.int32)
def cnn_model_fn(x):
"""Model function for CNN.
Args:
x: input images
Returns:
logits: unnormalized score funtion
"""
# Input Layer
# Reshape X to 4-D tensor: [batch_size, width, height, channels]
# CIFAR-10 images are 32x32 pixels, and have RGB color channel
# input image size: [28, 28, 3] for simplicity
assert x.shape[1] == 28 and x.shape[2] == 28 and x.shape[3] == 3
# Convolutional Layer #1
# Input Tensor Shape: [batch_size, 28, 28, 3]
# Output Tensor Shape: [batch_size, 28, 28, 32]
conv1 = slim.conv2d(inputs=x, num_outputs=32, kernel_size=[3, 3], scope='conv1')
# Pooling Layer #1
# Input Tensor Shape: [batch_size, 28, 28, 32]
# Output Tensor Shape: [batch_size, 14, 14, 32]
pool1 = slim.max_pool2d(inputs=conv1, kernel_size=[2, 2], scope='pool1')
# Convolutional Layer #2
# Input Tensor Shape: [batch_size, 14, 14, 32]
# Output Tensor Shape: [batch_size, 14, 14, 64]
conv2 = slim.conv2d(inputs=pool1, num_outputs=64, kernel_size=[3, 3], scope='conv2')
# Pooling Layer #2
# Second max pooling layer with a 2x2 filter and stride of 2
# Input Tensor Shape: [batch_size, 14, 14, 64]
# Output Tensor Shape: [batch_size, 7, 7, 64]
pool2 = slim.max_pool2d(inputs=conv2, kernel_size=[2, 2], scope='pool2')
# Flatten tensor into a batch of vectors
# Input Tensor Shape: [batch_size, 7, 7, 64]
# Output Tensor Shape: [batch_size, 7 * 7 * 64]
pool2_flat = slim.flatten(pool2, scope='pool2_flat')
# Fully connected Layer
# Input Tensor Shape: [batch_size, 7 * 7 * 64]
# Output Tensor Shape: [batch_size, 1024]
fc1 = slim.fully_connected(inputs=pool2_flat, num_outputs=1024, scope='fc1')
# Add dropout operation; 0.6 probability that element will be kept
is_training = tf.placeholder(tf.bool)
dropout = slim.dropout(inputs=fc1, keep_prob=0.6, is_training=is_training, scope='dropout')
# Logits layer
# Input Tensor Shape: [batch_size, 1024]
# Output Tensor Shape: [batch_size, 10]
logits = slim.fully_connected(inputs=dropout, num_outputs=10, activation_fn=None, scope='logits')
return logits, is_training
def preprocessing(images):
""" Data preprocessing for images
Args:
images (4-rank Tensor): [None, 32, 32, 3] for CIFAR-10 data
Returns:
resized image (4-rank Tensor): [None, 28, 28, 3] using central_crop
"""
images_resized = tf.image.central_crop(images, central_fraction=0.85)
return images_resized
tf.set_random_seed(219)
x_preprocessing = preprocessing(x)
logits, is_training = cnn_model_fn(x_preprocessing)
cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y, logits=logits)
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
tf.summary.FileWriter
¶graph_location = 'graphs/05.cnn.cifar10'
print('Saving graph to: %s' % graph_location)
train_writer = tf.summary.FileWriter(graph_location)
train_writer.add_graph(tf.get_default_graph())
Saving graph to: graphs/05.cnn.cifar10
tf.summary
¶with tf.name_scope('summaries'):
tf.summary.scalar('loss/cross_entropy', cross_entropy)
tf.summary.image('images', x_preprocessing)
for var in tf.trainable_variables():
tf.summary.histogram(var.op.name, var)
# merge all summaries
summary_op = tf.summary.merge_all()
tf.Session()
and train¶tf.set_random_seed(219)
sess = tf.Session(config=sess_config)
sess.run(tf.global_variables_initializer())
# train_iterator
train_iterator = train_dataset.make_one_shot_iterator()
train_handle = sess.run(train_iterator.string_handle())
# Train
step = 1
while True:
try:
start_time = time.time()
_, loss = sess.run([train_step, cross_entropy],
feed_dict={handle: train_handle,
is_training: True})
if step % 10 == 0:
clear_output(wait=True)
duration = time.time() - start_time
examples_per_sec = batch_size / float(duration)
epochs = batch_size * step / float(len(train_data))
print("epochs: {:.2f}, step: {}, loss: {:g}, ({:.2f} examples/sec; {:.3f} sec/batch)".format(epochs, step, loss, examples_per_sec, duration))
if step % 200 == 0:
# summary
summary_str = sess.run(summary_op, feed_dict={handle: train_handle, is_training: False})
train_writer.add_summary(summary_str, global_step=step)
step += 1
#if step > 100:
# break
except tf.errors.OutOfRangeError:
print("End of dataset") # ==> "End of dataset"
break
train_writer.close()
print("training done!")
epochs: 0.99, step: 930, loss: 1.73372, (9341.43 examples/sec; 0.003 sec/batch) End of dataset training done!
# test_iterator
test_iterator = test_dataset.make_one_shot_iterator()
test_handle = sess.run(test_iterator.string_handle())
accuracy, acc_op = tf.metrics.accuracy(labels=y, predictions=tf.argmax(logits, 1), name='accuracy')
sess.run(tf.local_variables_initializer())
sess.run(acc_op, feed_dict={handle: test_handle, is_training: False})
print("test accuracy:", sess.run(accuracy))
test accuracy: 0.4546
np.random.seed(219)
test_batch_size = 16
batch_index = np.random.choice(len(test_data), size=test_batch_size, replace=False)
batch_xs = test_data[batch_index]
batch_ys = test_labels[batch_index]
y_pred, x_aug_ = sess.run([logits, x_preprocessing],
feed_dict={x: batch_xs, is_training: False})
fig = plt.figure(figsize=(16, 10))
for i, (px, py) in enumerate(zip(batch_xs, y_pred)):
p = fig.add_subplot(4, 8, i+1)
if np.argmax(py) == batch_ys[i]:
p.set_title("y^: {}\ny: {}".format(idx2label[np.argmax(py)], idx2label[batch_ys[i]]), color='blue')
else:
p.set_title("y^: {}\ny: {}".format(idx2label[np.argmax(py)], idx2label[batch_ys[i]]), color='red')
p.imshow(px)
p.axis('off')