'dict' object has no attribute 'training'

ynjiun_wang: <p>I tried to convert a pytorch model .pt file into a onnx by first build the model:</p>
<pre><code class="lang-auto">    model = PredictModel(**vars(args)).eval()
    ckpt = torch.load(args.ckpt)
    if "state_dict" in ckpt:
        model.load_state_dict(ckpt["state_dict"])
    else:
        model.model.load_state_dict(ckpt)
</code></pre>
<p>After the model built and successfully loaded, I run the following code:</p>
<pre><code class="lang-auto">        L = torch.ones((1, 3, 320, 1152), dtype=torch.float32)
        R = torch.ones((1, 3, 320, 1152), dtype=torch.float32)
        onnxfile=args.ckpt.replace('.pt','.onnx')
        torch.onnx.export(
            model(L,R),
            (L,R),
            onnxfile,
            opset_version=12,
            do_constant_folding=True,
        )
</code></pre>
<p>And I got the following error message:</p>
<blockquote>
<p>/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  /opt/conda/conda-bld/pytorch_1634272068694/work/aten/src/ATen/native/TensorShape.cpp:2157.)<br>
return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]<br>
Traceback (most recent call last):<br>
File “/home/paul/pytorch/TinyHITNet/predict.py”, line 108, in <br>
torch.onnx.export(<br>
File “/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/<strong>init</strong>.py”, line 316, in export<br>
return utils.export(model, args, f, export_params, verbose, training,<br>
File “/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/utils.py”, line 107, in export<br>
_export(model, args, f, export_params, verbose, training, input_names, output_names,<br>
File “/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/utils.py”, line 709, in _export<br>
with select_model_mode_for_export(model, training):<br>
File “/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/contextlib.py”, line 113, in <strong>enter</strong><br>
return next(self.gen)<br>
File “/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/utils.py”, line 42, in select_model_mode_for_export<br>
is_originally_training = model.training<br>
AttributeError: ‘dict’ object has no attribute ‘training’</p>
</blockquote>
<p>What am I missing? could you please help? Thank you.</p>

ptrblck: <p>Based on the error message it seems you are not passing a valid <code>nn.Module</code> as the model to <code>export</code> but a <code>dict</code>.<br>
The <code>.training</code> attribute query seems to be failing for:</p>
<pre><code class="lang-python">model.training
AttributeError: ‘dict’ object has no attribute ‘training’
</code></pre>

ynjiun_wang: <p><a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>Thank you for your help. I tried again by creating the model directly:</p>
<p><code>model = HITNet_KITTI()</code></p>
<p>Then I tried to access</p>
<p><code>model.training</code></p>
<p>and it response</p>
<p><code>True</code></p>
<p>Then tried to export onnx,</p>
<pre><code class="lang-auto">        L = torch.ones((1, 3, 320, 1152), dtype=torch.float32)
        R = torch.ones((1, 3, 320, 1152), dtype=torch.float32)
        onnxfile="../model.onnx"
        torch.onnx.export(
            model,
            (L,R),
            onnxfile,
            opset_version=12,
            do_constant_folding=True,
        )
</code></pre>
<p>Now it encounter the following errors:</p>
<pre><code class="lang-auto">/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  /opt/conda/conda-bld/pytorch_1634272068694/work/aten/src/ATen/native/TensorShape.cpp:2157.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:9: 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!
  out_h = math.ceil(x.size(2) / s[0])
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:10: 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!
  out_w = math.ceil(x.size(3) / s[1])
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:12: 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!
  pad_h = max((out_h - 1) * s[0] + w.size(2) - x.size(2), 0)
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:13: 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!
  pad_w = max((out_w - 1) * s[1] + w.size(3) - x.size(3), 0)
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:14: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
  pad_top = pad_h // 2
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:16: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
  pad_left = pad_w // 2
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:147: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
  scale = left.size(3) // hyp.size(3)
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:148: 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!
  assert scale == 4
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:172: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
  cost = cost.reshape(n, c, h // scale, scale, w // scale, scale)
/home/paul/pytorch/TinyHITNet/models/hit_net_kitti.py:174: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
  cost = cost.reshape(n, scale * scale * c, h // scale, w // scale)
Traceback (most recent call last):
  File "/home/paul/pytorch/TinyHITNet/predict.py", line 106, in &lt;module&gt;
    torch.onnx.export(
  File "/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/__init__.py", line 316, in export
    return utils.export(model, args, f, export_params, verbose, training,
  File "/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/utils.py", line 107, in export
    _export(model, args, f, export_params, verbose, training, input_names, output_names,
  File "/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/utils.py", line 724, in _export
    _model_to_graph(model, args, verbose, input_names,
  File "/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/utils.py", line 493, in _model_to_graph
    graph, params, torch_out, module = _create_jit_graph(model, args)
  File "/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/utils.py", line 437, in _create_jit_graph
    graph, torch_out = _trace_and_get_graph_from_model(model, args)
  File "/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/utils.py", line 388, in _trace_and_get_graph_from_model
    torch.jit._get_trace_graph(model, args, strict=False, _force_outplace=False, _return_inputs_states=True)
  File "/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/jit/_trace.py", line 1166, in _get_trace_graph
    outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
  File "/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/jit/_trace.py", line 127, in forward
    graph, out = torch._C._create_graph_by_tracing(
RuntimeError: output 1 (4
[ CPULongType{} ]) of traced region did not have observable data dependence with trace inputs; this probably indicates your program cannot be understood by the tracer.
</code></pre>
<p>You may duplicate the problem by download model from <a href="https://github.com/zjjMaiMai/TinyHITNet/blob/master/models/hit_net_kitti.py" rel="noopener nofollow ugc">here</a> and insert the above torch onnx export code at the end of main.</p>
<p>What am I missing?</p>
<p>Thank you very much for your help again.</p>

youcef4k: <p>Hello,<br>
I am facing the exact same problem, did you find any workaround for this? thanks.</p>

youcef4k: <p>I just found the problem. The model returns a literal int value, for which the tracer used by ONNX export could not find a valid path to the inputs of the model. To make the export work you need to use opset_version 12 and comment this output in this line:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/zjjMaiMai/TinyHITNet/blob/6a78b3453bef927f66f04ca3f8949a15509cf3fc/models/hit_net_kitti.py#L385">
  <header class="source">

      <a href="https://github.com/zjjMaiMai/TinyHITNet/blob/6a78b3453bef927f66f04ca3f8949a15509cf3fc/models/hit_net_kitti.py#L385" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/zjjMaiMai/TinyHITNet/blob/6a78b3453bef927f66f04ca3f8949a15509cf3fc/models/hit_net_kitti.py#L385" target="_blank" rel="noopener nofollow ugc">zjjMaiMai/TinyHITNet/blob/6a78b3453bef927f66f04ca3f8949a15509cf3fc/models/hit_net_kitti.py#L385</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="375" style="counter-reset: li-counter 374 ;">
          <li>h1, cv1, di1, wp1 = self.level[1](lf[-2], rf[-2], hyp_up(h0, 2, 1))</li>
          <li>h2, cv2, di2, wp2 = self.level[2](lf[-3], rf[-3], hyp_up(h1, 2, 1), lf[-1])</li>
          <li>h3, cv3, di3, wp3 = self.level[3](lf[-4], rf[-4], hyp_up(h2, 2, 1), lf[-2])</li>
          <li>h4, cv4, di4, wp4 = self.level[4](lf[-5], rf[-5], hyp_up(h3, 2, 1), lf[-3])</li>
          <li>
          </li>
<li>h5 = self.refine[0](h4, lf[-3])</li>
          <li>h6 = self.refine[1](hyp_up(h5, 1, 2), lf[-4])</li>
          <li>h7 = self.refine[2](hyp_up(h6, 1, 2), lf[-5])[:, :, :h, :w]</li>
          <li>
          </li>
<li>return {</li>
          <li class="selected">    "tile_size": 4,</li>
          <li>    "disp": h7[:, 0:1],</li>
          <li>    "multi_scale": [</li>
          <li>        h0[:, 0:1],</li>
          <li>        h1[:, 0:1],</li>
          <li>        h2[:, 0:1],</li>
          <li>        h3[:, 0:1],</li>
          <li>        h4[:, 0:1],</li>
          <li>        h5[:, 0:1],</li>
          <li>        h6[:, 0:1],</li>
          <li>        h7[:, 0:1],</li>
      </ol>
    </code></pre>



  </article>

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

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