1

The documentation states that keras.model.save("model_name") will save the model in the form of two files (1) saved_model.pb - containing the configuration and (2) variables - containing the model weights.

The model that is downloaded from object detection api of tensorflow also has this files. So when I use this directory to load the model,

keras.models.load_model("/content/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8/saved_model")

I run into errors of this sort

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:1045: UserWarning: google3.third_party.tensorflow.python.ops.nn_ops is not loaded, but a Lambda layer uses it. It may cause errors.
  , UserWarning)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:1045: UserWarning: google3.third_party.tensorflow_models.object_detection.models.feature_map_generators is not loaded, but a Lambda layer uses it. It may cause errors.
  , UserWarning)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-20-b099c035cea7> in <module>()
----> 1 loaded_model = keras.models.load_model("/content/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8/saved_model")
      2 # loaded_model = tf.saved_model.load("/content/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8/saved_model")

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile, options)
    185     if isinstance(filepath, six.string_types):
    186       loader_impl.parse_saved_model(filepath)
--> 187       return saved_model_load.load(filepath, compile, options)
    188 
    189   raise IOError(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in load(path, compile, options)
    119 
    120   model = tf_load.load_internal(
--> 121       path, options=options, loader_cls=KerasObjectLoader)
    122 
    123   # pylint: disable=protected-access

/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py in load_internal(export_dir, tags, options, loader_cls)
    631       try:
    632         loader = loader_cls(object_graph_proto, saved_model_proto, export_dir,
--> 633                             ckpt_options)
    634       except errors.NotFoundError as err:
    635         raise FileNotFoundError(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in __init__(self, *args, **kwargs)
    192     self._models_to_reconstruct = []
    193 
--> 194     super(KerasObjectLoader, self).__init__(*args, **kwargs)
    195 
    196     # Now that the node object has been fully loaded, and the checkpoint has

/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py in __init__(self, object_graph_proto, saved_model_proto, export_dir, ckpt_options)
    128       self._concrete_functions[name] = _WrapperFunction(concrete_function)
    129 
--> 130     self._load_all()
    131     self._restore_checkpoint()
    132 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in _load_all(self)
    219 
    220     # Finish setting up layers and models. See function docstring for more info.
--> 221     self._finalize_objects()
    222 
    223   @property

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in _finalize_objects(self)
    524         layers_revived_from_saved_model.append(node)
    525 
--> 526     _finalize_saved_model_layers(layers_revived_from_saved_model)
    527     _finalize_config_layers(layers_revived_from_config)
    528 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in _finalize_saved_model_layers(layers)
    704         call_fn = _get_keras_attr(layer).call_and_return_conditional_losses
    705         if call_fn.input_signature is None:
--> 706           inputs = infer_inputs_from_restored_call_function(call_fn)
    707         else:
    708           inputs = call_fn.input_signature[0]

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in infer_inputs_from_restored_call_function(fn)
    980     return tensor_spec.TensorSpec(defun.common_shape(x.shape, y.shape),
    981                                   x.dtype, x.name)
--> 982   spec = fn.concrete_functions[0].structured_input_signature[0][0]
    983   for concrete in fn.concrete_functions[1:]:
    984     spec2 = concrete.structured_input_signature[0][0]

IndexError: list index out of range

Are there any alternative ways to perform this conversion.

Tensorflow version: 2.3.0

Model downloaded from model source

Harish Babu
  • 311
  • 2
  • 9
  • you cant load the model has been trained on TPU if you don't have a TPU connect to your machine. Try to load another model – dtlam26 Sep 23 '20 at 01:52
  • @dtlam26 I tried to load the model after changing the runtime to tpu on colab, but still getting the same error. Also tried to load a gpu trained model with gpu runtime, that's raising some other error like `TypeError: 'module' object is not callable` – Harish Babu Sep 23 '20 at 04:04
  • it is clear that you load the model incorrect, the correct pattern should be new_model = tf.keras.models.load_model('saved_model/my_model'). – dtlam26 Sep 23 '20 at 04:11
  • 1
    `loaded_model = keras.models.load_model("/content/faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8/saved_model")`. This is what I'm trying.The 'saved_model' folder has a folder called variable with two files 'variables.data-00000-of-00001' and 'variables.index', and a file called 'saved_model.pb'. This exactly what is produced when I run model.save('my_model') on a keras model except that it gives an additional folder called 'assets'. – Harish Babu Sep 23 '20 at 04:36
  • can you try "/content/faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8/saved_model/saved_model" ? – dtlam26 Sep 23 '20 at 04:45
  • "saved_model" is the folder that contains the variables folder and the protobuf file. There is no another "saved_model" folder within that. So I don't think that's the issue. The only thing the "saved_model" folder missing is an "assets" folder, which I think is not so necessary for loading a model. – Harish Babu Sep 23 '20 at 04:50
  • the asset folder is not necessary. The saved model is the graph without weights and the variables is automatically map to the saved_model pb when you load the saved_model. I mean you add a saved_model because that is the name of your pb file. – dtlam26 Sep 23 '20 at 04:54
  • 1
    `loaded_model = keras.models.load_model("/content/faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8/saved_model/saved_model")`. This isn't working either. Getting this error, `OSError: SavedModel file does not exist at: /content/faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8/saved_model/saved_model/{saved_model.pbtxt|saved_model.pb}`. Tried ".../saved_model.pb" as well, its also not working – Harish Babu Sep 23 '20 at 05:03
  • 1
    I think you should append the version of tensorflow and model source so that other people can debug the problem... – dtlam26 Sep 23 '20 at 05:14

1 Answers1

1

You can try to restore the model and save it by adding saving method after this line of API:

https://github.com/tensorflow/models/blob/master/research/object_detection/model_lib_v2.py#L583-L584

Or you can create a graph and restore the checkpoint in the session and then save to model.h5 as you like in another script

dtlam26
  • 1,410
  • 11
  • 19
  • 1
    Can you elaborate more on the first method you described or can you share some sample code for it. – Harish Babu Sep 21 '20 at 15:23
  • 1
    you can check out here how to load and save model `https://www.tensorflow.org/tutorials/keras/save_and_load`, however, I would prefer the second option due to avoiding touching in the api – dtlam26 Sep 22 '20 at 01:01
  • @dtlam26, could you provide a piece of code? I'm struggling with the same issue, can't convert a model from object detection API to a keras model – pentavol Sep 27 '21 at 06:42
  • I recommend to convert back to tensorflow graph instead rather than Keras. You can check here for how I restore those checkpoint to Tf2 graph: `https://stackoverflow.com/questions/67702185/retrain-frozen-graph-in-tensorflow-2-x` – dtlam26 Oct 25 '21 at 08:01