AWSの「Neo-AI project」の復習。
Neo-AIは、Re:Inventにて、Amazon SageMaker Neo – Train Your Machine Learning Models Once, Run Them Anywhereとして発表したAmazon SageMaker Neoから。
・Frameworks and algorithms: TensorFlow, Apache MXNet, PyTorch, ONNX, and XGBoost. ・Hardware architectures: ARM, Intel, and NVIDIA starting today, with support for Cadence, Qualcomm, and Xilinx hardware coming soon. In addition, Amazon SageMaker Neo is released as open source code under the Apache Software License, enabling hardware vendors to customize it for their processors and devices.
おー、独自システムも対応できるんねん。
モデルは、次の3つから構成される。
・モデル定義 ・モデルパラメータ ・コンパイル済みのオペレーション
MXNetでの学習済みResnet50モデル(model.tar.gz )からモデル変換をします。
model.tar.gz から次の2つのファイルを解凍します。
model.tar.gz から次の2つのファイルを解凍します。
・resnet50_v1-symbol.json ・resnet50_v1-0000.paramsresnet50_v1-0000.params
resnet50_v1-symbol.jsonは、次のようなJsonになっています。
{ "CompilationJobName": "resnet50-mxnet-raspberrypi", "RoleArn": $SAGEMAKER_ROLE_ARN, "InputConfig": { "S3Uri": "s3://jsimon-neo/model.tar.gz", "DataInputConfig": "{\"data\": [1, 3, 224, 224]}", "Framework": "MXNET" }, "OutputConfig": { "S3OutputLocation": "s3://jsimon-neo/", "TargetDevice": "rasp3b" }, "StoppingCondition": { "MaxRuntimeInSeconds": 300 } }
コンパイル結果のファイルが、model-rasp3b.tar.gz で、
・compiled.params => モデルパラメータ ・compiled_model.json => モデル構造 ・compiled.so => コンパイル済みのオペレーション
これらのファイルを使って、Neo-AI DLRを使って、Raspberry Pi.3+にて推論するようです。
推論用のPythonコードは、
import os import numpy as np from dlr import DLRModel # Load the compiled model input_shape = {'data': [1, 3, 224, 224]} # A single RGB 224x224 image output_shape = [1, 1000] # The probability for each one of the 1,000 classes device = 'cpu' # Go, Raspberry Pi, go! model = DLRModel('resnet50', input_shape, output_shape, device) # Load names for ImageNet classes synset_path = os.path.join(model_path, 'synset.txt') with open(synset_path, 'r') as f: synset = eval(f.read()) # Load an image stored as a numpy array image = np.load('dog.npy').astype(np.float32) print(image.shape) input_data = {'data': image} # Predict out = model.run(input_data) top1 = np.argmax(out[0]) prob = np.max(out) print("Class: %s, probability: %f" % (synset[top1], prob))
DLRModelを使ってモデルをロードして、runメソッドにて推論する。
結果をnp.argmax(out[0])でトップ1を取り出し、その結果をmaxにて確率を求めています。
結果をnp.argmax(out[0])でトップ1を取り出し、その結果をmaxにて確率を求めています。