In [1]:
import os, shutil
from IPython.display import Image as  IpyImage, display as ipyDisplay
In [2]:
if "WORKSPACE" not in locals():
    WORKSPACE="/workspace"
%cd {WORKSPACE}
/workspace

model from yunet-host-notenook.ipynb¶

In [3]:
modelSize = (224,224)
modelName = "yunet-224x224-sim"
modelFileName = f"{modelName}.onnx"
modelPath = os.path.join(WORKSPACE, "host", "notebook", "models", "yunet", modelFileName)
modelPath
Out[3]:
'/workspace/host/notebook/models/yunet/yunet-224x224-sim.onnx'

Cut post process nodes from model¶

  • use libs/ONNX_Convertor/optimizer_scripts/editor.py will be failed because of onnx.utils.polish_model
In [4]:
cutPostModelName = f"{modelName}-cutPost"
cutPostModelFileName = f"{cutPostModelName}.onnx"
cutPostModelPath = os.path.join(WORKSPACE, "host", "notebook", "models", "yunet", cutPostModelFileName)
cutPostModelPath
Out[4]:
'/workspace/host/notebook/models/yunet/yunet-224x224-sim-cutPost.onnx'

Bypass onnx.utils.polish_model¶

  • create editor_bypass_polish.py and comment out the line
In [5]:
!sed '31s/^/# /' libs/ONNX_Convertor/optimizer_scripts/editor.py > libs/ONNX_Convertor/optimizer_scripts/editor_bypass_polish.py
In [6]:
!diff libs/ONNX_Convertor/optimizer_scripts/editor.py libs/ONNX_Convertor/optimizer_scripts/editor_bypass_polish.py
31c31
< m = onnx.utils.polish_model(m)
---
> # m = onnx.utils.polish_model(m)

Run editor_bypass_polish.py instead of editor.py¶

In [7]:
!python libs/ONNX_Convertor/optimizer_scripts/editor_bypass_polish.py \
    $modelPath $cutPostModelPath \
    -c Reshape_100 \
    --rename-output onnx::Shape_297 output
/workspace/miniconda/lib/python3.7/site-packages/numpy/__init__.py:156: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init

Optimize model using libs/ONNX_Convertor/optimizer_scripts/onnx2onnx.py¶

In [8]:
optModelName = f"{cutPostModelName}-opt"
optModelFileName = f"{optModelName}.onnx"
optModelPath = os.path.join(WORKSPACE, "host", "notebook", "models", "yunet", optModelFileName)
optModelPath
Out[8]:
'/workspace/host/notebook/models/yunet/yunet-224x224-sim-cutPost-opt.onnx'
In [9]:
!python libs/ONNX_Convertor/optimizer_scripts/onnx2onnx.py $cutPostModelPath -o $optModelPath
/workspace/miniconda/lib/python3.7/site-packages/numpy/__init__.py:156: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
INFO:optimizer_scripts:Preprocessing the model...
INFO:optimizer_scripts:Doing nodes fusion and replacement... 
INFO:optimizer_scripts:Postprocessing the model...

Prepare input_params.json¶

In [10]:
inputImgDir = os.path.join(WORKSPACE, "host", "notebook", "quantizationImages")
# inputImg1Path = os.path.join(inputImgDir, "input_img1.jpg")
# inputImg2Path = os.path.join(inputImgDir, "input_img2.jpg")
# %ls $inputImgDir/*
# ipyDisplay(
#     IpyImage(width=224, data=inputImg1Path),
#     IpyImage(width=224, data=inputImg2Path)
# )
In [11]:
from math import ceil, log2
input_params = {
    "model_info":{
        "input_onnx_file": optModelPath,
        "model_inputs": [
            {
                "model_input_name": "input",
                "input_image_folder": inputImgDir
            }
        ],
        "quantize_mode": "default",
        "outlier": 0.999
    },
    "preprocess": {
        "img_preprocess_method": "none", # 0~255
        "img_channel": "BGR",
        "radix": 7-ceil(log2(255)),
        "keep_aspect_ratio": True,
        "pad_mode": 1
    },
    "simulator_img_files": [
        {
            "model_input_name": "input",
            "input_image": e.path
        } for e in os.scandir(inputImgDir) if e.is_file()
    ]
}
import json
with open('/data1/input_params.json', 'w') as f:
    json.dump(input_params, f)
