models icon indicating copy to clipboard operation
models copied to clipboard

How can I predict original image by the finetuned object_detection model?

Open hanzy123 opened this issue 9 years ago • 8 comments

I have already finetuned a faster_rcnn_resnet101 model by Object detection API and I also can watch the predict results on the tensorboard after run the eval.py file.

But if i use eval.py file, I must make my input to TFRecords. How can I predict the bbox directly, feed by the numpyarray image?

Thanks for your help!

hanzy123 avatar Aug 04 '17 02:08 hanzy123

please look at object_detection_tutorial.ipynb

heroinlin avatar Aug 04 '17 02:08 heroinlin

I load the .meta file and saver.restore the model.ckpt-xxxx,but when i run the function detect_objects, he tolds me The name 'image_tensor:0' refers to a Tensor which does not exist

hanzy123 avatar Aug 04 '17 03:08 hanzy123

my error code is :

#Load a frozen TF model 
detection_graph = tf.Graph()

with detection_graph.as_default():
    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())


    saver = tf.train.import_meta_graph(PATH_TO_CKPT+'.meta')
    saver.restore(sess, PATH_TO_CKPT)

with tf.Session(graph=detection_graph) as sess:
    for image_path in TEST_IMAGE_PATHS:
        image = Image.open(image_path)
        image_np = load_image_into_numpy_array(image)
        image_process = detect_objects(image_np, sess, detection_graph)
        print(image_process.shape)
        plt.figure(figsize=IMAGE_SIZE)
        plt.imshow(image_process)

hanzy123 avatar Aug 04 '17 03:08 hanzy123

I do not know how the object_detect function is implemented?Can you provide the details of the object_detect function?

heroinlin avatar Aug 04 '17 03:08 heroinlin

It was defined in Object_Detection_Tensorflow_API.ipynb, the code is:

def detect_objects(image_np, sess, detection_graph):
    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_np_expanded = np.expand_dims(image_np, axis=0)
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    # Each box represents a part of the image where a particular object was 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.
    scores = detection_graph.get_tensor_by_name('detection_scores:0')
    classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    # Actual detection.
    (boxes, scores, classes, num_detections) = sess.run(
        [boxes, scores, classes, num_detections],
        feed_dict={image_tensor: image_np_expanded})

    # Visualization of the results of a detection.
    vis_util.visualize_boxes_and_labels_on_image_array(
        image_np,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8)
    return image_np

hanzy123 avatar Aug 04 '17 04:08 hanzy123

Maybe you should transform the 'ckpt' model to .pb file. Use export_inference_graph.py. Like this PATH_TO_CKPT = "./ssd_mobilenet_v1_coco_11_06_2017/frozen_inference_graph.pb"

with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

heroinlin avatar Aug 04 '17 06:08 heroinlin

I have work it out! Thank you for your patience and kindness!

hanzy123 avatar Aug 04 '17 07:08 hanzy123

You are welcome!

heroinlin avatar Aug 04 '17 07:08 heroinlin