

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# モデル提供
<a name="model-serving"></a>

Deep Learning AMI with Conda にインストールされているモデル提供のオプションを以下に示します。いずれかのオプションをクリックして、使用方法をご覧ください。

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

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

[TensorFlow Serving は、機械学習モデル向けの柔軟で高パフォーマンスの処理システムです。](https://www.tensorflow.org/tfx/guide/serving)

`tensorflow-serving-api` には、シングルフレームワーク DLAMI が事前にインストールされています。Tensorflow Serving を使用するには、まず 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 Serving について詳しくお知りになりたい場合は、[TensorFlow ウェブサイト](https://www.tensorflow.org/serving/)を参照してください。

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

TorchServe は、PyTorch からエクスポートされた深層学習モデルを供給するための柔軟なツールです。TorchServe には、Deep Learning AMI with Conda がプリインストールされています。

TorchServe の使用方法の詳細については、[Model Server for PyTorch のドキュメント](https://github.com/pytorch/serve/blob/master/docs/README.md)を参照してください。

 **トピック** 

## TorchServe でイメージ分類モデルを供給する
<a name="tutorial-torchserve-serving"></a>

このチュートリアルでは、TorchServe でイメージ分類モデルを供給する方法を説明します。PyTorch で提供されている Densenet-161 モデルを使用します。サーバーが実行されると、予測リクエストをリッスンします。イメージをアップロードすると (この例では猫の画像)、サーバーはモデルがトレーニングされたクラスから最適な 5 つの一致するクラスの予測を返します。

**TorchServe でイメージ分類モデル例を供給するには**

1. Deep Learning AMI with Conda 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 Model archiver for TorchServe](https://github.com/pytorch/serve/blob/master/model-archiver/README.md) を参照してください。

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

   予測エンドポイントは次のような上位 5 つの予測に類似する予測を JSON で返します。ここでは、エジプシャンマウが含まれている可能性が 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)で見つかります。

 **詳細情報** 

 Docker で TorchServe を設定する方法や、最新の TorchServe 機能など、TorchServe のその他のドキュメントについては、GitHub で [TorchServe プロジェクトページ](https://github.com/pytorch/serve)を参照してください。