DEV Community

Hollow Man
Hollow Man

Posted on

Keras Model Errors on Loading using TF2.3 - IndexError: list index out of range

Here is an example to solve similar questions from the issue #43561

When I was 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
Enter fullscreen mode Exit fullscreen mode

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

Then I downgraded to TF 2.2 and 2.1 with the same code above, it threw the error just as #35934 Keras Model Errors on Loading - 'list' object has no attribute 'items'

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: 
Enter fullscreen mode Exit fullscreen mode

Then 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: https://github.com/tensorflow/tensorflow/issues/38589#issuecomment-665930503 . 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.],
......
Enter fullscreen mode Exit fullscreen mode

So this way fails.

Solutions

One way is 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.

Another is to modify the TF 2.3.1 source code so that the model can be used in latest version using tensorflow keras. You have to redefining _should_skip_first_node in file tensorflow/python/keras/engine/functional.py:

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)
Enter fullscreen mode Exit fullscreen mode

Afterwards

I have submitted a PR #43570 to tensorflow, and it get fixed in Tensorflow 2.5.0.

Top comments (0)