Image classification

# Image classification

Uses for image classification include:

- Quality control of produced goods
- Sorting images based on desired criteria
- Searching for similar images
- Face recognition
- Autonomous driving

This tutorial shows how you can train an image classification model. We will develop a Convolutional Neural Network (CNN) for the classification. We use the MNIST dataset which is an image dataset of handwritten digits. It has has 60,000 training images and 10,000 test images, each of which are grayscale 28 x 28 sized images.

## Install Anaconda on your computer

Download and install Anaconda. Select the Python 3.* version):

When Anaconda is installed, open "Anaconda Prompt" or any other terminal where you have ```conda``` available now.

## Make an isolated Python environment
Run ```conda create --name image-classification python=3.6``` in your terminal.
Then, to activate your new environment, run ```conda activate image-classification```.

##  Install the required packages

Run ```pip install mlflow[extras]==1.3.0 "mflux-ai>=0.6.0" Keras==2.2```  in your terminal.

## Loading imports

import sys
import warnings

import keras
import numpy as np
from keras import backend as K

import mlflow.sklearn

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.datasets import mnist
from keras import optimizers

import mflux_ai

##  Loading and pre-processing data

The Keras library provides a database of these digits in its keras.datasets module. First we load the MNIST data into train and test sets.

  (x_train, y_train), (x_test, y_test) = mnist.load_data()
Next, we do some pre-processing of the images. We reshape the images to a tensor of shape (num_samples image_height, image_width, num_channels), i.e (num_samples, 28, 28, 1) where num_samples = 60,000 for train dataset and num_samples = 10,00 for test dataset.


img_rows, img_cols = 28, 28
num_classes = 10

x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

We will re-scale the image data to a value between 0.0 and 1.0.
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255


Also, we will one-hot-encode the labels.

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

## Define and train a CNN model
Define a CNN model
model = Sequential()

model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=input_shape))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

Define the optimizer.
adam = optimizers.Adam()

Compile the model.


Fit the model using the training dataset.
batch_size = 20
nb_epoch=10, y_train,
          batch_size=20, nb_epoch=10, verbose=1)
Evaluate the model using test data
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

## Log metrics and store machine learning model in

Let's log the parameters, validation loss and accuracy metric and store the model in


# Note: in the following line, insert the project token shown on your dashboard page.

mlflow.log_param("batch_size", batch_size)
mlflow.log_param("nb_epochs", nb_epochs)
mlflow.log_metric("loss", score[0])
mlflow.log_metric("accuracy", score[1])

mlflow.sklearn.log_model(model, "model")

## Check your tracking UI

You should now be able to see the metric and model that you logged in your MLflow tracking UI

This tutorial is open source, if you have suggestions for how this tutorial can be improved, you are welcome to propose a change.