Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

转换upsample层时遇到了问题 #2

Open
RichardSong-dev opened this issue Nov 17, 2020 · 12 comments
Open

转换upsample层时遇到了问题 #2

RichardSong-dev opened this issue Nov 17, 2020 · 12 comments

Comments

@RichardSong-dev
Copy link

RichardSong-dev commented Nov 17, 2020

我尝试将pytorch版yolov3模型通过onnx转换为caffe模型。在转换upsample层时程序报错:
Traceback (most recent call last):
File "convertCaffe.py", line 122, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 67, in convertToCaffe
layer = converter_fn(node,graph,err)
File "/root/ONNXToCaffe-master/onnx2caffe/_operators.py", line 362, in _convert_upsample
factor = int(node.attrs["height_scale"])
KeyError: 'height_scale'

upsample层参数如图所示:
image
于是我将_operators.py中第362行与370行注释掉来尝试解决,结果又出现如下错误:
Traceback (most recent call last):
File "convertCaffe.py", line 122, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 79, in convertToCaffe
layers[id] = layer._to_proto()
File "/root/ONNXToCaffe-master/MyCaffe.py", line 100, in _to_proto
assign_proto(layer, k, v)
File "/root/ONNXToCaffe-master/MyCaffe.py", line 29, in assign_proto
is_repeated_field = hasattr(getattr(proto, name), 'extend')
AttributeError: upsample_param

python3.7.9 onnx1.8.0
求教该怎么解决!谢谢各位大佬!

@xxradon
Copy link
Owner

xxradon commented Nov 18, 2020

onnx 的opset版本是9吗?或者你把模型给我,我给你看看。

@RichardSong-dev
Copy link
Author

RichardSong-dev commented Nov 18, 2020

onnx 的opset版本是9吗?或者你把模型给我,我给你看看。

opset应该是9,之前pytorch导出时没指定version应该是默认9。我另一个模型yolo在转换reshape节点时还出现了其他错误:
Traceback (most recent call last):
File "convertCaffe.py", line 122, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 67, in convertToCaffe
layer = converter_fn(node,graph,err)
File "/root/ONNXToCaffe-master/onnx2caffe/_operators.py", line 257, in _convert_Reshape
return err.unsupported_op_configuration(node, "Reshape dimention number shall be 2 or 4")
File "/root/ONNXToCaffe-master/onnx2caffe/_error_utils.py", line 45, in unsupported_op_configuration
"Error while converting op of type: {}. Error message: {}\n".format(node.op_type, err_message, )
TypeError: Error while converting op of type: Reshape. Error message: Reshape dimention number shall be 2 or 4
这个问题在MTLab/onnx2caffe#39 也被提到了。

模型要怎么发给您呢?我上传个百度云可以么?谢谢大佬!!
链接:https://pan.baidu.com/s/13mQndV5CZ4AZrbhqbmUeLQ
提取码:hgjz
yolov3-tf-raw3.onnx upsample报错
yolov3-caffe-11.onnx reshape报错

@xxradon
Copy link
Owner

xxradon commented Nov 18, 2020

@RichardSong-dev upsample的bug修复了,源码已更新,你试试看,然后你的模型里面的reshape操作维度大于4维了,这个caffe不支持,所以你导出onnx时,需要把这部分去掉才行。
还有就是你的caffe版本需要添加https://github.com/jnulzl/caffe_plus 这个项目中Upsample实现才能支持nearest模式的操作

@RichardSong-dev
Copy link
Author

@RichardSong-dev upsample的bug修复了,源码已更新,你试试看,然后你的模型里面的reshape操作维度大于4维了,这个caffe不支持,所以你导出onnx时,需要把这部分去掉才行。
还有就是你的caffe版本需要添加https://github.com/jnulzl/caffe_plus 这个项目中Upsample实现才能支持nearest模式的操作

谢谢大佬,我去研究研究!

@RichardSong-dev
Copy link
Author

@xxradon 大佬,昨天按照您的方法试了一下,已经可以转换成功了!但当我在pytorch中把upsample改为bilinear之后又出现了问题:
Traceback (most recent call last):
File "convertCaffe.py", line 122, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 67, in convertToCaffe
layer = converter_fn(node,graph,err)
File "/root/ONNXToCaffe/onnx2caffe/_operators.py", line 402, in _convert_upsample
factor = int(node.attrs["height_scale"])
KeyError: 'height_scale'
upsample节点经过onnx转换,变成了linear模式
image

