本文是通过稍微改造官方 Paddle-Lite-Demo 库里的例子来运行的
1. 下载预测库
python -m pip install paddlelite
2. 下载 Paddle-Lite-Demo
git clone https://github.com/PaddlePaddle/Paddle-Lite-Demo.git
cd Paddle-Lite-Demo
mkdir _data # 创建 _data 存放模型文件和转换后的模型文件
cd _data
3. 模型下载
wget http://paddle-inference-dist.bj.bcebos.com/mobilenet_v1.tar.gz
tar zxf mobilenet_v1.tar.gz
4. 模型转换
paddle_lite_opt --model_dir=./mobilenet_v1 --optimize_out_type=naive_buffer --optimize_out=./mobilenet_v1_opt --valid_targets=x86
paddle_lite_opt
是安装上面安装paddlelite
后就有的工具
注意--valid_targets=x86
,选择x86
平台
此时会生成转换后的文件 mobilenet_v1_opt.nb
5. 改变demo里的参数来运行图像分类任务
进入相关目录,运行如下命令
cd ..
cd image_classification/armlinux/shell/python
python ./image_classification.py --model_dir ../../../../_data/mobilenet_v1_opt.nb \
--input_shape=1,3,224,224 --image_path ../../../assets/images/tabby_cat.jpg \
--label_path ../../../assets/labels/labels.txt \
--topk=3 --repeats=100 --warmup=10
如果有如下报错,则把image_classification.py
文件的79行config.set_threads(threads)
注释掉,改为# config.set_threads(threads)
Traceback (most recent call last):
File "./image_classification.py", line 197, in <module>
RunModel(args)
File "./image_classification.py", line 156, in RunModel
predictor = Init(model_dir, thread_num)
File "./image_classification.py", line 79, in Init
config.set_threads(threads)
AttributeError: 'paddlelite.lite.MobileConfig' object has no attribute 'set_threads'
把--model_dir
设置为上一步生成的nb文件路径../../../../_data/mobilenet_v1_opt.nb
会得到分类结果如下:
================== Speed Report ===================
model: ../../../../_data/mobilenet_v1_opt.nb, run avg_time: 1.2612977027893067e-05 ms, min_time: 1.2119770050048829e-05 ms
================== Precision Report ===================
i: 0, index: 285, name: cat
, score: 0.4729938805103302
i: 1, index: 281, name: cat
, score: 0.4363744556903839
i: 2, index: 282, name: cat
, score: 0.0766439363360405
================== Report End ===================
网友评论