

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# AWS Neuron TensorFlow Serving 사용
<a name="tutorial-inferentia-tf-neuron-serving"></a>

이 자습서에서는 TensorFlow Serving에 사용할 저장된 모델을 내보내기 전에 그래프를 구성하고 AWS Neuron 컴파일 단계를 추가하는 방법을 보여줍니다. TensorFlow Serving은 네트워크를 통해 추론을 확장할 수 있는 지원 시스템입니다. Neuron TensorFlow Serving은 일반적인 TensorFlow Serving과 동일한 API를 사용합니다. 유일한 차이점은 저장된 모델을 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\_model\_server\_neuron 이진 파일로 제공할 수 있습니다.

```
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)]]
```