Yolov3: PyTorch 1.1 ONNX export error

Created on 5 May 2019  路  9Comments  路  Source: ultralytics/yolov3

Hello with current master after setting ONNX_EXPORT to True, detect.py failed with
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

$ python3 detect.py --cfg yolov3.cfg --weights yolov3.weights
Namespace(cfg='yolov3.cfg', conf_thres=0.5, data_cfg='data/coco.data', images='data/samples', img_size=416, nms_thres=0.5, weights='yolov3.weights')
Using CUDA device0 _CudaDeviceProperties(name='GeForce GTX 1080 Ti', total_memory=11178MB)

/home/clement/Downloads/ultralytics/models.py:192: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
img_size = max(x.shape[-2:])
Traceback (most recent call last):
File "detect.py", line 140, in
nms_thres=opt.nms_thres
File "detect.py", line 49, in detect
torch.onnx.export(model, img, 'weights/export.onnx', verbose=True)
File "/home/clement/.local/lib/python3.6/site-packages/torch/onnx/__init__.py", line 25, in export
return utils.export(args, *kwargs)
File "/home/clement/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 131, in export
strip_doc_string=strip_doc_string)
File "/home/clement/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 363, in _export
_retain_param_name, do_constant_folding)
File "/home/clement/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 266, in _model_to_graph
graph, torch_out = _trace_and_get_graph_from_model(model, args, training)
File "/home/clement/.local/lib/python3.6/site-packages/torch/onnx/utils.py", line 225, in _trace_and_get_graph_from_model
trace, torch_out = torch.jit.get_trace_graph(model, args, _force_outplace=True)
File "/home/clement/.local/lib/python3.6/site-packages/torch/jit/__init__.py", line 231, in get_trace_graph
return LegacyTracedModule(f, _force_outplace, return_inputs)(args, *kwargs)
File "/home/clement/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
result = self.forward(input, *kwargs)
File "/home/clement/.local/lib/python3.6/site-packages/torch/jit/__init__.py", line 294, in forward
out = self.inner(trace_inputs)
File "/home/clement/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self._slow_forward(
input, *kwargs)
File "/home/clement/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward
result = self.forward(
input, *kwargs)
File "/home/clement/Downloads/ultralytics/models.py", line 199, in forward
x = module(x)
File "/home/clement/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self._slow_forward(
input, *kwargs)
File "/home/clement/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward
result = self.forward(
input, *kwargs)
File "/home/clement/.local/lib/python3.6/site-packages/torch/nn/modules/container.py", line 92, in forward
input = module(input)
File "/home/clement/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self._slow_forward(
input, *kwargs)
File "/home/clement/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward
result = self.forward(
input, **kwargs)
File "/home/clement/.local/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 338, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

bug

All 9 comments

Okay I worked around this issue by running PyTorch in CPU only.

@clementyau this seems to be a PyTorch 1.1 issue. See https://github.com/pytorch/pytorch/issues/19374.

@glenn-jocher , it seem to be fixed in master, and i reinstall pytorch by pip
following the command:
pip3 install https://download.pytorch.org/whl/cu100/torch-1.1.0-cp36-cp36m-linux_x86_64.whl
pip3 install https://download.pytorch.org/whl/cu100/torchvision-0.3.0-cp36-cp36m-linux_x86_64.whl
but still get error, any suggestion? Thanks

@peterhsu2018 your install command will only reinstall the same 1.1.0 you probably already had before. If you want the latest pytorch (which I don't recommend), then you need to install nightly.

In any case, these are all direct PyTorch issues and not unique to this repo, so I would look for answers on the pytorch repo or the pytorch message boards.

Also as an update, the torch.onnx export doesnt work with

  1. pytorch 1.0.1 with cuda 10
  2. pytorch nightly (1.2.XX) with cuda 10

@clementyau How did you change the code to use cpu only?
In detect.py, in line 24, I replaced the
device = torch_utils.select_device() to device = torch.device('cpu')
But this doesn't work, let me know how you changed the code to use cpu only.

In detect.py use device = torch_utils.select_device(force_cpu=True).

This cpu-issue should be corrected in the latest commit.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JiahongXue picture JiahongXue  路  5Comments

Alex1101a picture Alex1101a  路  5Comments

mehrdadazizi72 picture mehrdadazizi72  路  3Comments

JungminChung picture JungminChung  路  3Comments

CF2220160244 picture CF2220160244  路  5Comments