!file /data1/input_params.json
/data1/input_params.json: ASCII text, with very long lines, with no line terminators

Run compilerIpevaluator_720.sh and scripts/fpAnalyserCompilerIpevaluator_720.py¶

In [12]:
%pwd
optModelPath
Out[12]:
'/workspace/host/notebook/models/yunet/yunet-224x224-sim-cutPost-opt.onnx'
In [13]:
!cd scripts && ./compilerIpevaluator_720.sh $optModelPath
!echo '# ls /data1/compiler'
!ls /data1/compiler
!echo '# ls /data1/compiler/ProfileResult.txt'
!cat /data1/compiler/ProfileResult.txt
running compiler and ipEvaluator...
Compilation and IP Evaluation finished.
# ls /data1/compiler
ProfileResult.txt  command.bin	ioinfo.csv  setup.bin  weight.bin
# ls /data1/compiler/ProfileResult.txt
=========================
===== Configuration =====
=========================
Frequency = 700 Mhz
Bit width = 8
MAC# = 1024
Batch size = 1
DDR effective bandwidth = 6 GB/s
Effective model compression rate = 1
   
=========================
===== Output result =====
=========================
output_fps = 432.794
output_total_cycle = 1.6174e+06
output_total_time = 2.31057 ms
   
===== Data move =====
output_total_data_move_time = 0.432907 ms
output_total_data_move_amount = 0.00259744 GB
output_avg_data_bw = 1.12416 GB/s
   
===== Weight loading =====
output_total_weight_size = 3312.83 KB
output_avg_weight_bw = 1.43377 GB/s
   
