零基础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.csdn.net/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.csdn.net/JulyLi2019/article/details/135641886

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值