【CANN训练营第三季】【昇腾AI入门课】【模型转换】基于Caffe ResNet-101网络实现图片分类(仅推理)

样例地址:https://gitee.com/ascend/samples/tree/master/cplusplus/level2_simple_inference/1_classification/resnet50_imagenet_classification

ResNet101模型地址:https://www.hiascend.com/zh/software/modelzoo/models/detail/1/8bbb7c17dfa94408ab46ea0073fec355

模型转换

  1. 下载ResNet-101模型,并移动至指定定目录:resnet50_imagenet_classification样例的caffe_model目录(表示原始模型的存放路径)
  2. 模型转换
  • 切换至指定目录
cd ~/samples/cplusplus/level2_simple_inference/1_classification/resnet50_imagenet_classification

在这里插入图片描述

  • 执行模型转换
atc --model=caffe_model/resnet101_tf.pb --framework=3 --output=model/resnet101_tf --output_type=FP32 --soc_version=Ascend310 --input_shape="input:1,224,224,3" --log=info

在这里插入图片描述

生成测试数据

  1. 进入resnet50_imagenet_classification样例的script目录,修改transferPic.py脚本中的如下内容,将float16改为float32

由:

img = img.astype("float16")

转换为:

img = img.astype("float32")

在这里插入图片描述

  1. 切换到“样例目录/data“目录下,执行transferPic.py脚本,将*.jpg转换为*.bin,同时将图片从1024×683的分辨率缩放为224×224。在“样例目录/data“目录下生成2个*.bin文件。
python3 ../script/transferPic.py

在这里插入图片描述

调用接口

调用AscendCL接口(例如aclmdlLoadFromFileWithMem接口)加载ResNet-101模型,在src/sample_process.cpp文件中定制代码。
在这里插入图片描述

编译代码

1.进入样例目录

cd ~/samples/cplusplus/level2_simple_inference/1_classification/resnet50_imagenet_classification

2.设置环境变量

export DDK_PATH=$HOME/Ascend/ascend-toolkit/latest
export NPU_HOST_LIB=$DDK_PATH/runtime/lib64/stub

3.创建目录用于存放编译文件

mkdir -p build/intermediates/host

4.切换到“build/intermediates/host“目录

cd build/intermediates/host

5.生成编译文件

cd build/intermediates/host
cmake ../../../src -DCMAKE_CXX_COMPILER=g++ -DCMAKE_SKIP_RPATH=TRUE

6.生成可执行文件main

make

在这里插入图片描述在这里插入图片描述

运行应用

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值