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

Fix parse bug #904

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Fix parse bug #904

wants to merge 2 commits into from

Conversation

Sundrops
Copy link

@lxgyChen
Copy link

@Sundrops 好像不起作用啊#882,还是报一样的错误。

@Sundrops
Copy link
Author

It's ok on torch1.6.0

@lxgyChen
Copy link

我刚试过,在torch1.5.1中,当output_shape_str=['%500 : Float', '1, 256, 20, 20', ' ']时,在torch1.6.0中是['%500 : Float', '1:102400, 256:400, 20:20, 20:1', ' ``'],这种情况下在目前的mmdnn版本下也是能正确解析的。
但在我转换yolov5s的情况,见#882,输出的output_shape_str:

['%498 : Float', '1:102400, 256:400, 20:20, 20:1', ' '] ['%499 : Float', '1:102400, 256:400, 20:20, 20:1', ' '] ['%500 : Double', ' '] Traceback (most recent call last): File "./convertToIR.py", line 202, in <module> _main() File "./convertToIR.py", line 197, in _main ret = _convert(args) File "./convertToIR.py", line 97, in _convert parser = PytorchParser151(model, inputshape[0]) File "/home/user/anaconda3/envs/pytorch/lib/python3.7/site-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 533, in __init__ self.build_graph(input_shape) File "/home/user/anaconda3/envs/pytorch/lib/python3.7/site-packages/mmdnn/conversion/pytorch/pytorch_parser.py", line 92, in build_graph self.pytorch_graph.build(self.input_shape) File "/home/user/anaconda3/envs/pytorch/lib/python3.7/site-packages/mmdnn/conversion/pytorch/pytorch_graph.py", line 136, in build output_shape = [int(x.replace('!', '').split(':')[0]) for x in output_shape_str[1].split(',')] File "/home/user/anaconda3/envs/pytorch/lib/python3.7/site-packages/mmdnn/conversion/pytorch/pytorch_graph.py", line 136, in <listcomp> output_shape = [int(x.replace('!', '').split(':')[0]) for x in output_shape_str[1].split(',')] ValueError: invalid literal for int() with base 10: ' '

当output_shape_str=['%500 : Double', ' ']时,解析出来就是空字符串‘ ’了,转换为int型就报错了。这种情况怎么处理呢?
我把conversion/pytorch/pytorch_graph.py 大约在134行的

if len(output_shape_str) > 1 改成 if len(output_shape_str) > 2不会报错了,但是网络没有完全转换成功,只转换了第一层,而且提示PyTorch parser has not supported operator [onnx::Constant]. IR network strucuture may lost info.

@Sundrops
Copy link
Author

Sundrops commented Oct 22, 2020

@lxgyChen do you use Upsample layer in yolov5s? Upsample layer will be converted to

, %224 : Double() = onnx::Constant[value={2}](), scope: CustomizedNet/Upsample[Sampling30]
, %225 : Double() = onnx::Constant[value={2}](), scope: CustomizedNet/Upsample[Sampling30]
, %226 : Tensor = onnx::Unsqueeze[axes=[0]](%224), scope: CustomizedNet/Upsample[Sampling30]
, %227 : Tensor = onnx::Cast[to=1](%226), scope: CustomizedNet/Upsample[Sampling30]
, %228 : Tensor = onnx::Unsqueeze[axes=[0]](%225), scope: CustomizedNet/Upsample[Sampling30]
, %229 : Tensor = onnx::Cast[to=1](%228), scope: CustomizedNet/Upsample[Sampling30]
, %230 : Tensor = onnx::Constant[value= 1  1 [ CPUFloatType{2} ]](), scope: CustomizedNet/Upsample[Sampling30]
, %231 : Tensor = onnx::Concat[axis=0](%230, %227, %229), scope: CustomizedNet/Upsample[Sampling30]
, %232 : Tensor = onnx::Constant[value=[ CPUFloatType{0} ]](), scope: CustomizedNet/Upsample[Sampling30]
, %233 : Float(1:60768, 422:144, 12:12, 12:1) = onnx::Resize[coordinate_transformation_mode="asymmetric", cubic_coeff_a=-0.75, mode="nearest", nearest_mode="floor"](%223, %232, %231), scope: CustomizedNet/Upsampl
e[Sampling30] # /mnt/WXRC0020/release/gluoncvplus/.local/opt/anaconda3/envs/gp_v0.0.1/lib/python3.7/site-packages/torch/nn/functional.py:3143:0

And mmdnn does not support Constant op.

@lxgyChen
Copy link

@Sundrops 上面的output_shape_str=['%500 : Double', ' ']还没到Upsample,但也是一个Constant。YOLO v3 v4 v5都有Upsample层,那这样都不能转换了啊?

@Sundrops
Copy link
Author

@lxgyChen As mentioned above, Upsample layer will be converted to onnx:: Constant and other layers. If you want to use Upsample, you should modify code to merge onnx:: Constant, ... onnx::Resize in onnx graph(https://github.com/microsoft/MMdnn/blob/master/mmdnn/conversion/pytorch/pytorch_graph.py#L231)

@lxgyChen
Copy link

@Sundrops 好的,我研究一下。怎么避免Constant Op呢,用已知的固定值代替可以吗?PyTorch导出为ONNX时可以选择Constant Folding,但是MMdnn不支持ONNX to IR

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

Successfully merging this pull request may close these issues.

2 participants