Unable to convert PyTorch model to onnx

Hello. I have some troubles in converting my yolo model to onnx format.
I have next errors:

C:\Users\1\PycharmProjects\untitled\my_utils.py:145: TracerWarning: There are 3 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
  pred[:, :, 0] = torch.sigmoid(pred[:, :, 0])
C:\Users\1\PycharmProjects\untitled\my_utils.py:146: TracerWarning: There are 3 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
  pred[:, :, 1] = torch.sigmoid(pred[:, :, 1])
C:\Users\1\PycharmProjects\untitled\my_utils.py:147: TracerWarning: There are 3 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
  pred[:, :, 4] = torch.sigmoid(pred[:, :, 4])
C:\Users\1\PycharmProjects\untitled\my_utils.py:149: 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!
  grid = np.arange(grid_size)
C:\Users\1\PycharmProjects\untitled\my_utils.py:149: TracerWarning: Converting a tensor to a Python float 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!
  grid = np.arange(grid_size)
C:\Users\1\PycharmProjects\untitled\my_utils.py:160: TracerWarning: There are 3 live references to the data region being modified when tracing in-place operator add_. This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
  pred[:, :, :2] += x_y_offset
C:\Users\1\PycharmProjects\untitled\my_utils.py:160: TracerWarning: There are 5 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
  pred[:, :, :2] += x_y_offset
C:\Users\1\PycharmProjects\untitled\my_utils.py:162: TracerWarning: Converting a tensor to a Python float 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!
  anchors = torch.FloatTensor(anchors)
C:\Users\1\PycharmProjects\untitled\my_utils.py:167: TracerWarning: There are 3 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
  pred[:, :, 2:4] = torch.exp(pred[:, :, 2:4]) * anchors
C:\Users\1\PycharmProjects\untitled\my_utils.py:169: TracerWarning: There are 3 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
  pred[:, :, 5:5 + num_classes] = torch.sigmoid(pred[:, :, 5:5 + num_classes])
C:\Users\1\PycharmProjects\untitled\my_utils.py:170: TracerWarning: There are 3 live references to the data region being modified when tracing in-place operator mul_. This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
  pred[:, :, :4] *= stride
C:\Users\1\PycharmProjects\untitled\my_utils.py:170: TracerWarning: There are 5 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
  pred[:, :, :4] *= stride

What should I do to fix this problems. Thank you.

There cannot be any in-place assignment, like:

pred[:, :, 0] = torch.sigmoid(pred[:, :, 0])

I think that you need to modify it as:

pred = torch.cat((torch.sigmoid(pred[:, :, 0:1]), pred[:, :, 1:]), dim=2)

Anyway, slicing cannot occur on the left side of “=”

Sorry, I still not good at pytorch. Thank you for your answer.
I’m interested, why did you change indexing in cat function from [:, :, 0] to [:, :, 0:1], don’t understand that.

I understand, how to fix most of this warnings, but still, don’t understand, how to fix this warning:

C:\Users\1\PycharmProjects\untitled\util.py:84: TracerWarning: Converting a tensor to a Python float 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!
  anchors = torch.FloatTensor(anchors)

But anchors variable is just pythonic list. Thank you

If pred.shape is (a, b, c), pred[:, :, 0].shape is (a, b), while pred[:, :, 0:1].shape is (a, b, 1). To keep the dimensions consistent in the concatenation, I think that pred[:, :, 0:1] should be used.

The following code converts the Tensor to a Python float.

anchors = torch.FloatTensor(anchors)

As a result, the variable “anchors” will be treated as a constant when you use the ONNX model. If this is not your intention, you need to avoid this conversion, and always use Torch Tensor in computation.