Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TF1 Keras Model Errors on Loading using TF2 - IndexError: list index out of range #43561

Closed
HollowMan6 opened this issue Sep 25, 2020 · 9 comments
Assignees
Labels
TF 2.3 Issues related to TF 2.3 type:bug Bug

Comments

@HollowMan6
Copy link
Contributor

HollowMan6 commented Sep 25, 2020

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 18.04
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): 2.3.1
  • Python version: 3.7.3

Describe the current behavior

When When trying to load the sequential model here using tf.keras.models.load_model in TF 2.3.1, an error is thrown at the following location:

~/.local/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in _should_skip_first_node(layer)
   1031   return (isinstance(layer, Functional) and
   1032           # Filter out Sequential models without an input shape.
-> 1033           isinstance(layer._layers[0], input_layer_module.InputLayer))
   1034 
   1035 
IndexError: list index out of range

The model is believed to be trained using keras and under TF1.9, and the structure of the model can be found here, and here's the code for training.

Here you can find the full stack trace and running code under TF 2.3.1: https://colab.research.google.com/drive/1Lfo0O7D0cM8EtR0h6noqCoWqoqf8bzAD?usp=sharing

Then I downgraded to TF 2.2 and 2.1 with the same code above, it threw the error just as #35934 .

Then I downgraded to TF 2.0, the code was executing indefinitely. Finally I had to manually stop it:

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/pywrap_tensorflow_internal.py in IsMapping(o)
   2569 
   2570     """
-> 2571     return _pywrap_tensorflow_internal.IsMapping(o)
   2572 
   2573 def IsMappingView(o):
KeyboardInterrupt: 

Here you can find the full stack trace when I stopped the code under TF 2.0: https://colab.research.google.com/drive/1fCR-ci05NuYhQ8M9O2lRVG0F0YzI9Ggo?usp=sharing

Then I had no choice but to use TF 1.15.4 and Keras 2.3.1, and finally it worked out fine, inputs, outputs, summary etc. are all parsed correctly, as well as being able to run data through the model: https://colab.research.google.com/drive/1XaRMeiT1SefS6Q10wsa0y9rEercyFlCR?usp=sharing

Describe the expected behavior

I hope the Bug can be resolved so that TF2 can enhance the support for old models.

@HollowMan6 HollowMan6 added the type:bug Bug label Sep 25, 2020
@HollowMan6 HollowMan6 changed the title TF1 Keras Model Errors on Loading - IndexError: list index out of range TF1 Keras Model Errors on Loading using TF2 - IndexError: list index out of range Sep 25, 2020
@Saduf2019 Saduf2019 added the TF 2.3 Issues related to TF 2.3 label Sep 25, 2020
@bhack
Copy link
Contributor

bhack commented Sep 25, 2020

I don't see your model definition but please double check our compatibility checklist:
https://www.tensorflow.org/guide/versions#compatibility_of_savedmodels_graphs_and_checkpoints

@HollowMan6
Copy link
Contributor Author

I don't see your model definition but please double check our compatibility checklist:
https://www.tensorflow.org/guide/versions#compatibility_of_savedmodels_graphs_and_checkpoints

Thanks for your suggestion. It's a Keras Model, and I don't believe the compatibility checklist includes my issue. The model definition is here, and here's the code for training.

@bhack
Copy link
Contributor

bhack commented Sep 25, 2020

I think you can try to upgrade your code:
https://www.tensorflow.org/guide/upgrade

Also in the model definition you was using from keras import but keras in Tensorflow is now from tensorflow.keras import

@HollowMan6
Copy link
Contributor Author

I think you can try to upgrade your code:
https://www.tensorflow.org/guide/upgrade

Also in the model definition you was using from keras import but keras in Tensorflow is now from tensorflow.keras import

Thank you for your quick reply. I know the code needs to upgrade since it was the code for TF1, but what I'm asking is that can I just reuse in TF2 the model model_6_combined_2_DA.h5 trained in TF1? When I was trying to load the model in TF2.3.1 with the following code:

from tensorflow.keras.models import load_model
model = load_model("model_6_combined_2_DA.h5")
print(model.summary())

An error was thrown:

~/.local/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in _should_skip_first_node(layer)
   1031   return (isinstance(layer, Functional) and
   1032           # Filter out Sequential models without an input shape.
-> 1033           isinstance(layer._layers[0], input_layer_module.InputLayer))
   1034 
   1035 
IndexError: list index out of range

Other details can be found in the description part above.

@bhack
Copy link
Contributor

bhack commented Sep 25, 2020

You can try to use Keras instead of tf.keras for model loading:
from keras.models import load_model but with a TF version that is compatible with Keras 2.3.1

@HollowMan6
Copy link
Contributor Author

You can try to use Keras instead of tf.keras for model loading:
from keras.models import load_model but with a TF version that is compatible with Keras 2.3.1

Well, I have tried to use keras instead of tf.keras with TF 2.3.1 and Keras 2.3.1, first I encountered an error that can be solved in this way: #38589 (comment) . Then another error occurs:

~/.local/lib/python3.7/site-packages/tensorflow/python/keras/backend.py in function(inputs, outputs, updates, name, **kwargs)
   3931     if updates:
   3932       raise ValueError('`updates` argument is not supported during '
-> 3933                        'eager execution. You passed: %s' % (updates,))
   3934     from tensorflow.python.keras import models  # pylint: disable=g-import-not-at-top
   3935     from tensorflow.python.keras.utils import tf_utils  # pylint: disable=g-import-not-at-top

ValueError: `updates` argument is not supported during eager execution. You passed: [<tf.Variable 'UnreadVariable' shape=() dtype=int64, numpy=0>, <tf.Variable 'UnreadVariable' shape=(3, 3, 3, 32) dtype=float32, numpy=
array([[[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0.],
......

The gist is here: https://colab.research.google.com/drive/1OovMHVrMBsIwcwn2PUcgbEXHUfPLMdyM?usp=sharing

Any suggestions?

@HollowMan6
Copy link
Contributor Author

HollowMan6 commented Sep 25, 2020

from tensorflow.keras.models import load_model
model = load_model("model_6_combined_2_DA.h5")
print(model.summary())

An error was thrown:

~/.local/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in _should_skip_first_node(layer)
   1031   return (isinstance(layer, Functional) and
   1032           # Filter out Sequential models without an input shape.
-> 1033           isinstance(layer._layers[0], input_layer_module.InputLayer))
   1034 
   1035 
IndexError: list index out of range

Other details can be found in the description part above.

I am able to fix this issue when replace the code into following:

def _should_skip_first_node(layer):
  """Returns True if the first layer node should not be saved or loaded."""
  # Networks that are constructed with an Input layer/shape start with a
  # pre-existing node linking their input to output. This node is excluded from
  # the network config.
  if layer._layers:
    return (isinstance(layer, Functional) and
          # Filter out Sequential models without an input shape.
          isinstance(layer._layers[0], input_layer_module.InputLayer))
  else:
    return isinstance(layer, Functional)

Can you suggest if there's some side effects? if no, I will make a pull request.

Here is the gist:https://colab.research.google.com/drive/1jmp77GoIdZ8wy-hplmvq4MqgMRkSePsK?usp=sharing

@bhack
Copy link
Contributor

bhack commented Sep 25, 2020

As it is not a bug I suggest you to close this and post you example in https://stackoverflow.com/questions/tagged/tensorflow

@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TF 2.3 Issues related to TF 2.3 type:bug Bug
Projects
None yet
Development

No branches or pull requests

3 participants