onnx: ONNX shape inference does not infer shapes

Bug Report

Describe the bug

onnx.shape_inference.infer_shapes does not correctly infer shape of each layer.

System information

  • OS Platform and Distribution: Windows 10
  • ONNX version: 1.7.0
  • Python version: 3.7.4

Reproduction instructions

  • Describe the code to reproduce the behavior.
model = onnx.load("models/conv_dummy.onnx")
onnx.checker.check_model(model)
inferred_model = onnx.shape_inference.infer_shapes(model)
print(inferred_model.graph.value_info)

output:

[name: "9"
type {
  tensor_type {
    elem_type: 1
  }
}
, name: "10"
type {
  tensor_type {
    elem_type: 1
  }
}
, name: "11"
type {
  tensor_type {
    elem_type: 1
  }
}
, name: "12"
type {
  tensor_type {
    elem_type: 1
  }
}
, name: "13"
type {
  tensor_type {
    elem_type: 1
  }
}
, name: "14"
type {
  tensor_type {
    elem_type: 1
  }
}
]

Model file: models.zip

Expected behavior

Expected each entry in model.graph.value_info to have tensor shape field which tells me the shape of that layer.

Notes

Model was exported from PyTorch using torch.onnx.export

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 26 (15 by maintainers)

Most upvoted comments

Good workaround. Adding a line add_value_info_for_constants(model) before shape inference runs correctly.

The shape inference problem still persists as lot of shapes are missing when trying to parse the gpt2 onnx model from https://github.com/onnx/models/blob/main/text/machine_comprehension/gpt-2/README.md . I am using onnx version 1.13.1 . Perhaps the problem is about dynamic shape inference. Can anyone suggest any way-out to tackle dynamic shape inference?