Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Problem Scenario

...

In this use case, we train the CNN model on MNIST dataset that consists of 70,000 images containing handwritten digits. Each image is 28 pixels by 28 pixels and contains one handwritten digit. We  train the model on 60,000 images and keep 10,000 images for testing the model.

The live demo is available at our Machine Learning Showcase.

Objectives

  1. Use Remote Python Script Snap from ML Core Snap Pack to deploy a Python script to train Neural Networks model on Iris flower dataset.
  2. Test the model with a sample.
  3. Use Remote Python Script Snap from ML Core Snap Pack to deploy a Python script to host the model and schedule an Ultra Task to provide API.
  4. Test the API.
  5. Develop a demo with HTML and Javascript.

...

The Remote Python Script Snap downloads the MNIST dataset (we use Keras library to get the dataset), train CNN model, and evaluate the model. Than, we format the model into JSON with JSON Formatter Snap and save the model on SnapLogic File System (SLFS) using File Writer Snap.Image Removed

Image Added

Python Script

The Python script can be found here.

Below is a piece of the Python script from the Remote Python Script Snap used in this pipeline. The code is modified from official Keras example.

There are 3 main functions: snaplogic_init, snaplogic_process, and snaplogic_final. The first function, snaplogic_init, will be executed before consuming documents from the upstream snap. The second function, snaplogic_process, will be called on each of the incoming document. The last function, snaplogic_final, will be processed after all incoming documents have been consumed by snaplogic_process. In this case, Remote Python Script Snap does not have an input view, so snaplogic_process will not be executed.

...

In the pipeline, the File Reader Snap reads the CNN model from SLFS. The JSON Generator Snap contains 1 handwritten image. The correct label is "1".Image Removed

Image Added

The screenshot below shows the handwritten image in the JSON Generator Snap.

Image RemovedImage Added

The prediction of the Python Script Snap is shown below. It can be see that the character in the input image has been correctly identified.

Image RemovedImage Added

Python Script

The Python script can be found here.

Model Hosting

This pipeline is scheduled as an Ultra Task to provide a REST API that is accessible by external applications. The core snaps are File Reader, JSON Parser, and Remote Python Script that are the same from the Model Testing pipeline. The rest are for authentication, parameter extraction, and Cross-Origin Resource Sharing (CORS) handling.

Image RemovedImage Added

Scheduling Ultra Task

To build an API from this pipeline, create a Task. You can either use Triggered Task or Ultra Task. Triggered Task is good for batch processing since it starts a new pipeline instance for each request. Ultra Task is good to provide REST API to external applications that require low latency. In this case, we will use Ultra Task. You do not need to specify the bearer token here since we use the Router Snap to perform authentication inside the pipeline. You can go to the Manager by clicking at Show tasks in this project in Manager to see task details as shown in the screenshot below (Right). 

Image RemovedImage Added

Testing

After scheduling an Ultra Task, you can test it. The below screenshot shows a sample request and response. Based on the image provided, the pipeline returns "1" as the first prediction.

Image RemovedImage Added

Demo

Once we have the API ready, it is time to build an application that demonstrates the power of our handwritten-digit recognition model. Below is the video demo. The live demo is available at our Machine Learning Showcase, Feel free to try and let us know your feedback. You can access the code here.

HTML Code

In this demo, we have four main components: canvas, CLEAR button, READ button and result label. You can use mouse or touch screen to write a digit on the canvas. You can clear the canvas using the CLEAR button. If you are ready, click the READ button to send the request to the API. Once it is done, the result will be displayed.

...