# 'dict' object has no attribute 'training'

**URL:** <https://discuss.pytorch.org/t/dict-object-has-no-attribute-training/154840>\
**Category:** Uncategorized\
**Created:** [June 22, 2022, 11:21pm UTC](https://discuss.pytorch.org/t/dict-object-has-no-attribute-training/154840 "2022-06-22T23:21:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ynjiun\_wang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ynjiun_wang/32/32014_2.png) [@ynjiun\_wang](https://discuss.pytorch.org/u/ynjiun_wang)\
**Post date:** [June 22, 2022, 11:21pm UTC](https://discuss.pytorch.org/t/dict-object-has-no-attribute-training/154840/1 "2022-06-22T23:21:17Z")

</div>

I tried to convert a pytorch model .pt file into a onnx by first build the model:

```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)

```

After the model built and successfully loaded, I run the following code:

```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,
        )

```

And I got the following error message:

> /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]  
> Traceback (most recent call last):  
> File “/home/paul/pytorch/TinyHITNet/predict.py”, line 108, in   
> 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 709, in \_export  
> with select\_model\_mode\_for\_export(model, training):  
> File “/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/contextlib.py”, line 113, in **enter**  
> return next(self.gen)  
> File “/home/paul/anaconda3/envs/pytorch3d/lib/python3.8/site-packages/torch/onnx/utils.py”, line 42, in select\_model\_mode\_for\_export  
> is\_originally\_training = model.training  
> AttributeError: ‘dict’ object has no attribute ‘training’

What am I missing? could you please help? Thank you.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 23, 2022, 4:01am UTC](https://discuss.pytorch.org/t/dict-object-has-no-attribute-training/154840/2 "2022-06-23T04:01:44Z")

</div>

Based on the error message it seems you are not passing a valid `nn.Module` as the model to `export` but a `dict`.  
The `.training` attribute query seems to be failing for:

```python
model.training
AttributeError: ‘dict’ object has no attribute ‘training’

```

---

<div class="post-metadata">

**Author:** ![ynjiun\_wang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ynjiun_wang/32/32014_2.png) [@ynjiun\_wang](https://discuss.pytorch.org/u/ynjiun_wang)\
**Post date:** [June 29, 2022, 10:44pm UTC](https://discuss.pytorch.org/t/dict-object-has-no-attribute-training/154840/3 "2022-06-29T22:44:54Z")

</div>

@ptrblck

Thank you for your help. I tried again by creating the model directly:

`model = HITNet_KITTI()`

Then I tried to access

`model.training`

and it response

`True`

Then tried to export onnx,

```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,
        )

```

Now it encounter the following errors:

```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 <module>
    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.

```

You may duplicate the problem by download model from [here](https://github.com/zjjMaiMai/TinyHITNet/blob/master/models/hit_net_kitti.py) and insert the above torch onnx export code at the end of main.

What am I missing?

Thank you very much for your help again.

---

<div class="post-metadata">

**Author:** ![youcef4k](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/youcef4k/32/44889_2.png) [@youcef4k](https://discuss.pytorch.org/u/youcef4k)\
**Post date:** [July 28, 2022, 3:46pm UTC](https://discuss.pytorch.org/t/dict-object-has-no-attribute-training/154840/4 "2022-07-28T15:46:40Z")

</div>

Hello,  
I am facing the exact same problem, did you find any workaround for this? thanks.

---

<div class="post-metadata">

**Author:** ![youcef4k](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/youcef4k/32/44889_2.png) [@youcef4k](https://discuss.pytorch.org/u/youcef4k)\
**Post date:** [July 29, 2022, 7:24am UTC](https://discuss.pytorch.org/t/dict-object-has-no-attribute-training/154840/5 "2022-07-29T07:24:41Z")

</div>

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:

> <https://github.com/zjjMaiMai/TinyHITNet/blob/6a78b3453bef927f66f04ca3f8949a15509cf3fc/models/hit_net_kitti.py#L385>
