ZFNet For CIFAR-10 Classification
ZFNet For CIFAR-10 Classification
3 Import Libraries
[1]: import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import tensorflow as tf
1
from keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D
from keras.layers import Dropout, Flatten, BatchNormalization
from keras.regularizers import l2
from keras.optimizers import Adam
from keras.callbacks import ReduceLROnPlateau, EarlyStopping
from keras.models import load_model
%matplotlib inline
2024-05-23 06:52:36.094594: E
external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register
cuDNN factory: Attempting to register factory for plugin cuDNN when one has
already been registered
2024-05-23 06:52:36.094712: E
external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register
cuFFT factory: Attempting to register factory for plugin cuFFT when one has
already been registered
2024-05-23 06:52:36.229763: E
external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to
register cuBLAS factory: Attempting to register factory for plugin cuBLAS when
one has already been registered
2
x_val shape: (10000, 32, 32, 3)
y_val shape: (10000, 1)
plt.figure(figsize=(12, 6))
sns.barplot(x='Class', y='Count', data=class_counts_df, palette='hsv')
plt.title('Class Distribution in CIFAR-10 Training Dataset')
plt.xlabel('Class')
plt.ylabel('Count')
plt.xticks(rotation=45)
plt.show()
[8]: class_counts_df
3
2 bird 3970
3 cat 3977
4 deer 4067
5 dog 3985
6 frog 4004
7 horse 4006
8 ship 3983
9 truck 3960
plt.figure(figsize=(10, 8))
plt.pie(class_counts_df['Count'], labels=class_counts_df['Class'], autopct='%1.
↪1f%%', startangle=140, colors=sns.color_palette('RdBu', len(label_name)))
4
[10]: unique, counts = np.unique(y_train, return_counts=True)
class_counts = dict(zip(label_name, counts))
plt.figure(figsize=(10, 8))
↪colors=sns.color_palette('seismic', len(label_name)))
plt.axis('equal')
plt.title('Class Distribution in CIFAR-10 Training Dataset')
5
plt.show()
[11]: # let's see the first 25 images from our cifar-10 dataset
plt.figure(figsize=(14,14))
for i in range(36):
plt.subplot(6,6,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train[i])
plt.title(label_name[y_train[i][0]])
plt.show()
6
4 Data Preprocessing
• Normalization
• Encoding
• Data Augmentation
[12]: # pixel values to float
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_val = x_val.astype('float32')
7
[13]: mean = np.mean(x_train)
std = np.std(x_train)
print(mean)
print(std)
120.73008
64.074066
[17]: x_train.shape[1:]
# Second layer
model.add(layers.Conv2D(256, (5, 5), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
8
# Third layer
model.add(layers.Conv2D(384, (3, 3), padding='same', activation='relu'))
# Fourth layer
model.add(layers.Conv2D(384, (3, 3), padding='same', activation='relu'))
# Fifth layer
model.add(layers.Conv2D(256, (3, 3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
# Flatten
model.add(layers.Flatten())
optimizer = Adam(learning_rate=0.0001)
model.compile(optimizer=optimizer, loss='categorical_crossentropy',␣
↪metrics=['accuracy'])
model.summary()
/opt/conda/lib/python3.10/site-
packages/keras/src/layers/convolutional/base_conv.py:99: UserWarning: Do not
pass an `input_shape`/`input_dim` argument to a layer. When using Sequential
models, prefer using an `Input(shape)` object as the first layer in the model
instead.
super().__init__(
Model: "sequential"
����������������������������������������������������������������������������
� Layer (type) � Output Shape � Param # �
����������������������������������������������������������������������������
� conv2d (Conv2D) � (None, 16, 16, 96) � 14,208 �
����������������������������������������������������������������������������
� max_pooling2d (MaxPooling2D) � (None, 7, 7, 96) � 0 �
����������������������������������������������������������������������������
� conv2d_1 (Conv2D) � (None, 7, 7, 256) � 614,656 �
����������������������������������������������������������������������������
� max_pooling2d_1 (MaxPooling2D) � (None, 3, 3, 256) � 0 �
����������������������������������������������������������������������������
� conv2d_2 (Conv2D) � (None, 3, 3, 384) � 885,120 �
����������������������������������������������������������������������������
9
� conv2d_3 (Conv2D) � (None, 3, 3, 384) � 1,327,488 �
����������������������������������������������������������������������������
� conv2d_4 (Conv2D) � (None, 3, 3, 256) � 884,992 �
����������������������������������������������������������������������������
� max_pooling2d_2 (MaxPooling2D) � (None, 1, 1, 256) � 0 �
����������������������������������������������������������������������������
� flatten (Flatten) � (None, 256) � 0 �
����������������������������������������������������������������������������
� dense (Dense) � (None, 4096) � 1,052,672 �
����������������������������������������������������������������������������
� dropout (Dropout) � (None, 4096) � 0 �
����������������������������������������������������������������������������
� dense_1 (Dense) � (None, 4096) � 16,781,312 �
����������������������������������������������������������������������������
� dropout_1 (Dropout) � (None, 4096) � 0 �
����������������������������������������������������������������������������
� dense_2 (Dense) � (None, 10) � 40,970 �
����������������������������������������������������������������������������
[41]:
10
11
[19]: batch_size =64
epochs = 100
Epoch 1/100
15/625 �������������������� 7s 11ms/step - accuracy:
0.1034 - loss: 2.3012
WARNING: All log messages before absl::InitializeLog() is called are written to
STDERR
I0000 00:00:1716447187.423526 157 device_compiler.h:186] Compiled cluster
using XLA! This line is logged at most once for the lifetime of the process.
W0000 00:00:1716447187.441835 157 graph_launch.cc:671] Fallback to op-by-op
mode because memset node breaks graph update
625/625 �������������������� 0s 11ms/step -
accuracy: 0.2897 - loss: 1.8732
W0000 00:00:1716447195.136756 159 graph_launch.cc:671] Fallback to op-by-op
mode because memset node breaks graph update
625/625 �������������������� 19s 15ms/step -
accuracy: 0.2898 - loss: 1.8728 - val_accuracy: 0.5206 - val_loss: 1.3152 -
learning_rate: 1.0000e-04
Epoch 2/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.5393 - loss: 1.2634 - val_accuracy: 0.5874 - val_loss: 1.1365 -
learning_rate: 1.0000e-04
Epoch 3/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.6247 - loss: 1.0468 - val_accuracy: 0.6451 - val_loss: 1.0119 -
learning_rate: 1.0000e-04
Epoch 4/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.6863 - loss: 0.8828 - val_accuracy: 0.6648 - val_loss: 0.9211 -
12
learning_rate: 1.0000e-04
Epoch 5/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.7332 - loss: 0.7462 - val_accuracy: 0.6980 - val_loss: 0.8668 -
learning_rate: 1.0000e-04
Epoch 6/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.7854 - loss: 0.6195 - val_accuracy: 0.7153 - val_loss: 0.8450 -
learning_rate: 1.0000e-04
Epoch 7/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.8275 - loss: 0.4958 - val_accuracy: 0.7267 - val_loss: 0.8215 -
learning_rate: 1.0000e-04
Epoch 8/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.8670 - loss: 0.3843 - val_accuracy: 0.7240 - val_loss: 0.8707 -
learning_rate: 1.0000e-04
Epoch 9/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.8966 - loss: 0.2976 - val_accuracy: 0.7325 - val_loss: 0.8983 -
learning_rate: 1.0000e-04
Epoch 10/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.9210 - loss: 0.2271 - val_accuracy: 0.7337 - val_loss: 0.9643 -
learning_rate: 1.0000e-04
Epoch 11/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.9424 - loss: 0.1645 - val_accuracy: 0.7209 - val_loss: 1.1078 -
learning_rate: 1.0000e-04
Epoch 12/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.9529 - loss: 0.1378 - val_accuracy: 0.7264 - val_loss: 1.1221 -
learning_rate: 1.0000e-04
Epoch 13/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.9878 - loss: 0.0437 - val_accuracy: 0.7472 - val_loss: 1.2859 -
learning_rate: 5.0000e-05
Epoch 14/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.9966 - loss: 0.0129 - val_accuracy: 0.7426 - val_loss: 1.4302 -
learning_rate: 5.0000e-05
Epoch 15/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.9912 - loss: 0.0283 - val_accuracy: 0.7360 - val_loss: 1.4540 -
learning_rate: 5.0000e-05
Epoch 16/100
625/625 �������������������� 10s 11ms/step -
accuracy: 0.9919 - loss: 0.0247 - val_accuracy: 0.7491 - val_loss: 1.4797 -
13
learning_rate: 5.0000e-05
Epoch 17/100
625/625 �������������������� 7s 11ms/step -
accuracy: 0.9897 - loss: 0.0327 - val_accuracy: 0.7427 - val_loss: 1.5428 -
learning_rate: 5.0000e-05
Epoch 17: early stopping
[20]: model.save('trained_model_10.h5')
[21]: plt.figure(figsize=(15,6))
plt.legend()
plt.title('Loss Evolution')
plt.legend()
plt.title('Accuracy Evolution')
plt.show()
14
[22]: test_loss, test_acc = model.evaluate(x_test, y_test, verbose=1)
[23]: pred=model.predict(x_test)
final_pred=np.argmax(pred,axis=1)
y_test_labels = np.argmax(y_test, axis=1)
6 confusion matrix
[24]: from sklearn.metrics import confusion_matrix, roc_curve, roc_auc_score,␣
↪classification_report, log_loss,accuracy_score
plt.title('Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.show()
15
[25]: print(classification_report(y_test_labels,final_pred,target_names=label_name))
16
weighted avg 0.75 0.74 0.74 10000
7 Classification report
[26]: report = classification_report(y_test_labels, final_pred,␣
↪target_names=label_name, output_dict=True)
report_df = pd.DataFrame(report).transpose()
plt.figure(figsize=(12, 8))
sns.heatmap(report_df.iloc[:-1, :-1], annot=True, cmap='ocean', fmt='.2f')
plt.title('Classification Report Heatmap')
plt.show()
8 Accuracy Score
[27]: plt.figure(figsize=(10,5),facecolor="white")
acc_score = accuracy_score(y_test_labels,final_pred)
plt.plot([])
plt.text(0,0, f'Accuracy Score Score: {acc_score:.4f}', fontsize=20,␣
↪ha='center', va='center',color="crimson")
17
plt.axis('off')
plt.show()
plt.axis('off')
plt.show()
18
10 Cohen Kappa Score
[29]: from sklearn.metrics import cohen_kappa_score
plt.figure(figsize=(10,5),facecolor="white")
kappa = cohen_kappa_score(y_test_labels,final_pred)
plt.plot([])
plt.text(0,0, f'Cohen Kappa Score: {kappa:.4f}', fontsize=20, ha='center',␣
↪va='center',color="crimson")
plt.axis('off')
plt.show()
19
11 Matthews Correlation Coefficient
[30]: from sklearn.metrics import matthews_corrcoef
plt.figure(figsize=(10,5),facecolor="white")
mcc = matthews_corrcoef(y_test_labels, final_pred)
plt.axis('off')
plt.show()
20
12 Brier Score Loss _1
[31]: import pandas as pd
from sklearn.metrics import brier_score_loss
pred = model.predict(x_test)
final_pred = np.argmax(pred, axis=1)
y_test_labels = np.argmax(y_test, axis=1)
brier_scores = []
for class_label in range(num_classes):
class_mask = (y_test_labels == class_label)
class_pred = (final_pred == class_label)
brier_score = brier_score_loss(class_mask, class_pred)
brier_scores.append(brier_score)
21
313/313 �������������������� 1s 2ms/step
[32]: df
plt.figure(figsize=(10, 8))
ax = plt.gca()
22
wedges, texts, autotexts = ax.pie(df['Brier Score Loss'], labels=df['Class'],␣
↪autopct='%1.1f%%', startangle=140, colors=colors)
ax.axis('equal')
plt.title('Brier Score Loss for Each Class')
plt.show()
14 Precision,Recall Score
[34]: from sklearn.metrics import precision_score, recall_score
precisions = []
23
recalls = []
precisions.append(precision)
recalls.append(recall)
# Plotting precision
plt.figure(figsize=(10, 5), facecolor="white")
plt.bar(df['Class'], df['Precision'], color='darkviolet')
plt.xlabel('Class')
plt.ylabel('Precision')
plt.title('Precision for Each Class')
plt.xticks(rotation=45)
plt.show()
# Plotting recall
plt.figure(figsize=(10, 5), facecolor="white")
plt.bar(df['Class'], df['Recall'], color='crimson')
plt.xlabel('Class')
plt.ylabel('Recall')
plt.title('Recall for Each Class')
plt.xticks(rotation=45)
plt.show()
24
[35]: colors = ['brown', 'coral', 'orchid', 'olive', 'yellowgreen', 'lime', 'yellow',␣
↪'teal', 'grey', 'purple']
25
plt.figure(figsize=(10, 8))
ax = plt.gca()
ax.axis('equal')
plt.title('Recall Score for Each Class')
plt.show()
plt.figure(figsize=(10, 8))
26
ax = plt.gca()
ax.axis('equal')
plt.title('Precision Score for Each Class')
plt.show()
fpr = dict()
tpr = dict()
roc_auc = dict()
27
num_classes = len(label_name)
↪format(label_name[class_index], roc_auc[class_index]))
28
[38]: num_classes = len(label_name)
precision = dict()
recall = dict()
average_precision = dict()
for i in range(num_classes):
precision[i], recall[i], _ = precision_recall_curve(y_test_labels == i,␣
↪pred[:, i])
plt.figure(figsize=(10, 8))
colors = cycle(cm.rainbow(np.linspace(0, 1, num_classes)))
for i, color in zip(range(num_classes), colors):
plt.plot(recall[i], precision[i], color=color, lw=2,
label='Precision-Recall curve of class {0} (area = {1:0.2f})'.
↪format(label_name[i], average_precision[i]))
29
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall', fontsize=15, color="black")
plt.ylabel('Precision', fontsize=15, color="black")
plt.title('Precision-Recall curve\n', fontsize=20, color="black")
plt.legend(loc="lower right")
plt.show()
15 Prediction
[39]: plt.figure(figsize=(30, 30))
number_images = (5, 5)
num_samples = min(number_images[0] * number_images[1], len(x_test))
30
for i in range(num_samples):
plt.subplot(number_images[0], number_images[1], i + 1)
plt.axis("off")
true_label = label_name[y_test_labels[i]]
predicted_label = label_name[final_pred[i]]
color = "darkgreen"
if true_label != predicted_label:
color = "red"
31
16 Custom Data Prediction
[40]: plt.figure(figsize=(15, 15))
import urllib.request
import os
from PIL import Image
img_url = "https://t3.ftcdn.net/jpg/00/41/06/42/
↪360_F_41064239_IaGdGyf1vxHFaNDS5K164OFOwiMe1hC9.jpg"
max_prob = np.max(probs)
print(f'Predicted class: {pred_class_name}')
print(f'Maximum probability: {max_prob}')
↪alpha=0.8))
plt.show()
32
[ ]:
33