0
votes

I have been trying to use the object detection api of tensorflow. After alot of trouble I have installed all the necessary modules for the same. Then when I run the "object_detection_tutorial" from the models/research/object_detection I come up with the error as in the detection section as follows:

Detection

Load an object detection model:

model_name = 'ssd_mobilenet_v1_coco_2017_11_17'
detection_model = load_model(model_name)

tensorflow:Saver not created because there are no variables in the graph to restore

print(detection_model.inputs)

tf.Tensor 'image_tensor:0' shape=(?, ?, ?, 3) dtype=uint8

And again I get the error in below line as:

for image_path in TEST_IMAGE_PATHS:
  show_inference(detection_model, image_path)

error:

* in run_inference_for_single_image(model, image)

     12   # Convert to numpy arrays, and take index [0] to remove the batch dimension.
     13   # We're only interested in the first num_detections.
---> 14   num_detections = int(output_dict.pop('num_detections'))
     15   output_dict = {key:value[0, :num_detections].numpy() 
     16                  for key,value in output_dict.items()}

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'*

Please any help why am I getting this error. I am new to tensorflow just want to run this tutorial successfully. I have installed "Tensorflow 2.2.0" and all the modules. I also did the same with Tensorflow lower version but same issue.

1

1 Answers

1
votes

Try the following code:

import tensorflow as tf
import cv2
import numpy as np
import os
import random
from IPython import embed

IMAGE_PATH = "/Users/vedanshu/tfrecord/dataset"

input_names = ['image_tensor']

def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
               font_scale=0.5, thickness=2):
    size = cv2.getTextSize(label, font, font_scale, thickness)[0]
    x, y = point
    cv2.rectangle(image, (x, y - size[1]),
                  (x + size[0], y), (255, 0, 0), cv2.FILLED)
    cv2.putText(image, label, point, font, font_scale,
                (255, 255, 255), thickness)

detection_graph1 = tf.Graph()

with detection_graph1.as_default():
    tf_sess1 = tf.Session(graph=detection_graph1)
    model = tf.saved_model.loader.load(tf_sess1, ["serve"], "/Users/vedanshu/saved_model")

tf_input = tf_sess1.graph.get_tensor_by_name(input_names[0] + ':0')
tf_scores = tf_sess1.graph.get_tensor_by_name('detection_scores:0')
tf_boxes = tf_sess1.graph.get_tensor_by_name('detection_boxes:0')
tf_classes = tf_sess1.graph.get_tensor_by_name('detection_classes:0')
tf_num_detections = tf_sess1.graph.get_tensor_by_name('num_detections:0')

for img in os.listdir(IMAGE_PATH):
    if img.endswith("jpg"):
        image = os.path.join(IMAGE_PATH, img)
        image = cv2.imread(image)
        _img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        scores, boxes, classes, num_detections = tf_sess1.run([tf_scores, tf_boxes, tf_classes, tf_num_detections], feed_dict={tf_input: _img[None, ...]})

        boxes = boxes[0]  # index by 0 to remove batch dimension
        scores = scores[0]
        classes = classes[0]
        num_detections = int(num_detections[0])

        # embed()
        boxes_pixels = []
        for i in range(num_detections):
            # scale box to image coordinates
            box = boxes[i] * np.array([image.shape[0], image.shape[1], image.shape[0], image.shape[1]])
            box = np.round(box).astype(int)
            boxes_pixels.append(box)

        boxes_pixels = np.array(boxes_pixels)

        for i in range(num_detections):
            if scores[i] > 0.15:
                box = boxes_pixels[i]
                box = np.round(box).astype(int)
                (startY, startX, endY, endX) = box.astype("int")
                boxW = endX - startX
                boxH = endY - startY

                image = cv2.rectangle(image, (box[1], box[0]), (box[3], box[2]), (0, 255, 0), 2)
                label = "{}:{:.2f}".format(int(classes[i]), scores[i])
                draw_label(image, (box[1], box[0]), label)

                cv2.imwrite("/Users/vedanshu/tfrecord/out/"+img, image)

You need to give the location of your saved_model.pb and location of images.