Image classification 101
By: Vishnu Nandakumar
Tags: TensorFlow, Image Classification, Machine Learning

One of the talking points in the world of football apart from the pandemic was the functioning of VAR which was more inconsistent during that time and still it is. We can get to know the sentiment of people towards VAR if we get on to any social media platform. In this article, I am going to give you a brief method of how AI can actually help and make the decisions more consistently than the officials themselves. I am going to introduce you to two fantastic tools if you haven’t used them before yet.

Bing image download API — https://pypi.org/project/bing-image-downloader/

https://pypi.org/project/bing-image-downloader/ TensorFlow hub — https://www.tensorflow.org/hub

Bing image download API is one of the coolest tools that I found recently which can be used to download images recursively in a loop just by defining some arguments. Let me show you a snippet of code for the example of how to download a set of parrots images.

from bing_image_downloader import downloader query_string='parrots' downloader.download(query_string, limit=100, output_dir='dataset', adult_filter_off=True, force_replace=False, timeout=60)

As far as the TensorFlow hub is concerned there is no need for a big introduction, it’s a repository of state-of-the-art models and their weights which can be downloaded to your session on and you either fine-tune or just implement transfer learning on top of it. But there is a catch not all models are made fine-tunable in the hub, only a certain set of models has that capability now. But the task that we have in hand doesn’t require tuning job we can keep them not trainable for now. The dataset consists of two sets with one having possible foul images and the other with images of normal football game images. I have used MobileNetV2 to train the model attributing to the fact that I have not used GPU and my dataset is pretty small. Below is a snippet of how you can load the data using the Image data generator from TensorFlow API and the model using the TensorFlow hub API

Loading the data and augmenting it

train_datagen = ImageDataGenerator(

rescale=1./255,

rotation_range=30,

width_shift_range=0.2,

height_shift_range=0.4,

brightness_range=None,

shear_range=0.2,

zoom_range=0.2,

channel_shift_range=0.4,

fill_mode="nearest",

cval=0.4,

horizontal_flip=True,

vertical_flip=True

)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory( train_path, # this is the target directory target_size=(self.x, self.y), # all images will be resized to 150x150 batch_size=self.batch_size, class_mode='categorical') valid_generator = test_datagen.flow_from_directory( valid_path, target_size=(self.x, self.y), batch_size=self.batch_size,

class_mode='categorical')

Loading the model using tf_hub

model_link = 'https://tfhub.dev/google/imagenet/mobilenet_v2_035_224/classification/4' tl_model = tf.keras.Sequential([

hub.KerasLayer(model_link, trainable=False),

tf.keras.layers.Dropout(dropout),

tf.keras.layers.Dense(dense, activation='relu'),

tf.keras.layers.Dense(train_generator.num_classes, activation=self.activation)])

tl_model.build([None, self.x, self.y, 3])

optimizer = tf.keras.optimizers.Adam(lr=1e-3) tl_model.compile(optimizer=optimizer, loss=los, metrics=met)

Training the model

Here comes the catch, keeping more parameters at the end might lead to overfitting so choosing the penultimate Dense layer is critical alternatively you can skip the dense layer and only go with the final output layer. I trained the model with Adam optimizer and accuracy as loss metric. The model trained for 80 epochs and achieved a validation accuracy of .7 or 70% which I believe is acceptable for the dataset of 1000 images.

All the suggestions are welcome on how to enrich this more and better, with the officials in the Premier League having dodgy decisions frequently I have very less doubt that soon AI will start having a more say. As the next step I will try to do a multi-class classification with object detection following that.

Please have a try at this notebook and change things as you like :- https://github.com/Vishnunkumar/computer_vision