Floating Node Occuring in ONNX Graph

Darshan: <p>Hi Pytorch Team,</p>
<p>I am converting one PyTorch Model(yoloact) trained on my custom dataset to ONNX, the conversion is completing successfully, but in the ONNX model one of the node is not getting connected to the graph.<br>
I have attached the picture of the graph below.</p>
<p>Having said that, the pytorch model Inference is working fine, but not sure why one of the node is not getting connected to the graph in the onnx model.</p>
<p>Can you please assist me to solve this error? Would be a great help from yourside.</p>
<p>I am attaching the screenshot of the graph below:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/2/02ff6c3bebd5e3de7873f87bea571edd6272d7fd.png" data-download-href="https://discuss.pytorch.org/uploads/default/02ff6c3bebd5e3de7873f87bea571edd6272d7fd" title="Screenshot from 2021-05-18 12-17-13"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02ff6c3bebd5e3de7873f87bea571edd6272d7fd_2_690x219.png" alt="Screenshot from 2021-05-18 12-17-13" data-base62-sha1="qwc8r50bxdNu90bsMPHCV6YxFH" width="690" height="219" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02ff6c3bebd5e3de7873f87bea571edd6272d7fd_2_690x219.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/2/02ff6c3bebd5e3de7873f87bea571edd6272d7fd_2_1035x328.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/2/02ff6c3bebd5e3de7873f87bea571edd6272d7fd.png 2x" data-dominant-color="F7F7F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2021-05-18 12-17-13</span><span class="informations">1341×426 28.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Thanks,<br>
Darshan</p>

ptrblck: <p>Based on the screenshot <code>566</code> seems to indicate an <code>output</code>, so does the shape of the array look familiar and is it returned somewhere in the model?</p>

Darshan: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>Thanks for the reply.</p>
<p><code>566</code> and the shape is not occurring anywhere in the Model.</p>
<p>I am attaching the ONNX Model Below:</p>
<p><a href="https://drive.google.com/file/d/1kUGeVw16_1IQQMyqWUyPokZUjZyRLffS/view?usp=sharing" class="onebox" target="_blank" rel="noopener nofollow ugc">https://drive.google.com/file/d/1kUGeVw16_1IQQMyqWUyPokZUjZyRLffS/view?usp=sharing</a></p>
<p>Looking forward to your reply.</p>
<p>Thanks,<br>
Darshan</p>

Darshan: <p>Also during Conversion, I am getting the below warnings:</p>
<pre><code class="lang-auto">TracerWarning: Converting a tensor to a Python index 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!
  for j, i in product(range(conv_h), range(conv_w)):

TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
  self.anchors = torch.tensor(self.anchors, device=outs[0].device).reshape(-1, 4)

 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!
  self.anchors = torch.tensor(self.anchors, device=outs[0].device).reshape(-1, 4)
</code></pre>
<p>and the warnings are refering to this line in the Code:</p>
<pre><code>       ` self.anchors = torch.tensor(self.anchors, device=outs[0].device).reshape(-1, 4)`
</code></pre>
<p>the detail forward function is attached below:</p>
<pre><code class="lang-auto">    def forward(self, img, box_classes=None, masks_gt=None):
        outs = self.backbone(img)
        outs = self.fpn(outs[1:4])
        if isinstance(self.anchors, list):
            for i, shape in enumerate([list(aa.shape) for aa in outs]):
                self.anchors += make_anchors(self.cfg, shape[2], shape[3], self.cfg.scales[i])

            self.anchors = torch.tensor(self.anchors, device=outs[0].device).reshape(-1, 4)

        # outs[0]: [2, 256, 69, 69], the feature map from P3
        # proto_out = self.proto_net(outs[0])  # proto_out: (n, 32, 138, 138)
        # proto_out = F.relu(proto_out, inplace=True)
        # proto_out = proto_out.permute(0, 2, 3, 1).contiguous()

        class_pred, box_pred = [], []

        for aa in outs:
            class_p, box_p= self.prediction_layers[0](aa)
            class_pred.append(class_p)
            box_pred.append(box_p)
            #coef_pred.append(coef_p)

        class_pred = torch.cat(class_pred, dim=1)
        box_pred = torch.cat(box_pred, dim=1)
        #coef_pred = torch.cat(coef_pred, dim=1)

        if self.training:
            #seg_pred = self.semantic_seg_conv(outs[0])
            return self.compute_loss(class_pred, box_pred, box_classes)
        else:
            class_pred = F.softmax(class_pred, -1)
            return class_pred, box_pred,self.anchors# coef_pred, proto_out,
</code></pre>

Darshan: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>Looking forward to your Reply.</p>
<p>Thanks,<br>
Darshan</p>

Darshan: <p>Requesting for a Response.</p>

take-cheeze: <p>Seems <code>566</code> corresponds to return of <code>box_pred,self.anchors</code>.<br>
Does the model exported with <code>do_constant_folding=True</code> ?<br>
Also setting <code>output_names</code> should make value names more clear to human.</p>