nvidia-docker部署Tensorflow Serving(CPU+GPU) 環境準備
官網:TensorFlow Serving with Docker
一、Docker部署Tensorflow Serving CPU版本
# Download the TensorFlow Serving Docker image and repo
docker pull tensorflow/serving
git clone https://github.com/tensorflow/serving
# Location of demo models
TESTDATA="$(pwd)/serving/tensorflow_serving/servables/tensorflow/testdata"
# Start TensorFlow Serving container and open the REST API port
# 方法一
sudo docker run -t --rm -p 8501:8501 -v "/home/mochen/tmp/tfserving/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_cpu:/models/half_plus_two" -e MODEL_NAME=half_plus_two tensorflow/serving
# 方法二
sudo docker run -dt -p 8501:8501 -v "/home/mochen/tmp/tfserving/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_cpu:/models/half_plus_two" -e MODEL_NAME=half_plus_two tensorflow/serving
# 方法三
sudo docker run -d -p 8501:8501 --mount type=bind,source=/home/mochen/tmp/tfserving/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_cpu/,target=/models/half_plus_two -e MODEL_NAME=half_plus_two -t --name testserver tensorflow/serving
# Query the model using the predict API
curl -d '{"instances": [1.0, 2.0, 5.0]}' -X POST http://localhost:8501/v1/models/half_plus_two:predict
# Returns => { "predictions": [2.5, 3.0, 4.5] }
- -t:
- –rm:
- -p:
- -v:
- -e:
二、Docker部署Tensorflow Serving GPU版本
爲了使服務能調用底層GPU,需要安裝nvidia-docker,否則普通的docker是不能使用GPU的。
如圖所示,最下層是有NVIDIA GPU的服務器,再往上是操作系統和CUDA驅動,再往上就是不同的docker容器,裏邊會包含CUDA Toolkit和CUDNN,比如可以啓動一個docker來跑Tensorflow Serving 1.12.0,它使用的是CUDA 9.0和CUDNN 7;也可以啓動一個docker來跑Tensorflow Serving 1.6.0,它使用的是CUDA 8。
nvidia-docker的安裝方法如下,適用於ubuntu系統(官方鏈接)
1.nvidia-docker安裝
倉庫配置官網
Ubuntu 16.04/18.04, Debian Jessie/Stretch/Buster
此處我的docker爲最新版本:docker 19.03
# Add the package repositories
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker
更新倉庫key
curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | sudo apt-key add -
成功輸出
ok
安裝nvidia-contaner-runtime軟件包
# Ubuntu distributions
sudo apt-get install nvidia-container-runtime
Docker 引擎設置即將runtime註冊到docker
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/override.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --host=fd:// --add-runtime=nvidia=/usr/bin/nvidia-container-runtime
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
配置daemon配置文件
sudo tee /etc/docker/daemon.json <<EOF
{
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
}
}
EOF
sudo pkill -SIGHUP dockerd
2.下載docker鏡像,運行docker,啓動服務,端口映射
- latest:安裝TensorFlow Serving編譯好的二進制文件的最簡docker鏡像,無法進行任何修改,可直接部署。
- latest-gpu:GPU版本的latest鏡像。
- latest-devel -包括所有源/依賴項/工具鏈,以及在CPU上運行的已編譯二進制文件,devel是指development,可開啓鏡像容器bash修改配置,然後使用docker commit製作新鏡像。
- latest-devel-gpu -GPU版本的latest-devel鏡像。
gpu後綴代表可使用gpu加速,devel代表可開啓容器終端自定義配置容器
# Download the TensorFlow Serving Docker image and repo
docker pull tensorflow/serving:latest-gpu
mkdir -p /home/mochen/tmp/tfserving
cd /home/mochen/tmp/tfserving
# 下載示例程序
git clone https://github.com/tensorflow/serving
# Start TensorFlow Serving container and open the REST API port
# 方法一
sudo docker run -t --rm -p 8501:8501 -v "/home/mochen/tmp/tfserving/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_gpu:/models/half_plus_two" -e MODEL_NAME=half_plus_two tensorflow/serving
# 方法二
sudo docker run -dt -p 8501:8501 -v "/home/mochen/tmp/tfserving/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_gpu:/models/half_plus_two" -e MODEL_NAME=half_plus_two tensorflow/serving
# 方法三(建議)
sudo docker run --runtime=nvidia --restart always -d -p 8501:8501 --mount type=bind,source=$(pwd)/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_gpu/,target=/models/half_plus_two -e MODEL_NAME=half_plus_two -t --name testserver tensorflow/serving:latest-gpu &
- -d: 後臺運行(可選),只有執行前臺運行纔會顯示如下的Docker運行成功信息。
- –restart always: 讓其作爲服務方式自動啓動
- -e NVIDIA_VISIBLE_DEVICES=0選項,指定所使用GPU
運行docker容器,並指定用nvidia-docker,表示調用GPU,啓動TensorFlow服務模型,綁定REST API端口8501(gRPC端口爲8500),並映射出來到宿主機的8501端口,使外部可以訪問,當然也可以設置成其它端口,如1234,只需要指定-p 1234:8501即可。通過mount將我們所需的模型從主機(source)映射到容器中預期模型的位置(target)。我們還將模型的名稱作爲環境變量傳遞,這在查詢模型時非常重要。
提示:當出現如下提示表示GPU 版本的Docker運行成功,服務器準備好接受請求:
2018-07-27 00:07:20.773693: I tensorflow_serving/model_servers/main.cc:333]
Exporting HTTP/REST API at:localhost:8501 ...
3.啓動模型推理
# Query the model using the predict API
curl -d '{"instances": [1.0, 2.0, 5.0]}' -X POST http://localhost:8501/v1/models/half_plus_two:predict
# Returns => { "predictions": [2.5, 3.0, 4.5] }
三、gRPC部署手寫體mnist模型
1. 客戶端環境配置(必須提前配置好)
1.1 客戶端用conda創建單獨環境(建議採用)
# 創建conda環境並進入
conda create tensorflow python=3.6
source activate tensorflow
# 安裝tensorflow
conda install tensorflow-gpu==1.13.1
# 修改pip源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 安裝圖像相關軟件庫
pip install matplotlib Pillow opencv-python
# 安裝相關api
pip install -U grpcio grpcio-tools protobuf
pip install tensorflow-serving-api==1.13.1
1.2 在現有環境中添加包
安裝gprc包
pip install --upgrade pip
pip install -U grpcio
pip install -U grpcio-tools
pip install -U protobuf
- -U:表示如果原來已經安裝,此時會升級到最新版本
安裝tensorflow-serving-api包
參照官網
tensorflow-serving-api包 版本必須和客戶端執行環境的版本相同,此處我的tensorflow版本爲gpu-1.13.1
pip install tensorflow-serving-api==1.13.1
2. 訓練並保存mnist模型
首先訓練一個模型,如果要用GPU訓練,則需要修改mnist_saved_model.py文件,在頭文件處新增如下代碼
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
創建模型目錄中創建mnist文件夾,然後運行腳本,將結果文件保存在mnist文件夾中.
mkdir /home/mochen/hd/models_zoo/mnist
cd serving
python tensorflow_serving/example/mnist_saved_model.py /home/mochen/hd/models_zoo/mnist
訓練完之後會在mnist/1文件夾下生成模型文件
3. 以服務的形式運行模型
CPU版本
sudo docker run -p 8500:8500 --mount type=bind,source=/home/mochen/hd/models_zoo/mnist,target=/models/mnist -e MODEL_NAME=mnist -t tensorflow/serving
GPU版本
sudo docker run --runtime=nvidia -p 8500:8500 --mount type=bind,source=/home/mochen/hd/models_zoo/mnist,target=/models/mnist -e MODEL_NAME=mnist -t tensorflow/serving:latest-gpu
客戶端驗證:
python tensorflow_serving/example/mnist_client.py --num_tests=1000 --server=127.0.0.1:8500