为 TF 服务构建 Docker 镜像
我们继续使用 Docker 镜像进行如下操作:
- 使用以下内容创建名为
dockerfile
的文件:
FROM ubuntu:16.04
MAINTAINER Armando Fandango <[email protected]>
RUN apt-get update && apt-get install -y \
build-essential \
curl \
git \
libfreetype6-dev \
libpng12-dev \
libzmq3-dev \
mlocate \
pkg-config \
python-dev \
python-numpy \
python-pip \
software-properties-common \
swig \
zip \
zlib1g-dev \
libcurl3-dev \
openjdk-8-jdk\
openjdk-8-jre-headless \
wget \
&& \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" \
| tee /etc/apt/sources.list.d/tensorflow-serving.list
RUN curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg \
| apt-key add -
RUN apt-get update && apt-get install -y \
tensorflow-model-server
RUN pip install --upgrade pip
RUN pip install mock grpcio tensorflow tensorflow-serving-api
CMD ["/bin/bash"]
- 运行以下命令从此
dockerfile
构建 Docker 镜像:
$ docker build --pull -t $USER/tensorflow_serving -f dockerfile .
- 创建图像需要一段时间。当您看到类似于以下内容的内容时,您就会知道图像已构建:
Removing intermediate container 1d8e757d96e0
Successfully built 0f95ddba4362
Successfully tagged armando/tensorflow_serving:latest
- 运行以下命令以启动容器:
$ docker run --name=mnist_container -it $USER/tensorflow_serving
- 当您看到以下提示时,您将登录到容器:
root@244ea14efb8f:/#
- 将
cd
命令转到主文件夹。 - 在主文件夹中,提供以下命令以检查 TensorFlow 是否正在提供代码。我们将使用此代码中的示例来演示,但您可以查看自己的 Git 仓库来运行您自己的模型:
$ git clone --recurse-submodules https://github.com/tensorflow/serving
克隆仓库后,我们就可以构建,训练和保存 MNIST 模型了。
- 使用以下命令删除临时文件夹(如果尚未删除):
$ rm -rf /tmp/mnist_model
- 运行以下命令以构建,训练和保存 MNIST 模型。
$ python serving/tensorflow_serving/example/mnist_saved_model.py /tmp/mnist_model
您将看到类似于以下内容的内容:
Training model...
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting /tmp/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting /tmp/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting /tmp/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting /tmp/t10k-labels-idx1-ubyte.gz
2017-11-22 01:09:38.165391: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
training accuracy 0.9092
Done training!
Exporting trained model to /tmp/mnist_model/1
Done exporting!
- 按
Ctrl + P
和Ctrl + Q
从 Docker 镜像中分离。 - 提交对新映像的更改并使用以下命令停止容器:
$ docker commit mnist_container $USER/mnist_serving
$ docker stop mnist_container
- 现在,您可以通过提供以下命令随时运行此容器:
$ docker run --name=mnist_container -it $USER/mnist_serving
- 删除我们为保存图像而构建的临时 MNIST 容器:
$ docker rm mnist_container