onnx / tensorflow-onnx

Convert TensorFlow, Keras, Tensorflow.js and Tflite models to ONNX
Apache License 2.0
2.33k stars 432 forks source link

WARNING - Shape of placeholder image_tensor is unknown, treated it as a scalar #538

Closed mrgloom closed 5 years ago

mrgloom commented 5 years ago

python -m tf2onnx.convert --input ./faceboxes_feature_extractor.pb --output ./faceboxes_feature_extractor.onnx --inputs image_tensor:0 --outputs prediction_layers/box_encoding_predictor_0/BiasAdd:0

2019-05-23 17:05:14,867 - INFO - Using tensorflow=1.12.0, onnx=1.4.1, tf2onnx=1.5.1/0c735a
2019-05-23 17:05:14,868 - INFO - Using opset <onnx, 7>
2019-05-23 17:05:14,868 - WARNING - Shape of placeholder image_tensor is unknown, treated it as a scalar
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 418, in import_graph_def
    graph._c_graph, serialized, options)  # pylint: disable=protected-access
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 4 but is rank 0 for 'conv1/Conv2D' (op: 'Conv2D') with input shapes: [], [7,7,3,24].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/site-packages/tf2onnx/convert.py", line 145, in <module>
    main()
  File "/usr/local/lib/python3.6/site-packages/tf2onnx/convert.py", line 127, in main
    inputs_as_nchw=args.inputs_as_nchw)
  File "/usr/local/lib/python3.6/site-packages/tf2onnx/tfonnx.py", line 699, in process_tf_graph
    tf_graph = infer_shape(tf_graph, shape_override)
  File "/usr/local/lib/python3.6/site-packages/tf2onnx/shape_inference.py", line 33, in infer_shape
    tf_graph = infer_shape_for_graph(tf_graph)
  File "/usr/local/lib/python3.6/site-packages/tf2onnx/shape_inference.py", line 95, in infer_shape_for_graph
    tf_graph = reload_tf_graph(tf_graph)
  File "/usr/local/lib/python3.6/site-packages/tf2onnx/shape_inference.py", line 73, in reload_tf_graph
    tf.import_graph_def(graph_def, name="")
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 422, in import_graph_def
    raise ValueError(str(e))
ValueError: Shape must be rank 4 but is rank 0 for 'conv1/Conv2D' (op: 'Conv2D') with input shapes: [], [7,7,3,24].

Looking at output of summarize_graph:

image_tensor (Placeholder): []
mul (Mul): [image_tensor, mul/y]
standardize_input/mul (Mul): [standardize_input/mul/x, mul]
standardize_input/sub (Sub): [standardize_input/mul, standardize_input/sub/y]
conv1/Conv2D (Conv2D): [standardize_input/sub, conv1/weights]

So looks like it can't estimate shape of input placeholder, but how can I specify it?

In code it's: https://github.com/onnx/tensorflow-onnx/blob/master/tf2onnx/shape_inference.py#L106 https://github.com/onnx/tensorflow-onnx/blob/master/tf2onnx/shape_inference.py#L114

lucienwang1009 commented 5 years ago

Some models specify placeholders with unknown ranks and dims which can not be mapped to onnx. In those cases one can add the shape behind the input name in [], for example --inputs X:0[1,28,28,3]

guschmue commented 5 years ago

I assume this is no longer an issue.

llk2why commented 4 years ago

Some models specify placeholders with unknown ranks and dims which can not be mapped to onnx. In those cases one can add the shape behind the input name in [], for example --inputs X:0[1,28,28,3]

What if the rank is known, but the batchsize, height and width in NHWC(C=3) are unknown? Is it possible to add the shape behind the input name in [], for example -- inputs X:0[-1,-1,-1,3]? I did converted a .pb model to a .onnx model with the input share=[?x224x224x3], but in this case, I have no idea how to convert it.

guschmue commented 4 years ago

We support adding the shape behind the input but the way the regex is the -1 would not work. We can change it to support -1. Depends on the model if this works since we sometimes need to know the shape to calculate attributes. opset-11 or better makes us depend less on knowing the shape.