'Tensorflow2 Object Detection API Loading saved_model.pb and use for prediction

I trained a model and saved it. Now I want to go back that model. I have a checkpoint file, saved_model file (assets,variables and saved_model.pb),pipeline.config and labelmap.pbtxt. How can I load this model and make predict? Thanks.



Solution 1:[1]

You can load your model by using detect_fn = tf.saved_model.load("/path/to/saved_model") and run inference using detections = detect_fn(input_tensor). To run inference and visualize:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils

detect_fn = tf.saved_model.load("/path/to/saved_model")
PATH_TO_LABELS = 'path/to/labels.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
IMAGE_PATHS = ["/path/to/test/image1","/path/to/test/image2"]
def load_image_into_numpy_array(path):
    return np.array(Image.open(path))


for image_path in IMAGE_PATHS:
    print('Running inference for {}... '.format(image_path), end='')
    image_np = load_image_into_numpy_array(image_path)
    input_tensor = tf.convert_to_tensor(image_np)
    input_tensor = input_tensor[tf.newaxis, ...]
    # input_tensor = np.expand_dims(image_np, 0)
    detections = detect_fn(input_tensor)
    num_detections = int(detections.pop('num_detections'))
    detections = {key: value[0, :num_detections].numpy()
                   for key, value in detections.items()}
    detections['num_detections'] = num_detections
    detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
    image_np_with_detections = image_np.copy()
    viz_utils.visualize_boxes_and_labels_on_image_array(
          image_np_with_detections,
          detections['detection_boxes'],
          detections['detection_classes'],
          detections['detection_scores'],
          category_index,
          use_normalized_coordinates=True,
          max_boxes_to_draw=200,
          min_score_thresh=.6,
          agnostic_mode=False)
    plt.figure(figsize=(20, 20))
    plt.imshow(image_np_with_detections)
    print('Done')
plt.show()

Please refer to: https://github.com/tensorflow/models/blob/master/research/object_detection/colab_tutorials/inference_tf2_colab.ipynb

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 Buur