-
Notifications
You must be signed in to change notification settings - Fork 0
/
fashion_mnist_cnn3.py
162 lines (137 loc) · 5.65 KB
/
fashion_mnist_cnn3.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
# Convolutional neural network (CNN) for Fashion-MNIST dataset with random dropout
# 1.61M parameters with random initialization
# 4 convolutional layers: 64 + 128 + 256 + 512 filters
# 1 fully connected layer: 128 units
# EarlyStopping(patience=20)
# loss: 0.1774 - accuracy: 0.9358 - val_loss: 0.2055 - val_accuracy: 0.9300 - 6s/epoch - 13ms/step
# batch_size=128, epochs=300
# best result obtained at epoch 87
import numpy as np
import matplotlib.pyplot as plt
from keras.utils import to_categorical, plot_model
from keras.datasets import fashion_mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.callbacks import EarlyStopping, ModelCheckpoint
def load_data():
(X_train, Y_train), (X_test, Y_test) = fashion_mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32') / 255
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32') / 255
Y_train = to_categorical(Y_train) # one-hot encoding
Y_test = to_categorical(Y_test)
return (X_train, Y_train), (X_test, Y_test)
def create_model():
model = Sequential()
# convoluted layer 1
model.add(Conv2D(filters=64,
kernel_size=(3,3),
padding='same', # same size as the image
input_shape=(28,28,1),
activation='relu'))
model.add(Dropout(0.5)) # random drop 25%
model.add(MaxPool2D(pool_size=(2,2)))
# convoluted layer 2
model.add(Conv2D(filters=128,
kernel_size=(3, 3),
padding='same', # same size as the image
activation='relu'))
model.add(Dropout(0.5)) # random drop 25%
model.add(MaxPool2D(pool_size=(2,2)))
# convoluted layer 3
model.add(Conv2D(filters=256,
kernel_size=(3, 3),
padding='same', # same size as the image
activation='relu'))
model.add(Dropout(0.5)) # random drop 25%
model.add(MaxPool2D(pool_size=(2,2)))
# convoluted layer 4
model.add(Conv2D(filters=512,
kernel_size=(3, 3),
padding='same', # same size as the image
activation='relu'))
model.add(Dropout(0.5)) # random drop 25%
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
# fully connected layer
model.add(Dense(units=128,
kernel_initializer='normal',
activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=10,
kernel_initializer='normal',
activation='softmax'))
print(model.summary()) # summary of model
plot_model(model, to_file='network/fmc3.png', show_shapes=True)
model.compile(loss='categorical_crossentropy',
optimizer='adam', # gradient descent
metrics=['accuracy'])
return model
def train(model, batch_size, epochs, X_train, Y_train, X_test, Y_test):
checkpoint = ModelCheckpoint('bestModel/fmc3.keras',
monitor='val_loss',
mode='min',
save_best_only=True,
verbose=1)
early_stopping = EarlyStopping(patience=20)
train_history = model.fit(x=X_train,
y=Y_train,
validation_data=(X_test, Y_test),
epochs=epochs,
batch_size=batch_size,
callbacks=[early_stopping, checkpoint],
verbose=2)
return train_history
# evaluate model
def evaluate(model, X_test, Y_test):
scores = model.evaluate(X_test, Y_test, verbose=0)
return scores
def result_plt(hist):
train_acc = hist.history['accuracy']
val_acc = hist.history['val_accuracy']
train_loss = hist.history['loss']
val_loss = hist.history['val_loss']
plt.figure(figsize=(9, 6))
x = np.arange(len(train_loss))
plt.subplot(1, 2, 1)
plt.plot(x, train_acc)
plt.plot(val_acc)
plt.title("Train History of accuracy")
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train_acc', 'val_acc'], loc='lower right')
plt.subplot(1, 2, 2)
plt.plot(train_loss)
plt.plot(val_loss)
plt.title("Train History of loss")
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train_loss', 'val_loss'], loc='upper right')
fig, loss_ax = plt.subplots()
acc_ax = loss_ax.twinx()
acc_ax.plot(train_acc, 'b', label='train_acc')
acc_ax.plot(val_acc, 'g', label='val_acc')
loss_ax.plot(train_loss, 'y', label='train_loss')
loss_ax.plot(val_loss, 'r', label='val_loss')
loss_ax.legend(loc='lower left')
acc_ax.legend(loc='upper left')
plt.show()
def main():
(X_train, Y_train), (X_test, Y_test) = load_data()
model = create_model()
# (model, batch_size, epochs, X_train, Y_train, X_test, Y_test)
hist = train(model, 128, 300, X_train, Y_train, X_test, Y_test)
result_plt(hist)
model.load_weights("bestModel/fmc3.keras")
print("\nsaved model to disk")
print("accuracy:", evaluate(model, X_test, Y_test)[1])
# use model to predict
index_list = np.random.choice(X_test.shape[0], 10)
data = X_test[index_list]
y_preds = model.predict(data)
print("\npredicts===>>>")
for i in range(10):
print('True:' + str(np.argmax(Y_test[index_list[i]])) +
', Predict:' + str(np.argmax(y_preds[i])) +
', index:' + str(index_list[i]))
if __name__ == "__main__":
main()