How to use Caffe model generated by Labellio on your machine/AWS

Labellio makes it super easy to build your own image recognition model in a few minutes.  The last thing you want to do is to use your model in your application.  In this entry, I explain how to use your model using Docker and AWS, via Labellio’s python interface that’s publicly available.

The simplest example to use the Caffe model exported by Labellio is to build a Web API server that returns classification result from input image data.  We recommend using GPU instance in AWS to accelerate the computation.

As a starting point, I describe how to setup the server environment to run such an API server, and will show how to write API server in the following post.  If you follow the procedure I show here, it’s not difficult to build your own API server at all.

 

Setup GPU instance on your machine or AWS

Here is how you configure your machine with GPU attached.  You may need to use sudo to run docker depending on your system configuration.  Alternatively, you can use our AMI ami-9fe512db that we make publicly available, our AWS Northern California Region.

1. We assume you have a GPU machine with the following installed in.

2. Download the docker image.

$ sudo docker pull alpacadb/labellio-caffe-image

3. Confirm the image is pulled.  You can see alpacadb/labellio-caffe-image is available after that.

$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED	VIRTUAL SIZ
alpacadb/labellio-caffe-image   latest 4bba1de016cb 9 days ago 3.661 GB

4. It is necessary to install and load the GPU driver on the host machine to use CUDA on docker container.  Until you run the deviceQuery command below, the driver has not yet been loaded.

$ lsmod | grep nvidia
nvidia           	8370760  0
drm               	303102  4 ttm,drm_kms_helper,cirrus,nvidia

5. Load the driver.  In order to load it, you need to some command that uses CUDA.  Here we use the deviceQuery command.  From the CUDA Toolkit, build the command and run it.

$ cd NVIDIA_CUDA-7.0_Samples/1_Utilities/deviceQuery
$ make
$ ./deviceQuery

6. You can now see the driver is loaded.

$ lsmod | grep nvidia
nvidia_uvm         	67139  0
nvidia           	8370760  1 nvidia_uvm
drm               	303102  1 nvidia

7. Kick off the alpacadb/labellio-caffe-image container using the docker command.  Remember to add –privileged option.

$ sudo docker run -ti --privileged -v /dev/null:/dev/raw1394 alpacadb/labellio-caffe-image:latest /bin/bash

Note: -v /dev/null:/dev/raw1394 is not mandatory, but you may see “libdc1394 error: Failed to initialize libdc1394” error.  This error is not problematic, but with the “-v” option we can suppress it.

8. Now you can go to the next step.

 

Use your image recognition model from the command line tool

We assume you are on the AWS system logged in via SSH.  In the docker environment, you may need to add the `docker exec` command.

1. Build your Caffe model to recognize images in Labellio.  In this example, “gorilla”, “alpaca”, and “sheep” labels are used.

2. Download the Caffe model files from Labellio.  If you have your model finished training, you can download it from `Download Model` button.  The model filename should look like 6db79d9b4e6552ef15586ab3ddd08c15fe260892.tar.gz.  Untar the file and rename it to `model`.

$ tar xzf [model filename]
$ mv [untarred directory] model

3. Have images to be classified.  In this example, we use images that are OK to use commercially.  Download them into your working directory named `img`.

4. Make sure the following file is sourced before using labellio_classify.

$ source /opt/caffe/caffe.bashrc

5. Run this command as it is installed in the system already.

$ labellio_classify model img

6. You will see the output as follows.

img/alpaca.jpg    alpaca    [ 0.66606939  0.07539421  0.25853643]
img/sheep.jpg    sheep    [  7.81783648e-03   3.10965086e-04   9.91871238e-01]
img/gorilla.jpg    gorilla    [ 0.00264297  0.99490935  0.00244769]

It shows filename, classification result and classification score in the order.  You can know the index in the array from the labels.json file in the model directory.  In this example, alpaca is the first, gorilla is the second and sheep is the third number.

$ cat model/labels.json && echo
{"sheep": 2, "gorilla": 1, "alpaca": 0}

Also read...

Comments

  1. Pingback: How to build image recognition API server with Labellio by Alpaca

Leave a Reply

Your email address will not be published. Required fields are marked *