docstr: 将预测的结果转换成COCOeval指定的格式，针对实例分割任务
docstr_tok: ["\u5c06\u9884\u6d4b\u7684\u7ed3\u679c\u8f6c\u6362\u6210COCOeval\u6307\u5b9a\u7684\u683c\u5f0f\uff0c\u9488\u5bf9\u5b9e\u4f8b\u5206\u5272\u4efb\u52a1"]
code: def prepare_for_coco_segmentation(self, targets, outputs):
      """将预测的结果转换成COCOeval指定的格式，针对实例分割任务"""
      # 遍历每张图像的预测结果
      for target, output in zip(targets, outputs):
          if len(output) == 0:
              continue
  
          img_id = int(target["image_id"])
          if img_id in self.img_ids:
              # 防止出现重复的数据
              continue
  
          self.img_ids.append(img_id)
          per_image_masks = output["masks"]
          per_image_classes = output["labels"].tolist()
          per_image_scores = output["scores"].tolist()
  
          masks = per_image_masks > 0.5
  
          res_list = []
          # 遍历每个目标的信息
          for mask, label, score in zip(
              masks, per_image_classes, per_image_scores
          ):
              rle = mask_util.encode(
                  np.array(
                      mask[0, :, :, np.newaxis],
                      dtype=np.uint8,
                      order="F",
                  )
              )[0]
              rle["counts"] = rle["counts"].decode("utf-8")
  
              class_idx = int(label)
              if self.classes_mapping is not None:
                  class_idx = int(self.classes_mapping[str(class_idx)])
  
              res = {
                  "image_id": img_id,
                  "category_id": class_idx,
                  "segmentation": rle,
                  "score": round(score, 3),
              }
              res_list.append(res)
          self.results.append(res_list)
  
file_path: deep-learning-for-image-processing/pytorch_object_detection/mask_rcnn/train_utils/coco_eval.py
imports: import json
  import copy
  import numpy
  from pycocotools.coco import COCO
  from pycocotools.cocoeval import COCOeval
  import pycocotools.mask
  from .distributed_utils import all_gather, is_main_process
label_desc: Use image node_id 3 for calling the EvalCOCOMetric obj's underlying member method code with example usage: obj.prepare_for_coco_segmentation(targets, outputs) without return types