'SavedModel file does not exist at saved_model/{saved_model.pbtxt|saved_model.pb}

I'm try running Tensorflow Object Detection API on Tensorflow 2 and I got that error, can someone have a solution?

The code :

Loader

def load_model(model_name):
  base_url = 'http://download.tensorflow.org/models/object_detection/'
  model_file = model_name + '.tar.gz'
  model_dir = tf.keras.utils.get_file(
    fname=model_name, 
    origin=base_url + model_file,
    untar=True)
​
  model_dir = pathlib.Path(model_dir)/"saved_model"
​
  model = tf.saved_model.load(str(model_dir))
  model = model.signatures['serving_default']
​
  return model

Loading label map

Label maps map indices to category names, so that when our convolution network predicts 5, we know that this corresponds to airplane. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = 'data/mscoco_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

For the sake of simplicity we will test on 2 images:

# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = pathlib.Path('test_images')
TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpg")))
TEST_IMAGE_PATHS

Detection

Load an object detection model:

model_name = 'ssd_mobilenet_v1_coco_11_06_2017'
detection_model = load_model(model_name)

and i got this error

OSError                                   Traceback (most recent call last)
<ipython-input-7-e89d9e690495> in <module>
      1 model_name = 'ssd_mobilenet_v1_coco_11_06_2017'
----> 2 detection_model = load_model(model_name)

<ipython-input-4-f8a3c92a04a4> in load_model(model_name)
      9   model_dir = pathlib.Path(model_dir)/"saved_model"
     10 
---> 11   model = tf.saved_model.load(str(model_dir))
     12   model = model.signatures['serving_default']
     13 

D:\Anaconda\lib\site-packages\tensorflow_core\python\saved_model\load.py in load(export_dir, tags)
    515     ValueError: If `tags` don't match a MetaGraph in the SavedModel.
    516   """
--> 517   return load_internal(export_dir, tags)
    518 
    519 

D:\Anaconda\lib\site-packages\tensorflow_core\python\saved_model\load.py in load_internal(export_dir, tags, loader_cls)
    524     # sequences for nest.flatten, so we put those through as-is.
    525     tags = nest.flatten(tags)
--> 526   saved_model_proto = loader_impl.parse_saved_model(export_dir)
    527   if (len(saved_model_proto.meta_graphs) == 1
    528       and saved_model_proto.meta_graphs[0].HasField("object_graph_def")):

D:\Anaconda\lib\site-packages\tensorflow_core\python\saved_model\loader_impl.py in parse_saved_model(export_dir)
     81                   (export_dir,
     82                    constants.SAVED_MODEL_FILENAME_PBTXT,
---> 83                    constants.SAVED_MODEL_FILENAME_PB))
     84 
     85 

OSError: SavedModel file does not exist at: C:\Users\Asus\.keras\datasets\ssd_mobilenet_v1_coco_11_06_2017\saved_model/{saved_model.pbtxt|saved_model.pb}


Solution 1:[1]

I assume that you are running detection_model_zoo tutorial here. Note that maybe you can change the model name from ssd_mobilenet_v1_coco_11_06_2017 to ssd_mobilenet_v1_coco_2017_11_17, this will solve the problem in my test.

The content of these files can be seen below:

# ssd_mobilenet_v1_coco_11_06_2017
frozen_inference_graph.pb  model.ckpt.data-00000-of-00001  model.ckpt.meta
graph.pbtxt        model.ckpt.index

# ssd_mobilenet_v1_coco_2017_11_17
checkpoint         model.ckpt.data-00000-of-00001  model.ckpt.meta
frozen_inference_graph.pb  model.ckpt.index        saved_model

Reference:

  1. Where to find tensorflow pretrained models (list or download link)
  2. detect_model_zoo

  3. Using the SavedModel format official blog

Solution 2:[2]

Do not link all the way to the model name. Use the pathname to the folder containing the model.

Solution 3:[3]

In my case, this code is worked for me. I gave the path of the folder of my .pd file that was created by model checkpoint module :

import tensorflow as tf
if __name__ == '__main__':
    # Update the input name and path for your Keras model
    input_keras_model = 'my path/weights/my_trained_model/{the files inside this folder are: assets(folder), variables(folder),keras_metadata.pd,saved_model.pd}'


    model = tf.keras.models.load_model(input_keras_model)

Solution 4:[4]

I was getting exactly this error when trying to use the saved_model.pb file. I had gotten the .pb file along with a pre-trained model following some tutorial.

It was happening due to the following reasons:

  • first your already existing saved_model.pb file might be corrupt
  • second as the user @Mark Silla has mentioned, you are giving the wrong path to the file, just give the path of folder containing the .pb file excluding the file name
  • third, it might be due to Tensorflow versioning issues

I had to follow all of the above steps and upgraded Tensorflow from v2.3 to v2.3, and it finally created a new saved_model.pb which was not corrupt and I could run it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 V Demo
Solution 2 Mark Silla
Solution 3 omid
Solution 4 Singh