diff --git a/.travis.yml b/.travis.yml index bf26f14..ed846b8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,7 @@ env: - DOCKERFILE_DIR=nginx - DOCKERFILE_DIR=node - DOCKERFILE_DIR=openjdk + - DOCKERFILE_DIR=openvino - DOCKERFILE_DIR=os-core - DOCKERFILE_DIR=perl - DOCKERFILE_DIR=php diff --git a/openvino/Dockerfile b/openvino/Dockerfile new file mode 100644 index 0000000..eb392d3 --- /dev/null +++ b/openvino/Dockerfile @@ -0,0 +1,62 @@ +FROM clearlinux:latest AS builder + +ARG swupd_args +# Move to latest Clear Linux release to ensure +# that the swupd command line arguments are +# correct +RUN swupd update --no-boot-update $swupd_args + +# Grab os-release info from the minimal base image so +# that the new content matches the exact OS version +COPY --from=clearlinux/os-core:latest /usr/lib/os-release / + +# Install additional content in a target directory +# using the os version from the minimal base +RUN source /os-release && \ + mkdir /install_root \ + && swupd os-install -V ${VERSION_ID} \ + --path /install_root --statedir /swupd-state \ + --bundles=os-core-update,curl,python3-basic,computer-vision-basic,computer-vision-models --no-boot-update + +# For some Host OS configuration with redirect_dir on, +# extra data are saved on the upper layer when the same +# file exists on different layers. To minimize docker +# image size, remove the overlapped files before copy. +RUN mkdir /os_core_install +COPY --from=clearlinux/os-core:latest / /os_core_install/ +RUN cd / && \ + find os_core_install | sed -e 's/os_core_install/install_root/' | xargs rm -d &> /dev/null || true + +FROM clearlinux/os-core:latest +MAINTAINER qi.zheng@intel.com + +COPY --from=builder /install_root / + +WORKDIR /work + +# path to save pre-downloaded models +ENV MODEL_DIR="/models" +ENV MO_PATH="/usr/share/openvino/model-optimizer/mo.py" + +# MODEL will be used by the container +# If not pre-downloaded, the entrypoint will try download it and set the MODEL_PATH +# ENVs values could be passed by users to dynamically choose model to be used +ENV MODEL_NAME="face-detection-retail-0005" +ENV MODEL_PRECISION="INT8" + +# pre-downloaded and converted models for openvino +COPY ./models.txt /work +RUN for m in $(cat /work/models.txt); do \ + model-downloader --name $m -o $MODEL_DIR && \ + model-converter --name $m -d $MODEL_DIR -o $MODEL_DIR --mo $MO_PATH; \ + done + +# Pre-install some python libs for serivce to use +COPY ./requirements.txt /work +RUN pip3 install -r requirements.txt + +COPY ./set_model_path.sh /work +COPY docker-entrypoint.sh /usr/local/bin/ +RUN chmod +x /usr/local/bin/docker-entrypoint.sh +ENTRYPOINT ["docker-entrypoint.sh"] + diff --git a/openvino/README.md b/openvino/README.md new file mode 100644 index 0000000..e84b731 --- /dev/null +++ b/openvino/README.md @@ -0,0 +1,183 @@ +# Clear Linux* OS `openvino` container image + + +## What is this image? + +`clearlinux/openvino` is a Docker image with `dldt` running on top of the +[official clearlinux base image](https://hub.docker.com/_/clearlinux). + + +> [openvino](https://01.org/openvinotoolkit) OpenVINO™ toolkit, short +> for Open Visual Inference and Neural network Optimization toolkit, provides +> developers with improved neural network performance on a variety of +> Intel® processors and helps them further unlock cost-effective, real-time +> vision applications. + +For other Clear Linux* OS +based container images, see: https://hub.docker.com/u/clearlinux + +## Why use a clearlinux based image? + + +> [Clear Linux* OS](https://clearlinux.org/) is an open source, rolling release +> Linux distribution optimized for performance and security, from the Cloud to +> the Edge, designed for customization, and manageability. + +Clear Linux* OS based container images use: +* Optimized libraries that are compiled with latest compiler versions and + flags. +* Software packages that follow upstream source closely and update frequently. +* An aggressive security model and best practices for CVE patching. +* A multi-staged build approach to keep a reduced container image size. +* The same container syntax as the official images to make getting started + easy. + +To learn more about Clear Linux* OS, visit: https://clearlinux.org. + + +## Environment variables + +#### MODEL_DIR +It is the root directory to save openvino models, default /models. + +#### MO_PATH +It points to the path of Model Optimizer mo.py. +It is "/usr/share/openvino/model-optimizer/mo.py" in default. + +#### MODEL_NAME +The model name to be used. +If it is not pre-downloaded, the entrypoint script will do the downloading and +converting to IR format. +Run the below command can get all supported models. +``` +docker run --rm clearlinux/openvino model-downloader --print_all +``` + +#### MODEL_PRECISION +The model precision to be chosen, FP32, FP16 or INT8. + +#### MODEL_PATH +The chosen model path, automatically set by the entrypoint script. + +For example, if using alexnet model to do classification, two environment variables +need to be passed to the container. Details can refer to the deployment below. + + + +## Deployment: + +### Deploy with Docker +The easiest way to get started with this image is by simply pulling it from +Docker Hub. + +1. Pull the image from Docker Hub: + ``` + docker pull clearlinux/openvino + ``` + +2. Start one-time classification_sample with alexnet model as below: + + * Use docker-compose to start the example: + ``` + docker-compose up -f docker-compose.yml + ``` + The configuration is defined in the + [`docker-compose.yml`](https://github.com/clearlinux/dockerfiles/blob/master/openvino/docker-compose.yml) + + +Or + +3. Start a simple openvino-server to accept image to do classification_sample with alexnet model: + + * Use docker-compose to start the server first: + ``` + docker-compose up -f docker-compose-server.yml + ``` + The configuration is defined in the + [`docker-compose-server.yml`](https://github.com/clearlinux/dockerfiles/blob/master/openvino/docker-compose-server.yml) + + * Use curl to send image to the server for classification: + ``` + curl -H "Content-type: application/octet-stream" -X POST http://localhost:5000/image --data-binary @cat.bmp + ``` + + + +### Deploy with Kubernetes +This image can also be deployed on a Kubernetes cluster, such as +[minikube](https://kubernetes.io/docs/setup/learning-environment/minikube/).The +following example YAML files are provided in the repository as +reference for Kubernetes deployment: + + * [`classification.yaml`](https://github.com/clearlinux/dockerfiles/blob/master/openvino/classification.yaml): + yaml file to deploy the openvino classification example + +To deploy the image on a Kubernetes cluster: + + * Start the openvino classification example server. + ``` + kubectl apply -f classification.yaml + ``` + + * Then check if the pods are running well. + ``` + kubectl get pods -o wide + ``` + This may take some time because it requires downloading/converting the + alexnet model. + Note, if your cluster is behind some proxy, you may need set the proxy + environment in the yaml file to make the model-init can download the model. + + * Get server PORT and IP + ``` + PORT=`kubectl get -o jsonpath="{.spec.ports[0].nodePort}" services openvino-server` + NODEIP=`kubectl get nodes -o jsonpath="{.items[0].status.addresses[0].address}"` + ``` + + * Use curl to send image to the server for classification: + ``` + curl -H "Content-type: application/octet-stream" -X POST http://$NODEIP:$PORT/image --data-binary @cat.bmp + ``` + + +## Build and modify: + +The Dockerfiles for all Clear Linux* OS based container images are available at +https://github.com/clearlinux/dockerfiles. These can be used to build and +modify the container images. + +1. Clone the clearlinux/dockerfiles repository. + ``` + git clone https://github.com/clearlinux/dockerfiles.git + ``` + +2. Change to the directory of the application: + ``` + cd openvino/ + ``` + +3. Build the container image: + ``` + docker build -t clearlinux/openvino . + ``` + + [`models.txt`](https://github.com/clearlinux/dockerfiles/blob/master/openvino/models.txt): + It defines the models will be pre-downloaded and converted to IR format in container image. + + [`requirements.txt`](https://github.com/clearlinux/dockerfiles/blob/master/openvino/requirements.txt): + It defines the python packages to be installed in container image. + + Refer to the Docker documentation for [default build + arguments](https://docs.docker.com/engine/reference/builder/#arg). + Additionally: + + - `swupd_args` - specifies arguments to pass to the Clear Linux* OS software + manager. See the [swupd man + pages](https://github.com/clearlinux/swupd-client/blob/master/docs/swupd.1.rst#options) + for more information. + + +## Licenses + +All licenses for the Clear Linux* Project and distributed software can be found +at https://clearlinux.org/terms-and-policies diff --git a/openvino/app.py b/openvino/app.py new file mode 100644 index 0000000..77571b2 --- /dev/null +++ b/openvino/app.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 +import subprocess +from flask import Flask, request +app = Flask(__name__) + +@app.route('/') +def classification_sample(): + return 'Classification sample' + +@app.route('/image', methods=['POST']) +def do_classification(): + if request.headers['Content-Type'] == 'application/octet-stream': + f = open('./image', 'wb') + f.write(request.data) + return subprocess.check_output("classification_sample_async -i ./image -m $MODEL_PATH/$MODEL_NAME.xml", shell=True) + else: + return "415 Unsupported Media Type ;)" + +if __name__ == '__main__': + app.run(debug=True,host='0.0.0.0') diff --git a/openvino/cat.bmp b/openvino/cat.bmp new file mode 100644 index 0000000..a9f792f Binary files /dev/null and b/openvino/cat.bmp differ diff --git a/openvino/classification.yaml b/openvino/classification.yaml new file mode 100644 index 0000000..3a7deb4 --- /dev/null +++ b/openvino/classification.yaml @@ -0,0 +1,81 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: server +data: + app.py: | + #!/usr/bin/python3 + + import subprocess + from flask import Flask, request + app = Flask(__name__) + + @app.route('/') + def classification_sample(): + return 'Classification sample' + + @app.route('/image', methods=['POST']) + def do_classification(): + if request.headers['Content-Type'] == 'application/octet-stream': + f = open('./image', 'wb') + f.write(request.data) + return subprocess.check_output("classification_sample_async -i cat.bmp -m $MODEL_PATH/$MODEL_NAME.xml", shell=True) + else: + return "415 Unsupported Media Type ;)" + + if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0') + +--- + +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: openvino-deploy +spec: + replicas: 1 + template: + metadata: + labels: + app: classification + spec: + containers: + - name: classification + image: clearlinux/openvino + imagePullPolicy: IfNotPresent + env: +# - name: http_proxy +# value: +# - name: https_proxy +# value: + - name: MODEL_NAME + value: alexnet + - name: MODEL_PRECISION + value: FP32 + args: + - "python3" + - "app.py" + ports: + - containerPort: 5000 + volumeMounts: + - name: server-py + mountPath: /work/app.py + subPath: app.py + volumes: + - name: server-py + configMap: + name: server + +--- + +apiVersion: v1 +kind: Service +metadata: + name: openvino-server +spec: + type: NodePort + ports: + - port: 5000 + nodePort: 30008 + selector: + app: classification diff --git a/openvino/demo.sh b/openvino/demo.sh new file mode 100755 index 0000000..ce9d388 --- /dev/null +++ b/openvino/demo.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# Copyright (C) 2018 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +# demo to use alexnet modle to do classification + +classification_sample_async -i cat.bmp -m $MODEL_PATH/$MODEL_NAME.xml diff --git a/openvino/deploy.yaml b/openvino/deploy.yaml new file mode 100644 index 0000000..8366bfe --- /dev/null +++ b/openvino/deploy.yaml @@ -0,0 +1,94 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: server +data: + app.py: | + #!/usr/bin/python3 + + import subprocess + from flask import Flask, request + app = Flask(__name__) + + @app.route('/') + def classification_sample(): + return 'Classification sample' + + @app.route('/image', methods=['POST']) + def do_classification(): + if request.headers['Content-Type'] == 'application/octet-stream': + f = open('./image', 'wb') + f.write(request.data) + return subprocess.check_output("classification_sample -i ./image -m /model/alexnet.xml", shell=True) + else: + return "415 Unsupported Media Type ;)" + + if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0') + +--- + +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: openvino-deploy +spec: + replicas: 1 + template: + metadata: + labels: + app: classification + spec: + initContainers: + - name: model-init + image: sevenzheng/clr-openvino + imagePullPolicy: IfNotPresent + env: + - name: http_proxy + #value: $http_proxy + value: http://child-prc.intel.com:913 + - name: https_proxy + #value: $https_proxy + value: http://child-prc.intel.com:913 + command: + - "bash" + - "-c" + - "cd /model && downloader.py --name alexnet && mo.py --input_model ./classification/alexnet/caffe/alexnet.caffemodel" + volumeMounts: + - mountPath: /model + name: model + containers: + - name: classification + image: sevenzheng/clr-openvino + imagePullPolicy: IfNotPresent + command: + - "python3" + - "app.py" + ports: + - containerPort: 5000 + volumeMounts: + - mountPath: /model + name: model + - name: server-py + mountPath: /work/app.py + subPath: app.py + volumes: + - name: model + emptyDir: {} + - name: server-py + configMap: + name: server + +--- + +apiVersion: v1 +kind: Service +metadata: + name: openvino-server +spec: + type: NodePort + ports: + - port: 5000 + nodePort: 30008 + selector: + app: classification diff --git a/openvino/docker-compose-server.yml b/openvino/docker-compose-server.yml new file mode 100644 index 0000000..1875763 --- /dev/null +++ b/openvino/docker-compose-server.yml @@ -0,0 +1,16 @@ +version: '2' + +services: + openvino: + image: clearlinux/openvino:latest + environment: + http_proxy: $http_proxy + https_proxy: $https_proxy + MODEL_NAME: alexnet + MODEL_PRECISION: FP32 + ports: + - "5000:5000" + volumes: + - "./app.py:/work/app.py" + command: "python3 app.py" + diff --git a/openvino/docker-compose.yml b/openvino/docker-compose.yml new file mode 100644 index 0000000..13c37f7 --- /dev/null +++ b/openvino/docker-compose.yml @@ -0,0 +1,15 @@ +version: '2' + +services: + openvino: + image: clearlinux/openvino:latest + environment: + http_proxy: $http_proxy + https_proxy: $https_proxy + MODEL_NAME: alexnet + MODEL_PRECISION: FP32 + volumes: + - "./cat.bmp:/work/cat.bmp" + - "./demo.sh:/work/demo.sh" + command: "/work/demo.sh" + diff --git a/openvino/docker-entrypoint.sh b/openvino/docker-entrypoint.sh new file mode 100755 index 0000000..5a4f950 --- /dev/null +++ b/openvino/docker-entrypoint.sh @@ -0,0 +1,8 @@ +#!/usr/bin/bash +source /work/set_model_path.sh + +echo "MODEL NAME: $MODEL_NAME" +echo "MODEL PRECISION: $MODEL_PRECISION" +echo "MODEL PATH: $MODEL_PATH" + +/usr/bin/bash -c "$@" diff --git a/openvino/models.txt b/openvino/models.txt new file mode 100644 index 0000000..a11dfbc --- /dev/null +++ b/openvino/models.txt @@ -0,0 +1,5 @@ +face-detection-retail-0005 +facial-landmarks-35-adas +mobilenet-ssd +person-vehicle-bike-detection-crossroad-0078 +person-detection-retail-0013 diff --git a/openvino/requirements.txt b/openvino/requirements.txt new file mode 100644 index 0000000..1a5dc97 --- /dev/null +++ b/openvino/requirements.txt @@ -0,0 +1,2 @@ +flask +redis diff --git a/openvino/set_model_path.sh b/openvino/set_model_path.sh new file mode 100755 index 0000000..74d6d25 --- /dev/null +++ b/openvino/set_model_path.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# set -e + +# download model +model_dl() { + model-downloader --name $1 -o $MODEL_DIR && \ + model-converter --name $1 -d $MODEL_DIR -o $MODEL_DIR --mo $MO_PATH; \ +} + +# set model path +set_model_path() { + if [ "$MODEL_PRECISION" ]; then + MODEL_PATH=$(find $MODEL_DIR -name "$MODEL_NAME.xml" | grep $MODEL_PRECISION) + else + MODEL_PATH=$(find $MODEL_DIR -name "$MODEL_NAME.xml") + fi + + export MODEL_PATH=${MODEL_PATH%/*} +} + +# download models if not existed and set the model path +if [ "$MODEL_NAME" ]; then + set_model_path + + if [ -z "$MODEL_PATH" ]; then + model_dl $MODEL_NAME + set_model_path + fi +fi + +if [ -z "$MODEL_PATH" ]; then + echo "Wrong model $MODEL_NAME, couldn't set MODEL_PATH" +fi +