onnx / tensorflow-onnx

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

TypeError: object of type 'NoneType' has no len() #1475

Closed neso613 closed 3 years ago

neso613 commented 3 years ago

TypeError: object of type 'NoneType' has no len()

python3 -m tf2onnx.convert --saved-model my_saved_models/ --signature_def 'serving_default' --tag 'serve' --opset 10 --output my.onnx tried different opset also

TomWildenhain-Microsoft commented 3 years ago

Can you upload the model you are trying to convert?

neso613 commented 3 years ago

Can you upload the model you are trying to convert?

Please accept my apologies. I can not share the model. However, I am sharing some details of model- image Thanks!

TomWildenhain-Microsoft commented 3 years ago

Hmm, the signature names isn't going to help much. Can you provide the full stack trace/full console output?

neso613 commented 3 years ago

Hmm, the signature names isn't going to help much. Can you provide the full stack trace/full console output? signature_def['__saved_model_init_op']: The given SavedModel SignatureDef contains the following input(s): The given SavedModel SignatureDef contains the following output(s): outputs['__saved_model_init_op'] tensor_info: dtype: DT_INVALID shape: unknown_rank name: NoOp Method name is:

signature_def['serving_default']: The given SavedModel SignatureDef contains the following input(s): inputs['input_2'] tensor_info: dtype: DT_FLOAT shape: (-1, 256, 121, 1) name: serving_default_input_2:0 The given SavedModel SignatureDef contains the following output(s): outputs['conv1d_151'] tensor_info: dtype: DT_FLOAT shape: (-1, 256, 121) name: StatefulPartitionedCall:0 Method name is: tensorflow/serving/predict

Defined Functions: Function Name: 'call' Option #1 Callable with: Argument #1 inputs: TensorSpec(shape=(None, 256, 121, 1), dtype=tf.float32, name='inputs') Argument #2 DType: bool Value: False Argument #3 DType: NoneType Value: None Option #2 Callable with: Argument #1 input_2: TensorSpec(shape=(None, 256, 121, 1), dtype=tf.float32, name='input_2') Argument #2 DType: bool Value: False Argument #3 DType: NoneType Value: None Option #3 Callable with: Argument #1 input_2: TensorSpec(shape=(None, 256, 121, 1), dtype=tf.float32, name='input_2') Argument #2 DType: bool Value: True Argument #3 DType: NoneType Value: None Option #4 Callable with: Argument #1 inputs: TensorSpec(shape=(None, 256, 121, 1), dtype=tf.float32, name='inputs') Argument #2 DType: bool Value: True Argument #3 DType: NoneType Value: None

Function Name: '_default_save_signature' Option #1 Callable with: Argument #1 input_2: TensorSpec(shape=(None, 256, 121, 1), dtype=tf.float32, name='input_2')

Function Name: 'call_and_return_all_conditional_losses' Option #1 Callable with: Argument #1 input_2: TensorSpec(shape=(None, 256, 121, 1), dtype=tf.float32, name='input_2') Argument #2 DType: bool Value: False Argument #3 DType: NoneType Value: None Option #2 Callable with: Argument #1 input_2: TensorSpec(shape=(None, 256, 121, 1), dtype=tf.float32, name='input_2') Argument #2 DType: bool Value: True Argument #3 DType: NoneType Value: None Option #3 Callable with: Argument #1 inputs: TensorSpec(shape=(None, 256, 121, 1), dtype=tf.float32, name='inputs') Argument #2 DType: bool Value: True Argument #3 DType: NoneType Value: None Option #4 Callable with: Argument #1 inputs: TensorSpec(shape=(None, 256, 121, 1), dtype=tf.float32, name='inputs') Argument #2 DType: bool Value: False Argument #3 DType: NoneType Value: None

TomWildenhain-Microsoft commented 3 years ago

It looks like tf2onnx is calling 'len' on some object that is None. Does the stack trace tell you what line of code this error occurred on? I think you gave me the console output but not the stack trace.

neso613 commented 3 years ago

