3

In reference to object_detection_tutorial.ipynb. I am wondering if its possible to run for all the images in a directory.

Rather than writing a for loop and running a "run_inference_for_single_image(image, graph)". Is there a way to run the inference on all the images in a directory or run the inference on multiple images. link

  for f in files:
    if f.lower().endswith(('.png', '.jpg', '.jpeg')):
      image_path = files_dir + '/' + f
       .... // Read image etc.
      output_dict = run_inference_for_single_image(image_np, detection_graph)

This will create tf.session each time and i think its computationally expensive. Please correct me if i am wrong.

3 Answers 3

5

As you know, 'run_inference_for_single_image' method create each time. If you wanna inference for multiple images, you should change code like,

  • Method Call

    images = []
    for f in files:
      if f.lower().endswith(('.png', '.jpg', '.jpeg')):
        image_path = files_dir + '/' + f
        image =  .... // Read image etc.
        images.append(image)
        output_dicts = run_inference_for_multiple_images(images, detection_graph)
    
  • run_inference_for_multiple_images

    def run_inference_for_multiple_images(images, grapg):
      with graph.as_default():
        with tf.Session() as sess:
          output_dicts = []
    
          for index, image in enumerate(images):
            ... same as inferencing for single image
    
             output_dicts.append(output_dict)
    
       return output_dicts
    

This code will be performed without creating tf.session each time but once.

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

2 Comments

If we were to evaluate batches of 10 images at a time, is the bulk image eval 10x faster?
In my case, It's not exactly 10x faster. First or sometimes second image takes almost same as Single version's elapsed time. But remains are definitely faster. Practically when I run object detection with my video clip, It only takes about 2 hrs but single version takes 18~24 hrs.
3

I found this tutorial from google - creating-object-detection-application-tensorflow. After looking into its github page --> object_detection_app --> app.py we only need to run detect_objects(image_path) function every single time we want to detect an object.

1 Comment

This gave me a great Idea. Thank you @BhanuKiran . I hope my performance gets a small boost.
1

It is possible to run inference on batch of images depending on computational power of GPU and size of the images.

step 1: stacking all the test images in one array:

for image_path in glob.glob(PATH_TO_TEST_IMAGES_DIR + '/*.jpg'):
    image_np = io.imread(image_path)  #
    image_array.append(image_np)
image_array = np.array(image_array)

step 2: run inference on batches: (higher batch size might cause out of memory issues)

  BATCH_SIZE = 5
  for i in range(0, image_array.shape[0],BATCH_SIZE):
    output_dict = sess.run(tensor_dict, feed_dict={image_tensor: image_array[i:i+BATCH_SIZE]})


    print("number of images inferenced = ", i+BATCH_SIZE)
    output_dict_array.append(output_dict)

make sure dimensions of image_tensor and image_array match. In this example image_array is (?, height, width, 3)

some tips:

  1. You would want to load the graph only once as it takes few seconds to load.
  2. I observed that using skimage.io.imread() or cv2.imread() is pretty fast in loading images. These functions directly load images as numpy arrays.
  3. skimage or opencv for saving images are faster than matplotlib.

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.