Convolutional Net¶
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np
WARNING:tensorflow:From C:\Users\tkvkh\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.
Load data¶
The data that we are loading is the famous CIFAR10 dataset, consists of 10 classes of object.
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images.astype("float") / 255.0, test_images.astype("float") / 255.0
Image labels¶
Assume that we don't know about the image label, we can check by see how many unique value of the labels vector. The code below shows that we have 10 classes, which is true according to the description of CIFAR dataset
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
n_classes = np.unique(train_labels)
print("Total number of classes:", len(n_classes))
Total number of classes: 10
print("Image of:", class_names[train_labels[0,0]])
plt.imshow(train_images[0])
plt.show()
Image of: frog
Convolutional layers¶
input_shape = train_images[0].shape
model = models.Sequential([
layers.Conv2D(32, (3,3), activation="relu", input_shape=input_shape),
layers.MaxPool2D((2,2)),
layers.Conv2D(64, (3,3), activation="relu"),
layers.MaxPool2D((2,2)),
layers.Conv2D(64, (3,3), activation="relu"),
])
model.summary()
WARNING:tensorflow:From C:\Users\tkvkh\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\backend.py:873: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead. WARNING:tensorflow:From C:\Users\tkvkh\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\layers\pooling\max_pooling2d.py:161: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead. Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 30, 30, 32) 896 max_pooling2d (MaxPooling2 (None, 15, 15, 32) 0 D) conv2d_1 (Conv2D) (None, 13, 13, 64) 18496 max_pooling2d_1 (MaxPoolin (None, 6, 6, 64) 0 g2D) conv2d_2 (Conv2D) (None, 4, 4, 64) 36928 ================================================================= Total params: 56320 (220.00 KB) Trainable params: 56320 (220.00 KB) Non-trainable params: 0 (0.00 Byte) _________________________________________________________________
Dense layers¶
After the convolutional layers, now we can add dense connected layers with the final prediction layers of 10 neurons represent 10 possible outcomes of an image
model.add(layers.Flatten())
model.add(layers.Dense(64, activation="relu"))
model.add(layers.Dense(10))
model.summary()
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 30, 30, 32) 896 max_pooling2d (MaxPooling2 (None, 15, 15, 32) 0 D) conv2d_1 (Conv2D) (None, 13, 13, 64) 18496 max_pooling2d_1 (MaxPoolin (None, 6, 6, 64) 0 g2D) conv2d_2 (Conv2D) (None, 4, 4, 64) 36928 flatten (Flatten) (None, 1024) 0 dense (Dense) (None, 64) 65600 dense_1 (Dense) (None, 10) 650 ================================================================= Total params: 122570 (478.79 KB) Trainable params: 122570 (478.79 KB) Non-trainable params: 0 (0.00 Byte) _________________________________________________________________
Train the model¶
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
WARNING:tensorflow:From C:\Users\tkvkh\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\optimizers\__init__.py:309: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead. Epoch 1/10 WARNING:tensorflow:From C:\Users\tkvkh\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\tf_utils.py:492: The name tf.ragged.RaggedTensorValue is deprecated. Please use tf.compat.v1.ragged.RaggedTensorValue instead. WARNING:tensorflow:From C:\Users\tkvkh\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\engine\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead. 1563/1563 [==============================] - 13s 7ms/step - loss: 1.5181 - accuracy: 0.4455 - val_loss: 1.2971 - val_accuracy: 0.5301 Epoch 2/10 1563/1563 [==============================] - 11s 7ms/step - loss: 1.1683 - accuracy: 0.5844 - val_loss: 1.0628 - val_accuracy: 0.6185 Epoch 3/10 1563/1563 [==============================] - 11s 7ms/step - loss: 1.0151 - accuracy: 0.6431 - val_loss: 1.0282 - val_accuracy: 0.6405 Epoch 4/10 1563/1563 [==============================] - 11s 7ms/step - loss: 0.9172 - accuracy: 0.6779 - val_loss: 0.9613 - val_accuracy: 0.6616 Epoch 5/10 1563/1563 [==============================] - 11s 7ms/step - loss: 0.8464 - accuracy: 0.7037 - val_loss: 0.9102 - val_accuracy: 0.6854 Epoch 6/10 1563/1563 [==============================] - 11s 7ms/step - loss: 0.7895 - accuracy: 0.7248 - val_loss: 0.8828 - val_accuracy: 0.6940 Epoch 7/10 1563/1563 [==============================] - 11s 7ms/step - loss: 0.7383 - accuracy: 0.7416 - val_loss: 0.9317 - val_accuracy: 0.6794 Epoch 8/10 1563/1563 [==============================] - 11s 7ms/step - loss: 0.6951 - accuracy: 0.7555 - val_loss: 0.8673 - val_accuracy: 0.7015 Epoch 9/10 1563/1563 [==============================] - 11s 7ms/step - loss: 0.6588 - accuracy: 0.7705 - val_loss: 0.8494 - val_accuracy: 0.7109 Epoch 10/10 1563/1563 [==============================] - 11s 7ms/step - loss: 0.6214 - accuracy: 0.7833 - val_loss: 0.9158 - val_accuracy: 0.6958
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
313/313 - 1s - loss: 0.9158 - accuracy: 0.6958 - 802ms/epoch - 3ms/step
Conclusion¶
We can see that convolutional network is more powerful than just simple neural network since in the convolutional layers, it will extract features from the image. With CIFAR10 dataset consists of 10 completely different classes of object but the model still manage to achieve accuracy of 77.28%