-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_model.py
94 lines (74 loc) · 2.26 KB
/
train_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
"""
Title: Simple MNIST convnet
Author: [fchollet](https://twitter.com/fchollet)
Date created: 2015/06/19
Last modified: 2020/04/21
Description: A simple convnet that achieves ~99% test accuracy on MNIST.
"""
"""
## Setup
"""
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
"""
## Prepare the data
"""
# Model / data parameters
num_classes = 10
input_shape = (28, 28, 1)
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# 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)
"""
## Build the model
"""
model = keras.Sequential(
[
keras.Input(shape=input_shape),
layers.Conv2D(64, (3, 3), padding='valid', input_shape=(28, 28, 1)),
layers.Activation('relu'),
layers.Conv2D(64, (3, 3)),
layers.Activation('relu'),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Dropout(.5),
layers.Flatten(),
layers.Dense(128),
layers.Activation('relu'),
layers.Dropout(.5),
layers.Dense(10),
layers.Activation('softmax')
]
)
model.summary()
"""
## Train the model
"""
batch_size = 128
epochs = 12
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)
"""
## Evaluate the trained model
"""
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
# EXPORT MODEL ARCHITECTURE AND WEIGHTS
# Exporting the entire model allows to checkpoint a model and resume training later—from the exact same state—without access to the original code.
model.save('models/mnist_classifier.h5')
"""
Test loss: 0.024538548663258553
Test accuracy: 0.9912999868392944
"""