Unable to convert PyTorch model to onnx

username: thearheus
cooked: <p>Hello. I have some troubles in converting my yolo model to onnx format.<br>
I have next errors:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>What should I do to fix this problems. Thank you.</p>

username: HeavyWeapon
cooked: <p>There cannot be any in-place assignment, like:</p>
<pre><code class="lang-auto">pred[:, :, 0] = torch.sigmoid(pred[:, :, 0])
</code></pre>
<p>I think that you need to modify it as:</p>
<pre><code class="lang-auto">pred = torch.cat((torch.sigmoid(pred[:, :, 0:1]), pred[:, :, 1:]), dim=2)
</code></pre>
<p>Anyway, slicing cannot occur on the left side of “=”</p>

username: thearheus
cooked: <p>Sorry, I still not good at pytorch. Thank you for your answer.<br>
I’m interested, why did you change indexing in cat function from <code>[:, :, 0]</code> to <code>[:, :, 0:1]</code>, don’t understand that.</p>
<p>I understand, how to fix most of this warnings, but still, don’t understand, how to fix this warning:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>But anchors variable is just pythonic list. Thank you</p>

username: HeavyWeapon
cooked: <p>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.</p>
<p>The following code converts the Tensor to a Python float.</p>
<pre><code class="lang-auto">anchors = torch.FloatTensor(anchors)
</code></pre>
<p>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.</p>