tensorflow: "IndexError: list index out of range" when load EfficientDet SavedModel with tf.keras

Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): 2.3.0
  • Python version: 3.6
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version: No
  • GPU model and memory: No

You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with:

  1. TF 1.0: python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
  2. TF 2.0: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the current behavior

I try to load SavedModel efficientDetD0 with load_model of keras:

model = tf.keras.models.load_model("saved_model")' 'model.summary()

And following error occur:

Traceback (most recent call last): File “E:/Detection/test_infer_model.py”, line 129, in <module> model = tf.keras.models.load_model(“saved_model”) File “C:\Users\Luxury\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\saving\save.py”, line 187, in load_model return saved_model_load.load(filepath, compile, options) File “C:\Users\Luxury\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py”, line 121, in load path, options=options, loader_cls=KerasObjectLoader) File “C:\Users\Luxury\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\saved_model\load.py”, line 633, in load_internal ckpt_options) File “C:\Users\Luxury\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py”, line 194, in init super(KerasObjectLoader, self).init(*args, **kwargs) File “C:\Users\Luxury\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\saved_model\load.py”, line 130, in init self._load_all() File “C:\Users\Luxury\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py”, line 221, in _load_all self._finalize_objects() File “C:\Users\Luxury\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py”, line 526, in _finalize_objects _finalize_saved_model_layers(layers_revived_from_saved_model) File “C:\Users\Luxury\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py”, line 706, in _finalize_saved_model_layers inputs = infer_inputs_from_restored_call_function(call_fn) File “C:\Users\Luxury\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py”, line 982, in infer_inputs_from_restored_call_function spec = fn.concrete_functions[0].structured_input_signature[0][0] IndexError: list index out of range **Standalone code to reproduce the issue ** link colab: https://colab.research.google.com/drive/1fR8nu3INEWVUJJhex3Dline10296vxri?usp=sharing and model: https://drive.google.com/file/d/18D8NQp9zP4UW06jeG_75YQWyB531CWUj/view?usp=sharing I lost 2 days for searching on internet, stackoverflow even this repo’s issue but no result, please help !

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 21 (5 by maintainers)

Most upvoted comments

There’s a workaround works for me that no need to change your tf version:

tf version: 2.4.0, but I think others would be ok.

My model settings: Model_type: keras subclassed model Save_way: used model.save() to save Load_way: used tf.keras.models.load_model() to load.

MY Error raised in: tensorflow\python\keras\saving\saved_model\load.py line_1067 (or around)

I changed the original code:

  spec = fn.concrete_functions[0].structured_input_signature[0][0]
  for concrete in fn.concrete_functions[1:]:
    spec2 = concrete.structured_input_signature[0][0]
    spec = nest.map_structure(common_spec, spec, spec2)
  return spec

to:

  spec = None
  if len(fn.concrete_functions) > 0:
    spec = fn.concrete_functions[0].structured_input_signature[0][0]
  for concrete in fn.concrete_functions[1:]:
    spec2 = concrete.structured_input_signature[0][0]
    spec = nest.map_structure(common_spec, spec, spec2)
  return spec

What I met was a error induced by the fn.concrete_functions which lets the code run twice, I haven’t dig into python and tensorflow so I don’t know how could it be like this, but the first time the above part has a length of 4 (in my case), while at the second time it has a length of 0. So here maybe where the question is.

I used

print(len(fn.concrete_functions))
print(type(fn.concrete_functions))

for test, it turns out to be:

4, list, 0, list

roughly, but just like this. (If anyone know what’s this is, I’d appreciate it if you tell me sth about it : )

Wish I could help you guys to solve your problem.

I am also getting the same error with models:

  • SSD with Resnet 50 v1 FPN feature extractor, shared box predictor and focal loss (a.k.a Retinanet).
  • SSD with EfficientNet-b4 + BiFPN feature extractor, shared box predictor and focal loss (a.k.a EfficientDet-d4)

The config files are the same the ones in: models/research/object_detection/configs/tf2/

I first train with: models/research/object_detection/model_main_tf2.py

And then I export with: models/research/object_detection/exporter_main_v2.py

But then when I try to load the exported model I get the error:

Traceback (most recent call last):
  File "/Users/harrythomas/projects/xrfiber_effdet/pb_to_coreml.py", line 82, in <module>
    main()
  File "/Users/harrythomas/projects/xrfiber_effdet/pb_to_coreml.py", line 22, in main
    model = tf.keras.models.load_model(PATH_TO_MODEL, compile=False)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py", line 187, in load_model
    return saved_model_load.load(filepath, compile, options)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 120, in load
    model = tf_load.load_internal(
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 632, in load_internal
    loader = loader_cls(object_graph_proto, saved_model_proto, export_dir,
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 194, in __init__
    super(KerasObjectLoader, self).__init__(*args, **kwargs)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 130, in __init__
    self._load_all()
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 221, in _load_all
    self._finalize_objects()
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 526, in _finalize_objects
    _finalize_saved_model_layers(layers_revived_from_saved_model)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 706, in _finalize_saved_model_layers
    inputs = infer_inputs_from_restored_call_function(call_fn)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 982, in infer_inputs_from_restored_call_function
    spec = fn.concrete_functions[0].structured_input_signature[0][0]
IndexError: list index out of range

System Information:

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 18.04
  • Mobile device name if the issue happens on a mobile device:
  • TensorFlow installed from (source or binary): pip
  • TensorFlow version (use command below): 2.3.1
  • Python version: 3.8.3
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version: CUDA 10.1.243, cuDNN 7.6.4
  • GPU model and memory: 2x 1080 Ti 10Gb

Code:

Import tensorflow as tf
import object_detection.models.keras_models.resnet_v1
import object_detection.models.feature_map_generators
PATH_TO_MODEL = ‘./saved_model/‘
model = tf.keras.models.load_model(PATH_TO_MODEL)

I can load and make predictions with

model = tf.saved_model.load(PATH_TO_MODEL)
tf_out = model(inputs)

However I wish to load the saved model so that I can edit it and then export it with coreML.

Also fails with another model:

import os
import tensorflow as tf
path = os.path.join('.', 'models', 'ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8', 'saved_model')
base_model = tf.keras.models.load_model(path)

TF 2.3.1