Search code examples
tensorflowobject-detection-api

Probablity distributions/confidence score for each bounding box for Tensorflow Object Detection API


Normally we have the only one confidence score for each bounding box that is printed for each detected object. My understanding is that for each detected Tensorflow Object Detection API has multiple scores but finally it uses argmax to print name of the object having the highest one. Now if If I want to print all the scores (not just the highest one), where can I find them?


Solution

  • You can get all the scores and respective labels for the image using output_dict which will contain all the information for the image about detection_scores, detection_classes, detection_boxes and num_detections.

    For a given image you can just simply do the following to print all the scores of the objects detected.

    all_scores ={}
    image_np = np.array(Image.open(image_path))
    #Actual detection.
    output_dict = run_inference_for_single_image(model, image_np)
    for cls_idx,scr in zip(output_dict["detection_classes"],output_dict["detection_scores"]):
       all_scores[cls_idx] = scr
    

    The output of all_scores looks something like this:

    {1: 0.044680625,
     3: 0.05898307,
     9: 0.051253375,
     16: 0.04842608,
     18: 0.0313316,
     21: 0.031728894,
     33: 0.13961948,
     34: 0.07948237,
     38: 0.044567253,
     42: 0.047603823,
     47: 0.030417146,
     57: 0.0398438,
     62: 0.032532312,
     63: 0.04614881,
     84: 0.0338195,
     88: 0.036108077} 
    

    You can map the index name with the category name if you want the name to be printed.

    Hope this answeres your question, Happy Learning.