-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
115 lines (90 loc) · 3.31 KB
/
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
"""
LiuShi
A deep-learning project that utilizes a custom-made Convolutional Neural Network (CNN) architecture to recognize handwritten Chinese numerals.
This project is under the MIT license.
Please read the terms and conditions stated within the license before attempting any modification or distribution of the software.
Copyright © 2024 Justine Paul Vitan. All rights reserved.
License Information: https://github.com/jpvitan/liushi/blob/master/LICENSE
Developer's Website: https://jpvitan.com/
"""
import matplotlib.pyplot as plt
from tensorflow.keras import Sequential
from tensorflow.keras import layers
from tensorflow.keras.callbacks import ModelCheckpoint
def create_model():
model = Sequential()
model.add(
layers.Conv2D(
filters=32,
kernel_size=(3, 3),
strides=(1, 1),
padding="same",
activation="relu",
input_shape=(64, 64, 1),
)
)
model.add(layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding="same"))
model.add(
layers.Conv2D(
filters=64,
kernel_size=(3, 3),
strides=(1, 1),
padding="same",
activation="relu",
)
)
model.add(layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding="same"))
model.add(
layers.Conv2D(
filters=128,
kernel_size=(3, 3),
strides=(1, 1),
padding="same",
activation="relu",
)
)
model.add(layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding="same"))
model.add(layers.Flatten())
model.add(layers.Dense(units=1024, activation="relu"))
model.add(layers.Dense(units=512, activation="relu"))
model.add(layers.Dense(units=256, activation="relu"))
model.add(layers.Dense(units=15, activation="softmax"))
model.compile(
optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
)
return model
def train_model(data, epochs, checkpoint_location):
def plot_history(history):
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
axs[0].plot(history["loss"], label="training loss")
axs[0].plot(history["val_loss"], label="validation loss")
axs[0].legend(loc="upper left")
axs[0].set_title("training data vs validation data")
axs[1].plot(history["accuracy"], label="testing accuracy")
axs[1].plot(history["val_accuracy"], label="validation accuracy")
axs[1].set_ylim([0, 1])
axs[1].legend(loc="upper left")
axs[1].set_title("accuracy")
axs.flat[0].set(xlabel="epochs", ylabel="loss")
axs.flat[1].set(xlabel="epochs", ylabel="accuracy")
plt.show()
model = create_model()
training_feature = data[0]
training_label = data[1]
validation_feature = data[2]
validation_label = data[3]
callback = ModelCheckpoint(checkpoint_location, save_weights_only=True, verbose=1)
history = model.fit(
training_feature,
training_label,
validation_data=(validation_feature, validation_label),
epochs=epochs,
callbacks=[callback],
verbose=1,
).history
plot_history(history)
return model
def load_model(checkpoint_location):
model = create_model()
model.load_weights(checkpoint_location)
return model