python开发者

Sophon也封装了python API以便python开发者快速上手

  • 安装Sophon python3 library

** 安装sophon python库 **
SplitModel# pip3 install --user python3/x86/sophon-1.1.3-py3-none-any.whl
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Processing ./python3/x86/sophon-1.1.3-py3-none-any.whl
Installing collected packages: sophon
Successfully installed sophon-1.1.3
  • 下载yolov3模型

** Sophon将下载转换模型封装成Sophon utils python库 **
# cd SplitModel/bin/x86
# ./get_bmodel -h
usage: get_bmodel [-h] [--save_path SAVE_PATH] arg
Download test data, or download and convert models.
positional arguments:
  arg                   Options: model_name, 'model_list', 'test_data', 'all'
optional arguments:
  -h, --help            show this help message and exit
  --save_path SAVE_PATH
                        Save sophon test model and test data.
** 下载Yolov3测试数据和模型,编译模型测试精度 **
./get_bmodel test_data --save_path ${run_yolov3_caffe}/tmp
./get_bmodel yolov3 --save_path ${run_yolov3_caffe}/tmp
  • 使用Sophon complier编译模型

** get_bmodel 中调用bmnetc 转换caffe-yolov3模型 **
** 开发者也可以使用Sophon提供的python bmnetc API编译模型 **
bmnetc.compile(
model = "/path/to/prototxt", ## Necessary
weight = "/path/to/caffemodel", ## Necessary
outdir = "xxx", ## Necessary
target = "BM1682", ## Necessary
shapes = [[x,x,x,x], [x,x,x]], ## optional, default use shape in prototxt
net_name = "name", ## optional, default use the network name in prototxt
opt = 2, ## optional, if not set, default equal to 2
dyn = False, ## optional, if not set, default equal to False
cmp = True, ## optional, if not set, default equal to True
enable_profile = False ## optional, default equal to False
)
** 示例 **
# cat convert_bmodel.py
import bmnetc

bmnetc.compile(
  model = "./tmp/models/yolov3/yolov3.prototxt",
  weight = "./tmp/models/yolov3/yolov3.caffemodel",
  outdir = "./out/yolov3",
  target = "BM1682",
  shapes = [[1, 3, 416, 416]],
  net_name = "yolov3",
  opt = 2,
  dyn = False,
  cmp = True, 
  enable_profile = False
  ) 
# python3 convert_bmodel.py
 ............................................................
 src/subnets/bmcompiler_subnet.cpp clear 43

============================================================
*** Store bmodel of BMCompiler...
===========================================================
# ls out/yolov3
# compilation.bmodel  input_ref_data.dat  output_ref_data.dat
  • 加载Sophon bmodel

...
import sophon.sail as sail
...
net = sail.Engine(bmodel, len(bmodel), "0", sail.IOMode.SYSIO))
  • 数据预处理

...
import cv2
# open image file 
img = cv2.imread(input_path)
# resize image 
resized_image = cv2.resize(image, (new_w, new_h))
# canvas
canvas = np.full((net_w, net_h, 3), 127.5)
canvas[(net_h - new_h) // 2:(net_h - new_h) // 2 + new_h,\ 
    (net_w - new_w) // 2:(net_w - new_w) // 2 + new_w, :]\ 
    = resized_image 
# (hwc to chw, scale: 1/255)...
canvas[:, :, ::-1].transpose([2, 0, 1]) / 255.0
  • 执行Sophon Model推理

# do inference
output = net.process (graph_name, input_tensors)
  • 数据后处理

# postProcess 
# 根据指标进行预测, 参考 
# examples/SplitModel/samples/python/run_yolov3_caffe/run_yolov3.py 
bboxes, classes, probs = yolov3_postprocess(output, img, detected_size,\ 
        num_classes, threshold,\ 
        all_anchors, nms_threshold);
  • 实例演示

$ cd bmnnsdk2-bm1682_v1.1.4
$ ./docker_run_bmnnsdk.sh 
# cd scripts 
# ./intall_lib.sh nntc
# source envsetup_cmodel.sh
# cd /workspace/examples/SplitModel ** 安装sophon python库 ** 
# pip3 install --user python3/x86/sophon-1.1.3-py3-none-any.whl 
** 生成bmodel ** 
# cd samples/python/run_yolov3_caffe/ 
# ./test_yolov3_caffe.sh
** 将yolov3 python脚本拷贝到soc单板进行测试 ** 
**"YOUR_SOC_IP"字符串替换为实际的soc单板ip地址**
# scp -r /workspace/examples/SplitModel/samples/\
        python/run_yolov3_caffe linaro@YOUR_SOC_IP:~/
# exit
$ ssh linaro@YOUR_SOC_IP
** 安装 SE3 driver **
$ sudo /system/data/chdriver.sh
$ cd run_yolov3_caffe
$ python3.5 run_yolov3.py \ 
    --ir_path ./tmp/models/yolov3_ir/compilation.bmodel \
    --input_path ./tmp/data/det_coco.jpg
** 执行结果 ** 
bmcpu init: skip cpu_user_defined
open usercpu.so, init user_cpu_init
################################################################################
id:0 bbox:[96, 178, 251, 418] cls_idx: 16 prob:0.9986375570297241
id:1 bbox:[124, 88, 445, 347] cls_idx: 1 prob:0.9923182129859924
id:2 bbox:[373, 68, 538, 133] cls_idx: 7 prob:0.8461064696311951
################################################################################

Last updated

Was this helpful?