Image Recognition using MobileNet


Introduction

The process of identifying an object or feature with an image is known as Image Recognition. Image recognition finds its place in diverse domains be it Medical imaging, automobiles, security, or detecting defects.

What is MobileNet and Why is it so Popular?

MobileNet is deep learning CNN model developed using depth−wise separable convolutions. This model highly decreases the number of parameters when compared to other models of the same depth. This model is lightweight and is optimized to run on mobile and edge devices. There are three versions of Mobilenet released so far.ie MobileNet v1, v2 and v3. Mobilenet is developed by Google.

Let's talk a bit about MobileNet V1 and V2 which have been in the ML space for quite some time now.

MobileNetV2 provides two extensive features over MobileNet V1. They are

  • MobileNetV2 has a linear bottleneck between the layers. It preserves information by not allowing non−linearities to destroy too much information

  • Short connections between bottlenecks

Architecture of Mobilenetv2

INPUT

OPERATOR

T

C

N

S

2242 X 3

conv2d

32

1

2

1122X 32

bottleneck

1

16

1

1

1122X 16

bottleneck

6

24

2

2

562 X 24

bottleneck

6

32

3

2

282 X 32

bottleneck

6

64

4

2

142 X 64

bottleneck

6

96

3

1

142 X 96

bottleneck

6

160

3

2

72 X 160

bottleneck

6

320

1

1

72 X 320

conv2d 1x1 1

1280

1

1

72 X 1280

avgpool 7x7

1

1 X1 X 1280

conv2d 1x1

k

A comparison between MobileNet v1 and Mobilenet V2

SIZE

MOBILENETV1

MOBILENETV2

SHUFFLENET (2X,G=3)

112X112

64/1600

16/400

32/800

56x56

128/800

32/200

48/300

28x28

256/400

64/100

400/600K

14x14

512/200

160/62

800/310

7x7

1024/199

320/32

1600/156

1x1

1024/2

1280/2

1600/3

max

1600K

400K

600K

Code Implementation of Image Recognition

Example

## MOBILENET import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras.layers import Dense, Activation from tensorflow.keras.metrics import categorical_crossentropy from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.preprocessing import image from tensorflow.keras.models import Model from tensorflow.keras.optimizers import Adam from tensorflow.keras.applications import imagenet_utils import matplotlib.pyplot as plt from IPython.display import Image,display %matplotlib inline mobile = tf.keras.applications.mobilenet.MobileNet() def format_image(file): image_path = '/content/images/' img = image.load_img(image_path + file, target_size=(224, 224)) img_array = image.img_to_array(img) img_array_exp_dims = np.expand_dims(img_array, axis=0) return tf.keras.applications.mobilenet.preprocess_input(img_array_exp_dims) display(Image(filename='/content/images/image.jpg', width=300,height=200)) preprocessed_img = format_image('image.jpg') prediction_results = mobile.predict(preprocessed_img) results = imagenet_utils.decode_predictions(prediction_results) print(results)

Output

[[('n02279972', 'monarch', 0.58884907), ('n02281406', 'sulphur_butterfly', 
0.18508224), ('n02277742', 'ringlet', 0.15471826), ('n02281787', 'lycaenid', 0.04744451), 
('n02276258', 'admiral', 0.01013135)]]

Advantages of MobileNet over other networks

  • MobileNets have higher classification accuracy and fewer parameters.

  • MobileNets are small and have low latency with optimized power consumption most suitable for mobile and embedded devices.

  • They are highly effective feature extractors for segmentation and object detection

Benefits of Image Recognition

  • Used by autonomous vehicles and robots for detecting obstacles

  • Highly used in OCR techniques for retrieving information from images

  • Traffic lane detection

  • Face Detection and attendance systems

  • Captioning and tagging images, useful in social media sites.

Conclusion

Image Recognition has become the preliminary task of every object detection and video−related task. With the plethora of pre−trained models and architectures already available, it has become very relevant in the current AI space related to Vision.

Updated on: 30-Dec-2022

344 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements