tensorflow-onnx icon indicating copy to clipboard operation
tensorflow-onnx copied to clipboard

AttributeError: 'FuncGraph' object has no attribute '_captures'

Open rgaufman opened this issue 2 years ago • 14 comments

Describe the bug I installed tf2onnx.convert with (I also tried the stable version, same result):

pip uninstall tf2onnx
pip3 install git+https://github.com/onnx/tensorflow-onnx

When I run it, I see:

$ python3 -m tf2onnx.convert --opset 10 \
  --saved-model ssd_mobilenet_v1_coco_2018_01_28/saved_model \
  --output model.onnx
<frozen runpy>:128: RuntimeWarning: 'tf2onnx.convert' found in sys.modules after import of package 'tf2onnx', but prior to execution of 'tf2onnx.convert'; this may result in unpredictable behaviour
2023-05-10 18:55:25,257 - WARNING - '--tag' not specified for saved_model. Using --tag serve
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
2023-05-10 18:55:25,739 - INFO - Saver not created because there are no variables in the graph to restore
2023-05-10 18:55:26,926 - INFO - Fingerprint not found. Saved model loading will continue.
2023-05-10 18:55:26,927 - INFO - Signatures found in model: [serving_default].
2023-05-10 18:55:26,927 - WARNING - '--signature_def' not specified, using first signature: serving_default
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/opt/homebrew/lib/python3.11/site-packages/tf2onnx/convert.py", line 710, in <module>
    main()
  File "/opt/homebrew/lib/python3.11/site-packages/tf2onnx/convert.py", line 242, in main
    graph_def, inputs, outputs, initialized_tables, tensors_to_rename = tf_loader.from_saved_model(
                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/tf2onnx/tf_loader.py", line 611, in from_saved_model
    _from_saved_model_v2(model_path, input_names, output_names,
  File "/opt/homebrew/lib/python3.11/site-packages/tf2onnx/tf_loader.py", line 573, in _from_saved_model_v2
    graph_captures = concrete_func.graph._captures  # pylint: disable=protected-access
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'FuncGraph' object has no attribute '_captures'. Did you mean: 'captures'?

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 18.04*): MacOS 13.3.1 on Apple Silicon M1
  • TensorFlow Version: 2.13.0rc0
  • Python version: 3.11.3

To Reproduce I'm following the instructions here: https://ankane.org/tensorflow-ruby - I downloaded this pre-trained model: https://storage.googleapis.com/download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2018_01_28.tar.gz and trying to convert it to onnx format.

Unfortunately I am not able to install any other versions of tensorflow, only 2.13.0rc0 appears to be available:

$ pip3 install tensorflow==2.11.1
ERROR: Could not find a version that satisfies the requirement tensorflow==2.11.1 (from versions: 2.13.0rc0)
ERROR: No matching distribution found for tensorflow==2.11.1

rgaufman avatar May 10 '23 17:05 rgaufman

This looks like an issue of tensorflow.

Could you please lower down your python version to 3.9 so that you can install different versions of tensorflow for a retry?

fatcat-z avatar May 25 '23 11:05 fatcat-z

We have a dependency on python 3.11 for a few other things, is support for this version of python and newer versions of TensorFlow something that's in the works?

rgaufman avatar May 25 '23 13:05 rgaufman

This is a compatibility issue between Python and TensorFlow 2.11.1, so tf2onnx can't help on it. Do you mind to try a newer TensorFlow version on Python 3.11?

fatcat-z avatar May 26 '23 02:05 fatcat-z

I believe 2.13.0rc0 is the latest? (it is also the only version selectable in pip3 install)

rgaufman avatar May 26 '23 10:05 rgaufman

I believe 2.13.0rc0 is the latest? (it is also the only version selectable in pip3 install)

Yes, it should be the latest rc version.

fatcat-z avatar May 26 '23 11:05 fatcat-z

That's the one I used above in the issue report. It's the only one that's available for python 3.11.

rgaufman avatar May 26 '23 21:05 rgaufman

I have the same problem; my Tensorflow version is 2.13.0 (Python 3.9).

guoqingbao avatar Jul 13 '23 06:07 guoqingbao

@rgaufman This problem can be fixed by replacing:

graph_captures = concrete_func.graph._captures

to

    if hasattr(concrete_func.graph, "captures"):
        graph_captures = concrete_func.graph.captures  
        captured_inputs = [t_name.name for t_val, t_name in graph_captures]
    else:
        graph_captures = concrete_func.graph._captures  
        captured_inputs = [t_name.name for t_val, t_name in graph_captures.values()]

guoqingbao avatar Jul 13 '23 06:07 guoqingbao

I got the same problem. It worked when I decreased the tensorflow version to 2.12.1

David-Leroye avatar Jul 19 '23 15:07 David-Leroye

@rgaufman were you able to find a solution?

carlos21 avatar Jul 27 '23 18:07 carlos21

@guoqingbao Do you consider a pull request for your fix? I discovered this after posting another fix for the same issue at https://github.com/onnx/tensorflow-onnx/issues/2180#issuecomment-1613201326, and your fix looks better than mine.

yan12125 avatar Aug 07 '23 05:08 yan12125

@guoqingbao Do you consider a pull request for your fix? I discovered this after posting another fix for the same issue at #2180 (comment), and your fix looks better than mine.

Sure, I have created a pull request to fix this issue.

guoqingbao avatar Aug 07 '23 06:08 guoqingbao

I got the same problem. It worked when I decreased the tensorflow version to 2.12.1

This worked for me. For someone who is converting Transformers to ONNX with Hugging Face Optimum:

  • Tensorflow 2.12.1
  • Numpy: 1.23.0

tuannguyen90 avatar Aug 17 '23 04:08 tuannguyen90

Awesome work at https://github.com/onnx/tensorflow-onnx/pull/2216 has been merged. The next tensorflow-onnx version should be compatible with tensorflow 2.13.

This issue can probably be closed?

EDIT: https://github.com/onnx/tensorflow-onnx/issues/2180 cannot be closed yet, as flatbuffers is still incompatible

yan12125 avatar Aug 19 '23 04:08 yan12125