Convolutional Neural Networks with Tensorflow (Safari Dataset)

“Deep Learning is a general term that usually refers to the use of neural networks with multiple layers that synthesize the way the human brain learns and makes decisions. A convolutional neural network is a kind of neural network that extracts features from matrices of numeric values (often images) by convolving multiple filters over the matrix values to apply weights and identify patterns, such as edges, corners, and so on in an image. The numeric representations of these patterns are then passed to a fully-connected neural network layer to map the features to specific classes.”

In this article, we are going to use Tensorflow to solve the classification problem. We are going to use Safari Dataset. I have solved this challenge in my learning so the dataset is not available. However, feel free to use datasets you want such as Fingerprint (SOCOFing) or Breast Cancer Dataset (BreakHis), etc. Search them on kaggle, they should be available. Alright, let’s start!

First up, upgrade the Tensorflow library:

!pip install — upgrade tensorflow

Second, let’s verify we have everything that we need:

import tensorflow
from tensorflow import keras
print('TensorFlow version:',tensorflow.__version__)
print('Keras version:',keras.__version__)

Always a good practice to do the above steps! Alright, let’s explore the dataset and see what they look like:

import numpy as np
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline

The dataset is placed in the following directory (my local system):

Dataset!

That means, elephant, giraffe, lion, and zebra folder exists in the training directory. We have to select the images from these directories and pre-process them to fed into the CNN model. Let’s see the dataset visualization by using the above code:

Four Labels (Classifications Right?)

Now, we are going to pre-process the dataset so that it is ready to train in the CNN model that we are going to initialize later!

# Data Pre-processing
from tensorflow.keras.preprocessing.image import ImageDataGenerator

In Tensorflow, ImageDataGenerator is used to process the image dataset! We have set the image size and batch size. We are going to set all the images by 128x128 pixels and feed 30 batches of data in each training. Using these parameters, we have used “flow_from_directory” to generate the training and validation data from the directories. So, training data means, we are going to use these data to learn the model, and “validation dataset is a sample of data held back from training your model that is used to give an estimate of model skill while tuning model’s hyperparameters”.

Next up, let’s check the features and labels:

# let's check the shapes of the training dataset
train_gen.image_shape
# that means 3 sample of the data with 128x128 pixels will enter the CNN Model: (128, 128, 3)

Now, we know about the features and labels. Also, we have a training and validation set. Let’s create the CNN model.

# Building the CNN Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense

In the CNN model, we have used three hidden layers along with MaxPooling2D and Model Dropouts. We have also used Conv2D layers to filter the image to extract the features. In the model, you will be able to see Flatten() the model. Before that, everything is used to extract the features from the images, and then finally we have used Adam optimizer, and Categorical Crossentropy to classify the image properly. The most important thing in the CNN model is to specify the input features shape (first layer: (128, 128, 3)) and label shape (last layer: 4). let’s observe the model structure:

Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 123, 123, 32) 3488
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 61, 61, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 56, 56, 32) 36896
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 28, 28, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 23, 23, 32) 36896
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 11, 11, 32) 0
_________________________________________________________________
dropout (Dropout) (None, 11, 11, 32) 0
_________________________________________________________________
flatten (Flatten) (None, 3872) 0
_________________________________________________________________
dense (Dense) (None, 4) 15492
=================================================================
Total params: 92,772
Trainable params: 92,772
Non-trainable params: 0
_________________________________________________________________
None

Now, it is time to train the model:

# Train the model over 5 epochs and using the validation holdout dataset for validation
num_epochs = 5
history = model.fit(
train_gen,
steps_per_epoch = train_gen.samples // batch_size,
validation_data = val_gen,
validation_steps = val_gen.samples // batch_size,
epochs = num_epochs)

During the training, we have used 5 epochs which you can see from the output. We have used the batch size to divide the input into 5 epochs which are 9. You can see the validation accuracy from the output which shows the model performances while fine-tuning the parameters. The model produces 100% model accuracy. It makes sense because the dataset is limited! Well, let’s look at loss function projection for both training and validation!

# let's view the loss throughout the training
%matplotlib inline
from matplotlib import pyplot as plt

Output:

Loss!!

We can also use the confusion matrix to further verify that our model is suitable in this particular case!

# Further Verification of model efficiency
import numpy as np
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
%matplotlib inline

And the confusion Matrix would be:

Perfect Classifier!

Also, we can save the model to test it with the testing images. Let’s see how to save the model to the local directory!

# Save the trained model
modelFileName = 'models/safari_classifier.h5'
model.save(modelFileName)
del model # deletes the existing model variable
print('model saved as', modelFileName)

Later on, we can reload the model and test it with the new images! The benefit is that we don’t have to retrain the CNN model again! More about saving and loading the model can be found here. Well, let’s leave that part up to you! Go have fun with the CNN model!

Cheers! 😃

Data Scientists / Full Stack Deep Learning

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store