# CNN
course: Academy — 54-Next-Gen-AI-GenAI-Agents-Future-Trends
module: Academy/54-Next-Gen-AI-GenAI-Agents-Future-Trends
type: notebook
source_url: https://personal-learn.armco.dev/files/Academy/54-Next-Gen-AI-GenAI-Agents-Future-Trends/folders/Session-3_Folder/CNN.ipynb
---
[cell 2 markdown]
# CNN with Keras
INSTRUCTIONS:
- Run the cells
- Observe and understand the results
- Answer the questions
[cell 3 markdown]
## CIFAR10 small image classification
- [CIFAR10](https://www.cs.toronto.edu/~kriz/cifar.html) dataset of color training images, labeled over 10 categories.
It has the classes:
- airplane
- automobile
- bird
- cat
- deer
- dog
- frog
- horse
- ship
- truck
[cell 4 markdown]
## Import libraries
[cell 5 code]
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
[cell 6 code]
## Import libraries
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
[cell 7 code]
# import tensorflow and keras
import tensorflow
from tensorflow import keras
from keras.datasets import cifar10
from keras.layers import Conv2D
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers import MaxPool2D
from keras.models import Sequential
from keras.utils import to_categorical
[cell 8 code]
import keras
print(keras.__version__)
[cell 9 code]
print(tensorflow.__version__)
[cell 10 code]
# Reproducibility set up
# Set random seed for numpy
np.random.seed(42)
# Set random seed for TensorFlow
tensorflow.random.set_seed(42)
tensorflow.keras.utils.set_random_seed(42) # sets seeds for base-python, numpy and tf
tensorflow.config.experimental.enable_op_determinism()
[cell 11 code]
from keras.callbacks import TensorBoard
[cell 12 markdown]
## Load data
[cell 13 code]
(X_train_all, t_train_all), (X_test_all, t_test_all) = cifar10.load_data()
[cell 14 code]
# take a small sample of data
size = 50000
X_train = X_train_all[:size,:,:,:]
t_train = t_train_all[0:size,:]
X_test = X_test_all[0:size,:,:,:]
t_test = t_test_all[0:size,:]
[cell 15 code]
classes = (
'plane',
'car',
'bird',
'cat',
'deer',
'dog',
'frog',
'horse',
'ship',
'truck')
[cell 16 markdown]
## Check some data
[cell 17 code]
def check_one(data, label, id = None, actual = None, compare = False):
# check one
if id is None:
id = np.random.randint(data.shape[0])
im = data[id]
plt.figure(figsize = (3, 3))
plt.imshow(im)
l_id = label[id]
if (compare) and (actual is not None) and (l_id != np.argmax(actual[id])):
#if (compare) and (actual is not None) and (l_id != np.argmax(actual[id], axis=0)):
a_id = np.argmax(actual[id])
plt.title('Class %d (%s) [\u2260 %d-%s]' % (l_id, classes[l_id], a_id, classes[a_id]))
else:
plt.title('Class %d (%s)' % (l_id, classes[l_id]))
plt.xticks([])
plt.yticks([])
plt.show()
[cell 18 code]
def check_ten(data, label, actual = None, compare = False):
# check ten
fig, ax = plt.subplots(2, 5, figsize = (11, 5))
fig.subplots_adjust(left = 0.02, right = 0.98, top = 0.8, wspace = 0.2, hspace = 0.2)
fig.suptitle('Check Data', fontsize = 12, fontweight = 'bold')
plt.rcParams.update({'font.size': 10})
ids = np.random.randint(data.shape[0], size = 10)
r = 0
c = 0
for id in ids:
im = data[id]
# original image
ax[r, c].imshow(im)
l_id = label[id]
if (compare) and (actual is not None) and (l_id != np.argmax(actual[id])):
a_id = np.argmax(actual[id])
ax[r, c].set_title('Class %d (%s) [\u2260 %d-%s]' % (l_id, classes[l_id], a_id, classes[a_id]))
else:
ax[r, c].set_title('Class %d (%s)' % (l_id, classes[l_id]))
ax[r, c].set_xticks([])
ax[r, c].set_yticks([])
c += 1
if c > 4:
r += 1
c = 0
plt.show()
[cell 19 code]
check_one(X_train, t_train.flatten())
[cell 20 code]
check_ten(X_train, t_train.flatten())
[cell 21 markdown]
## Prepare data
[cell 22 code]
# Prepare input data
_, img_rows, img_cols, img_channels = X_train.shape
num_classes = len(set(t_train.flatten()))
# Convert the target to categorical
y_train = to_categorical(
t_train,
num_classes = num_classes)
y_test = to_categorical(
t_test,
num_classes = num_classes)
[cell 23 code]
t_train[0]
[cell 24 code]
y_train[0]
[cell 25 markdown]
## Create the model's architecture
[cell 26 code]
model = Sequential()
[cell 27 code]
model.add(Conv2D(48, kernel_size = 3, activation = 'relu', padding = 'same', input_shape = (32, 32, 3)))
model.add(Conv2D(48, kernel_size = 3, activation = 'relu'))
model.add(MaxPool2D(pool_size = (2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(96, kernel_size = 3, activation = 'relu', padding = 'same'))
model.add(Conv2D(96, kernel_size = 3, activation = 'relu'))
model.add(MaxPool2D(pool_size = (2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(192, kernel_size = 3, activation = 'relu', padding = 'same'))
model.add(Conv2D(192, kernel_size = 3, activation = 'relu'))
model.add(MaxPool2D(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(256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation = 'softmax'))
[cell 28 code]
print(model.summary())
[cell 29 markdown]
## Compile the model
[cell 30 code]
model.compile(optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
[cell 31 markdown]
## Fit the training data
[cell 32 code]
# Create a directory named 'logs' if it doesn't exist. This is necessary for TensorBoard to work
log_dir = 'logs'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
[cell 33 code]
tensorboard = TensorBoard(log_dir = 'logs')
[cell 34 code]
%%time
# Fit the model on a training set
history = model.fit(
X_train,
y_train,
validation_split = 0.2,
epochs = 10,
batch_size = 10,
callbacks = [tensorboard],
verbose = 1)
print(f'Training accuracy:{history.history["accuracy"][-1]:.2f} validation accuracy:{history.history["val_accuracy"][-1]:.2f} ')
[cell 35 code]
%load_ext tensorboard
%tensorboard --logdir logs
# If you are running into a "localhost refused to connect" issue, try running the following code in your anaconda powershell
# activate your environment
# and run this below:
# Remove-Item -Path $env:TMP\.tensorboard-info\* -ErrorAction Ignore
# Note: Everytime you restart your notebook, if you want to visualize your TensorBoard, you need to repeat the step above
[cell 36 markdown]
## Create predictions
[cell 37 code]
%%time
predictions = model.predict(X_test)
[cell 38 markdown]
## Evaluate the model
[cell 39 code]
score = model.evaluate(X_test, y_test, batch_size = 10)
print('\nTest loss: %.6f, Test accuracy: %.6f' % tuple(score))
[cell 40 code]
def print_cm(cm):
d_size = max(len('%d' % cm.max()), len('%d' % cm.shape[1]))
if min(cm.shape) > 10: # make sparse
print('Sparse Matrix (*=diagonal)')
fmt_c = ', c%%0%dd%%s= %%%dd' % (d_size, d_size)
for i in range(cm.shape[0]):
s = fmt_r % i
for j in range(cm.shape[1]):
if cm[i, j] > 0:
s += fmt_c % (j, '*' if i == j else ' ', cm[i, j])
print(s)
else: # make dense
c = '%%%dd ' % d_size
s = '%s| ' % (' ' * d_size)
s += ''.join([c % i for i in range(len(cm[0]))])
print(s)
print('-' * len(s))
for i, r in enumerate(cm):
s = '%%%dd| ' % d_size
s = s % i
s += c * len(r)
print(s % tuple(r))
[cell 41 code]
# Convert predicted probabilities to class labels
predictions_class = np.argmax(predictions, axis=1)
# Convert y_test to class indices for comparison with predictions
y_test_target = np.argmax(y_test, axis = 1)
# Now use these class labels to compute the confusion matrix
cm = confusion_matrix(y_test_target, predictions_class)
print_cm(cm)
[cell 42 code]
print(type(y_test_target), type(predictions_class))
print(len(y_test_target), len(predictions_class))
[cell 43 markdown]
## Visualization of cost
[cell 44 code]
# When you run the code below, it will display a list of available keys. These keys correspond to the recorded metrics during training.
print(history.history.keys())
[cell 45 code]
fig, ax = plt.subplots(1, 2, figsize = (18, 6))
fig.subplots_adjust(left = 0.02, right = 0.98, wspace = 0.2)
plt.rcParams.update({'font.size': 18})
# Plot training & validation accuracy values
ax[0].plot(history.history['accuracy'])
ax[0].plot(history.history['val_accuracy'])
ax[0].set_title('Model accuracy')
ax[0].set_ylabel('Accuracy')
ax[0].set_xlabel('Epoch')
ax[0].legend(['Train', 'Validation'])
# Plot training & validation loss values
ax[1].plot(history.history['loss'])
ax[1].plot(history.history['val_loss'])
ax[1].set_title('Model loss')
ax[1].set_ylabel('Loss')
ax[1].set_xlabel('Epoch')
ax[1].legend(['Train', 'Validation'])
plt.show()
[cell 46 markdown]
## Results
[cell 47 code]
check_one(X_test, predictions_class, actual = y_test, compare = True)
[cell 48 code]
check_ten(X_test, predictions_class, y_test, True)