How to get the prediction percentage of objects by class Tensorflow object detection

Viewed 955

I am starting to study TensorFlow. I refer to TensorFlow object-detection research models and execute them. it works and displays the image with objection detection in the rectangular area. I need the prediction percentage of the class in the images. Means human - some %, birds - some% , kite - some% etc. How can I get the prediction percentage

def run_inference_for_single_image(model, image):
  image = np.asarray(image)
  # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
  input_tensor = tf.convert_to_tensor(image)
  # The model expects a batch of images, so add an axis with `tf.newaxis`.
  input_tensor = input_tensor[tf.newaxis,...]

  # Run inference
  output_dict = model(input_tensor)


  num_detections = int(output_dict.pop('num_detections'))

  output_dict = {key:value[0, :num_detections].numpy() 
                 for key,value in output_dict.items()}
  output_dict['num_detections'] = num_detections

  # detection_classes should be ints.
  output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)


  # Handle models with masks:
  if 'detection_masks' in output_dict:
    # Reframe the the bbox mask to the image size.
    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
              output_dict['detection_masks'], output_dict['detection_boxes'],
               image.shape[0], image.shape[1])      
    detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
                                       tf.uint8)
    output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()

  return output_dict


def show_inference(model, image_path):
  image_np = np.array(Image.open(image_path))
  # Actual detection.
  output_dict = run_inference_for_single_image(model, image_np)
  print("---------------------------------------------------------------------------------------")

  print("---------------------------------------------------------------------------------------")

  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks_reframed', None),
      use_normalized_coordinates=True,
      line_thickness=8)

  display(Image.fromarray(image_np))

for image_path in TEST_IMAGE_PATHS:
  show_inference(detection_model, image_path
1 Answers

Have you tried looking at output_dict['detection_scores']?

So it's only the percentage of the detected class, not all of them, so what you want doesn't come out of the box. I see two options, either you try to look for the name of the tf.Operation in the tensorflow graph that does what you want and try to output it alongside the other outputs (the hard but fast way), or just run your boxes trough the classifier backbone again (e.g. if the CNN backbone is VGG16, run it through VGG 16) (the easy but slow way).

Related