1.问题描述
如标题所述,使用tf2onnx将tensorflow模型转换为onnx时候报错,AttributeError: 'FuncGraph' object has no attribute '_captures'。
python版本为3.9,tensorflow版本为2.13,运行的转换命令为:
python -m tf2onnx.convert --saved-model keras_graph_model --output model.onnx --opset 11 --verbose
2.解决方案
解决的方案有两个,其中一个是tensorflow和python的版本不匹配导致的,需要更换其对应正确的版本。
另一个较为简单快捷的方式是修改tf2onnx文件中的代码。github中的解决方案如下图所示:
实际操作如下。在tf_loader.py文件中修改如下代码:
graph_captures = concrete_func.graph.captures
captured_inputs = [t_name.name for _, t_name in graph_captures.values()]
修改为:
graph_captures = concrete_func.graph.captures
captured_inputs = [t_name.name for t_val, t_name in graph_captures]