Image Classification with Convolutional Neural Networks in TensorFlow
In my previous post Multiclass Classification Model, I
wrote about creating classification models using TensorFlow and Fashion MNIST dataset from Keras.
We used a Sequential model with several Dense layers to build a model categorising fashion items
into their respective categories, such as “T-shirt/top” or “Trouser.” The dataset was already
prepared for usage, and the model created was quite simple, however, quite efficient. We could
further improve our model. However, in practice, we rarely have an available dataset at hand.
We can generate or collect datasets. Moreover, a simple Dense layer-based Neural Network (NN) might
not work well with image data. I will focus on the more appropriate NN architecture type,
which best operates when dealing with image data. We also practice working with image data
presented in JPG format.
What Are Convolutional Neural Networks (CNN)?
A Convolutional Neural Network (CNN) is a deep neural network architecture that learns spatial features from grid-structured data such as images by applying trainable convolutional kernels. For Deep Learning applications on image data — visual object recognition, image segmentation, and classification — the CNN architecture requires few preprocessing steps and little human involvement, because the network learns its parameters (weights) automatically. To be used with CNN, images are presented as tensors (data structures such as matrices
holding numerical data for storing image pixels and their colors). For instance, tensors can be stored in a
3-dimensional matrices with RGB-color channels (Red, Green, and Blue) defined for each pixel is what we are going to explore in
this article. Generally, the CNN input is presented as a tensor with a shape:
(number of inputs) x (image height) x (image width) x (number of color channels).
For instance, the CNN explainer website presents a Tiny VGG neural network trained
to classify images. Per the CNN Explainer paper, the network consists of four hidden convolutional layers (organised as two convolution-convolution-pool blocks), and the input takes
pictures with a height and width of 64 pixels and three colour channels.
CNN are deep networks that use hidden Convolutional layers perfectly designed for working with image data and are widely applied
in computer vision problems. Conv2D layers perform mathematical convolution operations,
which use a small matrix called “kernel” (or “filter”) applied to the input image matrix.
The smaller kernel size usually leads to better performance, while a larger kernel
learns more prominent features [1].
The CNN explainer website is interactive, and we can press on a convolution element to see how the
kernel moving across its input window.
The kernel moves through the image with some defined step size called “stride”; in Keras’s Conv2D the default
strides value is (1, 1), meaning “pixel by pixel”. The output of a convolutional neuron is an element-wise
dot product with the previous layer’s output and its weight, plus bias [1].
The result of such moves is an activation map. The activation map is created to extract image patterns and determine
if an image region is relevant to a specific class. The activation function is usually a “ReLU”.
ReLU fits well to non-linear data. If you are interested in activation functions, please read my post
“Artificial Neural Networks” about neural networks and some widely-used activation functions.
A CNN is a fully connected deep network. It thus operates with many parameters, which can affect negatively computational effectiveness and
even lead to overfitting (when the model “learns” training data by “heart” while failing
to work effectively with new data). This is why we can add “Pooling layers,” decreasing the spatial
extend of the network while discarding some percentage of activations. We are going to use
TensorFlow’s MaxPool2D layer, which condenses the inputs and helps find out the most essential parts of
the features found by previous convolutional layers [1].
In this post, I will reproduce an architecture similar to the Tiny VGG network. However, I will use
a different Bird images dataset downloaded from Kaggle. I will also show how we can improve this model make and
plot predictions. You are encouraged to further improve this model and send me your code!
Downloading the Kaggle Bird Species Dataset in Google Colab
I am using Google Colab notebooks with GPU
support. You can enable the GPU with Runtime -> Change runtime type -> Hardware accelerator = GPU.
For downloading Kaggle Datasets, you will need to perform a setup described by Kaustubh Gupta [2].
Herein you see the adopted code for downloading the Bird species Kaggle dataset.
There are three datasets, including 58388 train, 2000 test, and 2000 validation
224x224x3 JPG images.
You can check your Nvidia information with:
Fri Feb 18 17:16:01 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.32.03 Driver Version: 460.32.03 CUDA Version: 11.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla K80 Off | 00000000:00:04.0 Off | 0 |
| N/A 34C P8 27W / 149W | 0MiB / 11441MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
For using Kaggle datasets, we need a Kaggle API token to be generated and downloaded in
your Kaggle profile. You will need to create a Kaggle profile and go into the “Account” section.
When you press “Create New API Token,” the kaggle.json file is saved (it is kept in ~/Downloads for me).
This JSON can be further used for all datasets. You will need to upload the kaggle.json file to
Google Colab before running the commands below.
Inspecting Image Data: Counting Files and Reading Class Names
🔒 Subscribe to keep reading.
Image Preprocessing: Normalising Pixels with ImageDataGenerator
🔒 Subscribe to keep reading.
Creating and Testing CNN Models (Tiny VGG Architecture)
🔒 Subscribe to keep reading.
Conclusion: CNNs and Data Augmentation for Image Classification
🔒 Subscribe to keep reading.
References
🔒 Subscribe to keep reading.