因为我还需要将caffe模型转化为另一种可部署在服务器上的格式,而转换工具只支持
message UpsampleParameter{ optional int32 scale = 1 [default = 1]; }
这种caffe的upsample,所以还麻烦大佬帮忙看一下。谢谢大佬!

模型链接
链接:https://pan.baidu.com/s/1QuLHEjwSYCmcmfYDQRQUHg
提取码:gq81

@xxradon
Copy link
Owner

xxradon commented Nov 20, 2020

@RichardSong-dev 像你这种定制的caffe解析,需要自己手动修改源码,自己调试。

@RichardSong-dev
Copy link
Author

RichardSong-dev commented Nov 20, 2020

@xxradon 大佬,对于昨天的问题,我按照您的方式改了一下_operators.py代码:

elif str(mode,encoding="gbk") == "linear":
    scales = node.input_tensors.get(node.inputs[1])
    scale = scales[2]
    layer = myf("Upsample", node_name, [input_name], [output_name],
                       upsample_param=dict(
                       scale = int(scale)
                ))

然后,使用https://github.com/Royzon/caffe_extension_layer 这个项目中的upsample layer重新编译了一下caffe。模型可以转换成功,但显示model output different。cos sim 大概在0.98-0.99左右。想请问大佬知道pytorch->onnx时bilinear具体使用的什么操作吗?谢谢大佬的回复!

@xxradon
Copy link
Owner

xxradon commented Nov 20, 2020

@RichardSong-dev 你可以看看这个https://www.zhihu.com/question/63890195?sort=created 还有onnx关于upsample的功能描述,https://github.com/onnx/onnx/blob/master/docs/Changelog.md#Upsample-9 ,cos sim 大概在0.98-0.99左右说明值已经比较相似了,但是两者的实现差距,这个你得看源码去理解其中的运算差异。

@bestzsq
Copy link

bestzsq commented Dec 1, 2020

@RichardSong-dev upsample的bug修复了,源码已更新,你试试看,然后你的模型里面的reshape操作维度大于4维了,这个caffe不支持,所以你导出onnx时,需要把这部分去掉才行。
还有就是你的caffe版本需要添加https://github.com/jnulzl/caffe_plus 这个项目中Upsample实现才能支持nearest模式的操作

谢谢大佬,我去研究研究!

请问大佬,是怎么在自己的caffe版本中添加https://github.com/jnulzl/caffe_plus 这个项目中Upsample的,是需要重新编译吗?

@RichardSong-dev
Copy link
Author

@bestzsqhttps://github.com/jnulzl/caffe_plus 这个项目中
include/caffe/layers/upsample_layer.hpp
src/caffe/layers/upsample_layer.cpp
src/caffe/layers/upsample_layer.cu
这三个文件放到你caffe目录相应的位置下,然后更改src/caffe/proto/caffe.proto中的代码,根据注释在相应位置添加如下代码:

optional UpsampleParameter upsample_param = 151;    //数值根据你使用的caffe.proto中注释更改
/*
...
*/
message UpsampleParameter {		
	optional int32 height = 1 [default = 32];
	optional int32 width = 2 [default = 32];
	optional int32 height_scale = 3 [default = 2];
	optional int32 width_scale = 4 [default = 2];
	enum UpsampleOp {
		NEAREST = 0;
		BILINEAR = 1;
	}
	optional UpsampleOp mode = 5 [default = BILINEAR];
}

之后重新编译就可以了。

@bestzsq
Copy link

bestzsq commented Dec 2, 2020

@RichardSong-dev 谢谢大佬。我昨天把整个caffe_plus项目编译了一遍,现在可以了

@lfydegithub
Copy link

@RichardSong-dev 你可以看看这个https://www.zhihu.com/question/63890195?sort=created 还有onnx关于upsample的功能描述,https://github.com/onnx/onnx/blob/master/docs/Changelog.md#Upsample-9 ,cos sim 大概在0.98-0.99左右说明值已经比较相似了,但是两者的实现差距,这个你得看源码去理解其中的运算差异。

大佬你好, 我转换模型时提示:
cos sim between onnx and caffe models: 1.0000001192092896
请问这个数字多少是理想值呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants