Image Classification using Transfer Learning

Saket Srivastawa
5 min readAug 30, 2021

--

Artificial Intelligence | Deep Learning | CNN | Image Classification | Transfer Learning | Python | Tensorflow | EfficientNetB0 | Google Colab

Source : https://www.analyticssteps.com/blogs/how-transfer-learning-done-neural-networks-and-convolutional-neural-networks

Hi everyone! This is going to be my first article, and I’ll try my best to explain it in ELI5 (Explain Like I’m 5) format.

As the name suggests, Image Classification is about Classification of Images into two or more categories/classes, known as binary-classification and multi-class classification respectively.

In technical context, image classification comes under the umbrella of Deep Learning. We use CNN (Convolution Neural Network) from scratch to create architecture in a way that it extracts and learns the features from the Train dataset, and using those features, classifies the new images from the Test dataset.

In business context, image classification is nowadays getting involved in a lot of use-cases belonging to various domains. For example:

1. For Food Processing Industry, classification of vegetables into Good or Bad for further processing.

2. For Asset maintenance, classifying images for different assets into Defective or Good for automated asset maintenance.

3. For Supply chain, image classification could be used to streamline the process and make sure everything is in place.

4. For HealthCare industry, Diabetic Retinopathy is one such use-case which is very significant.

If we talk about a day-to-day use, image classification is used in Face recognition systems. Some time back, it was possible to show the camera an image of owner (on another phone, or a hard-copy) and unlock the phone. But with further advancements, we could say that a layered image classification helps resolve that issue.

This example is for a binary-classification problem. There are various examples for multi-class classification, such as : Classification of flowers into five categories.

Now we go further into the classification using Transfer Learning. As per definition in Wiki, Transfer learning (TL) is a research problem in machine learning (ML) that focuses on storing knowledge gained while solving one problem and applying it to a different but related problem. For example, knowledge gained while learning to recognize cars could apply when trying to recognize trucks.

So, why is Transfer Learning used? It would be used because:

1. It saves you the trouble of creating everything from scratch.

2. It would enhance the performance of models (as opposed to the one created from scratch), because of the features it has already learnt beforehand.

3. It would work well with very few numbers of images (even with 30–50 odd images for each class).

4. With help of HyperParameter tuning, Data Augmentation and Regularization techniques, the performance could even be enhanced.

There are various Transfer Learning Models trained on different sets of Images and publicly available for us to Load the Model Weights, Re-train it with our data (training only the Dense layer for our labels or maybe few layers before that as well) and use it in production environment.

EfficientNet is one such Transfer Learning Model available. It comes with different variants : B0 to B7.

Source : https://keras.io/api/applications/

There are other Models available as well, such as ResNet, VGG etc. Here is a performance chart for such models. We would be using EfficientNet because comparing to other models with similar accuracy, it is much smaller.

Source : https://ai.googleblog.com/2019/05/efficientnet-improving-accuracy-and.html

Now that we have basic understanding of what Image Classification is, and how Transfer Learning helps us improve our image classification models, we will now go ahead with the implementation/coding for everyone to get hands-on experience. For this, we’ll be using:

1. Google Colab

2. Multi-class classification images for 5 classes of flowers

3. Pre-trained Model : EfficientNetB0

4. Python — Jupyter Notebook

5. Tensorflow-keras (tf_nightly) package

Let’s get started with the implementation now:

  1. Importing the packages

2. Downloading the data and printing out one image

dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
roses = list(data_dir.glob('roses/*'))
print(roses[0])
PIL.Image.open(str(roses[0]))

3. Breaking data into Training and Validation set. I won’t be creating Test set as for model accuracy, I will just be using train_accuracy and val_accuracy.

IMG_SIZE = 224img_height,img_width=IMG_SIZE,IMG_SIZE
batch_size=32
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size,
label_mode = "categorical")
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size,
label_mode = "categorical")
class_names = train_ds.class_names
print(class_names)

4. More images could be printed out for different classes with some modifications in the below code

5. Now we built the model using pre-defined weights for EfficientNetB0 model trained on ImageNet dataset

num_classes = 5def build_model(num_classes):inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
x = inputs#img_augmentation(inputs)
model = EfficientNetB0(include_top=False, input_tensor=x, weights="imagenet")
# Freeze the pretrained weights
model.trainable = False
# Rebuild top
x = layers.GlobalAveragePooling2D(name="avg_pool")(model.output)
x = layers.BatchNormalization()(x)
top_dropout_rate = 0.2
x = layers.Dropout(top_dropout_rate, name="top_dropout")(x)
outputs = layers.Dense(num_classes, activation="softmax", name="pred")(x)
# Compile
model = tf.keras.Model(inputs, outputs, name="EfficientNet")
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-2)
model.compile(
optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"]
)
return model
model = build_model(num_classes)

6. Now we train the model and see the model performance based on loss and accuracy on train and validation set

epochs=2
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)

As we can see, the accuracy is quite good (Almost 90%) even for just 2 epochs and it became possible only because Transfer Learning was used.

Depending on different use cases, more layers from the model could be made as trainable and other metrics (such as recall etc.) could be used.

Github URL : https://github.com/Mr-Saket/Image_Classification_Transfer_Learning

Hoping this was helpful to someone. Do let me know in comments how I could improve this article, or any upcoming ones so that it becomes more helpful for the readers.

--

--

Saket Srivastawa

Data Scientist | GCP Certified Professional ML Engineer | AI | ML | DL | NLP | Conversation AI