===== Dram =====
output_avg_dram_bw = 2.55793 GB/s
In [14]:
!python scripts/fpAnalyserCompilerIpevaluator_720.py
/workspace/miniconda/lib/python3.7/site-packages/numpy/__init__.py:156: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
[=====================================================================>]98% 1.202000s0s% 0.001000s              ]31% 0.001000s===>                          ]62% 1.182000s                                       ]30% 0.001000s===========================>                           ]60% 1.181000s==============================================>                ]75% 1.219000s.002000s  ]54% 1.160000s==========================]100% 1.203000s
In [15]:
!echo
!echo '# ls /data1/fpAnalyser'
!ls /data1/fpAnalyser
bieModelFileName = !ls /data1/fpAnalyser
bieModelName = optModelName
bieModelFileName = f"{bieModelName}.quan.wqbi.bie"
bieModelPath = os.path.join('/','data1','fpAnalyser', bieModelFileName)
bieModelPath
# ls /data1/fpAnalyser
yunet-224x224-sim-cutPost-opt.quan.wqbi.bie
Out[15]:
'/data1/fpAnalyser/yunet-224x224-sim-cutPost-opt.quan.wqbi.bie'
In [16]:
!python /workspace/scripts/hardware_validate_720.py
/workspace/miniconda/lib/python3.7/site-packages/numpy/__init__.py:156: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
encrypt = true
output = /data1/simulator/
Done!
[info] hardware validating successes!
In [17]:
!ls /data1/simulator
image_99.txt		layer_input.32_fx.txt
layer_input.100_fl.txt	layer_input.40_fl.txt
layer_input.100_fx.txt	layer_input.40_fx.txt
layer_input.108_fl.txt	layer_input.48_fl.txt
layer_input.108_fx.txt	layer_input.48_fx.txt
layer_input.112_fl.txt	layer_input.4_fl.txt
layer_input.112_fx.txt	layer_input.4_fx.txt
layer_input.120_fl.txt	layer_input.52_fl.txt
layer_input.120_fx.txt	layer_input.52_fx.txt
layer_input.128_fl.txt	layer_input.60_fl.txt
layer_input.128_fx.txt	layer_input.60_fx.txt
layer_input.12_fl.txt	layer_input.68_fl.txt
layer_input.12_fx.txt	layer_input.68_fx.txt
layer_input.136_fl.txt	layer_input.72_fl.txt
layer_input.136_fx.txt	layer_input.72_fx.txt
layer_input.140_fl.txt	layer_input.80_fl.txt
layer_input.140_fx.txt	layer_input.80_fx.txt
layer_input.148_fl.txt	layer_input.88_fl.txt
layer_input.148_fx.txt	layer_input.88_fx.txt
layer_input.152_fl.txt	layer_input.92_fl.txt
layer_input.152_fx.txt	layer_input.92_fx.txt
layer_input.160_fl.txt	layer_input_input_fl.bin
layer_input.160_fx.txt	layer_input_input_fl.txt
layer_input.164_fl.txt	layer_input_input_fx.bin
layer_input.164_fx.txt	layer_input_input_fx.txt
layer_input.16_fl.txt	layer_input_input_fx_cl_rgba_520.bin
layer_input.16_fx.txt	layer_input_input_output_size.csv
layer_input.172_fl.txt	layer_output_output_fl.txt
layer_input.172_fx.txt	layer_output_output_fx.bin
layer_input.24_fl.txt	layer_output_output_fx.txt
layer_input.24_fx.txt	layer_output_output_fx_cl_rgba_520.bin
layer_input.32_fl.txt	layer_output_output_output_size.csv
In [18]:
import numpy as np
from itertools import product
import cv2
/workspace/miniconda/lib/python3.7/site-packages/numpy/__init__.py:156: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
In [19]:
outputImgDir = os.path.join(WORKSPACE, "host", "notebook", "outputImg")
if not os.path.exists(outputImgDir):
    os.makedirs(outputImgDir)
simInputImg2 = np.loadtxt('/data1/simulator/image_99.txt').reshape((224,224,3)).astype(np.uint8)
simInputImg2Path = os.path.join(outputImgDir, "sim_input_imgage_99.png")
cv2.imwrite(simInputImg2Path, simInputImg2)
IpyImage(data=simInputImg2Path)
Out[19]:
In [20]:
import common
import yunet_utils as yunet
import cv2_imgproc as cv2imgproc

prior = yunet.getPriors(modelSize)

output = np.loadtxt('/data1/simulator/layer_output_output_fl.txt').reshape((len(prior),17))



locs = output[:,0:14]
confs = output[:,14:16]
ious = output[:,16]
decoded = yunet.decodeRawFaceDataList(modelSize, confs, ious, locs, prior)
nms_dets = yunet.nms(decoded)

inputImg2 = cv2.imread(f"{inputImgDir}/image_99.jpg")
inputImg2Size = cv2imgproc.size(inputImg2)
inputAsceptRatioSize = cv2imgproc.getKARSizeByFitMaxSize(inputImg2Size, modelSize)
inW, inH = inputAsceptRatioSize
nms_dets[:, yunet.FACE_X_AXIS_DATA_INDEXES] /= inW
nms_dets[:, yunet.FACE_Y_AXIS_DATA_INDEXES] /= inH

nms_dets[:, yunet.FACE_X_AXIS_DATA_INDEXES] *= inputImg2Size[0]
nms_dets[:, yunet.FACE_Y_AXIS_DATA_INDEXES] *= inputImg2Size[1]
# print(nms_dets)
bboxes = nms_dets[:,:4].astype(int)
landmarks = nms_dets[:,4:14].reshape((-1, 5, 2)).astype(int)
outputImage2 = common.bboxVisualize(inputImg2, bboxes, landmarks, landmark_colors=yunet.FACE_LANDMARK_COLORS)
outputImage2Path = os.path.join(outputImgDir, "hardware_validate_output_image_99.png")
cv2.imwrite(outputImage2Path, outputImage2)
IpyImage(data=outputImage2Path)
/workspace/host/notebook/yunet_utils.py:59: RuntimeWarning: invalid value encountered in sqrt
  scores = np.sqrt(cls_scores * ious)
