import tensorflow as tf
from tensorflow import keras
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt
# Print the version of tf, and tfds
print("TensorFlow's version used in notebook:{}".format(tf.__version__))
print("TensorFlow datasets's version used in notebook:{}".format(tfds.__version__))
TensorFlow's version used in notebook:2.5.0
TensorFlow datasets's version used in notebook:4.0.1
# (train_data, train_label), (test_data, test_label) = keras.datasets.cifar10.load_data()
(train_data, val_data, test_data), info = tfds.load('cifar10',
split=['train', 'test[:50%]','test[50%:]'],
as_supervised=True,
shuffle_files=False,
with_info=True
)
Downloading and preparing dataset cifar10/3.0.2 (download: 162.17 MiB, generated: 132.40 MiB, total: 294.58 MiB) to /root/tensorflow_datasets/cifar10/3.0.2...
IPyWidgets are not supported
IPyWidgets are not supported
IPyWidgets are not supported
IPyWidgets are not supported
Shuffling and writing examples to /root/tensorflow_datasets/cifar10/3.0.2.incompleteDB9DNF/cifar10-train.tfrecord
IPyWidgets are not supported
IPyWidgets are not supported
Shuffling and writing examples to /root/tensorflow_datasets/cifar10/3.0.2.incompleteDB9DNF/cifar10-test.tfrecord
IPyWidgets are not supported
Dataset cifar10 downloaded and prepared to /root/tensorflow_datasets/cifar10/3.0.2. Subsequent calls will reuse this data.
print("The number images in training set: {}".format(len(train_data)))
print("The number images in validation set: {}".format(len(val_data)))
print("The number images in test set: {}".format(len(test_data)))
The number images in training set: 50000
The number images in validation set: 5000
The number images in test set: 5000
# Getting the number of examples
info.splits['train'].num_examples
# Getting the number of examples
info.splits['test'].num_examples
# getting class names
info.features['label'].names
# Number of classes
info.features['label'].num_classes
fig = tfds.show_examples(train_data, info)
def preprocess(image, label):
"""
Take image and label,
convert images to float32, and return converted image &label
"""
normalized_img = tf.cast(image, tf.float32)/255.0
return normalized_img, label
def train_data_prep(data, shuffle_size, batch_size):
data = data.map(preprocess)
data = data.cache()
data = data.shuffle(shuffle_size).repeat()
data = data.batch(batch_size)
data = data.prefetch(1)
return data
def test_data_prep(data, batch_size):
data = data.map(preprocess)
data = data.batch(batch_size)
data = data.cache()
data = data.prefetch(1)
return data
train_data_prepared = train_data_prep(train_data, 1000, 32)
test_data_prepared = test_data_prep(test_data, 32)
val_data_prepared = test_data_prep(val_data, 32)
input_shape = [32,32,3]
cifar_cnn = tf.keras.models.Sequential([
# First Convolutional layer: 64 filters, kernel/filter size of 3
tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu', input_shape=input_shape),
# First Pooling layer
tf.keras.layers.MaxPooling2D(pool_size=2),
# Second Convolutional layer & Pooling
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=2),
# Third Convolutional & Pooling layer
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=2),
# Flattening layer: For converting previous output into 1D column vector
tf.keras.layers.Flatten(),
# Fully Connected layers
tf.keras.layers.Dense(units=128, activation='relu'),
tf.keras.layers.Dense(units=64, activation='relu'),
# Last layer: 10 neurons for 10 classes, activated by softmax
tf.keras.layers.Dense(units=10, activation='softmax')
])
# Model summary
cifar_cnn.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_3 (Conv2D) (None, 30, 30, 64) 1792
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 15, 15, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 13, 13, 128) 73856
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 6, 6, 128) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 4, 4, 256) 295168
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 2, 2, 256) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 1024) 0
_________________________________________________________________
dense_3 (Dense) (None, 128) 131200
_________________________________________________________________
dense_4 (Dense) (None, 64) 8256
_________________________________________________________________
dense_5 (Dense) (None, 10) 650
=================================================================
Total params: 510,922
Trainable params: 510,922
Non-trainable params: 0
_________________________________________________________________
tf.keras.utils.plot_model(cifar_cnn, rankdir='LR',show_dtype=True)
cifar_cnn.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
batch_size = 32
train_size = info.splits["train"].num_examples
val_size = len(val_data_prepared)
train_steps = int(train_size/batch_size)
val_steps = int(val_size/batch_size)
model_history = cifar_cnn.fit(train_data_prepared,
validation_data=val_data_prepared,
epochs=9,
steps_per_epoch=train_steps,
validation_steps=val_steps)
Epoch 1/9
1562/1562 [==============================] - 8s 5ms/step - loss: 1.0155 - accuracy: 0.6438 - val_loss: 1.1868 - val_accuracy: 0.5859
Epoch 2/9
1562/1562 [==============================] - 8s 5ms/step - loss: 0.8516 - accuracy: 0.7007 - val_loss: 0.9781 - val_accuracy: 0.6484
Epoch 3/9
1562/1562 [==============================] - 8s 5ms/step - loss: 0.7368 - accuracy: 0.7428 - val_loss: 0.9412 - val_accuracy: 0.6797
Epoch 4/9
1562/1562 [==============================] - 8s 5ms/step - loss: 0.6447 - accuracy: 0.7745 - val_loss: 0.8999 - val_accuracy: 0.6719
Epoch 5/9
1562/1562 [==============================] - 7s 5ms/step - loss: 0.5623 - accuracy: 0.8032 - val_loss: 0.9244 - val_accuracy: 0.6641
Epoch 6/9
1562/1562 [==============================] - 7s 5ms/step - loss: 0.5041 - accuracy: 0.8230 - val_loss: 1.0261 - val_accuracy: 0.6953
Epoch 7/9
1562/1562 [==============================] - 7s 5ms/step - loss: 0.4413 - accuracy: 0.8441 - val_loss: 1.0780 - val_accuracy: 0.6562
Epoch 8/9
1562/1562 [==============================] - 7s 5ms/step - loss: 0.3939 - accuracy: 0.8621 - val_loss: 1.0975 - val_accuracy: 0.7031
Epoch 9/9
1562/1562 [==============================] - 7s 5ms/step - loss: 0.3516 - accuracy: 0.8763 - val_loss: 1.1452 - val_accuracy: 0.7188
import pandas as pd
# Getting the model training history
model_history = model_history.history
# Model history into a pandas dataframe
loss_acc_df = pd.DataFrame(model_history)
# Plotting the loss and accuracy
loss_acc_df.plot(figsize=(10,5))
acc = model_history['accuracy']
val_acc = model_history['val_accuracy']
loss = model_history['loss']
val_loss = model_history['val_loss']
epochs = cifar_cnn.history.epoch
# function to plot accuracy
def plot_acc(acc, val_acc, epochs):
plt.figure(figsize=(10,5))
plt.plot(epochs, acc, 'r', label='Training Accuracy')
plt.plot(epochs, val_acc, 'g', label='Validation Accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()
def plot_loss(loss, val_loss, epochs):
plt.figure(figsize=(10,5))
plt.plot(epochs, loss, 'b', label='Training Loss')
plt.plot(epochs, val_loss, 'y', label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend(loc=0)
plt.figure()
#Plotting acc
plot_acc(acc, val_acc, epochs)
plot_loss(loss, val_loss, epochs)
# reloading the data
# Training set = 80% of orginal training set (50,000 images)
# Validation set = 20% of training set = 10,000 images
# Test set = full orginal test set = 10,000 images
(train_data, val_data, test_data), info = tfds.load('cifar10',
split=['train[:80%]', 'train[80%:]','test'],
as_supervised=True,
shuffle_files=True,
with_info=True
)
train_final = train_data_prep(train_data, 1000, 32)
val_final = test_data_prep(val_data, 32)
test_final = test_data_prep(test_data, 32)
input_shape = [32,32,3]
cifar_cnn_2 = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), padding='same',activation='relu', input_shape=input_shape),
tf.keras.layers.MaxPooling2D(pool_size=2),
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=2),
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(units=128, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(units=10, activation='softmax')
])
# Nadam optimizer is a version of adam
cifar_cnn_2.compile(loss='sparse_categorical_crossentropy',
optimizer='nadam',
metrics='accuracy')
# Training the model for 10 epochs
batch_size = 32
train_size = 40000
val_size = 10000
train_steps = int(train_size/batch_size)
val_steps = int(val_size/batch_size)
model_history_2 = cifar_cnn_2.fit(train_final,
validation_data=val_final,
epochs=10,
steps_per_epoch=train_steps,
validation_steps=val_steps)
Epoch 1/10
1250/1250 [==============================] - 8s 6ms/step - loss: 1.6176 - accuracy: 0.4100 - val_loss: 1.2067 - val_accuracy: 0.5677
Epoch 2/10
1250/1250 [==============================] - 7s 6ms/step - loss: 1.2107 - accuracy: 0.5696 - val_loss: 0.9822 - val_accuracy: 0.6585
Epoch 3/10
1250/1250 [==============================] - 7s 6ms/step - loss: 1.0364 - accuracy: 0.6356 - val_loss: 0.8793 - val_accuracy: 0.6897
Epoch 4/10
1250/1250 [==============================] - 7s 6ms/step - loss: 0.9407 - accuracy: 0.6689 - val_loss: 0.7914 - val_accuracy: 0.7224
Epoch 5/10
1250/1250 [==============================] - 8s 6ms/step - loss: 0.8663 - accuracy: 0.7006 - val_loss: 0.7671 - val_accuracy: 0.7341
Epoch 6/10
1250/1250 [==============================] - 8s 6ms/step - loss: 0.8183 - accuracy: 0.7145 - val_loss: 0.7386 - val_accuracy: 0.7490
Epoch 7/10
1250/1250 [==============================] - 8s 6ms/step - loss: 0.7659 - accuracy: 0.7297 - val_loss: 0.7462 - val_accuracy: 0.7378
Epoch 8/10
1250/1250 [==============================] - 7s 6ms/step - loss: 0.7292 - accuracy: 0.7430 - val_loss: 0.7406 - val_accuracy: 0.7443
Epoch 9/10
1250/1250 [==============================] - 8s 6ms/step - loss: 0.6991 - accuracy: 0.7552 - val_loss: 0.7469 - val_accuracy: 0.7402
Epoch 10/10
1250/1250 [==============================] - 8s 6ms/step - loss: 0.6683 - accuracy: 0.7652 - val_loss: 0.7016 - val_accuracy: 0.7586
model_history = model_history_2.history
acc = model_history['accuracy']
val_acc = model_history['val_accuracy']
loss = model_history['loss']
val_loss = model_history['val_loss']
epochs = model_history_2.epoch
# Plot accuracy
plot_acc(acc, val_acc, epochs)
# Plot loss
plot_loss(loss, val_loss, epochs)
# Evaluating a model on test set
loss, acc = cifar_cnn_2.evaluate(test_final)
313/313 [==============================] - 2s 7ms/step - loss: 0.7157 - accuracy: 0.7552
# Print loss and acc with 3 only decimal points
print('Test Loss: {:.3f}'.format(loss))
print('Test Accuracy: {:.3f}'.format(acc))
Test Loss: 0.716
Test Accuracy: 0.755
# Saving a model
cifar_cnn_2.save('Cifar Best.h5')
# Loading a saved model
from keras.models import load_model
load_cifar = load_model('Cifar Best.h5')
load_cifar.summary()
Model: "sequential_8"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_24 (Conv2D) (None, 32, 32, 64) 1792
_________________________________________________________________
max_pooling2d_24 (MaxPooling (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_25 (Conv2D) (None, 16, 16, 128) 73856
_________________________________________________________________
max_pooling2d_25 (MaxPooling (None, 8, 8, 128) 0
_________________________________________________________________
conv2d_26 (Conv2D) (None, 8, 8, 256) 295168
_________________________________________________________________
max_pooling2d_26 (MaxPooling (None, 4, 4, 256) 0
_________________________________________________________________
flatten_8 (Flatten) (None, 4096) 0
_________________________________________________________________
dropout_10 (Dropout) (None, 4096) 0
_________________________________________________________________
dense_19 (Dense) (None, 128) 524416
_________________________________________________________________
dropout_11 (Dropout) (None, 128) 0
_________________________________________________________________
dense_20 (Dense) (None, 10) 1290
=================================================================
Total params: 896,522
Trainable params: 896,522
Non-trainable params: 0
_________________________________________________________________