TensorFlow サービス - AWS Deep Learning AMIs

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

TensorFlow サービス

TensorFlow Serving は、機械学習モデル用の柔軟で高性能なサービスシステムです。

tensorflow-serving-api には Deep Learning with Conda AMIがプリインストールされています。MNIST モデルをトレーニング、エクスポート、提供するためのスクリプトの例は、 にあります~/examples/tensorflow-serving/

これらの例を実行するには、まず Deep Learning AMI with Conda に接続し、 TensorFlow 環境をアクティブ化します。

$ source activate tensorflow2_p310

ここで、処理するサンプルスクリプトのフォルダにディレクトリを変更します。

$ cd ~/examples/tensorflow-serving/

事前トレーニング済み Inception モデルを提供する

以下に示しているのは、Inception のような異なるモデルを提供するために試すことができる例です。原則として、 に既にダウンロードされている使用可能なモデルとクライアントスクリプトが必要ですDLAMI。

インセプションモデルによる推論の提供とテスト
  1. モデルをダウンロードします。

    $ curl -O https://s3-us-west-2.amazonaws.com/tf-test-models/INCEPTION.zip
  2. モデルを展開します。

    $ unzip INCEPTION.zip
  3. ハスキーの画像をダウンロードします。

    $ curl -O https://upload.wikimedia.org/wikipedia/commons/b/b5/Siberian_Husky_bi-eyed_Flickr.jpg
  4. サーバーを起動します。Amazon Linux の場合、model_base_path に使用されるディレクトリを /home/ubuntu から /home/ec2-user に変更する必要があります。

    $ tensorflow_model_server --model_name=INCEPTION --model_base_path=/home/ubuntu/examples/tensorflow-serving/INCEPTION/INCEPTION --port=9000
  5. サーバーをフォアグラウンドで実行している場合、続行するには別のターミナルセッションを開始する必要があります。新しいターミナルを開き、 TensorFlow で を有効にしますsource activate tensorflow2_p310。次に、任意のテキストエディタを使用して、以下の内容のスクリプトを作成します。このスクリプトに inception_client.py という名前を付けます。このスクリプトは画像のファイル名をパラメータとして受け取り、事前トレーニング済みモデルから予測結果を取得します。

    from __future__ import print_function import grpc import tensorflow as tf import argparse from tensorflow_serving.apis import predict_pb2 from tensorflow_serving.apis import prediction_service_pb2_grpc parser = argparse.ArgumentParser( description='TF Serving Test', formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument('--server_address', default='localhost:9000', help='Tenforflow Model Server Address') parser.add_argument('--image', default='Siberian_Husky_bi-eyed_Flickr.jpg', help='Path to the image') args = parser.parse_args() def main(): channel = grpc.insecure_channel(args.server_address) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) # Send request with open(args.image, 'rb') as f: # See prediction_service.proto for gRPC request/response details. request = predict_pb2.PredictRequest() request.model_spec.name = 'INCEPTION' request.model_spec.signature_name = 'predict_images' input_name = 'images' input_shape = [1] input_data = f.read() request.inputs[input_name].CopyFrom( tf.make_tensor_proto(input_data, shape=input_shape)) result = stub.Predict(request, 10.0) # 10 secs timeout print(result) print("Inception Client Passed") if __name__ == '__main__': main()
  6. サーバーの場所、ポート、ハスキーの写真のファイル名をパラメータとして渡してスクリプトを実行します。

    $ python3 inception_client.py --server=localhost:9000 --image Siberian_Husky_bi-eyed_Flickr.jpg

MNIST モデルのトレーニングと提供

このチュートリアルでは、モデルをエクスポートして、tensorflow_model_server アプリケーションで処理します。最終的に、サンプルのクライアントスクリプトを使用してモデルサーバーをテストできます。

MNIST モデルのトレーニングとエクスポートを行うスクリプトを実行します。スクリプトの唯一の引数として、モデルを保存するフォルダのロケーションを指定する必要があります。ここでは、mnist_model に入力するだけです。スクリプトによってフォルダが作成されます。

$ python mnist_saved_model.py /tmp/mnist_model

このスクリプトは出力に時間がかかることがあるため、少し待ちます。トレーニングが完了して最後にモデルがエクスポートされると、次の内容が表示されます。

Done training! Exporting trained model to mnist_model/1 Done exporting!

次のステップでは、tensorflow_model_server を実行してエクスポートされたモデルを処理します。

$ tensorflow_model_server --port=9000 --model_name=mnist --model_base_path=/tmp/mnist_model

サーバーをテストするクライアントスクリプトが用意されています。

これ テストするには、新しいターミナルウィンドウを開く必要があります。

$ python mnist_client.py --num_tests=1000 --server=localhost:9000

その他の機能と例

TensorFlow Serving の詳細については、 TensorFlow のウェブサイトを参照してください。

Amazon Elastic Inference で TensorFlow Serving を使用することもできます。 https://docs.aws.amazon.com/elastic-inference/latest/developerguide/what-is-ei.html詳細については、 TensorFlow Serving で Elastic Inference を使用する方法に関するガイドを参照してください。