Search code examples
tensorflowtensorflow2.0object-detectionobject-detection-api

how to load a saved_model.pb that was previously exported from Tensorflow Object Detection API


So previously I have trained a model based on the Tensorflow Models Zoo on Tensorflow Object Detection API using my own data. After that, I had exported the saved model in a pb format, just as the tutorial had suggested using exporter_main_v2.py

I wanted to load the same model again in another notebook to use for new predictions but I was not successful in loading the model.

Previously, I tried to load the model to be able to predict labels with my test data. The tutorial said to use tf.saved_model.load() to do this like:

import time
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils

PATH_TO_SAVED_MODEL = "/content/saved_model"

print('Loading model...', end='')
start_time = time.time()

# Load saved model and build the detection function
detect_fn = tf.saved_model.load(PATH_TO_SAVED_MODEL)
detect_fn

end_time = time.time()
elapsed_time = end_time - start_time
print('Done! Took {} seconds'.format(elapsed_time))

However, when I tried running it using my .pb model, it shows up an error:

FileNotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /content/saved_model/variables/variables

I wanted to know whether I exported my model wrong or was there any steps that I missed regarding this? I tried finding a solution on the Tensorflow Github and on here but I couldn't find any that matches my situation.


Solution

  • I had same problem - very frustrating. But in fact (at least in my case), i needed the complete /saved_model directory, including the /assets, /variables directories as well as the saved_model.pb file. So your PATH_TO_SAVED_MODEL should point to this populated /saved_model directory.