0

I've successfully trained and saved a faster RCNN model for tensorflow using their object detection API. I'm now trying to run some inferences on the code, taking bits of code from this tutorial.

However, after I successfully restore the metagraph and the checkpoint, the system can't find the input and output nodes, I get the following error:

KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."

The checkpoint and metagraph were created by the train.py script, on my own data, following the instructions given here.

This is my code:

OUTPUT_DIR = "my_path/models/SSD_v1/train"
CKPT_DIR = OUTPUT_DIR
LATEST_CKPT_FILENAME = "checkpoint"
LAST_CKPT_FILE = os.path.join(CKPT_DIR, LATEST_CKPT_FILENAME)
MODEL_FILENAME_PATH = os.path.join(OUTPUT_DIR, "model.ckpt.meta")
def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height, im_width, 3)).astype(np.uint8)


def test_model(images_list, path_to_ckpt=None,
               meta_graph=None):
    if path_to_ckpt is None:
        path_to_ckpt = tf.train.latest_checkpoint(CKPT_DIR, LATEST_CKPT_FILENAME)
    if meta_graph is None:
        meta_graph = MODEL_FILENAME_PATH
    print("test_model launched")

    tf.reset_default_graph()
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Restore graph
            saver = tf.train.import_meta_graph(meta_graph, clear_devices=True)
            print('metagraph restored')
            saver.restore(sess, path_to_ckpt)
            print('graph restored')

            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')  # This is where the error happens
            # Each box represents a part of the image where a particular object was detected.
            detected_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detected_scores = detection_graph.get_tensor_by_name('detection_scores:0')
            detected_classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = graph.get_tensor_by_name('num_detections:0')

            print("Output tensors: ")
            print(detected_boxes)
            print(detected_scores)
            print(detected_classes)
            print('')

            for i, image in enumerate(images_list):
                detected_boxes, detected_scores, detected_classes, num_detect = sess.run([detected_boxes, detected_scores, detected_classes, num_detections],
                         feed_dict={image_tensor: image})
                print(i, num_detect, detected_boxes, detected_scores, detected_classes)


def main():
    directory_path = "../data/samples/"
    image_files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_list = [ np.expand_dims(load_image_into_numpy_array(Image.open(os.path.join(directory_path, f))), axis=0) for f in image_files]
    test_model(images_list=image_list)

if __name__=="__main__":
    main()

Full error stacktrace:

Traceback (most recent call last):   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/pano_faster_rcnn/src/run_faster_rcnn_inference.py", line 99, in <module>
    main()   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/pano_faster_rcnn/src/run_faster_rcnn_inference.py", line 95, in main
    test_model(images_list=image_list)   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/pano_faster_rcnn/src/run_faster_rcnn_inference.py", line 48, in test_model
    image_tensor = graph.get_tensor_by_name('image_tensor:0')   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2733, in get_tensor_by_name
    return self.as_graph_element(name, allow_tensor=True, allow_operation=False)   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2584, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)   File "/home/guillaumedelaboulaye/PR8210PANO/faster-rcnn/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2626, in _as_graph_element_locked
    "graph." % (repr(name), repr(op_name))) KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."

2 Answers 2

2

In the train graph, the input/output nodes are not given those names. What you will need to do is to "export" your trained model via the export_inference_graph.py tool. I believe it currently exports it to a frozen graph or a SavedModel, but in future releases, it will export to ordinary checkpoint as well.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot ! :)
i have the frozen model.Im trying to use it with objectdetection api. Im getting the same error
I try to use: export_inference_graph.py --input_type image_tensor --pipeline_config_path ./faster_rcnn_resnet101_coco_2018_01_28_VIOREL/pipeline_vio.config --trained_checkpoint_pr efix train_folder\model.ckpt-497555 --output_file train_folder\inference_graph\frozen_inference_graph_new.pb and I get the same error when trying to use the exported graph
0

If you want sample code for finding the node names of the graph, referring to the object_detection_tutorial.ipynb, after the "Load a (frozen) Tensorflow model into memory." block:

for node in od_graph_def.node: print node.name

That should list all the node names that you can then enter in the subsequent blocks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.