class_docstr: An interface between DeformableDETR implementation and COCO.
class_docstr_tok: ["An","interface","between","DeformableDETR","implementation","and","COCO","."]
docstr: Add a single example to the evaluator.
  
  Note that the postprocessing of the predictions is significantly different
  from the one used in DETR. In DETR, each query gives one predicted box. Here
  each query gives num_classes predicted boxes, one for each class, and all
  these num_queries * num_classes predicted boxes are mixed together and the
  top 100 of them are sent to the evaluator. This different postprocessing
  gives about 0.5 final AP increase.
  
  Args:
    prediction: Model prediction dictionary with keys 'pred_img_ids',
      'pred_probs' in shape of `[num_objects, num_classes]` and 'pred_boxes'
      in shape of `[num_objects, 4]`. Box coordinates should be in raw DETR
      format, i.e. [cx, cy, w, h] in range [0, 1].
    target: Target dictionary with keys 'orig_size', 'size', and 'image/id'.
      Must also contain 'padding_mask' if the input image was padded.
docstr_tok: ["Add","a","single","example","to","the","evaluator",".","Note","that","the","postprocessing","of","the","predictions","is","significantly","different","from","the","one","used","in","DETR",".","In","DETR",",","each","query","gives","one","predicted","box",".","Here","each","query","gives","num_classes","predicted","boxes",",","one","for","each","class",",","and","all","these","num_queries","*","num_classes","predicted","boxes","are","mixed","together","and","the","top","100","of","them","are","sent","to","the","evaluator",".","This","different","postprocessing","gives","about","0.5","final","AP","increase",".","Args",":","prediction",":","Model","prediction","dictionary","with","keys","'pred_img_ids","'",",","'pred_probs","'","in","shape","of","`","[","num_objects",",","num_classes","]","`","and","'pred_boxes'","in","shape","of","`","[","num_objects",",","4","]","`",".","Box","coordinates","should","be","in","raw","DETR","format",",","i.e",".","[","cx",",","cy",",","w",",","h","]","in","range","[","0",",","1","]",".","target",":","Target","dictionary","with","keys","'orig_size","'",",","'size","'",",","and","'image\/id","'",".","Must","also","contain","'padding_mask","'","if","the","input","image","was","padded","."]
code: def add_example(
      self,
      prediction: Dict[str, np.ndarray],
      target: Dict[str, np.ndarray],
  ):
      """Add a single example to the evaluator.
  
      Note that the postprocessing of the predictions is significantly different
      from the one used in DETR. In DETR, each query gives one predicted box. Here
      each query gives num_classes predicted boxes, one for each class, and all
      these num_queries * num_classes predicted boxes are mixed together and the
      top 100 of them are sent to the evaluator. This different postprocessing
      gives about 0.5 final AP increase.
  
      Args:
        prediction: Model prediction dictionary with keys 'pred_img_ids',
          'pred_probs' in shape of `[num_objects, num_classes]` and 'pred_boxes'
          in shape of `[num_objects, 4]`. Box coordinates should be in raw DETR
          format, i.e. [cx, cy, w, h] in range [0, 1].
        target: Target dictionary with keys 'orig_size', 'size', and 'image/id'.
          Must also contain 'padding_mask' if the input image was padded.
      """
      if "pred_boxes" not in prediction:
          # Add dummy to make eval work:
          prediction = copy.deepcopy(prediction)
          prediction["pred_boxes"] = (
              np.zeros((prediction["pred_logits"].shape[0], 4)) + 0.5
          )
  
      # Convert from DETR [cx, cy, w, h] to COCO [x, y, w, h] bounding box format:
      boxes = box_utils.box_cxcywh_to_xyxy(prediction["pred_boxes"])
      boxes = np.array(boxes)
      boxes[:, 2] -= boxes[:, 0]
      boxes[:, 3] -= boxes[:, 1]
  
      # Scale from relative to absolute size:
      # Note that the padding is implemented such that such that the model's
      # predictions are [0,1] normalized to the non-padded image, so scaling by
      # `orig_size` will convert correctly to the original image coordinates. No
      # image flipping happens during evaluation.
      h, w = np.asarray(target["orig_size"])
      scale_factor = np.array([w, h, w, h])
      boxes = boxes * scale_factor[np.newaxis, :]
      boxes = np.asarray(boxes)
  
      # Get scores, excluding the background class:
      if "pred_probs" in prediction:
          scores = prediction["pred_probs"][:, 1:]
      else:
          scores = scipy.special.softmax(
              prediction["pred_logits"], axis=-1
          )[:, 1:]
      scores = np.asarray(scores)
  
      num_classes = scores.shape[1]
      topk_indices = np.argsort(scores.flatten())[-1:-101:-1]
      topk_box_indices = topk_indices // num_classes
      topk_score_indices = topk_indices % num_classes
  
      for i in range(len(topk_indices)):
          # Add example to evaluator:
          img_id = int(target["image/id"])
          single_classification = {
              "image_id": img_id,
              "category_id": self.coco_evaluator.label_to_coco_id[
                  topk_score_indices[i]
              ],
              "bbox": boxes[topk_box_indices[i]].tolist(),
              "score": scores[topk_box_indices[i]][
                  topk_score_indices[i]
              ],
          }
          self.coco_evaluator.annotations.append(single_classification)
          self.coco_evaluator.annotated_img_ids.append(img_id)
  
      self._num_examples_added += 1
  
file_path: scenic/scenic/projects/baselines/deformable_detr/coco_eval.py
imports: import copy
  from typing import Any, Callable, Dict, Optional, Tuple
  import jax
  import jax.numpy
  import numpy
  from scenic.dataset_lib.coco_dataset import coco_eval
  from scenic.model_lib.base_models import box_utils
  from scenic.projects.baselines.detr.train_utils import DetrGlobalEvaluator
  import scipy
label_desc: Use image node_id 2 for calling the DeformableDetrGlobalEvaluator obj's underlying member method code with example usage: obj.add_example(prediction, target) without return types