-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcifar10-classification.py
217 lines (167 loc) · 5.78 KB
/
cifar10-classification.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
import random
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter)
from dataclasses import dataclass
SEED_VALUE = 42
random.seed(SEED_VALUE)
np.random.seed(SEED_VALUE)
tf.random.set_seed(SEED_VALUE)
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print(X_train.shape)
print(X_test.shape)
plt.figure(figsize=(18, 8))
num_rows = 4
num_cols = 8
# plot each of the images in the batch and the associated ground truth labels
for i in range(num_rows * num_cols):
ax = plt.subplot(num_rows, num_cols, i + 1)
plt.imshow(X_train[i, :, :])
plt.axis("off")
X_train = X_train.astype("float32") / 255
X_test = X_test.astype("float32") / 255
print('Original (integer) label for the first training sample: ', y_train[0])
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
print('After conversion to categorical one-hot encoded labels: ', y_train[0])
@dataclass(frozen=True)
class DatasetConfig:
NUM_CLASSES: int = 10
IMG_HEIGHT: int = 32
IMG_WIDTH: int = 32
NUM_CHANNELS: int = 3
@dataclass(frozen=True)
class TrainingConfig:
EPOCHS: int = 31
BATCH_SIZE: int = 256
LEARNING_RATE: float = 0.001
def cnn_model(input_shape=(32, 32, 3)):
model = Sequential()
# Conv Block 1
model.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=input_shape))
model.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# Conv Block 2
model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# Conv Block 3
model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
return model
model = cnn_model()
model.summary()
model.compile(
optimizer="rmsprop",
loss="categorical_crossentropy",
metrics=["accuracy"],
)
history = model.fit(X_train,
y_train,
batch_size=TrainingConfig.BATCH_SIZE,
epochs=TrainingConfig.EPOCHS,
verbose=1,
validation_split=.3,
)
def plot_results(metrics, title=None, ylabel=None, ylim=None, metric_name=None, color=None):
fig, ax = plt.subplots(figsize=(15, 4))
if not (isinstance(metric_name, list) or isinstance(metric_name, tuple)):
metrics = [metrics,]
metric_name = [metric_name,]
for idx, metric in enumerate(metrics):
ax.plot(metric, color=color[idx])
plt.xlabel("Epoch")
plt.ylabel(ylabel)
plt.title(title)
plt.xlim([0, TrainingConfig.EPOCHS - 1])
plt.ylim(ylim)
ax.xaxis.set_major_locator(MultipleLocator(5))
ax.xaxis.set_major_formatter(FormatStrFormatter("%d"))
ax.xaxis.set_minor_locator(MultipleLocator(1))
plt.grid(True)
plt.legend(metric_name)
plt.show()
plt.close()
train_loss = history.history["loss"]
train_acc = history.history["accuracy"]
valid_loss = history.history["val_loss"]
valid_acc = history.history["val_accuracy"]
plot_results(
[train_loss, valid_loss],
ylabel="Loss",
ylim=[0.0, 5.0],
metric_name=["Training Loss", "Validation Loss"],
color=["g", "b"],
)
plot_results(
[train_acc, valid_acc],
ylabel="Accuracy",
ylim=[0.0, 1.0],
metric_name=["Training Accuracy", "Validation Accuracy"],
color=["g", "b"],
)
# 1.Evaluate the model on the test dataset
test_loss, test_acc = model.evaluate(X_test, y_test)
print(f"Test accuracy: {test_acc*100:.3f}")
# 2.Make predictions on sample test images
def evaluate_model(dataset, model):
class_names = [
"airplane",
"automobile",
"bird",
"cat",
"deer",
"dog",
"frog",
"horse",
"ship",
"truck",
]
num_rows = 3
num_cols = 6
data_batch = dataset[0 : num_rows * num_cols]
predictions = model.predict(data_batch)
plt.figure(figsize=(20, 8))
num_matches = 0
for idx in range(num_rows * num_cols):
ax = plt.subplot(num_rows, num_cols, idx + 1)
plt.axis("off")
plt.imshow(data_batch[idx])
pred_idx = tf.argmax(predictions[idx]).numpy()
truth_idx = np.nonzero(y_test[idx])
title = str(class_names[truth_idx[0][0]]) + " : " + str(class_names[pred_idx])
title_obj = plt.title(title, fontdict={"fontsize": 13})
if pred_idx == truth_idx:
num_matches += 1
plt.setp(title_obj, color="g")
else:
plt.setp(title_obj, color="r")
acc = num_matches / (idx + 1)
print("Prediction accuracy: ", int(100 * acc) / 100)
return
evaluate_model(X_test, model)
# 3.Confusion Matrix
predictions = model.predict(X_test)
predicted_labels = [np.argmax(i) for i in predictions]
y_test_integer_labels = tf.argmax(y_test, axis=1)
cm = tf.math.confusion_matrix(labels=y_test_integer_labels, predictions=predicted_labels)
plt.figure(figsize=[12, 6])
sn.heatmap(cm, annot=True, fmt="d", annot_kws={"size": 12})
plt.title("Confusion Matrix")
plt.xlabel("Predicted")
plt.ylabel("Truth")
plt.show()