code: def _compare_against_coco_fn(
      preds,
      target,
      iou_type,
      iou_thresholds=None,
      rec_thresholds=None,
      class_metrics=True,
  ):
      """Taken from https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb."""
      with contextlib.redirect_stdout(io.StringIO()):
          gt = COCO(_DETECTION_VAL)
          dt = (
              gt.loadRes(_DETECTION_BBOX)
              if iou_type == "bbox"
              else gt.loadRes(_DETECTION_SEGM)
          )
  
          coco_eval = COCOeval(gt, dt, iou_type)
          if iou_thresholds is not None:
              coco_eval.params.iouThrs = np.array(
                  iou_thresholds, dtype=np.float64
              )
          if rec_thresholds is not None:
              coco_eval.params.recThrs = np.array(
                  rec_thresholds, dtype=np.float64
              )
  
          coco_eval.evaluate()
          coco_eval.accumulate()
          coco_eval.summarize()
      global_stats = deepcopy(coco_eval.stats)
  
      map_per_class_values = torch.Tensor([-1])
      mar_100_per_class_values = torch.Tensor([-1])
      classes = torch.tensor(
          list(
              set(torch.arange(91).tolist())
              - {
                  0,
                  12,
                  19,
                  26,
                  29,
                  30,
                  45,
                  66,
                  68,
                  69,
                  71,
                  76,
                  83,
                  87,
                  89,
              }
          )
      )
  
      if class_metrics:
          map_per_class_list = []
          mar_100_per_class_list = []
          for class_id in classes.tolist():
              coco_eval.params.catIds = [class_id]
              with contextlib.redirect_stdout(io.StringIO()):
                  coco_eval.evaluate()
                  coco_eval.accumulate()
                  coco_eval.summarize()
              class_stats = coco_eval.stats
              map_per_class_list.append(torch.Tensor([class_stats[0]]))
              mar_100_per_class_list.append(
                  torch.Tensor([class_stats[8]])
              )
  
          map_per_class_values = torch.Tensor(map_per_class_list)
          mar_100_per_class_values = torch.Tensor(
              mar_100_per_class_list
          )
  
      return {
          "map": Tensor([global_stats[0]]),
          "map_50": Tensor([global_stats[1]]),
          "map_75": Tensor([global_stats[2]]),
          "map_small": Tensor([global_stats[3]]),
          "map_medium": Tensor([global_stats[4]]),
          "map_large": Tensor([global_stats[5]]),
          "mar_1": Tensor([global_stats[6]]),
          "mar_10": Tensor([global_stats[7]]),
          "mar_100": Tensor([global_stats[8]]),
          "mar_small": Tensor([global_stats[9]]),
          "mar_medium": Tensor([global_stats[10]]),
          "mar_large": Tensor([global_stats[11]]),
          "map_per_class": map_per_class_values,
          "mar_100_per_class": mar_100_per_class_values,
          "classes": classes,
      }
  
imports: import contextlib
  import io
  import json
  from copy import deepcopy
  from functools import partial
  from itertools import product
  import numpy
  import pytest
  import torch
  from lightning_utilities import apply_to_collection
  from pycocotools.coco import COCO
  from pycocotools.cocoeval import COCOeval
  from torch import IntTensor, Tensor
  from torchmetrics.detection.mean_ap import MeanAveragePrecision
  from torchmetrics.utilities.imports import _FASTER_COCO_EVAL_AVAILABLE, _PYCOCOTOOLS_AVAILABLE, _TORCHVISION_GREATER_EQUAL_0_8
  from unittests.detection import _DETECTION_BBOX, _DETECTION_SEGM, _DETECTION_VAL
  from unittests.helpers.testers import MetricTester
label_desc: Use image node_id 3 for calling a global function with example usage: _compare_against_coco_fn(preds, target, iou_type, iou_thresholds, rec_thresholds, class_metrics) and returns: dict