Out[20]:
In [21]:
batch_input_params = {
    "models": [
        {
            "id": 80888,
            "version": "1",
            "path": bieModelPath
        }
    ]
}
import json
with open('/data1/batch_input_params.json', 'w') as f:
    json.dump(batch_input_params, f)
!cat /data1/batch_input_params.json
{"models": [{"id": 80888, "version": "1", "path": "/data1/fpAnalyser/yunet-224x224-sim-cutPost-opt.quan.wqbi.bie"}]}
In [22]:
!python /workspace/scripts/batchCompile_720.py
/workspace/miniconda/lib/python3.7/site-packages/numpy/__init__.py:156: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init
[tool][info][batch_compile.cc:561][BatchCompile] compiling yunet-224x224-sim-cutPost-opt.quan.wqbi.bie
[tool][info][batch_compile.cc:594][LayoutBins] Re-layout binaries
[tool][info][batch_compile.cc:643][LayoutBins] output start: 0x8030f850, end: 0x8030f850
[tool][info][batch_compile.cc:561][BatchCompile] compiling yunet-224x224-sim-cutPost-opt.quan.wqbi.bie
[tool][info][batch_compile.cc:753][CombineAllBin] Combine all bin files of all models into all_models.bin
[tool][info][batch_compile.cc:829][WriteFwInfo] Generate firmware info to fw_info.txt & fw_info.bin
[tool][info][batch_compile.cc:695][VerifyOutput] 
=> 1 models
[tool][info][batch_compile.cc:703][VerifyOutput]      id: 80888
[tool][info][batch_compile.cc:704][VerifyOutput]      version: 0x1
[tool][info][batch_compile.cc:709][VerifyOutput]      addr: 0x80220000, size: 0x31000
[tool][info][batch_compile.cc:709][VerifyOutput]      addr: 0x80251000, size: 0xbe850
[tool][info][batch_compile.cc:709][VerifyOutput]      addr: 0x8030f850, size: 0xbec00
[tool][info][batch_compile.cc:709][VerifyOutput]      addr: 0x803ce450, size: 0x1844
[tool][info][batch_compile.cc:709][VerifyOutput]      addr: 0x803cfca0, size: 0x1aad00
[tool][info][batch_compile.cc:709][VerifyOutput]      addr: 0x8057a9a0, size: 0xb8
[tool][info][batch_compile.cc:712][VerifyOutput] 

[tool][info][batch_compile.cc:716][VerifyOutput]   end addr 0x8057aa58, 
[tool][info][batch_compile.cc:718][VerifyOutput] total bin size 0x1ac608
In [ ]:
!ls /data1/batch_compile
nefModelName = bieModelName
nefModelFileName = f"{nefModelName}.nef"

nefModelPath = os.path.join(WORKSPACE, "host", "notebook", "models", "yunet", nefModelFileName)
shutil.move(os.path.join("/", "data1", "batch_compile", "models_720.nef"), nefModelPath)
nefModelPath
all_models.bin
batch_compile.log
batch_compile_bconfig.json
batch_compile_config.json
compile.log
fw_info.bin
fw_info.txt
models_720.nef
yunet-224x224-sim-cutPost-opt_config.json
yunet-224x224-sim-cutPost-opt_modelid_80888_command.bin
yunet-224x224-sim-cutPost-opt_modelid_80888_ioinfo.csv
yunet-224x224-sim-cutPost-opt_modelid_80888_setup.bin
yunet-224x224-sim-cutPost-opt_modelid_80888_weight.bin
In [ ]: