

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 模型处理
<a name="model-serving"></a>

以下是已在带 Conda 的深度学习 AMI 上安装的模型处理选项。单击其中一个选项可了解如何使用该选项。

**Topics**
+ [TensorFlow 上菜](tutorial-tfserving.md)
+ [TorchServe](tutorial-torchserve.md)

# TensorFlow 上菜
<a name="tutorial-tfserving"></a>

TensorFlow S@@ [er](https://www.tensorflow.org/tfx/guide/serving) ving 是一款适用于机器学习模型的灵活、高性能的服务系统。

`tensorflow-serving-api` 预先安装了单一框架 DLAMI。要使用 tensorflow 服务，请先激活环境。 TensorFlow 

```
$ source /opt/tensorflow/bin/activate
```

然后，使用您的首选文本编辑器创建具有以下内容的脚本。将它命名为 `test_train_mnist.py`。此脚本引自[TensorFlow 教程，该教程](https://github.com/tensorflow/docs/blob/master/site/en/tutorials/quickstart/beginner.ipynb)将训练和评估对图像进行分类的神经网络机器学习模型。

```
import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
```

现在，运行将服务器位置和端口以及哈士奇照片的文件名作为参数传递的脚本。

```
$ /opt/tensorflow/bin/python3 test_train_mnist.py
```

 请耐心等待，因为此脚本可能需要一段时间才能提供输出。在训练完成后，您应该看到以下内容：

```
I0000 00:00:1739482012.389276    4284 device_compiler.h:188] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.
1875/1875 [==============================] - 24s 2ms/step - loss: 0.2973 - accuracy: 0.9134 
Epoch 2/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1422 - accuracy: 0.9582
Epoch 3/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.1076 - accuracy: 0.9687
Epoch 4/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0872 - accuracy: 0.9731
Epoch 5/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0731 - accuracy: 0.9771
313/313 [==============================] - 0s 1ms/step - loss: 0.0749 - accuracy: 0.9780
```

## 更多功能和示例
<a name="tutorial-tfserving-project"></a>

如果您有兴趣了解有关 TensorFlow 服务的更多信息，请[TensorFlow 访问网站](https://www.tensorflow.org/serving/)。

# TorchServe
<a name="tutorial-torchserve"></a>

TorchServe 是一款灵活的工具，用于提供已从中导出的深度学习模型 PyTorch。 TorchServe 预装了带有 Conda 的深度学习 AMI。

有关使用的更多信息 TorchServe，[请参阅 PyTorch文档模型服务器](https://github.com/pytorch/serve/blob/master/docs/README.md)。

 **主题** 

## 在上提供图像分类模型 TorchServe
<a name="tutorial-torchserve-serving"></a>

本教程介绍如何使用提供图像分类模型 TorchServe。它使用提供的 DenseNet -161 模型。 PyTorch 服务器运行后，它会监听预测请求。在这种情况下，如果您上传图像（一张小猫的图像），服务器会返回在其上训练该模型的类中匹配的前 5 个类的预测。

**在上提供图像分类模型示例 TorchServe**

1. 使用带 Conda 的深度学习 AMI（v34 或更高版本）来连接到 Amazon Elastic Compute Cloud (Amazon EC2) 实例。

1. 激活 `pytorch_p310` 环境。

   ```
   source activate pytorch_p310
   ```

1. 克隆 TorchServe 存储库，然后创建一个目录来存储您的模型。  

   ```
   git clone https://github.com/pytorch/serve.git
   mkdir model_store
   ```

1. 使用模型存档程序来存档模型。该`extra-files`参数使用`TorchServe`存储库中的文件，因此如有必要，请更新路径。 有关模型存档器的更多信息，请参阅 [Torch 模型存档器](https://github.com/pytorch/serve/blob/master/model-archiver/README.md)。 TorchServe

   ```
   wget https://download.pytorch.org/models/densenet161-8d451a50.pth
   torch-model-archiver --model-name densenet161 --version 1.0 --model-file ./serve/examples/image_classifier/densenet_161/model.py --serialized-file densenet161-8d451a50.pth --export-path model_store --extra-files ./serve/examples/image_classifier/index_to_name.json --handler image_classifier
   ```

1. 运行 TorchServe 以启动终端节点。添加 `> /dev/null` 会使日志输出静音。

   ```
   torchserve --start --ncs --model-store model_store --models densenet161.mar > /dev/null
   ```

1. 下载小猫的图像并将其发送到 TorchServe预测端点：

   ```
   curl -O https://s3.amazonaws.com/model-server/inputs/kitten.jpg
   curl http://127.0.0.1:8080/predictions/densenet161 -T kitten.jpg
   ```

   该预测终端节点将返回一个 JSON 格式的预测（类似于下面的前 5 个预测），其中，图像具有 47% 的可能性为埃及猫，然后有 46% 的可能性为虎斑猫。

   ```
   {
    "tiger_cat": 0.46933576464653015,
    "tabby": 0.463387668132782,
    "Egyptian_cat": 0.0645613968372345,
    "lynx": 0.0012828196631744504,
    "plastic_bag": 0.00023323058849200606
   }
   ```

1. 当您完成测试时，停止服务器：

   ```
   torchserve --stop
   ```

 **其他示例** 

TorchServe 提供了各种各样的示例，您可以在 DLAMI 实例上运行这些示例。您可以在[ TorchServe项目存储库示例页面上](https://github.com/pytorch/serve/tree/master/examples)查看它们。

 **更多信息** 

 有关更多 TorchServe 文档，包括如何 TorchServe使用 Docker 进行设置和最新 TorchServe 功能，请参阅[上的 TorchServe GitHub项目页面](https://github.com/pytorch/serve)。