MXNet -Neuron と AWS Neuron Compiler の使用 - Deep Learning AMI

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

MXNet -Neuron と AWS Neuron Compiler の使用

MXNet -Neuron コンパイル API は、 AWS Inferentia デバイスで実行できるモデルグラフをコンパイルする方法を提供します。

この例では、 API を使用して ResNet-50 モデルをコンパイルし、それを使用して推論を実行します。

Neuron SDK の詳細については、AWS Neuron SDK のドキュメントを参照してください。

前提条件

このチュートリアルを使用する前に、AWS Neuron を使用した DLAMI インスタンスの起動 の設定ステップを完了しておく必要があります。また、深層学習および DLAMI の使用にも精通している必要があります。

Conda 環境のアクティブ化

次のコマンドを使用して、MXNet-Neuron Conda 環境をアクティブにします。

source activate aws_neuron_mxnet_p36

現在の Conda 環境を終了するには、次のコマンドを実行します。

source deactivate

Resnet50 コンパイル

次の内容で mxnet_compile_resnet50.py という Python スクリプトを作成します。このスクリプトは、MXNet -Neuron コンパイル Python API を使用して ResNet-50 モデルをコンパイルします。

import mxnet as mx import numpy as np print("downloading...") path='http://data.mxnet.io/models/imagenet/' mx.test_utils.download(path+'resnet/50-layers/resnet-50-0000.params') mx.test_utils.download(path+'resnet/50-layers/resnet-50-symbol.json') print("download finished.") sym, args, aux = mx.model.load_checkpoint('resnet-50', 0) print("compile for inferentia using neuron... this will take a few minutes...") inputs = { "data" : mx.nd.ones([1,3,224,224], name='data', dtype='float32') } sym, args, aux = mx.contrib.neuron.compile(sym, args, aux, inputs) print("save compiled model...") mx.model.save_checkpoint("compiled_resnet50", 0, sym, args, aux)

次のコマンドを使用してモデルをコンパイルします。

python mxnet_compile_resnet50.py

コンパイルには数分かかります。コンパイルが終了すると、次のファイルが現在のディレクトリに表示されます。

resnet-50-0000.params resnet-50-symbol.json compiled_resnet50-0000.params compiled_resnet50-symbol.json

ResNet50 推論

次の内容で mxnet_infer_resnet50.py という Python スクリプトを作成します。このスクリプトは、サンプルイメージをダウンロードし、それを使用して、コンパイルされたモデルを持つ推論を実行します。

import mxnet as mx import numpy as np path='http://data.mxnet.io/models/imagenet/' mx.test_utils.download(path+'synset.txt') fname = mx.test_utils.download('https://raw.githubusercontent.com/awslabs/mxnet-model-server/master/docs/images/kitten_small.jpg') img = mx.image.imread(fname) # convert into format (batch, RGB, width, height) img = mx.image.imresize(img, 224, 224)  # resize img = img.transpose((2, 0, 1))  # Channel first img = img.expand_dims(axis=0)  # batchify img = img.astype(dtype='float32') sym, args, aux = mx.model.load_checkpoint('compiled_resnet50', 0) softmax = mx.nd.random_normal(shape=(1,)) args['softmax_label'] = softmax args['data'] = img # Inferentia context ctx = mx.neuron() exe = sym.bind(ctx=ctx, args=args, aux_states=aux, grad_req='null') with open('synset.txt', 'r') as f:     labels = [l.rstrip() for l in f] exe.forward(data=img) prob = exe.outputs[0].asnumpy() # print the top-5 prob = np.squeeze(prob) a = np.argsort(prob)[::-1]  for i in a[0:5]:     print('probability=%f, class=%s' %(prob[i], labels[i]))

次のコマンドを使用して、コンパイルされたモデルで推論を実行します。

python mxnet_infer_resnet50.py

出力は次のようになります。

probability=0.642454, class=n02123045 tabby, tabby cat probability=0.189407, class=n02123159 tiger cat probability=0.100798, class=n02124075 Egyptian cat probability=0.030649, class=n02127052 lynx, catamount probability=0.016278, class=n02129604 tiger, Panthera tigris
次のステップ

MXNet-Neuron モデルサービングの使用