-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_model.py
125 lines (96 loc) · 3.6 KB
/
cnn_model.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
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
from keras.src.callbacks import EarlyStopping, ModelCheckpoint, CSVLogger, ReduceLROnPlateau
from tensorflow import keras
from tensorflow.keras import layers
# Model / data parameters
num_classes = 10
input_shape = (28, 28, 1)
# Load the data and split it between train and test sets
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Visualize first few images
for i in range(9):
plt.subplot(330 + 1 + i) # define subplot
plt.imshow(x_train[i], cmap=plt.get_cmap("gray")) # plot raw pixel data
plt.show() # show the figure
# Scale images to the [0, 1] range
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
# Make sure images have shape (28, 28, 1)
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print("x_train shape:", x_train.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model_file = "cnn_digit01.keras"
# init model
model = keras.Sequential(
[
keras.Input(shape=input_shape),
# layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
# layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(128, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
# layers.GlobalAveragePooling2D(),
layers.Flatten(),
layers.Dense(128, activation="relu"),
layers.Dropout(0.5),
layers.Dense(num_classes, activation="softmax"),
]
)
model.summary()
batch_size = 128
epochs = 15
# model.compile(loss="mse", optimizer="sgd", metrics=["accuracy"])
model.compile(
# loss="mse",
loss="categorical_crossentropy",
optimizer=keras.optimizers.Adam(learning_rate=1e-3),
metrics=["accuracy"],
)
# creating callback's
early_stopping = EarlyStopping(monitor="val_loss", patience=5, verbose=1)
checkpoint = ModelCheckpoint("best_" + model_file, save_best_only=True, verbose=1)
reduce_lr = ReduceLROnPlateau(monitor="val_loss", factor=0.5, patience=3, verbose=1)
csv_logger = CSVLogger("training_log.csv", append=True)
model.fit(
x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1,
callbacks=[early_stopping, reduce_lr, csv_logger],
)
# Save the model in .keras file
model.save(model_file)
print(f"Model {model_file} saved!")
# Recreate the exact same model from the file:
# model = keras.models.load_model(model_file)
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
(_, _), (x_test, _) = keras.datasets.mnist.load_data()
x_test = x_test[:9]
# show 9 images from test dataset
for i in range(9):
plt.subplot(330 + 1 + i)
plt.imshow(x_test[i], cmap=plt.get_cmap("gray"))
# save to file
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") # YYYY-MM-DD_HH-MM-SS
filename = f"test_{current_time}.png"
plt.savefig(filename)
# show
plt.show()
# prepare for predicting
x_test = x_test.astype("float32") / 255
x_test = np.expand_dims(x_test, -1)
print(x_test.shape[0], "test samples")
print("x_test shape:", x_test.shape)
# perform predictions
predictions = model.predict(x_test)
image_classes = list(range(10))
print("Results of prediction: ")
for prediction in predictions:
print(image_classes[np.argmax(prediction)])