Deep Learning for Beginners Using TensorFlow

Build CNN model on GTSRB Dataset (acc > 80%)

Hargurjeet
Analytics Vidhya
6 min readApr 24, 2021

--

Photo by Brodie Vissers on burst

Deep learning is also known as deep neural learning or deep neural network. This branch of machine learning has evolved and developed with the advancement of technology.
One such platform to implement Deep learning is TensorFlow. Let us briefly discuss tensorflow.

  1. What is Tensorflow ?
    TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries and community resources that lets researchers push the state-of-the-art in ML and developers easily build and deploy ML powered applications.
  2. What is CNN model ?
    A Convolutional Neural Network (ConvNet/CNN) is a Deep Learning algorithm which can take in an input image, assign importance (learnable weights and biases) to various aspects/objects in the image and be able to differentiate one from the other. The pre-processing required in a ConvNet/CNN is much lower as compared to other classification algorithms. Hence CNN is most preferred algorithm for processing images

About Dataset

German Traffic Sign Recognition Dataset (GTSRB) is an image classification dataset. The images are photos of traffic signs. The images are classified into 43 classes. The training set contains 39209 labeled images and the test set contains 12630 images. Labels for the test set are not published.

Table of contents

  1. Introduction
  2. Data Pre Processing
    2.1 Loading the required libraries
    2.2 Get Data
  3. Exploring the GTRSB dataset
    3.1 How many images does the training and testing dataset contain ?
    3.2 How many output classes does the dataset contain ?
    3.3 What is the shape of an image tensor from the dataset ?
    3.4 Let us print a sample image along with along with its class ?
    3.5 Determine the number of images belonging to class in training set ?
  4. Setting up the CNN model
    4.1 Setting up the ImageDataGenerators
    4.2 Configuring the TensorFlow generators
    4.3 Building the CNN Model
  5. Training the model
    5.1 Model training and results analysis
    5.2 Running the model on test data
    5.3 Accuracy Score
    5.4 Confusion matrix
    5.5 Classification report
  6. Testing model on test images dataset
  7. Summary
  8. Future Work
  9. References

№1: Introduction

The German Traffic Sign Benchmark is a multi-class, single-image classification challenge held at the International Joint Conference on Neural Networks (IJCNN) 2011. Following are the details of the dataset

  1. Single-image, multi-class classification problem
  2. More than 40 classes
  3. More than 50,000 images in total
  4. Large, lifelike database

№2: Data Pre processing

Loading the required Libraries

I load all the TensorFlow and visualization libraries in one go.

Get Data

I use a library opendatasets to download the data from Kaggle.

I read all the downloaded files using the pandas DataFrame.

I now access sample records from each DataFrame.

№3: Exploring the GTRSB dataset

Q : How many images does the training and testing dataset contain ?

Q: How many output classes does the dataset contain ?

Q: What is the shape of an image from the dataset ?

It is evident that all the images within the dataset are of different size.

Q: Let us print a sample image along with along with its class ?

Q: Determine the number of images belonging to each class in training set ?

It is observed that few classes seems to have high no of images(like class 2,3..etc.) where as few classes are having very low no of images (like 19, 24…etc)

№4: Setting up the CNN model

Setting up the ImageDataGenerators

In TensorFlow, data augmentation is accomplished using the ImageDataGenerator class. It is exceedingly simple to understand and to use. The entire dataset is looped over in each epoch, and the images in the dataset are transformed as per the options and values selected.

Configuring the TensorFlow generators

The idea behind using a train generator is to get batches of input and corresponding output on the fly during training process, e.g. reading in 32 images, getting corresponding 32 label vectors and then feeding this set to the gpu for training step.

The flow from directory expects images in each class to be in an independent directory. This is what we had from the beginning.

Building the CNN Model

I use the keras wrapper to chain together the layers.

№5: Training the model

Model training and results analysis

An accuracy of 85 percent is achieved. Even after training the model for few more epochs the accuracy wont improve but we may end of overfitting the model. Let us see how our model is performing on the test dataset.

Note — Our model has never seen the test images hence it would be interesting to see the predictions.

Running the model on test data

As the test images are not categorized to its respective classes, We will pass the folder name which contains all the test images in ‘classes’ parameter as ‘Test’

Now I write a code block which would map the predictions to the respective images

Accuracy Score

I calculate the accuracy of the predicted class against the actual labels. For this I leverage sklearn library

This is great we have now achieved the accuracy of 87% on the test set.

Confusion matrix

A confusion matrix is a table that is often used to describe the performance of a classification model (or “classifier”) on a set of test data for which the true values are known.

Classification report

A classification score is any score or metric the algorithm is using (or the user has set) that is used in order to compute the performance of the classification. As a rule of thumb, the weighted average of F1 should be used to compare classifier models, not global accuracy. The current model exhibit's a score of 87% 🆒

№6: Testing with individual images

Let us randomly check the predicted and actual values along with the image.

№7: Summary

Here is a brief summary of the step by step process we followed

  1. We briefly learned about the TensorFlow and why CNN architecture is preferred for image datasets.
  2. We downloaded the dataset from Kaggle and access it using Pandas.
  3. We explored the GTRSB dataset and understood the overall images each class has, total images in training set, displayed sample images etc.
  4. We setup the CNN model and learned working with ImageDataGenerators, configuring testing and training generators and building connected layers of deep neural network using Tensorflow and Keras.
  5. We trained the CNN model and achieved an accuracy of about 85%. We also analyzed the various of accuracy and val_accuracy while training w.r.t no of epochs. We noted down our conclusions.
  6. We randomly checked the model performance by running it on the few testing samples

№8: Future Work

  1. Try difference combination of layers within the neural network to further improve accuracy.
  2. Try build the neural network on PyTorch and see if similar accuracy can be achieved.
  3. Try implement different CNN architectures like
    - LeNet -5
    - AlexNet
    - GoogleLeNet
    - VGGNet
    - ResNet

№9: References

  1. The complete notebook can be accessed here https://github.com/hargurjeet/DeepLearning/blob/main/GTRSB%20-%20CNN%20(TensorFlow).ipynb
  2. https://www.tensorflow.org/tutorials/keras/classification
  3. https://www.kaggle.com/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign
  4. https://www.coursera.org/professional-certificates/tensorflow-in-practice

I really hope you guys learned something from this post. Feel free to give a 👏if you like what you learnt. This keeps me motivated.

--

--

Hargurjeet
Analytics Vidhya

Data Science Practitioner | Machine Learning | Neural Networks | PyTorch | TensorFlow