Deploying the ML model using Keras and Flask

Data Science Authority
2 min readOct 3, 2020

Flask is a lightweight web framework written in python which makes it easier to get started with a web application and also supports extensions to build complex applications.

Let’s divide the entire process into 2 steps:
1. Train a model
2. Deploy the trained model using flask

1. Train a model

Let us build an image classification model using Keras to identify a specific type of cactus in aerial imagery. Our model should be able to identify whether a given image contains the cactus plant. More details about this dataset can be found in the research paper: https://doi.org/10.1016/j.ecoinf.2019.05.005

Getting Started

Keras is a high-level neural network library that runs on top of tensorflow. It was developed with a focus on enabling fast experimentation. ( https://keras.io/ )

The dataset has 2 folders of training and validation. Each of the folders contains a folder with cactus images and another folder with non-cactus images. There are 17500 images for training and 4000 images for testing. Keras has the ImageDataGenerator function to load images in batches from the source folders and do the necessary transformations. This is more useful when there is not enough memory to load all the images at once.

# this is a generator that will read pictures found in
# subfolers of ‘training_set’, and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
‘training_set’, # this is the target directory
target_size=(100, 100), # all images will be resized to 100x100
color_mode=”rgb”,
batch_size=batch_size,
class_mode=’binary’)
# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
‘validation_set’,
target_size=(100, 100),
color_mode=”rgb”,
batch_size=batch_size,
class_mode=’binary’)

--

--