code: def main(args):
      device = torch.device(
          args.device if torch.cuda.is_available() else "cpu"
      )
      print(f"using device: {device}")
  
      _, _, val_images_path, val_images_label = read_split_data(
          args.data_path
      )
  
      img_size = 384
      data_transform = {
          "val": transforms.Compose(
              [
                  transforms.Resize(int(img_size * 1.143)),
                  transforms.CenterCrop(img_size),
                  transforms.ToTensor(),
                  transforms.Normalize(
                      [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
                  ),
              ]
          )
      }
  
      # 实例化验证数据集
      val_dataset = MyDataSet(
          images_path=val_images_path,
          images_class=val_images_label,
          transform=data_transform["val"],
      )
  
      nw = min(
          [
              os.cpu_count(),
              args.batch_size if args.batch_size > 1 else 0,
              8,
          ]
      )  # number of workers
      print("Using {} dataloader workers every process".format(nw))
  
      val_loader = torch.utils.data.DataLoader(
          val_dataset,
          batch_size=args.batch_size,
          shuffle=False,
          pin_memory=True,
          num_workers=nw,
          collate_fn=val_dataset.collate_fn,
      )
  
      model = create_model(num_classes=args.num_classes)
      # load pretrain weights
      assert os.path.exists(args.weights), "cannot find {} file".format(
          args.weights
      )
      model.load_state_dict(
          torch.load(args.weights, map_location=device)
      )
      model.to(device)
  
      # read class_indict
      json_label_path = "./class_indices.json"
      assert os.path.exists(
          json_label_path
      ), "cannot find {} file".format(json_label_path)
      json_file = open(json_label_path, "r")
      class_indict = json.load(json_file)
  
      labels = [label for _, label in class_indict.items()]
      confusion = ConfusionMatrix(
          num_classes=args.num_classes, labels=labels
      )
      model.eval()
      with torch.no_grad():
          for val_data in tqdm(val_loader, file=sys.stdout):
              val_images, val_labels = val_data
              outputs = model(val_images.to(device))
              outputs = torch.softmax(outputs, dim=1)
              outputs = torch.argmax(outputs, dim=1)
              confusion.update(
                  outputs.to("cpu").numpy(),
                  val_labels.to("cpu").numpy(),
              )
      confusion.plot()
      confusion.summary()
  
file_path: deep-learning-for-image-processing/pytorch_classification/swin_transformer/create_confusion_matrix.py
imports: import os
  import json
  import argparse
  import sys
  import torch
  from torchvision import transforms
  import numpy
  from tqdm import tqdm
  import matplotlib.pyplot
  from prettytable import PrettyTable
  from utils import read_split_data
  from my_dataset import MyDataSet
  from model import swin_base_patch4_window12_384_in22k
label_desc: Use image node_id 1 for calling a global function with example usage: main(args) without return types