打开微信,使用扫一扫进入页面后,点击右上角菜单,
点击“发送给朋友”或“分享到朋友圈”完成分享
基本流程如下:
1、首先在ubuntu16.04上编译Caffe-SSD的CPU版本
2、在CPU上,使用自带的Python脚本,运行SSD网络模型
3、修改官方gitlab自带的Python执行脚本,在软件栈8.0MLU100执行在线逐层在线融合
4、在软件栈8.0中,移植在线和离线SSD-VGG16检测程序
5、在软件栈8.0中,进行SSD-vgg16网络的INT8量化
6、在软件栈8.0中,测试量化后的INT8在线和离线模型
运行环境:操作系统Ubuntu 16.04 ,MLU100 8.0软件栈
一、下载安装caffe-ssd版本
git clone https://github.com/weiliu89/caffe.git cd caffe git checkout ssd
(1).修改Makefile.config
cp Makefile.config.example Makefile.config vim Makefile.config #用vim打开配置文件
1)更改只使用CPU模式
CPU_ONLY:=1
2)更改python 相关的路径
3)加入hdf5的目录
(2)修改Makefile文件
vim Makefile
1)找到LIBRARIES += glog....hdf5,修改如下
2)找到NVCCFLAGS += -ccbin=$(cxx).... $(COMMON_FLAGS)修改如下
3)找到LIBRARIES += boost_thread stdc++,修改如下
mkdir build cd build cmake .. # 可视化配置 make all -j8 # “-j8”是使用CPU的多核进行编译,可以极大地增加编译的速度 make install
编译pycaffe
make pycaffe -j8
编译:
make test -j8 make runtest -j8
检验python中import caffe是否报错:
python import caffe
(1)如果报如下错误
Traceback (most recent call last): File "", line 1, in ImportError: No module named caffe
解决办法:
把环境变量路径放到 ~/.bashrc文件中,打开文件(注意,这时还是当时caffe编译的路径,不用切换)
sudo gedit ~/.bashrc
在文件下方写入,保存退出。
export PYTHONPATH=/home/*XXXX* /caffe/python:$PYTHONPATH #其中XXXX为你的用户名
在终端写入下面语句,使环境变量生效。
source ~/.bashrc
(2)继续输入import caffe如果报以下错误
Traceback (most recent call last): File "", line 1, in File "/home/matt/caffe-ssd/python/caffe/__init__.py", line 1, in from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver File "/home/matt/caffe-ssd/python/caffe/pycaffe.py", line 15, in import caffe.io File "/home/matt/caffe-ssd/python/caffe/io.py", line 2, in import skimage.io ImportError: No module named skimage.io
在终端输入:
apt-get install python-skimage apt-get install python-protobuf
将该模型放到$CAFFE_ROOT/models/VGGNet/路径下
执行以下命令可以查看最近的 snapshot.的评分:
python examples/ssd/score_ssd_pascal.py
修改./examples/ssd/ssd_detect.py文件,将GPU模式改为CPU模式
class CaffeDetection: def __init__(self, gpu_id, model_def, model_weights, image_resize, labelmap_file): #caffe.set_device(gpu_id) #caffe.set_mode_gpu() caffe.set_mode_cpu()
python ./examples/ssd/ssd_detect.py --image_file examples/images/fish-bike.jpg
python ./examples/ssd/ssd_detect.py --image_file examples/images/cat.jpg
(1).新建文件夹caffe_ssd
mkdir caffe_ssd
(2).将 ssd_detect.py,VGG_VOC0712_SSD_300x300_iter_120000.caffemodel,deploy.prototxt 、labelmap_voc.prototxt 、test_name_size.txt拷贝到 caffe_ssd文件夹下
(3).修改ssd_detect.py中的网络模型路径、图片路径、权重路径、标签路径,改为自己的路径。
parser.add_argument('--labelmap_file', default='labelmap_voc.prototxt') parser.add_argument('--model_def', default='VGGNet/VOC0712/SSD_300x300/deploy.prototxt') parser.add_argument('--image_resize', default=300, type=int) parser.add_argument('--model_weights', default='VGGNet/VOC0712/SSD_300x300/' 'VGG_VOC0712_SSD_300x300_iter_120000.caffemodel')
(4).注释caffe路径
(5).运行测试(采用CPU运行)
python ssd_detect.py --image_file images/cat.jpg
python ssd_detect.py --image_file images/fish-bike.jpg
修改ssd_detect.py 代码进行在线逐层,在线融合运行。
修改代码输出
# Forward pass. detections = self.net.forward()['detection_out'] # Parse the outputs. det_label = detections[0,0,:,1] det_conf = detections[0,0,:,2] det_xmin = detections[0,0,:,3] det_ymin = detections[0,0,:,4] det_xmax = detections[0,0,:,5] det_ymax = detections[0,0,:,6]
为:
det_label = [] det_conf = [] det_xmin = [] det_ymin = [] det_xmax = [] det_ymax = [] detections1=[[[]]] for item in detections[0]: det_label.append(item[0][5]) det_conf.append(item[0][4]) det_xmin.append(item[0][0]) det_ymin.append(item[0][1]) det_xmax.append(item[0][2]) det_ymax.append(item[0][3]) det_label = np.array(det_label) det_conf = np.array(det_conf) det_xmin = np.array(det_xmin) det_ymin = np.array(det_ymin) det_xmax = np.array(det_xmax) det_ymax = np.array(det_ymax)
1)修改caffe模式
caffe.set_mode_mlu()
2)测试一
python ssd_detect.py --image_file images/cat.jpg
3)测试二
python ssd_detect.py --image_file images/fish-bike.jpg
1)修改caffe模式
caffe.set_mode_mfus()
2)测试一
python ssd_detect.py --image_file images/cat.jpg
3)测试二
python ssd_detect.py --image_file images/fish-bike.jpg
如下图,修改deploy.prototxt
../build/examples/ssd/ssd_detect -model VGGNet/VOC0712/SSD_300x300/deploy.prototxt -weights VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel -images file_list -outputdir ./ -labelmapfile labelmap_voc.prototxt -confidencethreshold 0.5 -mmode MLU
../build/examples/ssd/ssd_detect -model VGGNet/VOC0712/SSD_300x300/deploy.prototxt -weights VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel -images file_list -outputdir ./ -labelmapfile labelmap_voc.prototxt -confidencethreshold 0.5 -mmode MFUS
1)生成离线模型
../build/tools/caffe genoff -model VGGNet/VOC0712/SSD_300x300/deploy.prototxt -weights ./VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel -mcore MLU100 -mname VGG_VOC0712_SSD_300x300_iter_120000 -Bangop 0
VGG_VOC0712_SSD_300x300_iter_120000.cambricon
VGG_VOC0712_SSD_300x300_iter_120000.cambricon_twins
2)运行离线模型
../build/examples/ssd/ssd_offline_multicore_pipe --offlinemodel ./VGG_VOC0712_SSD_300x300_iter_120000.cambricon --images ./file_list --labelmapfile ./labelmap_voc.prototxt --confidencethreshold 0.5 --dataparallel 1 --threads 1 --dump 1 -Bangop 0
1)生成离线模型
../build/tools/caffe genoff -model VGGNet/VOC0712/SSD_300x300/deploy.prototxt -weights ./VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel -mcore MLU100 -mname VGG_VOC0712_SSD_300x300_iter_120000b1 -Bangop 1
2)运行离线模型
../build/examples/ssd/ssd_offline_multicore_pipe --offlinemodel ./VGG_VOC0712_SSD_300x300_iter_120000b1.cambricon --images ./file_list --labelmapfile ./labelmap_voc.prototxt --confidencethreshold 0.5 --dataparallel 1 --threads 1 --dump 1 -Bangop 1
内容如下:
修改红框中的路径为自己的路径
配置文件内容:
[model]
;blow two are lists, depending on work
original_models_path = VGGNet/VOC0712/SSD_300x300/deploy.prototxt
save_model_path = ./deploy_int8zkg.prototxt
;input_nodes = input_node_1
;output_nodes = output_node_1, output_node_2
[data]
;only one should be set for below two
;images_db_path = /opt/shared/beta/caffe_data_models/data/ilsvrc12_val_lmdb/
images_folder_path = file_list
used_images_num = 2
[weights]
original_weights_path = VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel
[preprocess]
mean = 104,117,123
std = 1
scale = 300, 300
crop = 300, 300
[config]
int8_op_list = Conv, FC, LRN
use_firstconv = 1
../build/tools/generate_int8_pt --ini_file convert_int8zkg.ini
../build/examples/ssd/ssd_detect -model deploy_int8zkg.prototxt -weights VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel -images file_list -outputdir ./ -labelmapfile labelmap_voc.prototxt -confidencethreshold 0.5 -mmode MLU
../build/examples/ssd/ssd_detect -model deploy_int8zkg.prototxt -weights VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel -images file_list -outputdir ./ -labelmapfile labelmap_voc.prototxt -confidencethreshold 0.5 -mmode MFUS
1.Bangop 为0
1)生成离线模型
../build/tools/caffe genoff -model ./deploy_int8zkg.prototxt -weights VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel -mcore MLU100 -mname VGG_VOC0712_SSD_300x300_iter_120000_int8zkg -Bangop 0
2) 运行离线模型
../build/examples/ssd/ssd_offline_multicore_pipe --offlinemodel ./VGG_VOC0712_SSD_300x300_iter_120000_int8zkg.cambricon --images ./file_list --labelmapfile ./labelmap_voc.prototxt --confidencethreshold 0.5 --dataparallel 1 --threads 1 --dump 1 -Bangop 0
1)生成离线模型
../build/tools/caffe genoff -model ./deploy_int8zkg.prototxt -weights VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel -mcore MLU100 -mname VGG_VOC0712_SSD_300x300_iter_120000_int8zkgb1 -Bangop 1
2)运行离线模型
../build/examples/ssd/ssd_offline_multicore_pipe --offlinemodel ./VGG_VOC0712_SSD_300x300_iter_120000_int8zkgb1.cambricon --images ./file_list --labelmapfile ./labelmap_voc.prototxt --confidencethreshold 0.5 --dataparallel 1 --threads 1 --dump 1 -Bangop 1
1.https://blog.csdn.net/matt45m/article/details/89601015
2.https://github.com/weiliu89/caffe/tree/ssd#models
3.软件栈8.0操作手册
热门帖子
精华帖子