This article was published as a part of the Data Science Blogathon.
In this article, we will be learning how you can develop and deploy an image classifier using flask. Like my other articles, this article will give you hands-on experience with code and at the end of this article, you will be able to create a simple and effective flask API that will classify the image. This article will initially contain the theory part and then the coding part. I will be giving a link to the code later in this article which will save your hustle. So let’s focus on learning. Again this article will be completely based on PYTHON.
This article is open for all categories of viewers (Beginner, Intermediate, Advanced). Below is the table of content of this article so feel free to skip parts that you are already comfortable with.
This part will be focused on image classifier using flask. You can find the second part here if you are only interested in creating flask API.
Image Classification is the process of categorizing and labelling groups of pixels or vectors within an image based on specific rules. In layman terms, we are trying to somehow group related images or similar images together.
Image classification techniques are mainly divided into two types: Unsupervised and Supervised Image classification. We will be using the Supervised Image Classification Techniques here. I won’t be diving deeper into it.
The State of Art for image classification is based on Deep Learning known as Convolutional Neural Networks (CNN).
Convolutional Neural Network
Before we move on to CNN let’s have a look at Neural Network.
Neural Network: These are series of algorithms that recognizes the underlying patterns/relationships in a set of data through a process that mimics similar to working of human brain. Deep Learning: A subset of Machine Learning Algorithms that is very good at recognizing patterns but typically requires a large number of data.
CNN also known as Convnet is a special type of Neural Network that works in the same way as a regular neural network except that it has a convolution layer at the beginning.
Why do we use CNN?
The image below shows a classic example of CNN.
VIP terms in CNN
Convolution: It contains a set of filters/kernels (set of weights) that takes in input as a subset of the input image. Subsets of images are multiplied with the filters which are generally of lesser size than image. The below image will help you get insights into the Convolutional layer.
Pooling: This layer helps in downsampling our images. We run a filter over the matrix and apply a function over it to generate the output. MAXPOOL is a function that gives output as max value as shown below image.
Image is taken from https://cs231n.github.io/convolutional-networks/
ReLU
Rectified Linear Activation Function or ReLU for short is a piecewise linear function that will output the input directly if it is positive, otherwise, it will output zero. It has become the default activation function for many types of neural networks because a model that uses it is easier to train and often achieves better performance.
Dropout
This layer helps in avoiding the overfitting of our model by randomly dropping certain nodes in the neural network and using rest for prediction. It is a regularization technique that forces CNN to not depend on only certain nodes but has a component or says from all nodes.
Fully Connected Layer
Neurons in a fully connected layer have full connections to all activations in the previous layer. Their activations can hence be computed with a matrix multiplication followed by a bias offset.
You can read more details here.
Transfer Learning
Transfer Learning is the reuse of a previously learned model on a new problem. These are the most used techniques in Computer Vision because they save a lot of hustle when you use these techniques to solve a similar new problem. For example, we created a model to classify cat vs monkey, can we use it for classifying tiger vs gorilla? With Transfer learning, we sure can.
We are going to use transfer Learning too while creating our model. Examples of transfer learning models used for Image Classification are Resnet50, MobileNet, VGG16, VGG19, DenseNet.
Read more about Transfer Learning here.
Enough with theory SHOW ME THE CODE!
For image classifiers, there are many modules like TensorFlow, PyTorch,fastai, etc. For this blog, we will be using TensorFlow.
Import Libraries:
# Helper Libraries import os import numpy as np import matplotlib.pyplot as plt
# Tensorflow import tensorflow as tf
from
keras.preprocessing.image
import
ImageDataGenerator
Creating your dataset
You have to declare and change variables according to your use.
# Declaring Variables train_data_dir = 'v_data/train' validation_data_dir = 'v_data/test' nb_train_samples =400 nb_validation_samples = 100 epochs = 10 batch_size = 16img_width, img_height
=
224
,
224
input_shape
=
(img_width, img_height,
3
)
epochs: 1 Epoch is when the complete dataset is passed forward and backwards through the neural network only once.
batch_size: Since passing the complete dataset forward and backwards will cause computation cost to rise we use a batch size which is defined as the number of samples that will be propagated through the network.
train_datagen = ImageDataGenerator( rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='binary') validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='binary')
Creating model
model
=
Sequential()
model.add(Conv2D(
32
, (
2
,
2
), input_shape
=
input_shape))
model.add(Activation(
'relu'
))
model.add(MaxPooling2D(pool_size
=
(
2
,
2
)))
model.add(Conv2D(
32
, (
2
,
2
)))
model.add(Activation(
'relu'
))
model.add(MaxPooling2D(pool_size
=
(
2
,
2
)))
model.add(Conv2D(
64
, (
2
,
2
)))
model.add(Activation(
'relu'
))
model.add(MaxPooling2D(pool_size
=
(
2
,
2
)))
model.add(Flatten())
model.add(Dense(
64
))
model.add(Activation(
'relu'
))
model.add(Dropout(
0.5
))
model.add(Dense(
1
))
model.add(Activation(
'sigmoid'
))
We have created the structure for our model here.
|
The above code will create a compile function which will contain loss function, optimizer as adam, and evaluation for the model.
Transfer learning model
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 model = MobileNetV2(weights='imagenet')
This code will create a model with the structure of MobileNetV2 loaded with imagenet weights.
Fine Tuning Transfer Learning model
To adjust pretrained neural networks for our problem we need to train them on our dataset. We can also add some extra layers at end of the Image Classification model.
from keras.applications import Resnet50 base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3))
output_categories = 2 # number of categories in final layer. add_model = Sequential() add_model.add(Flatten(input_shape=base_model.output_shape[1:])) add_model.add(Dropout(0.3)) add_model.add(Dense(128, activation='relu')) add_model.add(Dropout(0.3)) add_model.add(Dense(output_categories, activation='softmax')) model = Model(inputs=base_model.input, outputs=add_model(base_model.output))
Here we have used Resnet50 as a pretrained model and then we added some layers at the end of our model so that we can fine-tune it for our case.
Training Model
model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size)
Predicting using Model
def model_predict(img, model): img = img.resize((224, 224))
# Preprocessing the image x = image.img_to_array(img) # x = np.true_divide(x, 255) x = np.expand_dims(x, axis=0)
# Be careful how your trained model deals with the input # otherwise, it won't make correct prediction! x = preprocess_input(x, mode='tf')
preds = model.predict(x) return preds
Saving Model
model_json = model.to_json() with open("ImageClassifier.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 model.save_weights("ImageClassifier.h5")
Locations will change according to your system.
Loading Model
model_json = open(path_to_model_json, 'r') loaded_model_json = model_json.read() model_json.close() model = model_from_json(loaded_model_json) model.load_weights(path_to_model_wieght)
In case you are confused and would like to see or copy the code. Reference for Image Classifier here.
This is the end of the image classifier using flask. Hope you got an idea of how can you create an image classifier and how can you apply transfer learning and use it to get better results for your computer vision problems. In the follow-up article, we will be creating an API to hold our image classifier and deploy it. I have already put the complete code for this tutorial in the GitHub repository below.
FIND CODE here.
ABOUT ME
I am a Final Year student from IIIT pursuing my bachelor’s in Computer Science and Engineering. I love to explore and try more and more about Deep and Machine Learning. If you want to collaborate you can use the below links:
Linkedin: Click Me
Github: Click Me
Kaggle: Click Me
Mail me: [email protected]
Hope you liked my article on image classifier using Flask. Thank You for reading!
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.