Open AakashKumarNain opened 4 years ago
opset_version=10
if it set to 11, the export will be normal except some warning. but the result seems good
@mzy97 I tried that but ran into some errors. Is it necessary to build torch
from source for this?
I use docker image
Hi,
Due to some recent changes in PyTorch, ONNX export for mask r-cnn and keypoint r-cnn will only work with PyTorch 1.4 and torchvision 0.5.0.
Newer versions of PyTorch or torchvision won't work for now, but we are working fixing this.
cc @neginraoof
@fmassa Hi, I'm facing the same issue exporting a fine-tuned Keypoint R-CNN with Mobilenet V2 backbone model to ONNX. Following your comment, I downgraded to PyTorch 1.4 and TorchVision 0.5.0 and I'm still facing the same issue (even after re-training the whole network):
...line 81, in _parse_arg "', since it's not constant, please try to make " RuntimeError: Failed to export an ONNX attribute 'onnx::Sub', since it's not constant, please try to make things (e.g., kernel size) static if possible
Could you assist me with that please?
@neginraoof could you have a look?
I added the following lines after line 78 in symbolic_helper.py
to check the node which is causing the error:
print(v.node())
print(v.node().kind())
Here is my full output:
C:\Users\yhashisho\Projects\git\keypoints-detection\venv\Scripts\python.exe C:/Users/yhashisho/Projects/git/keypoints-detection/pytorch-train/export_to_ONNX.py
PyTorch Version: 1.4.0
Torchvision Version: 0.5.0
C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\tensor.py:461: RuntimeWarning: Iterating over a tensor might cause the trace to be incorrect. Passing a tensor of different shape won't change the number of iterations executed (and might lead to errors or silently give incorrect results).
'incorrect results).', category=RuntimeWarning)
C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\symbolic_helper.py:248: UserWarning: You are trying to export the model with onnx:Upsample for ONNX opset version 9. This operator might cause results to not match the expected results by PyTorch.
ONNX's Upsample/Resize operator did not match Pytorch's Interpolation until opset 11. Attributes to determine how to transform the input were added in onnx:Resize in opset 11 to support Pytorch's behavior (like coordinate_transformation_mode and nearest_mode).
We recommend using opset 11 and above for models using this operator.
"" + str(_export_onnx_opset_version) + ". "
%482 : Long() = onnx::Constant[value={0}]()
onnx::Constant
%481 : Long() = onnx::Sub(%460, %475) # C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torchvision\models\detection\transform.py:132:0
onnx::Sub
Traceback (most recent call last):
File "C:/Users/yhashisho/Projects/git/keypoints-detection/pytorch-train/export_to_ONNX.py", line 62, in <module>
main()
File "C:/Users/yhashisho/Projects/git/keypoints-detection/pytorch-train/export_to_ONNX.py", line 58, in main
export_to_ONNX(model, [image], config.ONNX_path, opset_version=11)
File "C:/Users/yhashisho/Projects/git/keypoints-detection/pytorch-train/export_to_ONNX.py", line 49, in export_to_ONNX
torch.onnx.export(model, input, path, opset_version)
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\__init__.py", line 148, in export
strip_doc_string, dynamic_axes, keep_initializers_as_inputs)
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\utils.py", line 66, in export
dynamic_axes=dynamic_axes, keep_initializers_as_inputs=keep_initializers_as_inputs)
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\utils.py", line 416, in _export
fixed_batch_size=fixed_batch_size)
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\utils.py", line 296, in _model_to_graph
fixed_batch_size=fixed_batch_size, params_dict=params_dict)
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\utils.py", line 135, in _optimize_graph
graph = torch._C._jit_pass_onnx(graph, operator_export_type)
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\__init__.py", line 179, in _run_symbolic_function
return utils._run_symbolic_function(*args, **kwargs)
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\utils.py", line 657, in _run_symbolic_function
return op_fn(g, *inputs, **attrs)
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\symbolic_helper.py", line 130, in wrapper
args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\symbolic_helper.py", line 130, in <listcomp>
args = [_parse_arg(arg, arg_desc) for arg, arg_desc in zip(args, arg_descriptors)]
File "C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torch\onnx\symbolic_helper.py", line 83, in _parse_arg
"', since it's not constant, please try to make "
RuntimeError: Failed to export an ONNX attribute 'onnx::Sub', since it's not constant, please try to make things (e.g., kernel size) static if possible
Process finished with exit code 1
In addition to that, I set opset_version=11
and it is still giving me warning that opset_version used is 9, which is weird ..
I guess this is causing the error:
padding = [(s1 - s2) for s1, s2 in zip(max_size, tuple(img.shape))]
in C:\Users\yhashisho\Projects\git\keypoints-detection\venv\lib\site-packages\torchvision\models\detection\transform.py:132:0
:
# _onnx_batch_images() is an implementation of
# batch_images() that is supported by ONNX tracing.
@torch.jit.unused
def _onnx_batch_images(self, images, size_divisible=32):
# type: (List[Tensor], int) -> Tensor
max_size = []
for i in range(images[0].dim()):
max_size_i = torch.max(torch.stack([img.shape[i] for img in images]).to(torch.float32)).to(torch.int64)
max_size.append(max_size_i)
stride = size_divisible
max_size[1] = (torch.ceil((max_size[1].to(torch.float32)) / stride) * stride).to(torch.int64)
max_size[2] = (torch.ceil((max_size[2].to(torch.float32)) / stride) * stride).to(torch.int64)
max_size = tuple(max_size)
# work around for
# pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img)
# which is not yet supported in onnx
padded_imgs = []
for img in images:
padding = [(s1 - s2) for s1, s2 in zip(max_size, tuple(img.shape))]
padded_img = torch.nn.functional.pad(img, (0, padding[2], 0, padding[1], 0, padding[0]))
padded_imgs.append(padded_img)
return torch.stack(padded_imgs)
Please try opset 11 export. Export of the detection models Faster RCNN, Mask RCNN and Keypoint RCNN is enabled using ONNX opset 11. Would you please try setting opset_version=11 at export and see if you still get this error?
Please try opset 11 export. Export of the detection models Faster RCNN, Mask RCNN and Keypoint RCNN is enabled using ONNX opset 11. Would you please try setting opset_version=11 at export and see if you still get this error?
It is already set to 11
Ok, it is fixed now. I passed opset_version
to the wrong function parameter:
Error:
def export_to_ONNX(model, input, path, opset_version=11):
torch.onnx.export(model, input, path, opset_version)
Fix:
def export_to_ONNX(model, input, path, opset_version=11):
torch.onnx.export(model, input, path, opset_version=opset_version)
🐛 Bug
I was trying to convert the pretrained
resnet50_fpn
detection model toonnx
but I got a runtime error while trying it.To Reproduce
Expected behavior
An onnx format based converted model
Environment
conda
,pip
, source): condaAdditional context
Here is the error I am getting