Reusable Base Image
Overview
This document shows the best practice to reuse a base image and build the model image on top of the base image. Using the base image has some benefits:
If the way you load and use models is the same, these models can share the same base image
If you just want to update the model file, a base image can speed up the building process
The idea is that you write a general model serving code and assume the model file is placed under a certain path. As a model is ready, use docker build
to generate the model image from the base image along with the model files.
To prepare the base image, there are two methods
Build the base image by Language Wrapper
Use pre-packaged server as Base Image
Prerequisites
Build the Base Image by Language Wrapper
Here, we use Tensorflow 2 as a simple showcase. The code is in the Github repo.
Build the Base Image
Write a general model serving code
Model.py
.Create a
requirements.txt
file and write down all required packages.Create a
Dockerfile
with the following content.Build the base image.
Build the Model Image
Based on our previous base image, whenever you have a model outputted by
You can use this base image to build your model deployment image.
First, create a
Dockerfile
.(Please replace the
export_path
to your path)This means you copy your model files into the path that you pre-defined in the base image code.
Then, you can build the model deployment image.
Verify the Model Image
To verify the image, you can run it.
And send a post request by the following format.
The
${INPUT_DATA}
is the data that you can feed into the deployed model for prediction.The dimension of input data must be the same as the model's input shape.
For example, if we create our model with a specified
input_shape=(4,)
by the following definition.Then, we can send a post request that ${INPUT_DATA} with shape 4.
Or if we create our model with a specified
input_shape=(2,2)
by the following definition.Then, we can also send a post request that ${INPUT_DATA} with shape (2,2).
After sending the post request, we can obtain the response output in the following format.
The
${PREDICTION_RESULT}
is a list to represent the prediction value.For example, the following output shows three prediction values in each class.
After verifying your model deployment image, now you can use this image in the PrimeHub model deployment feature.
Use Pre-packaged Server as Base Image
Here, we use the tensorflow2 pre-packaged server as an example.
Build the Model Image
First, prepare the model files. We can use the example model in github. The model files can be found in
tensorflow2/example_model/mnist
.Then, create a Dockerfile, in which we copy the model files into the
/mnt/models
and tell the pre-packaged server to use this path asmodel_uri
.Build the image from the Dockerfile.
Verify the Model Image
Run the model server.
Verify the model server.
The response would be like
Share Your Base Image
Share your base image by pushing it to a docker registry.
Therefore, others can re-use the model serving code again. They can share the same base image and build a model image by
docker
.
Last updated