-
Notifications
You must be signed in to change notification settings - Fork 9
OpenFaaS based on ubuntu:16.04
This wiki explains how to deploy a serverless function for TensorFlow Object Detection API with the SSD MobileNet v1 model, using OpenFaaS and starting from the ubuntu:16.04 Docker image.
The instructions below were inspired by the information found in these links:
- https://github.com/faas-and-furious/inception-function
- https://blog.alexellis.io/quickstart-openfaas-cli/
Follow the installation instructions in https://github.com/salekd/rpizero_smart_camera3/wiki/First-serverless-Python-function-with-OpenFaaS
Build a Docker image for the serverless function.
cd faas-mobilenet
docker build -t faas-mobilenet .
docker run -p 8080:8080 faas-mobilenet
curl localhost:8080 -d "$(echo -n '{"filename": "50-20171111115421-01.jpg", "image_data": "'; base64 50-20171111115421-01.jpg | tr -d '\n'; echo '"}')"
Push the new image to Docker Cloud.
export DOCKER_ID_USER="salekd"
docker login
docker tag faas-mobilenet $DOCKER_ID_USER/faas-mobilenet:1.1.1
docker push $DOCKER_ID_USER/faas-mobilenet:1.1.1
The image can be downloaded from https://hub.docker.com/r/salekd/faas-mobilenet/
Deploy the function using the image.
faas-cli deploy --image faas-mobilenet --name faas-mobilenet
Invoke the function.
curl localhost:8080/function/faas-mobilenet -d "$(echo -n '{"filename": "50-20171111115421-01.jpg", "image_data": "'; base64 50-20171111115421-01.jpg | tr -d '\n'; echo '"}')"
Using jq, sed for JSON data, you can select only the classes with high probability.
https://stedolan.github.io/jq/
Install jq with apt-get install jq
or brew install jq
and try the following:
curl localhost:8080/function/faas-mobilenet -d "$(echo -n '{"filename": "50-20171111115421-01.jpg", "image_data": "'; base64 50-20171111115421-01.jpg | tr -d '\n'; echo '"}')" \
| jq -R 'fromjson?' | jq '.detected_objects[] | select(.score > 0.5)'
The python3-flask
template available via faas-cli template store pull
can preload the model, leading to much quicker inference.
docker build -t faas-mobilenet-flask faas-mobilenet-flask
docker run -p 8080:8080 faas-mobilenet-flask
curl -X POST -H "Content-Type: application/json" localhost:8080 -d "$(echo -n '{"filename": "50-20171111115421-01.jpg", "image_data": "'; base64 50-20171111115421-01.jpg | tr -d '\n'; echo '"}')"
This section serves as a reference for installing and running TensorFlow Object Detection API in the ubuntu:16.04 Docker image.
Start from the ubuntu:16.04 docker image
docker run -it ubuntu:16.04 /bin/bash
and do the following:
apt-get update
apt-get install git
git clone https://github.com/salekd/rpizero_smart_camera3.git
git clone https://github.com/tensorflow/models.git
apt-get install wget
wget http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz
tar -zxvf ssd_mobilenet_v1_coco_11_06_2017.tar.gz
apt-get install python-pip python-dev build-essential
pip install tensorflow
pip install Pillow
apt-get install -y protobuf-compiler
cd /models/research/
protoc object_detection/protos/*.proto --python_out=.
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
apt-get install python-tk
cd /rpizero_smart_camera3/
python object_detection_test.py