The author | PRUDHVI VARMA compile | source of vitamin k | Analytics Indiamag

Because of its wide application, computer vision has become one of the most developing sub-fields in the field of artificial intelligence. In some areas, they even surpass human intelligence in recognizing images quickly and accurately.

In this article, we will demonstrate one of the most popular computer vision applications – the multi-class image classification problem, using the fastAI library and TPU as hardware accelerators. TPU, or tensor processing unit, can speed up the training process of deep learning models.

Topics covered in this article:

  • Multicategory image classification

  • Commonly used image classification model

  • Use TPU and implement it in PyTorch

Multicategory image classification

We use image classification to identify objects in images, and it can be used to detect brand logos, classify objects, and so on. One limitation of these solutions, however, is that objects can only be identified, but their location cannot be found. But compared with target localization, image classification model is easier to implement.

Common models for image classification

We can use VGG-16/19, Resnet, Inception V1, V2, v3, Wideresnt, Resnext, DenseNet, etc. They are advanced variants of convolutional neural networks. These are popular image classification networks and are used as the backbone of many of the most advanced target detection and segmentation algorithms.

Image classification based on FasAI library and TPU hardware

We will take this step in the following areas:

1. Select a hardware accelerator

Here we use Google Colab. To use TPU in Google Colab, we need to open the Edit option, then open the Notebook Settings, and change the hardware accelerator to TPU.

You can check if your Notebook is using TPU by running the following code snippet.

import os
assert os.environ['COLAB_TPU_ADDR']
Path = 'grpc://'+os.environ['COLAB_TPU_ADDR']
print('TPU Address:', Path)
Copy the code

! [](qiniu.aihubs.net/Screenshot -158.png)

2. Load the FastAI library

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

from fastai.vision import *

from fastai.metrics import error_rate, accuracy
Copy the code

3. Customize the data set

You can also try using custom datasets in the code snippet below.

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))
Copy the code

4. Load the deep learning model of pre-training

In the code snippet below, we will import the VGG-19 BATCH_normalisation model. We will use it as an example of fastAI’s computer vision learning module.

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

5. Training model

In the code snippet below, we try to use an epoch.

learn.fit_one_cycle(1)
Copy the code

In the output, we can see that we achieved an accuracy of 0.99, which took 1 minute and 2 seconds.

In the code snippet below, we display the results using an obfuscation matrix.

con_matrix = ClassificationInterpretation.from_learner(learn)

con_matrix.plot_confusion_matrix()
Copy the code

6. Use models to make predictions

In the code snippet below, 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()
Copy the code

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

learn.predict(test)
Copy the code

As we can see in the output above, the model has predicted the class label of the input image, which belongs to the “flower” category.

conclusion

In the above demonstration, we implemented a multi-class image classification using the fastAI library with TPU and the pre-trained VGG-19 model. In this task, we achieved 0.99 accuracy in classifying the validation data set.

The original link: analyticsindiamag.com/fastai-with…

Welcome to panchuangai blog: panchuang.net/

Sklearn123.com/

Welcome to docs.panchuang.net/