零基础onnx-->caffe-->wk

总流程:安装VMware Workstation Pro—>安装ubuntu20.4—>安装caffe环境—下载onnx2caffe项目代码—>将onnx转为caffe—>caffe转wk
首先,将训练好的模型保存为onnx文件,Onnx模型的保存使用该命令:

torch.onnx.export(model, example_input, onnx_filename, verbose=True, opset_version=9,input_names=['input_name'],  # 指定输入名称
                  output_names=['output_name'] ) # 指定输出名称

另外,需要指定输入输出的名字,如果不指定的话,在caffe转wk模型是时会报错。opset的版本为9。有了onnx模型后就可以进行转caffe工作了。

下面详细介绍这些步骤:
VMware的安装,按网上的教程任意安装一个就好;
ubuntu20.4的安装按这个链接安装就好:
https://blog.youkuaiyun.com/m0_63324772/article/details/136380844

安装caffe环境,按这个链接,切记要一步步操作,指令一条条的输入:
https://gitee.com/wgm2022/wu_guiming.gitee.io/blob/master/01%20%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%E7%9B%B8%E5%85%B3/%E5%A6%82%E4%BD%95%E5%9C%A8Ubuntu20.02%20%E4%B8%8A%E9%9D%A2%E6%90%AD%E5%BB%BAcaffe%E7%8E%AF%E5%A2%83.md

安装好caffe环境后,再安装以下包:(顺序最好别乱)
pip install onnx
pip install protobuf==3.20.3
pip install onnxruntime
下载onnx2caffe项目代码:(可放在任意目录下)
https://github.com/xxradon/ONNXToCaffe

自己的onnx模型放在onnx2caffe/model目录,在onnx2caffe目录下进入终端,然后运行:
python3 convertCaffe.py ./model/MobileNetV2.onnx ./model/MobileNetV2.prototxt ./model/MobileNetV2.caffemodel
如果出现错误信息:
在这里插入图片描述
将modelComparator.py对应处改为:

    # generate input tensor
    # in NCHW format
    input_tensor = gen_input(models, in_node[0])
    if len(input_tensor.shape) >= 3:
    	dump_input_file = os.path.join(dump_path, "input_{}x{}.txt".format(input_tensor.shape[2], input_tensor.shape[3]))
    	np.savetxt(dump_input_file, input_tensor.flatten())
    	print("input tensor shape of {}: {}".format(in_node[0], input_tensor.shape))
    	print("dump input to {}".format(dump_input_file))
    else:
    	dump_input_file = os.path.join(dump_path, "input.txt")
    	np.savetxt(dump_input_file, input_tensor.flatten())
    	print("input tensor shape of {} is less than 3 dimensions: {}".format(in_node[0], input_tensor.shape))
    	print("dump input to {}".format(dump_input_file))
    	
    #dump_input_file = os.path.join(dump_path, "input_{}x{}.txt".format(input_tensor.shape[2], input_tensor.shape[3]))
    #np.savetxt(dump_input_file, input_tensor.flatten())
    #print("input tensor shape of {}: {}".format(in_node[0], input_tensor.shape))
    #print("dump input to {}".format(dump_input_file))

    # feed input tensors for each model and get result
    net_results = run_models(models, in_node[0], out_node, input_tensor)
    for i, node in enumerate(out_node):

