Open Yukyukuon opened 9 months ago
Docker 仮想環境:zepan/zhouyi
NNコンパイラはpb,tflite,caffemodel,onnx形式をサポートしています。 VGG16 pretrain model を使うため、Frozen modelは要ります。
ダウンロードモデル:
wget http://download.tensorflow.org/models/vgg_16_2016_08_28.tar.gz tar -xzvf vgg_16_2016_08_28.tar.gz
clone Tensorflow のモデル:
git clone https://github.com/tensorflow/models.git
推論図の導出:
cd models/research/slim python setup.py install python3 export_inference_graph.py \ --alsologtostderr \ --model_name=vgg_16 \ --image_size=224 \ --labels_offset=1 \ --output_file=/home/test/vgg/model/vgg.pb
output_fileの場所で .pb の推理図を生成しました。netron.appのウェブページで出力されたグラフ構造が見えます。
Tensorflow のソースコードをクローン:
git clone -b r1.15 --single-branch https://github.com/tensorflow/tensorflow.git
Frozen model:
cd tensorflow/tensorflow/python/tools python3 freeze_graph.py \ --input_graph=/home/vgg16/model/vgg.pb \ ##推論図 --input_checkpoint=/home/vgg16/vgg_16.ckpt \ ##ダウンロードvggモデル --input_binary=true \ --output_node_names=vgg_16/fc8/BiasAdd \ ## 凍結された層(Squeezeまで、名前がnetron.appで見える) --output_graph=/home/vgg16/model/vgg_frozen.pb ##frozen model 出力位置
modelにvgg_frozen.pbファイルを見ると、フリーズが成功した
簡易化(5枚データ): \DNNA\AI610-SDK-r0p1-00eac0\AI610-SDK-1003-r0p1-eac0\user-case-example\tf\preprocess_resnet_50_datasetのファイルを使う、 データは/imgにあり、ラベルはlabel.txtにある。 次はpreprocess_for_resnet50_dataset.pyを使って
python preprocess_for_resnet50_dataset.py
量子化用のデータセットdataset.npyとラベルlabel.npyが得られる。
/root/demos/pb/model下のgen_inputbin.pyをコピーする テスト用の1枚画像を用意して、gen_inputbin.py を使って、.binファイルに転換する (gen_inputbin.py 内の転換したい画像のファイル名が変えるべき)
python3 gen_inputbin.py
テスト画像input.binが生成する output_ref.binはDNNA\AI610-SDK-r0p1-00eac0\AI610-SDK-1003-r0p1-eac0\user-case-example\tf\output_ref下にある、コピーする
先ずは、DNNA\AI610-SDK-1002-r0p0-eac0下のsimulatorファイルをコピーする 次は、DNNA\AI610-SDK-r0p1-00eac0\AI610-SDK-1003-r0p1-eac0\user-case-example\tf\config下のtf_resnet_50_build.cfgとtf_resnet_50_run.cfgファイルをコピーする
コピーされたtf_resnet_50_run.cfg(名前tf_vgg_16_run.cfgに変更)内容を以下に変更する:
[Common] mode = run [Parser] model_name = vgg_16 ## モデル名はVGG16 detection_postprocess = model_domain = image_classification output = vgg_16/fc8/BiasAdd ##凍結までの層 input_model = ./model/vgg_frozen.pb ##フリーズモデルの位置 input = input input_shape = [1, 224, 224, 3] output_dir = ./ [AutoQuantizationTool] model_name = vgg_16 quantize_method = SYMMETRIC ops_per_channel = DepthwiseConv calibration_data = ./preprocess_vgg_16_dataset/dataset.npy ##量子化用のデータセットの位置 calibration_label = ./preprocess_vgg_16_dataset/label.npy ##量子化用のラベルの位置 preprocess_mode = normalize quant_precision=int8 reverse_rgb = False label_id_offset = 0 [GBuilder] inputs=./test/input.bin ##テスト画像.binファイルの位置 simulator=./sdk/simulator/bin/aipu_simulator_z1 ##コピーされたaipu_simulatorの位置 outputs=output_vgg_16.bin ##出力の名前 profile= True target=Z1_0701
以下の内容出力は成功でした
[INFO]:AIPU START RUNNING: BIN[0] [INFO]:TOTAL TIME: 22.809628s. [INFO]:SIMULATOR EXIT! [I] [main.cpp : 135] Simulator finished. Total errors: 0, warnings: 0
docker環境下の/root/demos/tfliteのquant_predict.pyをコピーして、以下の内容に修正する:
from PIL import Image import cv2 from matplotlib import pyplot as plt import matplotlib.patches as patches import numpy as np import os import imagenet_classes as class_name current_dir = os.getcwd() label_offset = 1 outputfile = current_dir + '/output_vgg_16.bin' ##前NNコンパイラ出力ファイルの名前に変更 npyoutput = np.fromfile(outputfile, dtype=np.int8) ## np.uint8からnp.int8に変更 outputclass = npyoutput.argmax() head5p = npyoutput.argsort()[-5:][::-1] labelfile = current_dir + '/test/output_ref.bin' ## 前コピーされたoutput_ref.bin の位置に変更 npylabel = np.fromfile(labelfile, dtype=np.int8) labelclass = npylabel.argmax() head5t = npylabel.argsort()[-5:][::-1] print("predict first 5 label:") for i in head5p: print(" index %4d, prob %3d, name: %s"%(i, npyoutput[i], class_name.class_names[i-label_offset])) print("true first 5 label:") for i in head5t: print(" index %4d, prob %3d, name: %s"%(i, npylabel[i], class_name.class_names[i-label_offset])) # Show input picture print('Detect picture save to result.jpeg') input_path = './test/input.bin' ## 前生成されたinput.binの位置を記入 npyinput = np.fromfile(input_path, dtype=np.int8) image = np.clip(np.round(npyinput)+128, 0, 255).astype(np.uint8) image = np.reshape(image, (224, 224, 3)) im = Image.fromarray(image) im.save('result.jpeg')
以下の内容出力すると成功した
predict first 5 label: index 349, prob 127, name: ram, tup index 351, prob 127, name: ibex, Capra ibex index 345, prob 127, name: hippopotamus, hippo, river horse, Hippopotamus amphibius index 352, prob 127, name: hartebeest index 353, prob 127, name: impala, Aepyceros melampus true first 5 label: index 231, prob 53, name: Shetland sheepdog, Shetland sheep dog, Shetland index 999, prob 0, name: ear, spike, capitulum index 343, prob 0, name: wild boar, boar, Sus scrofa index 341, prob 0, name: zebra index 340, prob 0, name: sorrel Detect picture save to result.jpeg
PS:ここでtrue first 5 label は前コピーしたoutput_ref.binからの結果のため、予測ラベルのみ見る。(output_ref.binの作成方法まだ見つかりません)
手順
Docker 仮想環境:zepan/zhouyi
参考VGG16の部署
1.モデルを生成する
NNコンパイラはpb,tflite,caffemodel,onnx形式をサポートしています。 VGG16 pretrain model を使うため、Frozen modelは要ります。
ダウンロードモデル:
clone Tensorflow のモデル:
推論図の導出:
output_fileの場所で .pb の推理図を生成しました。netron.appのウェブページで出力されたグラフ構造が見えます。
Tensorflow のソースコードをクローン:
Frozen model:
modelにvgg_frozen.pbファイルを見ると、フリーズが成功した
2. 量子化校正データセットの作成
簡易化(5枚データ): \DNNA\AI610-SDK-r0p1-00eac0\AI610-SDK-1003-r0p1-eac0\user-case-example\tf\preprocess_resnet_50_datasetのファイルを使う、
データは/imgにあり、ラベルはlabel.txtにある。
次はpreprocess_for_resnet50_dataset.pyを使って
量子化用のデータセットdataset.npyとラベルlabel.npyが得られる。
3. input.binとoutput_ref.binを生成
/root/demos/pb/model下のgen_inputbin.pyをコピーする
テスト用の1枚画像を用意して、gen_inputbin.py を使って、.binファイルに転換する
(gen_inputbin.py 内の転換したい画像のファイル名が変えるべき)
テスト画像input.binが生成する
output_ref.binはDNNA\AI610-SDK-r0p1-00eac0\AI610-SDK-1003-r0p1-eac0\user-case-example\tf\output_ref下にある、コピーする
4.NNコンパイラ設定ファイルの編集
先ずは、DNNA\AI610-SDK-1002-r0p0-eac0下のsimulatorファイルをコピーする
次は、DNNA\AI610-SDK-r0p1-00eac0\AI610-SDK-1003-r0p1-eac0\user-case-example\tf\config下のtf_resnet_50_build.cfgとtf_resnet_50_run.cfgファイルをコピーする
コピーされたtf_resnet_50_run.cfg(名前tf_vgg_16_run.cfgに変更)内容を以下に変更する:
以下の内容出力は成功でした
5.シミュレーションの結果の検証
docker環境下の/root/demos/tfliteのquant_predict.pyをコピーして、以下の内容に修正する:
以下の内容出力すると成功した
PS:ここでtrue first 5 label は前コピーしたoutput_ref.binからの結果のため、予測ラベルのみ見る。(output_ref.binの作成方法まだ見つかりません)