Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow object detection: how to detect on batch

Tags:

I am trying to perform detection on a batch using tensorflow detection tutorial, but the following code gives me setting an array element with a sequence. error.

# load multiple images 
np_images = []
for img_path in img_paths:
    img = Image.open(image_path)
    image_np = load_image_into_numpy_array(img)      
    image_np_expanded = np.expand_dims(image_np, axis=0)
    np_images.append(image_np)

#Get input and output tensors
image_tensor = det_graph.get_tensor_by_name('image_tensor:0')  
boxes = det_graph.get_tensor_by_name('detection_boxes:0')     
scores = det_graph.get_tensor_by_name('detection_scores:0')
classes = det_graph.get_tensor_by_name('detection_classes:0')
num_detections = det_graph.get_tensor_by_name('num_detections:0')

# detect on batch of images
detection_results = sess.run(
        [boxes, scores, classes, num_detections],
        feed_dict={image_tensor: np_images})  

How to feed an array of images correctly?

like image 460
Yaroslav Schubert Avatar asked Nov 14 '17 14:11

Yaroslav Schubert


People also ask

How many objects can TensorFlow detect?

The pre-trained models we provide are trained to detect 90 classes of objects.


1 Answers

The image_tensor in feed_dict is expected to have the dimension [batch_size, x, y, 3] where (x,y) is the size of each image. If your image sizes are all different, you cannot create such a numpy array. You can resize your images to solve this.

# If the NN was trained on (300,300) size images
IMAGE_SIZE = (300, 300)
for img_path in img_paths:
    img = Image.open(image_path).resize(IMAGE_SIZE)
    image_np = load_image_into_numpy_array(img)      
    np_images.append(image_np)
...
detection_results = sess.run(
    [boxes, scores, classes, num_detections],
    feed_dict={image_tensor: np.array(np_images)}) 
like image 166
Najih Km Avatar answered Sep 21 '22 12:09

Najih Km