按以上操作完成后,在对应的文件夹中会有*****.caffemodel和*****.prototxt文件,接下来就可以准备转wk模型了。另外转好caffe模型后可以用这个网址查看模型框架。(https://netron.app/)。

Caffe转wk
转wk 需要进行一定的环境配置以及软件安装:(以下是在window下进行)
下载安装RuyiStudio和mingw64
链接:https://pan.baidu.com/s/1lhSo_MNxegHK3dPt3byl-A?pwd=roln
提取码:roln

将mingw64放在C盘,RuyiStudio可放在任意文件夹。然后开始设置环境变量。
计算机上点击右键->属性->高级系统设置->环境变量,在系统变量中
添加以下变量(若已有同名变量则添加变量值用分号隔开):变量名Path,变量值mingw64的bin路径和msys的bin路径
C:\mingw64\bin; C:\mingw64\msys\bin
在系统环境变量中增加变量RUYI_PYTHON_PATH,
如果当前bat脚本放置在D:\ruyi_env_setup目录下,则变量值为D:\ruyi_env_setup\python35; D:\ruyi_env_setup
\python35\Scripts; D:\ruyi_env_setup\pyhon35\Library\bin
然后:添加%RUYI_PYTHON_PATH%到系统环境变量path的开头

然后:添加用户环境变量PYTHONPATH,
D:\RuyiStudio\RuyiStudio\ruyi_env_setup\python35\Lib\site-packages\caffe\python。
然后就可以点击,运行程序了。
在这里插入图片描述
安装好以上软件后,就可以将caffe转为wk了,具体操作参考以下网址的3.4 caffe->wk部分:
https://blog.youkuaiyun.com/JulyLi2019/article/details/135641886

### 海思 Hi3519 芯片部署 YOLOv8 模型的解决方案 #### 一、背景概述 海思 Hi3519 是一款广泛应用于视频监控领域的 AI 芯片,具备强大的图像处理能力和一定的神经网络加速性能。YOLOv8 是一种先进的目标检测模型,在精度和速度之间取得了良好的平衡[^2]。为了在该芯片上成功部署 YOLOv8 模型,需经历一系列复杂的换过程。 --- #### 二、整体部署流程 ##### 1. **模型训练与导出** - 使用 Ultralytics 提供的官方工具完成 YOLOv8 的训练工作。 - 训练完成后,通过 `torch.onnx.export` 将 PyTorch 格式的模型化为 ONNX 格式[^1]。 ```python import torch model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt') # 加载自定义权重 dummy_input = torch.randn(1, 3, 640, 640) # 输入张量形状 torch.onnx.export(model, dummy_input, "yolov8.onnx", opset_version=11) ``` 此部分操作确保了模型能够被后续工具兼容并进一步优化。 --- ##### 2. **ONNXCaffe 模型的换** 由于海思 SDK 更倾向于支持 Caffe 模型,因此需要借助第三方工具(如 `onnx-simplifier` 和 `mmdeploy`)将 ONNX 文件换为 Caffe 格式。 ```bash pip install onnxsim mmdeploy onnxsim yolov8.onnx simplified_yolov8.onnx # 简化 ONNX 结构 mmdeploy convert caffe --work-dir ./output --cfg work_dir/deploy_cfg.py simplified_yolov8.onnx output.prototxt output.caffemodel ``` 注意:在此过程中可能遇到层不支持的情况,建议提前验证所使用的算子是否已被完全覆盖。 --- ##### 3. **Caffe 模型到 WK 模型的换** 利用海思官方提供的 HiSVP 工具链,可将标准 Caffe 模型编译为目标硬件专用的工作流 (Workflow) 模型文件 (.wk)。 具体命令如下: ```bash hiconvertcaffe \ --prototxt=output.prototxt \ --caffemodel=output.caffemodel \ --om=model.wk \ --framework-version="ssd_v1" \ --input-shape="data:1,3,640,640" ``` 上述脚本指定了输入数据尺寸以及框架版本号等必要参数。 --- ##### 4. **模型加载与推理测试** 最后一步是在 Hi3519 平台上实际运行生成好的 `.wk` 文件。通常情况下会编写一段简单的应用程序接口(API),用于读取图片帧并通过调用 NNE 接口执行前向传播计算得出预测结果[^3]。 示例代码片段展示如何初始化环境并与预设函数交互: ```c #include <stdio.h> #include <stdlib.h> int main() { // 初始化资源管理器句柄 HI_S32 s32Ret; MV_HANDLE hModelHandle; s32Ret = MV_InitEnv(); if (s32Ret != HI_SUCCESS) { printf("Failed to init env.\n"); return -1; } // 打开指定路径下的 wk 模型 s32Ret = MV_LoadModel("./model.wk", &hModelHandle); if (s32Ret != HI_SUCCESS) { printf("Failed to load model.\n"); return -1; } // 准备输入缓冲区... float* pInputData = ... ; // 开始推断逻辑 MV_IN_DATA stInData; memset(&stInData, 0, sizeof(MV_IN_DATA)); stInData.pfDataBuf = pInputData; MV_OUT_DATA stOutData; s32Ret = MV_Forward(hModelHandle, &stInData, &stOutData); // 后续解析输出... // 清理释放内存空间 MV_UnloadModel(hModelHandle); MV_DeinitEnv(); return 0; } ``` 以上步骤构成了完整的端到端迁移学习实践指南。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值