|
| 1 | +# Deploying a TensorFlow Model |
| 2 | + |
| 3 | +This README showcases how to deploy a simple ResNet model on Triton Inference Server. |
| 4 | + |
| 5 | +## Step 1: Export the model |
| 6 | + |
| 7 | +Export a TensorFlow model as a saved model. |
| 8 | + |
| 9 | +``` |
| 10 | +# <xx.xx> is the yy:mm for the publishing tag for NVIDIA's Tensorflow |
| 11 | +# container; eg. 22.04 |
| 12 | +
|
| 13 | +docker run -it --gpus all -v ${PWD}:/workspace nvcr.io/nvidia/tensorflow:<xx.xx>-tf2-py3 |
| 14 | +python export.py |
| 15 | +``` |
| 16 | + |
| 17 | +## Step 2: Set Up Triton Inference Server |
| 18 | + |
| 19 | +To use Triton, we need to build a model repository. The structure of the repository as follows: |
| 20 | +``` |
| 21 | +model_repository |
| 22 | +| |
| 23 | ++-- resnet50 |
| 24 | + | |
| 25 | + +-- config.pbtxt |
| 26 | + +-- 1 |
| 27 | + | |
| 28 | + +-- model.savedmodel |
| 29 | + | |
| 30 | + +-- saved_model.pb |
| 31 | + +-- variables |
| 32 | + | |
| 33 | + +-- variables.data-00000-of-00001 |
| 34 | + +-- variables.index |
| 35 | +``` |
| 36 | + |
| 37 | +A sample model configuration of the model is included with this demo as `config.pbtxt`. If you are new to Triton, it is highly recommended to [review Part 1](../../Conceptual_Guide/Part_1-model_deployment/README.md) of the conceptual guide. |
| 38 | +``` |
| 39 | +docker run --gpus all --rm -p 8000:8000 -p 8001:8001 -p 8002:8002 -v ${PWD}/model_repository:/models nvcr.io/nvidia/tritonserver:<xx.yy>-py3 tritonserver --model-repository=/models --backend-config=tensorflow,version=2 |
| 40 | +``` |
| 41 | + |
| 42 | +## Step 3: Using a Triton Client to Query the Server |
| 43 | + |
| 44 | +Install dependencies & download an example image to test inference. |
| 45 | + |
| 46 | +``` |
| 47 | +pip install --upgrade tensorflow |
| 48 | +pip install pillow |
| 49 | +pip install nvidia-pyindex |
| 50 | +pip install tritonclient[all] |
| 51 | +
|
| 52 | +wget -O img1.jpg "https://www.hakaimagazine.com/wp-content/uploads/header-gulf-birds.jpg" |
| 53 | +``` |
| 54 | +Building a client requires three basic points. Firstly, we setup a connection with the Triton Inference Server. |
| 55 | +``` |
| 56 | +triton_client = httpclient.InferenceServerClient(url="localhost:8000") |
| 57 | +``` |
| 58 | +Secondly, we specify the names of the input and output layer(s) of our model. |
| 59 | +``` |
| 60 | +inputs = httpclient.InferInput("input_1", transformed_img.shape, datatype="FP32") |
| 61 | +inputs.set_data_from_numpy(transformed_img, binary_data=True) |
| 62 | +
|
| 63 | +output = httpclient.InferRequestedOutput("predictions", binary_data=True, class_count=1000) |
| 64 | +``` |
| 65 | +Lastly, we send an inference request to the Triton Inference Server. |
| 66 | +``` |
| 67 | +# Querying the server |
| 68 | +results = triton_client.infer(model_name="resnet50", inputs=[inputs], outputs=[output]) |
| 69 | +predictions = results.as_numpy('predictions') |
| 70 | +print(predictions) |
| 71 | +``` |
| 72 | +The output of the same should look like below: |
| 73 | +``` |
| 74 | +[b'0.301167:90' b'0.169790:14' b'0.161309:92' b'0.093105:94' |
| 75 | + b'0.058743:136' b'0.050185:11' b'0.033802:91' b'0.011760:88' |
| 76 | + b'0.008309:989' b'0.004927:95' b'0.004905:13' b'0.004095:317' |
| 77 | + b'0.004006:96' b'0.003694:12' b'0.003526:42' b'0.003390:313' |
| 78 | + ... |
| 79 | + b'0.000001:751' b'0.000001:685' b'0.000001:408' b'0.000001:116' |
| 80 | + b'0.000001:627' b'0.000001:933' b'0.000000:661' b'0.000000:148'] |
| 81 | +``` |
| 82 | +The output format here is `<confidence_score>:<classification_index>`. To learn how to map these to the label names and more, refer to our [documentation](https://github.com/triton-inference-server/server/blob/main/docs/protocol/extension_classification.md). The client code above is available in `client.py`. |
0 commit comments