2021-05-17 14:52:23,241 - INFO - Using tensorflow=2.5.0, onnx=1.9.0, tf2onnx=1.8.5/50049d 2021-05-17 14:52:23,242 - INFO - Using opset <onnx, 11> 2021-05-17 14:52:25,201 - INFO - Computed 0 values for constant folding 2021-05-17 14:52:32,675 - ERROR - Failed to convert node 'StatefulPartitionedCall/model_2/model_1/conv2d_4/BiasAdd_1' (fct=<bound method ConvOp.version_11 of <class 'tf2onnx.onnx_opset.nn.ConvOp'>>) 'OP=Conv\nName=StatefulPartitionedCall/model_2/model_1/conv2d_4/BiasAdd_1\nInputs:\n\tStatefulPartitionedCall/model_2/lambda_1/Slice:0=Slice, None, 1\n\tStatefulPartitionedCall/model_2/model_1/conv2d_4/Conv2D_1/ReadVariableOp:0=Const, [5, 5, 1, 16], 1\n\tStatefulPartitionedCall/model_2/model_1/conv2d_4/BiasAdd_1/ReadVariableOp:0=Const, [16], 1\nOutpus:\n\tStatefulPartitionedCall/model_2/model_1/conv2d_4/BiasAdd_1:0=[-1, 256, 121, 16], 1' Traceback (most recent call last): File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tfonnx.py", line 294, in tensorflow_onnx_mapping func(g, node, kwargs, initialized_tables=initialized_tables, dequantize=dequantize) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/onnx_opset/nn.py", line 386, in version_11 cls.any_version(11, ctx, node, kwargs) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/onnx_opset/nn.py", line 356, in any_version if len(input_shape) == spatial + 1: TypeError: object of type 'NoneType' has no len() 2021-05-17 14:52:32,924 - ERROR - Failed to convert node 'StatefulPartitionedCall/model_2/model_1/conv2d_4/BiasAdd' (fct=<bound method ConvOp.version_11 of <class 'tf2onnx.onnx_opset.nn.ConvOp'>>) 'OP=Conv\nName=StatefulPartitionedCall/model_2/model_1/conv2d_4/BiasAdd\nInputs:\n\tStatefulPartitionedCall/model_2/lambda/Slice:0=Slice, None, 1\n\tStatefulPartitionedCall/model_2/model_1/conv2d_4/Conv2D/ReadVariableOp:0=Const, [5, 5, 1, 16], 1\n\tStatefulPartitionedCall/model_2/model_1/conv2d_4/BiasAdd/ReadVariableOp:0=Const, [16], 1\nOutpus:\n\tStatefulPartitionedCall/model_2/model_1/conv2d_4/BiasAdd:0=[-1, 256, 121, 16], 1' Traceback (most recent call last): File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tfonnx.py", line 294, in tensorflow_onnx_mapping func(g, node, kwargs, initialized_tables=initialized_tables, dequantize=dequantize) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/onnx_opset/nn.py", line 386, in version_11 cls.any_version(11, ctx, node, kwargs) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/onnx_opset/nn.py", line 356, in any_version if len(input_shape) == spatial + 1: TypeError: object of type 'NoneType' has no len() Traceback (most recent call last): File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/convert.py", line 488, in main() File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/convert.py", line 256, in main output_path=args.output) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/convert.py", line 153, in _convert_common g = process_tf_graph(tf_graph, const_node_values=const_node_values, kwargs) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tfonnx.py", line 534, in process_tf_graph output_names, initialized_tables, outputs_to_values, outputs_to_dtypes, op_cnt, attr_cnt) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tfonnx.py", line 639, in process_parsed_graph raise exceptions[0] File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tfonnx.py", line 294, in tensorflow_onnx_mapping func(g, node, kwargs, initialized_tables=initialized_tables, dequantize=dequantize) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/onnx_opset/nn.py", line 386, in version_11 cls.any_version(11, ctx, node, **kwargs) File "/home/geek/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/onnx_opset/nn.py", line 356, in any_version if len(input_shape) == spatial + 1: TypeError: object of type 'NoneType' has no len()

TomWildenhain-Microsoft commented 3 years ago

Let's try #1524 and see if it fixes it.

TomWildenhain-Microsoft commented 3 years ago

The change has merged. @neso613 try the latest tf2onnx from GitHub and let me know if it works.

neso613 commented 3 years ago

The change has merged. @neso613 try the latest tf2onnx from GitHub and let me know if it works.

still have same issue. I have installed latest versions. 2021-05-21 12:20:41,129 - INFO - Using tensorflow=2.5.0, onnx=1.9.0, tf2onnx=1.8.5/50049d

TomWildenhain-Microsoft commented 3 years ago

The issue is caused because the input rank to the conv is unknown. This should be very rare. Do you have any idea why the input rank would be unknown for this model? You can try passing in a shape to the inputs --inputs someinp:0[1,2,3,4]

I would be very surprised if the line number is the same. Do you have a new stack trace?

neso613 commented 3 years ago

The issue is caused because the input rank to the conv is unknown. This should be very rare. Do you have any idea why the input rank would be unknown for this model? You can try passing in a shape to the inputs --inputs someinp:0[1,2,3,4]

I would be very surprised if the line number is the same. Do you have a new stack trace? Getting this error when --inputs someinp:0[1,2,3,4] used .

WARNING:tensorflow:From /home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tf_loader.py:603: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version. Instructions for updating: Use tf.compat.v1.graph_util.extract_sub_graph 2021-05-24 15:08:18,173 - WARNING - From /home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tf_loader.py:603: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version. Instructions for updating: Use tf.compat.v1.graph_util.extract_sub_graph Traceback (most recent call last): File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/convert.py", line 488, in main() File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/convert.py", line 213, in main args.large_model, return_initialized_tables=True, return_tensors_to_rename=True) File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tf_loader.py", line 512, in from_saved_model tag, signatures, concrete_function, large_model) File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tf_loader.py", line 469, in _from_saved_model_v2 frozen_graph = from_function(concrete_func, inputs, outputs, large_model) File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tf_loader.py", line 179, in from_function graph_def = tf_optimize(input_names, output_names, graph_def) File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tf2onnx/tf_loader.py", line 603, in tf_optimize graph_def = extract_sub_graph(graph_def, needed_names) File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 337, in new_func return func(*args, **kwargs) File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tensorflow/python/framework/graph_util_impl.py", line 209, in extract_sub_graph _assert_nodes_are_present(name_to_node, dest_nodes) File "/home/nehasoni/anaconda3/envs/tf2onnx/lib/python3.7/site-packages/tensorflow/python/framework/graph_util_impl.py", line 164, in _assert_nodes_are_present assert d in name_to_node, "%s is not in graph" % d AssertionError: someinp is not in graph

TomWildenhain-Microsoft commented 3 years ago

someinp:0[1,2,3,4] was an example. You will need to replace it with the actual model input name and shape.

guschmue commented 3 years ago

I assume this is resolved.