I am trying to convert a pytorch model to onnx model. Below is the code i used.
`import torch
import torchvision
model = torch.hub.load('ultralytics/yolov5', 'custom', path=r"C:User.......modelyolov5.pt", force_reload=True)
dummy_input = torch.randn(1, 3, 1280, 720)
torch.onnx.export(model, dummy_input, "yolov5_1280_720.onnx", verbose=True)
`
Below is the error i am getting:
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!y = self.model(im, augment=augment, visualize=visualize) if augment or visualize else self.model(im)
RuntimeError Traceback (most recent call last)Cell In[21], line 11 torch.onnx.export(model, dummy_input, "yolov5lv3_1280_720.onnx", verbose=True)
File ~anaconda3Libsite-packagestorchonnxutils.py:516, in export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, custom_opsets, export_modules_as_functions, autograd_inlining)189 @_beartype.beartype190 def export(191 model: Union[torch.nn.Module, torch.jit.ScriptModule, torch.jit.ScriptFunction],
(...)208 autograd_inlining: Optional[bool] = True,209 ) -> None:210 r"""Exports a model into ONNX format.211212 If model is not a :class:torch.jit.ScriptModule nor a(...)513 All errors are subclasses of :class:errors.OnnxExporterError.514 """516 _export(517 model,518 args,519 f,520 export_params,521 verbose,522 training,523 input_names,524 output_names,525 operator_export_type=operator_export_type,526 opset_version=opset_version,527 do_constant_folding=do_constant_folding,528 dynamic_axes=dynamic_axes,529 keep_initializers_as_inputs=keep_initializers_as_inputs,530 custom_opsets=custom_opsets,531 export_modules_as_functions=export_modules_as_functions,532 autograd_inlining=autograd_inlining,533 )
File ~anaconda3Libsite-packagestorchonnxutils.py:1612, in _export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, fixed_batch_size, custom_opsets, add_node_names, onnx_shape_inference, export_modules_as_functions, autograd_inlining)1609 dynamic_axes = {}1610 _validate_dynamic_axes(dynamic_axes, model, input_names, output_names)1612 graph, params_dict, torch_out = _model_to_graph(1613 model,1614 args,1615 verbose,1616 input_names,1617 output_names,1618 operator_export_type,1619 val_do_constant_folding,1620 fixed_batch_size=fixed_batch_size,1621 training=training,1622 dynamic_axes=dynamic_axes,1623 )1625 # TODO: Don't allocate a in-memory string for the protobuf1626 defer_weight_export = (1627 export_type is not _exporter_states.ExportTypes.PROTOBUF_FILE1628 )
File ~anaconda3Libsite-packagestorchonnxutils.py:1134, in _model_to_graph(model, args, verbose, input_names, output_names, operator_export_type, do_constant_folding, _disable_torch_constant_prop, fixed_batch_size, training, dynamic_axes)1131 args = (args,)1133 model = _pre_trace_quant_model(model, args)1134 graph, params, torch_out, module = _create_jit_graph(model, args)1135 params_dict = _get_named_param_dict(graph, params)1137 try:
File ~anaconda3Libsite-packagestorchonnxutils.py:1010, in _create_jit_graph(model, args)1005 graph = _C._propagate_and_assign_input_shapes(1006 graph, flattened_args, param_count_list, False, False1007 )1008 return graph, params, torch_out, None1010 graph, torch_out = _trace_and_get_graph_from_model(model, args)1011 _C._jit_pass_onnx_lint(graph)1012 state_dict = torch.jit._unique_state_dict(model)
File ~anaconda3Libsite-packagestorchonnxutils.py:914, in _trace_and_get_graph_from_model(model, args)912 prev_autocast_cache_enabled = torch.is_autocast_cache_enabled()913 torch.set_autocast_cache_enabled(False)914 trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(915 model,916 args,917 strict=False,918 _force_outplace=False,919 _return_inputs_states=True,920 )921 torch.set_autocast_cache_enabled(prev_autocast_cache_enabled)923 warn_on_static_input_change(inputs_states)
File ~anaconda3Libsite-packagestorchjit_trace.py:1310, in _get_trace_graph(f, args, kwargs, strict, _force_outplace, return_inputs, _return_inputs_states)1308 if not isinstance(args, tuple):1309 args = (args,)1310 outs = ONNXTracedModule(1311 f, strict, _force_outplace, return_inputs, _return_inputs_states1312 )(*args, **kwargs)1313 return outs
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1532, in Module._wrapped_call_impl(self, *args, **kwargs)1530 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]1531 else:1532 return self._call_impl(*args, **kwargs)
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1541, in Module._call_impl(self, *args, **kwargs)1536 # If we don't have any hooks, we want to skip the rest of the logic in1537 # this function, and just call forward.1538 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks1539 or _global_backward_pre_hooks or _global_backward_hooks1540 or _global_forward_hooks or _global_forward_pre_hooks):1541 return forward_call(*args, **kwargs)1543 try:1544 result = None
File ~anaconda3Libsite-packagestorchjit_trace.py:138, in ONNXTracedModule.forward(self, *args)135 else:136 return tuple(out_vars)138 graph, out = torch._C._create_graph_by_tracing(139 wrapper,140 in_vars + module_state,141 _create_interpreter_name_lookup_fn(),142 self.strict,143 self._force_outplace,144 )146 if self._return_inputs:147 return graph, outs[0], ret_inputs[0]
File ~anaconda3Libsite-packagestorchjit_trace.py:129, in ONNXTracedModule.forward.<locals>.wrapper(*args)127 if self._return_inputs_states:128 inputs_states.append(_unflatten(in_args, in_desc))129 outs.append(self.inner(*trace_inputs))130 if self._return_inputs_states:131 inputs_states[0] = (inputs_states[0], trace_inputs)
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1532, in Module._wrapped_call_impl(self, *args, **kwargs)1530 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]1531 else:1532 return self._call_impl(*args, **kwargs)
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1541, in Module._call_impl(self, *args, **kwargs)1536 # If we don't have any hooks, we want to skip the rest of the logic in1537 # this function, and just call forward.1538 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks1539 or _global_backward_pre_hooks or _global_backward_hooks1540 or _global_forward_hooks or _global_forward_pre_hooks):1541 return forward_call(*args, **kwargs)1543 try:1544 result = None
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1522, in Module._slow_forward(self, *input, **kwargs)1520 recording_scopes = False1521 try:1522 result = self.forward(*input, **kwargs)1523 finally:1524 if recording_scopes:
File ~anaconda3Libsite-packagestorchutils_contextlib.py:115, in context_decorator.<locals>.decorate_context(*args, **kwargs)112 @functools.wraps(func)113 def decorate_context(*args, **kwargs):114 with ctx_factory():115 return func(*args, **kwargs)
File ~/.cachetorchhubultralytics_yolov5_mastermodelscommon.py:843, in AutoShape.forward(self, ims, size, augment, profile)841 if isinstance(ims, torch.Tensor): # torch842 with amp.autocast(autocast):843 return self.model(ims.to(p.device).type_as(p), augment=augment) # inference845 # Pre-process846 n, ims = (len(ims), list(ims)) if isinstance(ims, (list, tuple)) else (1, [ims]) # number, list of images
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1532, in Module._wrapped_call_impl(self, *args, **kwargs)1530 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]1531 else:1532 return self._call_impl(*args, **kwargs)
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1541, in Module._call_impl(self, *args, **kwargs)1536 # If we don't have any hooks, we want to skip the rest of the logic in1537 # this function, and just call forward.1538 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks1539 or _global_backward_pre_hooks or _global_backward_hooks1540 or _global_forward_hooks or _global_forward_pre_hooks):1541 return forward_call(*args, **kwargs)1543 try:1544 result = None
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1522, in Module._slow_forward(self, *input, **kwargs)1520 recording_scopes = False1521 try:1522 result = self.forward(*input, **kwargs)1523 finally:1524 if recording_scopes:
File ~/.cachetorchhubultralytics_yolov5_mastermodelscommon.py:666, in DetectMultiBackend.forward(self, im, augment, visualize)663 im = im.permute(0, 2, 3, 1) # torch BCHW to numpy BHWC shape(1,320,192,3)665 if self.pt: # PyTorch666 y = self.model(im, augment=augment, visualize=visualize) if augment or visualize else self.model(im)667 elif self.jit: # TorchScript668 y = self.model(im)
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1532, in Module._wrapped_call_impl(self, *args, **kwargs)1530 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]1531 else:1532 return self._call_impl(*args, **kwargs)
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1541, in Module._call_impl(self, *args, **kwargs)1536 # If we don't have any hooks, we want to skip the rest of the logic in1537 # this function, and just call forward.1538 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks1539 or _global_backward_pre_hooks or _global_backward_hooks1540 or _global_forward_hooks or _global_forward_pre_hooks):1541 return forward_call(*args, **kwargs)1543 try:1544 result = None
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1522, in Module._slow_forward(self, *input, **kwargs)1520 recording_scopes = False1521 try:1522 result = self.forward(*input, **kwargs)1523 finally:1524 if recording_scopes:
File ~/.cachetorchhubultralytics_yolov5_mastermodelsyolo.py:263, in DetectionModel.forward(self, x, augment, profile, visualize)261 if augment:262 return self._forward_augment(x) # augmented inference, None263 return self._forward_once(x, profile, visualize)
File ~/.cachetorchhubultralytics_yolov5_mastermodelsyolo.py:167, in BaseModel._forward_once(self, x, profile, visualize)165 if profile:166 self._profile_one_layer(m, x, dt)167 x = m(x) # run168 y.append(x if m.i in self.save else None) # save output169 if visualize:
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1532, in Module._wrapped_call_impl(self, *args, **kwargs)1530 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]1531 else:1532 return self._call_impl(*args, **kwargs)
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1541, in Module._call_impl(self, *args, **kwargs)1536 # If we don't have any hooks, we want to skip the rest of the logic in1537 # this function, and just call forward.1538 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks1539 or _global_backward_pre_hooks or _global_backward_hooks1540 or _global_forward_hooks or _global_forward_pre_hooks):1541 return forward_call(*args, **kwargs)1543 try:1544 result = None
File ~anaconda3Libsite-packagestorchnnmodulesmodule.py:1522, in Module._slow_forward(self, *input, **kwargs)1520 recording_scopes = False1521 try:1522 result = self.forward(*input, **kwargs)1523 finally:1524 if recording_scopes:
File ~/.cachetorchhubultralytics_yolov5_mastermodelscommon.py:435, in Concat.forward(self, x)431 def forward(self, x):432 """Concatenates a list of tensors along a specified dimension; x is a list of tensors, dimension is an433 int.434 """435 return torch.cat(x, self.d)
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 46 but got size 45 for tensor number 1 in the list.
WHAT IS THE PROBLEM HERE?
I TRIED UPDATING THE PACKAGES. I DO NOT KNOW WHERE IS THE PROBLEM EXACTLY AND WHERE TO SOLVE IT?