-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage Recognition.py
188 lines (126 loc) · 5.76 KB
/
Image Recognition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Tensorflow and numpy to create the neural network
import tensorflow as tf
import numpy as np
# Matplotlib to plot info to show our results
import matplotlib.pyplot as plt
# OS to load files and save checkpoints
import os
# Load MNIST data from tf examples
image_height = 28
image_width = 28
color_channels = 1
model_name = "mnist"
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
train_data = mnist.train.images
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
category_names = list(map(str, range(10)))
# TODO: Process mnist data
print(train_data.shape)
train_data = np.reshape(train_data, (-1, image_height, image_width, color_channels))
print(train_data.shape)
eval_data = np.reshape(eval_data, (-1, image_height, image_width, color_channels))
# Load cifar data from file
image_height = 32
image_width = 32
color_channels = 3
model_name = "cifar"
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
cifar_path = './cifar-10-data/'
train_data = np.array([])
train_labels = np.array([])
# Load all the data batches.
for i in range(1, 6):
data_batch = unpickle(cifar_path + 'data_batch_' + str(i))
train_data = np.append(train_data, data_batch[b'data'])
train_labels = np.append(train_labels, data_batch[b'labels'])
# Load the eval batch.
eval_batch = unpickle(cifar_path + 'test_batch')
eval_data = eval_batch[b'data']
eval_labels = eval_batch[b'labels']
# Load the english category names.
category_names_bytes = unpickle(cifar_path + 'batches.meta')[b'label_names']
category_names = list(map(lambda x: x.decode("utf-8"), category_names_bytes))
# TODO: Process Cifar data
def process_data(data):
float_data = np.array(data, dtype=float) / 255.0
reshaped_data = np.reshape(float_data, (-1, color_channels, image_height, image_width))
transposed_data = np.transpose(reshaped_data, [0, 2, 3, 1])
return transposed_data
train_data = process_data(train_data)
eval_data = process_data(eval_data)
# TODO: The neural network
class ConvNet:
def __init__(self, image_height, image_width, channels, num_classes):
self.input_layer = tf.placeholder(dtype=tf.float32, shape=[None, image_height, image_width, channels],
name="inputs")
print(self.input_layer.shape)
conv_layer_1 = tf.layers.conv2d(self.input_layer, filters=32, kernel_size=[5, 5], padding="same",
activation=tf.nn.relu)
print(conv_layer_1.shape)
pooling_layer_1 = tf.layers.max_pooling2d(conv_layer_1, pool_size=[2, 2], strides=2)
print(pooling_layer_1.shape)
conv_layer_2 = tf.layers.conv2d(pooling_layer_1, filters=64, kernel_size=[5, 5], padding="same",
activation=tf.nn.relu)
print(conv_layer_2.shape)
pooling_layer_2 = tf.layers.max_pooling2d(conv_layer_2, pool_size=[2, 2], strides=2)
print(pooling_layer_2.shape)
flattened_pooling = tf.layers.flatten(pooling_layer_2)
dense_layer = tf.layers.dense(flattened_pooling, 1024, activation=tf.nn.relu)
print(dense_layer.shape)
dropout = tf.layers.dropout(dense_layer, rate=0.4, training=True)
outputs = tf.layers.dense(dropout, num_classes)
print(outputs.shape)
self.choice = tf.argmax(outputs, axis=1)
self.probability = tf.nn.softmax(outputs)
self.labels = tf.placeholder(dtype=tf.float32, name="labels")
self.accuracy, self.accuracy_op = tf.metrics.accuracy(self.labels, self.choice)
one_hot_labels = tf.one_hot(indices=tf.cast(self.labels, dtype=tf.int32), depth=num_classes)
self.loss = tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels, logits=outputs)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-2)
self.train_operation = optimizer.minimize(loss=self.loss, global_step=tf.train.get_global_step())
# TODO: initialize variables
training_steps = 20000
batch_size = 64
path = "./" + model_name + "-cnn/"
load_checkpoint = True
performance_graph = np.array([])
# TODO: implement the training loop
tf.reset_default_graph()
dataset = tf.data.Dataset.from_tensor_slices((train_data, train_labels))
dataset = dataset.shuffle(buffer_size=train_labels.shape[0])
dataset = dataset.batch(batch_size)
dataset = dataset.repeat()
dataset_iterator = dataset.make_initializable_iterator()
next_element = dataset_iterator.get_next()
cnn = ConvNet(image_height,image_width,color_channels,10)
saver = tf.train.Saver(max_to_keep=2)
if not os.path.exists(path):
os.makedirs(path)
with tf.Session() as sess:
if load_checkpoint:
checkpoint = tf.train.get_checkpoint_state(path)
saver.restore(sess, checkpoint.model_checkpoint_path)
else:
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(dataset_iterator.initializer)
for step in range(training_steps):
current_batch = sess.run(next_element)
batch_inputs = current_batch[0]
batch_labels = current_batch[1]
sess.run((cnn.train_operation, cnn.accuracy_op), feed_dict={cnn.input_layer: batch_inputs, cnn.labels: batch_labels})
if step % 10 == 0:
performance_graph = np.append(performance_graph, sess.run(cnn.accuracy))
if step % 1000 == 0 and step > 0:
current_acc = sess.run(cnn.accuracy)
print("Accuracy at step " + str(step) + ": " + str(current_acc))
print("Saving checkpoint")
saver.save(sess, path + model_name, step)
print("Saving final checkpoint for training session.")
saver.save(sess, path + model_name, step)