code: def main(parser_data):
      device = torch.device(
          parser_data.device if torch.cuda.is_available() else "cpu"
      )
      print("Using {} device training.".format(device.type))
  
      data_transform = {
          "val": transforms.Compose([transforms.ToTensor()])
      }
  
      # read class_indict
      label_json_path = parser_data.label_json_path
      assert os.path.exists(
          label_json_path
      ), "json file {} dose not exist.".format(label_json_path)
      with open(label_json_path, "r") as f:
          category_index = json.load(f)
  
      data_root = parser_data.data_path
  
      # 注意这里的collate_fn是自定义的，因为读取的数据包括image和targets，不能直接使用默认的方法合成batch
      batch_size = parser_data.batch_size
      nw = min(
          [os.cpu_count(), batch_size if batch_size > 1 else 0, 8]
      )  # number of workers
      print("Using %g dataloader workers" % nw)
  
      # load validation data set
      val_dataset = CocoDetection(
          data_root, "val", data_transform["val"]
      )
      # VOCdevkit -> VOC2012 -> ImageSets -> Main -> val.txt
      # val_dataset = VOCInstances(data_root, year="2012", txt_name="val.txt", transforms=data_transform["val"])
      val_dataset_loader = torch.utils.data.DataLoader(
          val_dataset,
          batch_size=batch_size,
          shuffle=False,
          pin_memory=True,
          num_workers=nw,
          collate_fn=val_dataset.collate_fn,
      )
  
      # create model
      backbone = resnet50_fpn_backbone()
      model = MaskRCNN(backbone, num_classes=args.num_classes + 1)
  
      # 载入你自己训练好的模型权重
      weights_path = parser_data.weights_path
      assert os.path.exists(weights_path), "not found {} file.".format(
          weights_path
      )
      model.load_state_dict(
          torch.load(weights_path, map_location="cpu")["model"]
      )
      # print(model)
  
      model.to(device)
  
      # evaluate on the val dataset
      cpu_device = torch.device("cpu")
  
      det_metric = EvalCOCOMetric(
          val_dataset.coco, "bbox", "det_results.json"
      )
      seg_metric = EvalCOCOMetric(
          val_dataset.coco, "segm", "seg_results.json"
      )
      model.eval()
      with torch.no_grad():
          for image, targets in tqdm(
              val_dataset_loader, desc="validation..."
          ):
              # 将图片传入指定设备device
              image = list(img.to(device) for img in image)
  
              # inference
              outputs = model(image)
  
              outputs = [
                  {k: v.to(cpu_device) for k, v in t.items()}
                  for t in outputs
              ]
              det_metric.update(targets, outputs)
              seg_metric.update(targets, outputs)
  
      det_metric.synchronize_results()
      seg_metric.synchronize_results()
      det_metric.evaluate()
      seg_metric.evaluate()
  
      save_info(
          det_metric.coco_evaluator,
          category_index,
          "det_record_mAP.txt",
      )
      save_info(
          seg_metric.coco_evaluator,
          category_index,
          "seg_record_mAP.txt",
      )
  
file_path: deep-learning-for-image-processing/pytorch_object_detection/mask_rcnn/validation.py
imports: import os
  import json
  import torch
  from tqdm import tqdm
  import numpy
  import transforms
  from backbone import resnet50_fpn_backbone
  from network_files import MaskRCNN
  from my_dataset_coco import CocoDetection
  from my_dataset_voc import VOCInstances
  from train_utils import EvalCOCOMetric
label_desc: Use image node_id 3 for calling a global function with example usage: main(parser_data) without return types