tensorflow-onnx
tensorflow-onnx copied to clipboard
ValueError: Input 0 of node save/AssignVariableOp was passed int32 from Variable:0 incompatible with expected resource.
Dear All,
I would like to report the issue when converting tensorflow model(ckpt) to onnx model
Describe the bug
I got a ckpt model on TF1 , then transformed it to .pb model by graph_util.convert_variables_to_constants in the environment of TF2 , when i used python -m tf2onnx.convert command to convert it to .onnx , i got the ERROR:
D:\evn\Python39\lib\runpy.py:127: 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
warn(RuntimeWarning(msg))
Traceback (most recent call last):
File "D:\code\python\pycharmProject\PytorchProj\venv\lib\site-packages\tensorflow\python\framework\importer.py", line 499, in _import_graph_def_internal
results = c_api.TF_GraphImportGraphDefWithResults(
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input 0 of node save/AssignVariableOp was passed int32 from Variable:0 incompatible with expected resource.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\evn\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\evn\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "D:\code\python\pycharmProject\PytorchProj\venv\lib\site-packages\tf2onnx\convert.py", line 696, in <module>
main()
File "D:\code\python\pycharmProject\PytorchProj\venv\lib\site-packages\tf2onnx\convert.py", line 229, in main
graph_def, inputs, outputs = tf_loader.from_graphdef(args.graphdef, args.inputs, args.outputs)
File "D:\code\python\pycharmProject\PytorchProj\venv\lib\site-packages\tf2onnx\tf_loader.py", line 359, in from_graphdef
tf.import_graph_def(graph_def, name='')
File "D:\code\python\pycharmProject\PytorchProj\venv\lib\site-packages\tensorflow\python\util\deprecation.py", line 561, in new_func
return func(*args, **kwargs)
File "D:\code\python\pycharmProject\PytorchProj\venv\lib\site-packages\tensorflow\python\framework\importer.py", line 403, in import_graph_def
return _import_graph_def_internal(
File "D:\code\python\pycharmProject\PytorchProj\venv\lib\site-packages\tensorflow\python\framework\importer.py", line 504, in _import_graph_def_internal
raise ValueError(str(e))
ValueError: Input 0 of node save/AssignVariableOp was passed int32 from Variable:0 incompatible with expected resource.
A clear and concise description of what the bug is.
System information
- Window 10
- tensorflow-gpu 2.9.1
- tf2onnx 1.11.1
To Reproduce
1/ got a .pbmodel from ckpt model with the code:
import tensorflow.compat.v1 as tf
from tensorflow.python.framework import graph_util
tf.disable_eager_execution()
#[0]nodename:Placeholder [-1]nodename:save/restore_all
def freeze_graph():
input_checkpoint = "./saved_model/dense121/ckpttest/af.ckpt"
output_node_names = "save/restore_all"
saver = tf.train.import_meta_graph("./saved_model/dense121/ckpttest/af.ckpt.meta",
clear_devices=True)
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
with tf.Session() as sess:
saver.restore(sess, input_checkpoint)
output_graph_def = graph_util.convert_variables_to_constants(
sess,
input_graph_def,
output_node_names.split(",")
)
with tf.gfile.GFile("./saved_model/dense121/pb_model/af.pb", "wb") as f:
f.write(output_graph_def.SerializeToString())
freeze_graph()
2/ run command to convert .pb model to onnx model:
python -m tf2onnx.convert --input af.pb --inputs Placeholder:0 --outputs save/restore_all:0 --output af.onnx --opset 11
Additional context I know this issue is similar to 1152#issue-732119197 , and in my model , i do used tf.Variable , but i don't know how to freeze it . BTW , the code how i saved model as ckpt is below:
saver = tf.train.Saver()
saver.save(sess , "./af.ckpt")
tf2onnx.convert also supports convert a checkpoint to ONNX. Could you please try to upgrade your TF1 version to 1.15.0 which is covered by our CI and use --checkpoint parameter to convert this checkpoint without converting it to a pb file?
Thank you for the advice ! My superiors changed their strategy , so I avoided this question.