×

签到

分享到微信

打开微信,使用扫一扫进入页面后,点击右上角菜单,

点击“发送给朋友”或“分享到朋友圈”完成分享

【经验分享】通过Pytorch生成darknet yolov3的MLU220离线模型 HelloAI2020-12-21 11:18:41 回复 12 查看
【经验分享】通过Pytorch生成darknet yolov3的MLU220离线模型
分享到:

一、搭建寒武纪MLU PyTorch开发环境

1、寒武纪提供了二次开发的PyTorch的开发docker image,可以快速搭建pytorch开发环境。从寒武纪FTP下载PyTroch开发镜像,下载地址如下:

ftp://download.cambricon.com:8821/product/MLU270/v1.5.0/docker-images/mlu270_v1.5.0_ubuntu16.04.pytorch_v1.0.tar.gz

2、启动docker开发环境:

假设MLU270驱动程序已安装(MLU270驱动安装方法另见《寒武纪Linux驱动安装手册-v4.4.4》),docker-ce已安装

docker load < mlu270_v1.5.0_ubuntu16.04.pytorch_v1.0.tar.gz

docker run -it --privileged --name pytorch-test-1.5.0 \

-v $PWD:/home/share \

cambricon/scm/dockerfile/mlu270_v1.5.0_ubuntu16.04.pytorch:v1.0 \

/bin/bash

--privileged选项可自动将系统的设备节点映射到docker容器中,也包括MLU270的设备节点

-v $PWD:/home/share可将当前工作目录映射到docker容器中的/home/share/


二、准备yolov3 pytorch模型

1、下载darknet预训练模型

mkdir /home/share/official_weight && cd /home/share/official_weight

wget https://pjreddie.com/media/files/yolov3.weights

2、激活pytorch虚拟环境

source /opt/cambricon/pytorch/src/catch/venv/pytorch/bin/activate

3、darknet模型转pytorch模型

寒武纪二次开发过的pytorch在docker容器内的/opt/cambricon/pytorch/src/catch,

   catch提供了darknet模型转pytorch模型的工具convert_weight,可将darknet的weigths转换成pth

cd /opt/cambricon/pytorch/src/catch/examples/tools/convert_weight

python convert_weight.py -original_path /home/share/official_weight/ \

-original_name yolov3.weights \

-model_name yolov3 \

-save_name yolov3.pth \

-cfg_file cfg/yolov3.cfg


export TORCH_HOME=/home/share/models/pytorch_models 

mkdir -p $TORCH_HOME/origin/checkpoints/ && mv yolov3.pth $TORCH_HOME/origin/checkpoints/

三、模型INT定点量化

1、准备数据集

export COCO_PATH_PYTORCH=/home/share/datasets

mkdir $COCO_PATH_PYTORCH

将COCO数据集解压到/home/share/datasets,下载地址如下:

ftp://download.cambricon.com:8821/product/MLU270/datasets/MLU270_datasets_COCO.tar.gz

2、执行模型量化

cd /opt/cambricon/pytorch/src/catch/examples/online/yolov3

mkdir -p $TORCH_HOME/int8/checkpoints/

python test.py --mlu false --jit false \

    --quantization true --quantized_mode 1 \

    --ann_dir $COCO_PATH_PYTORCH/COCO \

    --coco_path $COCO_PATH_PYTORCH/COCO \

    --image_number 100 \

    --quantized_model_path $TORCH_HOME/int8/checkpoints/

四、验证推理精度

python test.py --mlu true --jit true \

   --quantization false --quantized_mode 1 \

   --ann_dir $COCO_PATH_PYTORCH/COCO \

   --coco_path $COCO_PATH_PYTORCH/COCO \

   --batch_size 1 --core_number 4 \

   --image_number 1600

   

五、生成MLU220离线模型

1、先用同样的core_number、batch_size参数生成MLU270的离线模型,并在MLU270上验证结果正确性

cd /home/share/

python /opt/cambricon/pytorch/src/catch/examples/offline/genoff/genoff.py \

   -model yolov3 -mname yolov3_mlu220_batchsize1_corenumber4 \

   -mcore MLU220 -core_number 4 \

   -batch_size 1 -in_height 416 -in_width 416 \

   -modelzoo $TORCH_HOME/int8/

在当前目录下会生成2个文件:

离线模型文件:yolov3_mlu220_batchsize1_corenumber4.cambricon

离线模型摘要文件:yolov3_mlu220_batchsize1_corenumber4.cambricon_twins

打开yolov3_mlu220_batchsize1_corenumber4.cambricon_twins,检查输入的Layout和Shape是否和原模型一致。

版权所有 © 2024 寒武纪 Cambricon.com 备案/许可证号:京ICP备17003415号-1
关闭