forked from nhatnguyen12/Deep-Learning-For-Computer-Vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cifar10_checkpoint_improvements.py
46 lines (34 loc) · 1.29 KB
/
cifar10_checkpoint_improvements.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
from sklearn.preprocessing import LabelBinarizer
from pyimagesearch.nn.conv.minivggnet import MiniVGGNet
from keras.callbacks import ModelCheckpoint
from keras.optimizers import SGD
from keras.datasets import cifar10
import argparse
import os
ap = argparse.ArgumentParser()
ap.add_argument("-w", "--weights", required=True,
help="path to weights directory")
args = vars(ap.parse_args())
print("[INFO] loading CIFAR-10 data...")
((trainX, trainY), (testX, testY)) = cifar10.load_data()
trainX = trainX.astype("float") / 255.0
testX = testX.astype("float") / 255.0
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)
print("[INFO] compiling model")
opt = SGD(lr=0.01, decay=0.01/40, momentum=0.9, nesterov=True)
model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10)
model.compile(loss="categorical_crossentropy", optimizer=opt,
metrics=["accuracy"])
fname = os.path.sep.join([
args["weights"],
"weights-{epoch:03d}-{val_loss:.4f}.hdf5"
])
checkpoint = ModelCheckpoint(fname, monitor="val_loss", mode="min",
save_best_only=True, verbose=1)
callbacks = [checkpoint]
# train the network
print("[INFO] training network...")
model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=64,
epochs=100, callbacks=callbacks, verbose=1)