Training an image classifier using deep learning.

Suraj
4 min readJan 25, 2021

Hi, Welcome to this blog! In this blog, I will be teaching you to train a vision-based deep learning model to classify dogs and cats, which can easily be extended to your desired classes.

Without further ado, let’s get started! Before training any model, we need to collect, clean, analyse and pre-process the data. These are mandatory steps as often the collected data unwanted outliers which adversely affect the trained model’s performance. We’ll be using Kaggle’s famous Dogs vs Cats dataset for the same and Google Colab for training our model. The first step is to download the dataset in your localhost(Laptop/Personal Computer) and upload it in a folder on your Google Drive as shown in Figure 1.

Figure 1: Dataset uploaded on Google Drive.

Now, you should open a new notebook in Google Colab and mount Gdrive by clicking the Files icon on vertical left side panel followed by clicking the Mount Drive button. Once Google Drive is mounted, you are expected to go inside the folder and unzip the file.

#Migrate inside the Gdrive folder where you Dogs_cats.zip(I have #renamed the original one) is placed
cd /content/drive/MyDrive/Colab Notebooks/Dogs_cats_dataset
#unzip the dataset
!unzip Dogs_cats.zip

Once you have successfully unzipped the dataset, you will see two folders train and test. You can try it by yourself through ls command in Colab.

! ls
> Dogs_cats.zip test train

Now, from hereon you can open the colab notebook in a new tab to follow the steps which basically include checking the number of images inside the train and test folders and later pre-processing(resizing etc) them and appending them based upon their file naming convention in image_features and label_vectors list. Due to the RAM limitations of Google Colab, we chose 1000 image samples per class and 96*96 as the input image resolution. If you wish to train the network on your local machine with more number of samples, you can download the Jupyter notebook file from Github and change the dataset directory, input image resolution and number of samples in the pre-processing block accordingly.

It is to be noted that this approach has been adopted because we didn’t have the hierarchy where train folder contains subfolders renamed with class_labels and respective images as the content. If a dataset’s train directory’s hierarchy is similar to the aforementioned then one can feel free to use the ImageDataGenerator of Keras as depicted in the Official documentation of Keras

In the cell and onwards, we have split the training dataset into train and val, enquired about the number of samples per class in each training and validation set along with converting the labels to one hot-encoded. Till this stage, we have pre-processed our dataset for training a deep learning architecture for binary/multi-class classification. We were lucky that we didn’t encounter class imbalance or equivalent problems in the cell else the pre-processing stage would have more steps.

We decided to use EfficientNet B7 by Google from Keras application for incorporating transfer learning on our pre-processed dataset and the same can be viewed in the cell onwards. It is to be noted that while training transfer learning models for classification, we have to freeze the intermediate layers by setting their trainable flag to False. Also, if you plan to use any other input size than the original weight’s input size then don’t forget to set “include_top=False” while instantiating the model.

From cell onwards, we compiled the model with Adam and binary cross-entropy loss(change it to categorical cross-entropy if the number of classes is greater than 2; Also sigmoid is preferred as activation of the last layer in binary class classification but I intentionally set it to softmax for generalised version.)

It can be observed that after training for 10 epochs, the model’s accuracy(training and validation) were greater than 90% as depicted by the loss and accuracy plots in the cell. We considered the model to be inference ready and thus saved the model to .h5 file for later usage out here. We also loaded the model and tested it over 10 random images from the test set to achieve significant accuracy in the cell onwards.

Hope this detailed walk-through of the steps through Google Colab notebook gave you the tutorial to train any classification model on a dataset(clean) through transfer learning approaches. In case, your dataset is not clean I request you to follow standard data engineering techniques to make it training compatible.

Until, next time! Thank you!

--

--

Suraj

Seasoned machine learning engineer/data scientist versed with the entire life cycle of a data science project.