code: def main(args):
      device = torch.device(
          args.device if torch.cuda.is_available() else "cpu"
      )
      print("Using {} device training.".format(device.type))
  
      data_transform = {
          "val": transforms.Compose(
              [
                  transforms.AffineTransform(
                      scale=(1.25, 1.25), fixed_size=args.resize_hw
                  ),
                  transforms.ToTensor(),
                  transforms.Normalize(
                      mean=[0.485, 0.456, 0.406],
                      std=[0.229, 0.224, 0.225],
                  ),
              ]
          )
      }
  
      # read class_indict
      label_json_path = args.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:
          person_coco_info = json.load(f)
  
      data_root = args.data_path
  
      # 注意这里的collate_fn是自定义的，因为读取的数据包括image和targets，不能直接使用默认的方法合成batch
      batch_size = args.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 = CocoKeypoint(
          data_root,
          "val",
          transforms=data_transform["val"],
          det_json_path=None,
      )
      # 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
      model = HighResolutionNet()
  
      # 载入你自己训练好的模型权重
      weights_path = args.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")
      )
      # print(model)
      model.to(device)
  
      # evaluate on the val dataset
      key_metric = EvalCOCOMetric(
          val_dataset.coco, "keypoints", "key_results.json"
      )
      model.eval()
      with torch.no_grad():
          for images, targets in tqdm(
              val_dataset_loader, desc="validation..."
          ):
              # 将图片传入指定设备device
              images = images.to(device)
  
              # inference
              outputs = model(images)
              if args.flip:
                  flipped_images = transforms.flip_images(images)
                  flipped_outputs = model(flipped_images)
                  flipped_outputs = transforms.flip_back(
                      flipped_outputs, person_coco_info["flip_pairs"]
                  )
                  # feature is not aligned, shift flipped heatmap for higher accuracy
                  # https://github.com/leoxiaobin/deep-high-resolution-net.pytorch/issues/22
                  flipped_outputs[..., 1:] = flipped_outputs.clone()[
                      ..., 0:-1
                  ]
                  outputs = (outputs + flipped_outputs) * 0.5
  
              # decode keypoint
              reverse_trans = [t["reverse_trans"] for t in targets]
              outputs = transforms.get_final_preds(
                  outputs, reverse_trans, post_processing=True
              )
  
              key_metric.update(targets, outputs)
  
      key_metric.synchronize_results()
      key_metric.evaluate()
  
      save_info(key_metric.coco_evaluator, "keypoint_record_mAP.txt")
  
file_path: deep-learning-for-image-processing/pytorch_keypoint/HRNet/validation.py
imports: import os
  import json
  import torch
  from tqdm import tqdm
  import numpy
  from model import HighResolutionNet
  from train_utils import EvalCOCOMetric
  from my_dataset_coco import CocoKeypoint
  import transforms
label_desc: Use image node_id 3 for calling a global function with example usage: main(args) without return types