forked from nhatnguyen12/Deep-Learning-For-Computer-Vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shallownet_cifar10.py
56 lines (42 loc) · 1.61 KB
/
shallownet_cifar10.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
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import classification_report
from pyimagesearch.nn.conv.shallownet import ShallowNet
from keras.optimizers import SGD
from keras.datasets import cifar10
import matplotlib.pyplot as plt
import numpy as np
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
trainY = LabelBinarizer().fit_transform(trainY)
testY = LabelBinarizer().fit_transform(testY)
model = ShallowNet.build(width=32, height=32, depth=3, classes=10)
optimizer = SGD(lr=0.005)
model.compile(metrics=["accuracy"], loss="categorical_crossentropy",
optimizer=optimizer)
print ("[INFO] training network...")
H = model.fit(x=trainX, y=trainY, validation_data=(testX, testY), epochs=100,
batch_size=32, verbose=1)
print ("[INFO] evaluating network...")
predictions = model.predict(x=testX, batch_size=32)
print(classification_report(
testY.argmax(axis=1),
predictions.argmax(axis=1),
target_names=["airplane", "automobile", "bird", "cat", "deer", "dog", "frog",
"horse", "ship", "truck"]
))
print(H)
print(H.history)
# plot the results
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, 100), H.history["acc"], label="train_acc")
plt.plot(np.arange(0, 100), H.history["val_acc"], label="val_loss")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.show()