onnx / tensorflow-onnx

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

"AssertionError: module_apply_default/G_trunc_output is not in graph" converting TFHub BigGAN 128 to Onnx #789

Closed YanYas closed 4 years ago

YanYas commented 4 years ago

Hi, I'm trying to convert this model TFHub. I keep coming across the following above error.

To replicate use the Onnx Conversion Docker download the above model and unzip it to the environment directory

!curl https://storage.googleapis.com/tfhub-modules/deepmind/biggan-deep-128/1.tar.gz -o 1.tar.gz
!tar -zxvf 1.tar.gz

Then run the following conversion command on it

!python3 -m tf2onnx.convert \
--graphdef tfhub_module.pb \
--output Big128.onnx \
--opset 11 --fold_const \
--inputs y:0,z:0,truncation:0 \
            --outputs module_apply_default/G_trunc_output:0

I saw that this issue appeared here too which is why I've tried these specific settings, but no dice.

Any thanks would be great, its pretty urgent

yanyas

jignparm commented 4 years ago

What's the tag_set for this model? I don't see the tag_set 'serve' using saved_model_cli. How did you arrive at the input/output names above?

$ saved_model_cli.exe show --dir saved_model
be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
The given SavedModel contains the following tag-sets:
<empty>
YanYas commented 4 years ago

I opened a colab of the project to find them. Here

At the code cell below 'Load a BigGAN generator module from TF Hub':

tf.reset_default_graph()
print('Loading BigGAN module from:', module_path)
module = hub.Module(module_path)
inputs = {k: tf.placeholder(v.dtype, v.get_shape().as_list(), k)
          for k, v in module.get_input_info_dict().items()}
output = module(inputs)

print()
print('Inputs:\n', '\n'.join(
    '  {}: {}'.format(*kv) for kv in inputs.items()))
print()
print('Output:', output)

returns

Loading BigGAN module from: https://tfhub.dev/deepmind/biggan-128/2
INFO:tensorflow:Saver not created because there are no variables in the graph to restore

INFO:tensorflow:Saver not created because there are no variables in the graph to restore

Inputs:
   y: Tensor("y:0", shape=(?, 1000), dtype=float32)
  z: Tensor("z:0", shape=(?, 120), dtype=float32)
  truncation: Tensor("truncation:0", shape=(), dtype=float32)

Output: Tensor("module_apply_default/G_trunc_output:0", shape=(?, 128, 128, 3), dtype=float32)
YanYas commented 4 years ago

@jignparm did you see any problems with this setup?

jignparm commented 4 years ago

Sorry for the delay -- will reply back shortly on this.

jignparm commented 4 years ago

The output name is a bit different that the one above.

However, i'm still seeing an error, even after fixing the output name. The error is coming TF2ONNX loads the graphdef via tf.Session(config=tf.ConfigProto(allow_soft_placement=True), graph=g).

Investigating.

Command line

python -m tf2onnx.convert                                                                                                                                                                               
 --saved-model /d/modelsbiggan/saved_model                                                                                                                                                          
 --output Big128.onnx                                                                                                                                                                                   
 --opset 11                                                                                                                                                                                              
 --fold_const                                                                                                                                                                                            
 --inputs y:0,z:0,truncation:0                                                                                                                                                                           
 --outputs G_trunc_output:0

Logs

2020-02-11 03:05:04.563227: E tensorflow/tools/graph_transforms/transform_graph.cc:332] fold_constants: Ignoring error Input 0 of node cond/AssignVariableOp/Switch was passed float from prev_truncation:0 incompatible with expected resource.
Traceback (most recent call last):
  File "D:\anaconda37\lib\site-packages\tensorflow\python\framework\importer.py", line 427, in import_graph_def
    graph._c_graph, serialized, options)  # pylint: disable=protected-access
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input 0 of node cond/AssignVariableOp/Switch was passed float from prev_truncation:0 incompatible with expected resource.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\anaconda37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "D:\anaconda37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\jignparmtf2onnx\tf2onnx\convert.py", line 161, in <module>
    main()
  File "D:\jignparmtf2onnx\tf2onnx\convert.py", line 134, in main
    tf.import_graph_def(graph_def, name='')
  File "D:\anaconda37\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "D:\anaconda37\lib\site-packages\tensorflow\python\framework\importer.py", line 431, in import_graph_def
    raise ValueError(str(e))
ValueError: Input 0 of node cond/AssignVariableOp/Switch was passed float from prev_truncation:0 incompatible with expected resource.
guschmue commented 4 years ago

AssignVariableOp makes me think there are Variables in the graph that we could not make a constant and this specific op that assigns something to it.

jignparm commented 4 years ago

Thanks @guschmue . It looks like the graph cannot be loaded after freezing, and the error message above sounds like there are Variables remaining after freezing the graph.

There is probably a way detect this condition, and throw a friendly error instead of the error above.

@YanYas, since there are Variable operators remaining in the graph (after freezing), it does not look like this model can be easily converted to ONNX format, since ONNX does not support Variable operators. It's a known limitation, but the error messages can be made clearer.

YanYas commented 4 years ago

Nightmare, but good to know. Thank you both.