-
Notifications
You must be signed in to change notification settings - Fork 0
/
00_fashion_mnist.py
155 lines (134 loc) · 6.41 KB
/
00_fashion_mnist.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
from __future__ import absolute_import, division, print_function
import argparse
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import time
import util
class SimpleCNN(keras.Model):
def __init__(self, num_classes=10):
super(SimpleCNN, self).__init__(name='SimpleCNN')
self.num_classes = num_classes
self.conv1 = layers.Conv2D(input_shape=(28, 28, 1),
filters=32,
kernel_size=[5, 5],
padding="same",
activation='relu')
self.pool1 = layers.MaxPool2D(pool_size=(2, 2))
self.conv2 = layers.Conv2D(filters=64,
kernel_size=[5, 5],
padding="same",
activation='relu')
self.pool2 = layers.MaxPool2D(pool_size=(2, 2))
self.flat = layers.Flatten()
self.dense1 = layers.Dense(1024, activation='relu')
self.dropout = layers.Dropout(rate=0.4)
self.dense2 = layers.Dense(num_classes, activation='softmax')
def call(self, inputs, training=False):
x = self.conv1(inputs)
x = self.pool1(x)
x = self.conv2(x)
x = self.pool2(x)
flat_x = self.flat(x)
out = self.dense1(flat_x)
out = self.dropout(out, training=training)
out = self.dense2(out)
return out
def compute_output_shape(self, input_shape):
shape = tf.TensorShape(input_shape).as_list()
shape = [shape[0], self.num_classes]
return tf.TensorShape(shape)
def preprocess_data(images, labels, num_classes=10):
images = images.astype('float32') / 255.0
images = images.reshape(images.shape[0], 28, 28, 1)
labels = tf.keras.utils.to_categorical(labels, num_classes)
return images, labels
def main():
parser = argparse.ArgumentParser(description='TensorFlow Fashion MNIST Example')
parser.add_argument('--batch-size', type=int, default=100,
help='input batch size for training')
parser.add_argument('--epochs', type=int, default=4,
help='number of epochs to train')
parser.add_argument('--lr', type=float, default=0.001,
help='learning rate')
parser.add_argument('--seed', type=int, default=1,
help='random seed')
parser.add_argument('--log-interval', type=int, default=5,
help='how many batches to wait before'
' logging training status')
parser.add_argument('--eval-interval', type=int, default=100,
help='how many batches to wait before'
' evaluate the model')
args = parser.parse_args()
start_time = time.time()
util.set_random_seed(args.seed)
sess = util.set_session()
fashion_mnist = keras.datasets.fashion_mnist
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images, train_labels = preprocess_data(train_images,
train_labels,
num_classes=len(class_names))
test_images, test_labels = preprocess_data(test_images,
test_labels,
num_classes=len(class_names))
features_placeholder = tf.placeholder(train_images.dtype, train_images.shape)
labels_placeholder = tf.placeholder(train_labels.dtype, train_labels.shape)
train_dataset = tf.data.Dataset.from_tensor_slices((features_placeholder,
labels_placeholder))
train_dataset = train_dataset.shuffle(10000).batch(args.batch_size)
iterator = train_dataset.make_initializable_iterator()
next_element = iterator.get_next()
model = SimpleCNN(num_classes=len(class_names))
model.compile(optimizer=keras.optimizers.Adam(lr=args.lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
iter = 0
train_log = {'iter': [], 'loss': [], 'accuracy': []}
test_log = {'iter': [], 'loss': [], 'accuracy': []}
for ep in range(args.epochs):
sess.run(iterator.initializer,
feed_dict={features_placeholder: train_images,
labels_placeholder: train_labels})
try:
while True:
iter += 1
images, labels = sess.run(next_element)
train_loss, train_acc = model.train_on_batch(images, labels)
if iter % args.log_interval == 0:
print('Epoch: {0:d}/{1:d} Iteration:{2:d} Training Loss:{3:.4f} '
'Training Accuracy:{4:.4f}'.format(ep,
args.epochs,
iter,
train_loss,
train_acc))
train_log['iter'].append(iter)
train_log['loss'].append(train_loss)
train_log['accuracy'].append(train_acc)
if iter % args.eval_interval == 0:
test_loss, test_acc = model.evaluate(test_images, test_labels)
test_log['iter'].append(iter)
test_log['loss'].append(test_loss)
test_log['accuracy'].append(test_acc)
except tf.errors.OutOfRangeError:
pass
model.summary()
end_time = time.time()
print('Elapsed time: {0:.3f}s'.format(end_time - start_time))
fig1 = plt.figure()
plt.plot(train_log['iter'], train_log['loss'], 'r', label='Training')
plt.plot(test_log['iter'], test_log['loss'], 'b', label='Testing')
plt.title('Loss')
plt.legend()
plt.savefig('00_Loss.png')
fig2 = plt.figure()
plt.plot(train_log['iter'], train_log['accuracy'], 'r', label='Training')
plt.plot(test_log['iter'], test_log['accuracy'], 'b', label='Testing')
plt.title('Accuracy')
plt.legend()
plt.savefig('00_Acc.png')
plt.show()
if __name__ == '__main__':
main()