Cognitive

Kickstarting your ML projects using pre-trained models from IBM’s MAX

Share this post:

One of the biggest challenge when you want to use ML in your project is training models using right data. It’ not super tough if you have enough data to build a model but most of the time when I want to quickly experiment, i don’t necessarily have the data for it, forget about good data. This is true when you want to build a product quickly or experiment while trying to learn ML.  My solution to this problem is to use existing models to experiment and then build my own at later stage if required. One of the best places to get models is IBM Code Model Asset Exchange.

IBM Code Model Asset Exchange is a a place for developers to find and use free and open source deep learning models. They have all kinds of models , from Breast Cancer Mitosis Detector to Simple Object Detector. They are easy to use in your own projects. In this how-to we will use Simple Object Detector in our application to detect world’s biggest problem whether a given animal is a dog or cat.

There are two parts to our application. First part is to run the Simple Object Detector by building the docker image. It exposes a RESTFul API. Build a web app to consume that rest API by our simple Flask App.

Bring up the Simple Object Detector – API Server

The model comes as a Docker image with embedded app server which exposes a RESTFul API. To start build the Docker[1. Assuming you already know Docker] image and start it.

git clone https://github.com/IBM/MAX-Object-Detector.git

cd  MAX-Object-Detector

#build the image

docker build -t max-object-detector .

Building the image takes a while as it has to download [2. 100MB + downloads] the image codait/max-base from Docker Hub.  Once the image is ready lets start the docker server

docker run -it -p 5000:5000 max-object-detector

This starts the a simple flask server which deploys the model and exposes the a simple API at port 5000. If you are interested in how it’s doing you can check the code at max-base. It uses tensorflow (Python). It reads the tensorflow model from a specific disk path and is ready to serve. In fact if you don’t want to go Docker way then you can run this max-base flask app by downloading the model and data files [3. Built on COCO project. ] from IBM Servers. I am going Docker way as it’s easy to get started when you start experimenting.

So getting back to Docker. Once you start go http://localhost:5000/ to see OpenAPI aka Swagger UI. You can use the built in UI to call the APIs, sepecially model/predict API.

Now that we have the predictor API running. Let’s try it out on our cat and dog pictures. It turns out the predictions were quite good.

 


And the cat

Since it has a swagger file, you can easily generate the clients for your custom app to use this prediction server.   Go to Swagger Editor Online and upload the swagger.json of the prediction API. It’s available at http://localhost:5000/swagger.json. Download it and then open using online swagger editor.  Then you can use generate client menu to generate client side code to start. In the screenshot below, you can see it.

You can generate the code for any available platform and then include it in your app.  This makes integration straightforward.  It works. But if you don’t like the extra cumbersomeness or ease of Swagger Client [3. In python the package will be swagger_client, its probably good idea to change it. ] then I would suggest using Requests python library. Either way the APIs are super easy to use.  To use python Swagger Client. Extract the downloaded client code.

#install swagger_client code using

python setup.py install

Create an example.py to access the APIs. It should print the labels once you run this example.py

from __future__ import print_function

import time

import swagger_client

from swagger_client.rest import ApiException

from pprint import pprint

 

api_instance = swagger_client.ModelApi()

 

try:

api_response = api_instance.get_labels()

pprint(api_response)

except ApiException as e:

print("Exception when calling ModelApi->get_labels: %s\n" % e)

 

In my case first it threw an error for https. Then I checked swagger_client/models/configuration.py and edited the host url

self.host = "http://localhost:5000"

 

Then ran again to get the list of labels.

(swagger_client) thej@uma:~/code/object_detector_python$ python example.py

{'count': 80,

'labels': [{'id': '1', 'name': 'person'},

{'id': '2', 'name': 'bicycle'},

{'id': '3', 'name': 'car'},

{'id': '4', 'name': 'motorcycle'},

{'id': '5', 'name': 'airplane'},

{'id': '6', 'name': 'bus'},

{'id': '7', 'name': 'train'},

{'id': '8', 'name': 'truck'},

{'id': '9', 'name': 'boat'},

{'id': '10', 'name': 'traffic light'},

{'id': '11', 'name': 'fire hydrant'},

{'id': '13', 'name': 'stop sign'},

{'id': '14', 'name': 'parking meter'},

{'id': '15', 'name': 'bench'},

{'id': '16', 'name': 'bird'},

….

….

{'id': '89', 'name': 'hair drier'},

{'id': '90', 'name': 'toothbrush'}]}

Let me know if this tutorial helped you in kick-starting your ML learning.  Also there are many more real code examples on IBM Data Science site. You can directly clone the github projects and experiment with them.

Author– Thejesh GN, Independent technologist & developer .

Social Profile- https://www.facebook.com/thejeshgn

About – Thejesh GN “Thej” is an independent technologist, developer, hacker, maker, traveller, blogger and an open data/internet enthusiast from Bangalore, India. He is the co-founder and chairman of DataMeet trust. DataMeet is the biggest community of data science and open data enthusiasts in India.

More Cognitive stories

Just launched – IBM Security Command Center in India

IBM Security Command Center launch in Bengaluru, India

Continue reading

Insurance Company Brings Predictability into Sales Processes with AI

Generally speaking, sales drives everything else in the business – so, it's a no-brainer that the ability to accurately predict sales is very important for any business. It helps companies better predict and plan for demand throughout the year and enables executives to make wiser business decisions.

Continue reading

Never miss an incident with an application-centric AIOps platform

Applications are bound to face occasional outages and performance issues, making the job of IT Ops all the more critical. Here is where AIOps simplifies the resolution of issues, even proactively, before it leads to a loss in revenue or customers.

Continue reading