基本的に、以下のチュートリアルを参考に実施する。
https://aws.amazon.com/jp/getting-started/tutorials/build-train-deploy-machine-learning-model-sagemaker/
https://docs.aws.amazon.com/ja_jp/sagemaker/latest/dg/pytorch.html
https://sagemaker.readthedocs.io/en/stable/using_pytorch.html
Amazon SageMaker コンソールから ノートブックインスタンス に移動後、[ノートブックインスタンスの作成] を押下して作成する。
今回は以下の通り作成した。
※以下に記載のないものは、全て未選択 または デフォルト値で作成した。
設定名 | 設定内容 | 補足 |
ノートブックインスタンス名 | sample-notebook1 | |
ノートブックインスタンスのタイプ | ml.t2.medium | |
Elastic Inference | 今回は使用しない | 推論をガンガン行う場合は使用する(GPUが使える) |
ライフサイクル設定 | なし | |
ボリュームサイズ | 5GB | |
IAM ロール | [新しいロールの作成] から 作成 | 「任意の S3 バケット」へのアクセスを許可。 |
ルートアクセス | 有効化 |
しばらくするとステータスが [Pending] から [InService] に変わるので、しばらく待つ。
[Jupyter を開く] から jupyter notebook を開く。
import torch import os def model_fn(model_dir): model = Your_Model() with open(os.path.join(model_dir, 'model.pth'), 'rb') as f: model.load_state_dict(torch.load(f)) return model
# Deserialize the Invoke request body into an object we can perform prediction on input_object = input_fn(request_body, request_content_type) # Perform prediction on the deserialized object, with the loaded model prediction = predict_fn(input_object, model) # Serialize the prediction result into the desired response content type output = output_fn(prediction, response_content_type)