code: def plot_class_preds(
      net, images_dir: str, transform, num_plot: int = 5, device="cpu"
  ):
      if not os.path.exists(images_dir):
          print(
              "not found {} path, ignore add figure.".format(images_dir)
          )
          return None
  
      label_path = os.path.join(images_dir, "label.txt")
      if not os.path.exists(label_path):
          print(
              "not found {} file, ignore add figure".format(label_path)
          )
          return None
  
      # read class_indict
      json_label_path = "./class_indices.json"
      assert os.path.exists(json_label_path), "not found {}".format(
          json_label_path
      )
      json_file = open(json_label_path, "r")
      # {"0": "daisy"}
      flower_class = json.load(json_file)
      # {"daisy": "0"}
      class_indices = dict((v, k) for k, v in flower_class.items())
  
      # reading label.txt file
      label_info = []
      with open(label_path, "r") as rd:
          for line in rd.readlines():
              line = line.strip()
              if len(line) > 0:
                  split_info = [
                      i for i in line.split(" ") if len(i) > 0
                  ]
                  assert (
                      len(split_info) == 2
                  ), "label format error, expect file_name and class_name"
                  image_name, class_name = split_info
                  image_path = os.path.join(images_dir, image_name)
                  # 如果文件不存在，则跳过
                  if not os.path.exists(image_path):
                      print("not found {}, skip.".format(image_path))
                      continue
                  # 如果读取的类别不在给定的类别内，则跳过
                  if class_name not in class_indices.keys():
                      print(
                          "unrecognized category {}, skip".format(
                              class_name
                          )
                      )
                      continue
                  label_info.append([image_path, class_name])
  
      if len(label_info) == 0:
          return None
  
      # get first num_plot info
      if len(label_info) > num_plot:
          label_info = label_info[:num_plot]
  
      num_imgs = len(label_info)
      images = []
      labels = []
      for img_path, class_name in label_info:
          # read img
          img = Image.open(img_path).convert("RGB")
          label_index = int(class_indices[class_name])
  
          # preprocessing
          img = transform(img)
          images.append(img)
          labels.append(label_index)
  
      # batching images
      images = torch.stack(images, dim=0).to(device)
  
      # inference
      with torch.no_grad():
          output = net(images)
          probs, preds = torch.max(torch.softmax(output, dim=1), dim=1)
          probs = probs.cpu().numpy()
          preds = preds.cpu().numpy()
  
      # width, height
      fig = plt.figure(figsize=(num_imgs * 2.5, 3), dpi=100)
      for i in range(num_imgs):
          # 1：子图共1行，num_imgs:子图共num_imgs列，当前绘制第i+1个子图
          ax = fig.add_subplot(1, num_imgs, i + 1, xticks=[], yticks=[])
  
          # CHW -> HWC
          npimg = images[i].cpu().numpy().transpose(1, 2, 0)
  
          # 将图像还原至标准化之前
          # mean:[0.485, 0.456, 0.406], std:[0.229, 0.224, 0.225]
          npimg = (
              npimg * [0.229, 0.224, 0.225] + [0.485, 0.456, 0.406]
          ) * 255
          plt.imshow(npimg.astype("uint8"))
  
          title = "{}, {:.2f}%\n(label: {})".format(
              flower_class[str(preds[i])],  # predict class
              probs[i] * 100,  # predict probability
              flower_class[str(labels[i])],  # true class
          )
          ax.set_title(
              title, color=("green" if preds[i] == labels[i] else "red")
          )
  
      return fig
  
file_path: deep-learning-for-image-processing/pytorch_classification/tensorboard_test/data_utils.py
imports: import os
  import json
  import pickle
  import random
  from PIL import Image
  import torch
  import numpy
  import matplotlib.pyplot
label_desc: Use image node_id 5 for calling a global function with example usage: plot_class_preds(net, images_dir, transform, num_plot, device) and returns: fig, None, None, None