MITB Banner

FastAI With TPU In PyTorch For Multiclass Image Classification

Share

FastAI With TPU In PyTorch

Computer vision is one of the most trending subfields in Artificial Intelligence because of its wide variety of applications. In some domains, they even suppress human intelligence in recognising images with speed and accuracy. In this article, we will demonstrate one of the most popular computer vision applications — multiclass image classification problems using fastAI library and TPU as the hardware accelerator. TPU, or Tensor Processing Unit accelerates the training process of heavy deep learning models.

Topics covered in this article:

  • Multiclass Image classification
  • Popular models for image classification
  • Hands-on implementation in PyTorch using TPU

Multiclass Image classification

We use image classification for recognising objects in the image and can be used in detecting brand names using logo, classifying objects, etc. But these solutions have a limitation that they can just recognise the objects but they cannot find the location of the object. But for recognising the objects these image classification models are easy to implement compared to object localisation.

Popular Models for the Image Classification

We can use VGG -16,19, Resnet, Inception v1,v2,v3, Wideresnt, Resnext, DenseNet etc, which are the advanced variants of a convolutional neural network. These are the popular image classification networks and used as the backbone for many state-of-the-art object detection and segmentation algorithms.

Image Classification using FasAI library and TPU hardware

We will implement this work in the following steps:-

1. Selecting Hardware Accelerator

Here we are using Google Colab for our implementation. To use TPU in Google Colab, we need to open the edit option then notebook settings, and change the hardware accelerator to TPU.

By running the below code snippet you can check whether your notebook is using TPU or not.

import os

assert os.environ['COLAB_TPU_ADDR']

Path = 'grpc://'+os.environ['COLAB_TPU_ADDR']

print('TPU Address:', Path)

2. Loading FastAI library

In the below code snippet, we will import the fastAI library. 

from fastai.vision import *

from fastai.metrics import error_rate, accuracy

3. Customised Dataset

In the below code snippet, you can also try with your customised dataset.

PATH = '/content/images/dataset'

np.random.seed(24)

tfms = get_transforms(do_flip=True)

data = ImageDataBunch.from_folder(PATH, valid_pct=0.2, ds_tfms=tfms, size=299, bs=16).normalize(imagenet_stats)

data.show_batch(rows=4, figsize=(8, 8))

4. Loading Pre-Trained Deep Learning Model

In the below code snippet, we are going to import the VGG-19 batch_normalisation model. We will use this as an instance of the computer vision learner module of fastAI.

learn = cnn_learner(data, models.vgg19_bn, metrics=accuracy)

5. Training the Model

 In the below code snippet, we tried with one epoch. 

  learn.fit_one_cycle(1)

In the output, we can see that we have got an accuracy of 0.99 and it took 1 minute, 2 second.

In the below code snippet, we are displaying our results using the confusion matrix.

con_matrix = ClassificationInterpretation.from_learner(learn)

con_matrix.plot_confusion_matrix()

FastAI With TPU In PyTorch

6. Making Prediction using the Model

In the below code snippet, we can test our own image by giving the path of the image in test_your_image.

test_your_image='/content/images (3).jpg'

test = open_image(test_your_image)

test.show()

FastAI With TPU In PyTorch

In the below code snippet we can get the output tensor and which class it belongs.

learn.predict(test)

FastAI With TPU In PyTorch

As we can see in the above output, the model has predicted the class label for the input image that it belongs to the ‘flower’ category.

Conclusion

In the above demonstration, we implemented a multiclass image classification with few lines of code using the fastAI library with TPU and we used the pre-trained VGG-19 model. We achieved 0.99 accuracy in classifying the validation dataset in this task.

Share
Picture of Prudhvi varma

Prudhvi varma

AI enthusiast, Currently working with Analytics India Magazine. I have experience of working with Machine learning, Deep learning real-time problems, Neural networks, structuring and machine learning projects. I am a Computer Vision researcher and I am Interested in solving real-time computer vision problems.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India