

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 AWS Neuron TensorFlow 服務
<a name="tutorial-inferentia-tf-neuron-serving"></a>

本教學課程示範如何在匯出儲存的模型以搭配 TensorFlow Serving 使用之前，建構圖形並新增 AWS Neuron 編譯步驟。TensorFlow 服務是一個服務系統，允許您跨網路擴展推斷。Neuron TensorFlow Serving 使用相同的 API 做為一般 TensorFlow Serving。唯一的差別是，必須為 AWS Inferentia 編譯儲存的模型，且進入點是名為 的不同二進位 `tensorflow_model_server_neuron`。二進位檔位於 `/usr/local/bin/tensorflow_model_server_neuron` ，並預先安裝在 DLAMI 中。

 如需 Neuron SDK 的詳細資訊，請參閱 [AWS Neuron SDK 文件](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/neuron-guide/neuron-frameworks/tensorflow-neuron/index.html)。

**Topics**
+ [先決條件](#tutorial-inferentia-tf-neuron--serving-prerequisites)
+ [啟動 Conda 環境](#tutorial-inferentia-tf-neuron-serving-activate)
+ [編譯和匯出儲存的模型](#tutorial-inferentia-tf-neuron-serving-compile)
+ [為儲存的模型提供服務](#tutorial-inferentia-tf-neuron-serving-serving)
+ [向模型伺服器產生推論請求](#tutorial-inferentia-tf-neuron-serving-inference)

## 先決條件
<a name="tutorial-inferentia-tf-neuron--serving-prerequisites"></a>

在使用本教學課程之前，您應該已完成 [使用 AWS Neuron 啟動 DLAMI 執行個體](tutorial-inferentia-launching.md) 中的設置步驟。您也應該熟悉深度學習和使用 DLAMI。

## 啟動 Conda 環境
<a name="tutorial-inferentia-tf-neuron-serving-activate"></a>

 使用以下命令啟用 TensorFlow-Neuron conda 環境：

```
source activate aws_neuron_tensorflow_p36
```

 如果您需要退出目前的 conda 環境，請執行：

```
source deactivate
```

## 編譯和匯出儲存的模型
<a name="tutorial-inferentia-tf-neuron-serving-compile"></a>

使用下列內容建立名為 `tensorflow-model-server-compile.py` 的 Python 指令碼。 此指令碼建構圖形並使用 Neuron 編譯圖形。然後將編譯後的圖形匯出為儲存的模型。  

```
import tensorflow as tf
import tensorflow.neuron
import os

tf.keras.backend.set_learning_phase(0)
model = tf.keras.applications.ResNet50(weights='imagenet')
sess = tf.keras.backend.get_session()
inputs = {'input': model.inputs[0]}
outputs = {'output': model.outputs[0]}

# save the model using tf.saved_model.simple_save
modeldir = "./resnet50/1"
tf.saved_model.simple_save(sess, modeldir, inputs, outputs)

# compile the model for Inferentia
neuron_modeldir = os.path.join(os.path.expanduser('~'), 'resnet50_inf1', '1')
tf.neuron.saved_model.compile(modeldir, neuron_modeldir, batch_size=1)
```

 使用下列命令編譯模型：

```
python tensorflow-model-server-compile.py
```

 您的輸出看起來應如以下所示：

```
...
INFO:tensorflow:fusing subgraph neuron_op_d6f098c01c780733 with neuron-cc
INFO:tensorflow:Number of operations in TensorFlow session: 4638
INFO:tensorflow:Number of operations after tf.neuron optimizations: 556
INFO:tensorflow:Number of operations placed on Neuron runtime: 554
INFO:tensorflow:Successfully converted ./resnet50/1 to /home/ubuntu/resnet50_inf1/1
```

## 為儲存的模型提供服務
<a name="tutorial-inferentia-tf-neuron-serving-serving"></a>

一旦模型已編譯過，您可以使用以下命令，以 tensorflow\$1model\$1server\$1neuron 二進位檔案為儲存的模型提供服務：

```
tensorflow_model_server_neuron --model_name=resnet50_inf1 \
    --model_base_path=$HOME/resnet50_inf1/ --port=8500 &
```

 您的輸出看起來應該如下所示。編譯的模型 由伺服器在 Inferentia 裝置的 DRAM 中暫存，以準備推論。

```
...
2019-11-22 01:20:32.075856: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:311] SavedModel load for tags { serve }; Status: success. Took 40764 microseconds.
2019-11-22 01:20:32.075888: I tensorflow_serving/servables/tensorflow/saved_model_warmup.cc:105] No warmup data file found at /home/ubuntu/resnet50_inf1/1/assets.extra/tf_serving_warmup_requests
2019-11-22 01:20:32.075950: I tensorflow_serving/core/loader_harness.cc:87] Successfully loaded servable version {name: resnet50_inf1 version: 1}
2019-11-22 01:20:32.077859: I tensorflow_serving/model_servers/server.cc:353] Running gRPC ModelServer at 0.0.0.0:8500 ...
```

## 向模型伺服器產生推論請求
<a name="tutorial-inferentia-tf-neuron-serving-inference"></a>

建立一個叫做 `tensorflow-model-server-infer.py` 的 Python 指令碼，具有以下內容。此指令碼透過 gRPC (為一服務框架) 執行推斷。

```
import numpy as np
import grpc
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc
from tensorflow.keras.applications.resnet50 import decode_predictions

if __name__ == '__main__':
    channel = grpc.insecure_channel('localhost:8500')
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    img_file = tf.keras.utils.get_file(
        "./kitten_small.jpg",
        "https://raw.githubusercontent.com/awslabs/mxnet-model-server/master/docs/images/kitten_small.jpg")
    img = image.load_img(img_file, target_size=(224, 224))
    img_array = preprocess_input(image.img_to_array(img)[None, ...])
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'resnet50_inf1'
    request.inputs['input'].CopyFrom(
        tf.contrib.util.make_tensor_proto(img_array, shape=img_array.shape))
    result = stub.Predict(request)
    prediction = tf.make_ndarray(result.outputs['output'])
    print(decode_predictions(prediction))
```

 使用 gRPC，以下列命令在模型上執行推論：

```
python tensorflow-model-server-infer.py
```

 您的輸出看起來應如以下所示：

```
[[('n02123045', 'tabby', 0.6918919), ('n02127052', 'lynx', 0.12770271), ('n02123159', 'tiger_cat', 0.08277027), ('n02124075', 'Egyptian_cat', 0.06418919), ('n02128757', 'snow_leopard', 0.009290541)]]
```