'dict' object has no attribute 'training'

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

    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:

        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.

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:

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

@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,

        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:

/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 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.

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

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:

1 Like