torch.onnx.export failes due to missing __round__ function
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 17.9k
- Forks
- 7.3k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 13
Description
🐛 Describe the bug
When exporting a trained CNN to onnx file format using torch.onnx.export(), an exception is thrown from torchvision/transforms/functional.py and the model is not saved.
To Reproduce
Steps to reproduce the behavior:
- Define a CNN-torch.Model instance similar to a VGG-Network, train it
- Try to export the trained model to an *.onnx - file using the save_model() function below
from os import makedirs, join
import torch
import torch.onnx as onnx
def save_model(model, batch_size, image_size, save_dir, input_channels, tensor_type):
makedirs(save_dir, exist_ok=True)
torch.save(model.state_dict(), join(save_dir, f"model.pt"))
x = torch.randn(batch_size, len(input_channels), *image_size, requires_grad=True) # a bogus input tensor; needed for tracing onnx export
x = x.type(tensor_type) # move to device
onnx.export(model, # model being run
x, # model input (or a tuple for multiple inputs)
join(save_dir, f"model.onnx"), # where to save the model (can be a file or file-like object)
export_params=True, # store the trained parameter weights inside the model file
opset_version=10, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names = ['input'], # the model's input names
output_names = ['output'], # the model's output names
dynamic_axes={'input' : {0 : 'batch_size'}, # variable length axes
'output' : {0 : 'batch_size'}})
print("onnx model exported")
~/miniconda3/envs/playground/lib/python3.8/site-packages/torchvision/transforms/functional.py in center_crop(img, output_size)
514 return img
515
--> 516 crop_top = int(round((image_height - crop_height) / 2.))
517 crop_left = int(round((image_width - crop_width) / 2.))
518 return crop(img, crop_top, crop_left, crop_height, crop_width)
TypeError: type Tensor doesn't define __round__ method
Expected behavior
Model is saved to file and can be used with onnxruntime library later on.
Workaround (or possible solution)
in torchvision/transforms/functional.py function center_crop change lines 516-517 to use the numpy.round() instead of built-in round():
# old code for reference:
# crop_top = int(round((image_height - crop_height) / 2.))
# crop_left = int(round((image_width - crop_width) / 2.))
# working by using numpy.round():
crop_top = int(np.round((image_height - crop_height) / 2.))
crop_left = int(np.round((image_width - crop_width) / 2.))
Versions
Collecting environment information...
PyTorch version: 1.9.1+cu102
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A
OS: Ubuntu 20.04.3 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: 10.0.0-4ubuntu1
CMake version: version 3.16.3
Libc version: glibc-2.31
Python version: 3.8.10 (default, Jun 4 2021, 15:09:15) [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.0-81-generic-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce RTX 2080 Ti
Nvidia driver version: 460.91.03
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.2.4
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.2.4
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.2.4
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.2.4
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.2.4
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.2.4
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.2.4
/usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7
HIP runtime version: N/A
MIOpen runtime version: N/A
Versions of relevant libraries:
[pip3] numpy==1.21.2
[pip3] torch==1.9.1
[pip3] torchvision==0.10.1
[conda] numpy 1.21.2 pypi_0 pypi
[conda] torch 1.9.1 pypi_0 pypi
[conda] torchvision 0.10.1 pypi_0 pypi
cc @neginraoof
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in torchvision/transforms/functional.py at center_crop, then reproduce the reported torch.onnx.export failure with the provided CNN-style example and environment details. Check the existing transform behavior and relevant test coverage; done means the export completes without the round TypeError and the ONNX file is saved for later use.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- computer-vision
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100