{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# モデルの URI とコンテナイメージの取得\n", "import boto3\n", "sm_client = boto3.client('sagemaker')\n", "endpoint_config_name = sm_client.describe_endpoint(EndpointName='jumpstart-ftc-kinoko-detection-endpoint')['EndpointConfigName']\n", "model_name = sm_client.describe_endpoint_config(EndpointConfigName=endpoint_config_name)['ProductionVariants'][0]['ModelName']\n", "model_detail = sm_client.describe_model(ModelName=model_name)\n", "model_container_image = model_detail['PrimaryContainer']['Image']\n", "model_s3_uri = model_detail['PrimaryContainer']['ModelDataUrl']\n", "print(model_container_image)\n", "print(model_s3_uri)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# モデルのダウンロード\n", "!rm -rf model/\n", "!mkdir -p model/\n", "!aws s3 cp {model_s3_uri} ./model/\n", "%cd model/\n", "!tar zxvf model.tar.gz" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# 推論準備\n", "%cd code\n", "!pip install sagemaker-inference" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 依存しているライブラリをインストール\n", "!pip install sagemaker-inference\n", "\n", "import json, inference\n", "from PIL import Image, ImageDraw, ImageFont\n", "\n", "# モデルのロード\n", "model = inference.model_fn('../')\n", "# 推論に使う画像ファイル\n", "IMAGE_FILE = '../../../test_raw_images/lattice.jpg'\n", "# 画像ファイルをバイナリとして開く\n", "with open(IMAGE_FILE,'rb') as f:\n", " img_bin = f.read()\n", "# 推論\n", "pred = json.loads(inference.transform_fn(model,img_bin,'application/x-image','application/json'))\n", "# 推論結果の展開\n", "bboxes, classes, confidences = pred['normalized_boxes'], pred['classes'], pred['scores']\n", "# 推論結果の描画\n", "img = Image.open(IMAGE_FILE)\n", "draw = ImageDraw.Draw(img)\n", "for i in range(len(bboxes)):\n", " # 信頼度スコアが0.8以上のみ描画する\n", " if confidences[i]>0.8:\n", " # 矩形の相対座標を取得\n", " left, top, right, bottom = bboxes[i]\n", " # 矩形の相対座標を絶対座標に変換\n", " left = img.size[0] * left\n", " top = img.size[1] * top\n", " right = img.size[0] * right\n", " bottom = img.size[1] * bottom\n", " # 検出した物体の ID を take/kino に読み替える\n", " text = 'take' if int(classes[i])==0 else 'kino'\n", " # take/kinoに対して矩形で描画するための色を設定する\n", " color = 'blue' if text == 'take' else 'red'\n", " # 矩形の左上に表示する文字の大きさを設定、きのこの山なら大きくする\n", " TEXTSIZE=14 if classes[i]=='0' else 18\n", " # 矩形の先の太さを設定、きのこの山なら太くする\n", " LINEWIDTH=4 if classes[i]=='0' else 6\n", " # 矩形を描画する\n", " draw.rectangle([(left,top),(right,bottom)], outline=color, width=LINEWIDTH)\n", " # 矩形の左上に描画する信頼度スコアの取得\n", " text += f' {str(round(confidences[i],3))}'\n", " # テキストを描画する場所を取得\n", " txpos = (left, top-TEXTSIZE-LINEWIDTH//2)\n", " # フォントの設定\n", " font = ImageFont.truetype(\"/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf\", size=TEXTSIZE)\n", " # 描画するテキストのサイズを取得\n", " txw, txh = draw.textsize(text, font=font)\n", " # テキストの背景用の矩形を描画\n", " draw.rectangle([txpos, (left+txw, top)], outline=color, fill=color, width=LINEWIDTH)\n", " # テキストを描画\n", " draw.text(txpos, text, fill='white',font=font)\n", "# 画像をファイルに書き出す\n", "img.save('pred_lattice.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "img" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "instance_type": "ml.t3.medium", "kernelspec": { "display_name": "Python 3 (MXNet 1.8 Python 3.7 CPU Optimized)", "language": "python", "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-2:429704687514:image/mxnet-1.8-cpu-py37-ubuntu16.04-v1" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" } }, "nbformat": 4, "nbformat_minor": 4 }