code: def calculate_confusion_matrix(
      dataset, results, score_thr=0, nms_iou_thr=None, tp_iou_thr=0.5
  ):
      """Calculate the confusion matrix.
  
      Args:
          dataset (Dataset): Test or val dataset.
          results (list[ndarray]): A list of detection results in each image.
          score_thr (float|optional): Score threshold to filter bboxes.
              Default: 0.
          nms_iou_thr (float|optional): nms IoU threshold, the detection results
              have done nms in the detector, only applied when users want to
              change the nms IoU threshold. Default: None.
          tp_iou_thr (float|optional): IoU threshold to be considered as matched.
              Default: 0.5.
      """
      num_classes = len(dataset.metainfo["classes"])
      confusion_matrix = np.zeros(
          shape=[num_classes + 1, num_classes + 1]
      )
      assert len(dataset) == len(results)
      prog_bar = ProgressBar(len(results))
      for idx, per_img_res in enumerate(results):
          res_bboxes = per_img_res["pred_instances"]
          gts = dataset.get_data_info(idx)["instances"]
          analyze_per_img_dets(
              confusion_matrix,
              gts,
              res_bboxes,
              score_thr,
              tp_iou_thr,
              nms_iou_thr,
          )
          prog_bar.update()
      return confusion_matrix
  
file_path: mmdetection/tools/analysis_tools/confusion_matrix.py
imports: import argparse
  import os
  import matplotlib.pyplot
  import numpy
  from matplotlib.ticker import MultipleLocator
  from mmcv.ops import nms
  from mmengine import Config, DictAction
  from mmengine.fileio import load
  from mmengine.registry import init_default_scope
  from mmengine.utils import ProgressBar
  from mmdet.evaluation import bbox_overlaps
  from mmdet.registry import DATASETS
  from mmdet.utils import replace_cfg_vals, update_data_root
label_desc: Use image node_id 2 for calling a global function with example usage: calculate_confusion_matrix(dataset, results, score_thr, nms_iou_thr, tp_iou_thr) and returns: confusion_matrix