Export to ONNX Advice

username: johnorford
cooked: <p>I am trying to export some code to ONNX:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/Atten4Vis/ConditionalDETR/blob/main/models/backbone.py#L77C10-L77C10">
  <header class="source">

      <a href="https://github.com/Atten4Vis/ConditionalDETR/blob/main/models/backbone.py#L77C10-L77C10" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/Atten4Vis/ConditionalDETR/blob/main/models/backbone.py#L77C10-L77C10" target="_blank" rel="noopener nofollow ugc">Atten4Vis/ConditionalDETR/blob/main/models/backbone.py#L77C10-L77C10</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="77" style="counter-reset: li-counter 76 ;">
          <li>self.body = IntermediateLayerGetter(backbone, return_layers=return_layers)</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Some layers (‘IntermediateLayer’) are outputting dictionaries:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/vision/blob/main/torchvision/models/_utils.py#L37-L42">
  <header class="source">

      <a href="https://github.com/pytorch/vision/blob/main/torchvision/models/_utils.py#L37-L42" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/main/torchvision/models/_utils.py#L37-L42" target="_blank" rel="noopener nofollow ugc">pytorch/vision/blob/main/torchvision/models/_utils.py#L37-L42</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="37" style="counter-reset: li-counter 36 ;">
          <li>&gt;&gt;&gt; new_m = torchvision.models._utils.IntermediateLayerGetter(m,</li>
          <li>&gt;&gt;&gt;     {'layer1': 'feat1', 'layer3': 'feat2'})</li>
          <li>&gt;&gt;&gt; out = new_m(torch.rand(1, 3, 224, 224))</li>
          <li>&gt;&gt;&gt; print([(k, v.shape) for k, v in out.items()])</li>
          <li>&gt;&gt;&gt;     [('feat1', torch.Size([1, 64, 56, 56])),</li>
          <li>&gt;&gt;&gt;      ('feat2', torch.Size([1, 256, 14, 14]))]</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>…which ONNX export is complaining about (no <code>aten::Dict</code> support).</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/9/999297ad7094a16de0009fab50119f026d7af7e0.png" data-download-href="https://discuss.pytorch.org/uploads/default/999297ad7094a16de0009fab50119f026d7af7e0" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/999297ad7094a16de0009fab50119f026d7af7e0_2_690x16.png" alt="image" data-base62-sha1="lUz7O79FCZaiq5a64cxbWwCnrUY" width="690" height="16" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/999297ad7094a16de0009fab50119f026d7af7e0_2_690x16.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/999297ad7094a16de0009fab50119f026d7af7e0_2_1035x24.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/999297ad7094a16de0009fab50119f026d7af7e0_2_1380x32.png 2x" data-dominant-color="E9E9E9"><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">image</span><span class="informations">3022×74 56.8 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>Any general advice on best next steps?</p>
<p>I will try replacing the dictionary with a list of tuples.</p>
<p>But I’d appreciate any other advice.</p>