code: def val(args, model, test_dataloader, class_name):
      print(
          datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
          + "\t"
          + "Starting eval model..."
      )
  
      gt_list = []
      gt_mask_list = []
      test_imgs = []
      score_map = []
      paddle.device.set_device("gpu")
      # extract test set features
      for x, y, mask in tqdm(
          test_dataloader,
          "| feature extraction | test | %s |" % class_name,
      ):
          test_imgs.extend(x.cpu().detach().numpy())
          gt_list.extend(y.cpu().detach().numpy())
          gt_mask_list.extend(mask.cpu().detach().numpy())
          # model prediction
          out = model(x)
          out = model.project(out)
          score_map.append(model.generate_scores_map(out, x.shape[-2:]))
      del out
      score_map, image_score = list(zip(*score_map))
      score_map = np.concatenate(score_map, 0)
      image_score = np.concatenate(image_score, 0)
  
      # Normalization
      max_score = score_map.max()
      min_score = score_map.min()
      score_map = (score_map - min_score) / (max_score - min_score)
      print(f"max_score:{max_score} min_score:{min_score}")
      # calculate image-level ROC AUC score
      gt_list = np.asarray(gt_list)
      # fpr, tpr, _ = roc_curve(gt_list, image_score)
      img_auroc = compute_roc_score(
          gt_list,
          image_score,
          args.eval_threthold_step,
          args.non_partial_AUC,
      )
      # get optimal threshold
      precision, recall, thresholds = precision_recall_curve(
          gt_list, image_score
      )
      a = 2 * precision * recall
      b = precision + recall
      f1 = np.divide(a, b, out=np.zeros_like(a), where=b != 0)
      threshold = thresholds[np.argmax(f1)]
      print(f"F1 image:{f1.max()} threshold:{max_score}")
      # calculate per-pixel level ROCAUC
      gt_mask = np.asarray(gt_mask_list, dtype=np.int64).squeeze()
      # fpr, tpr, _ = roc_curve(gt_mask.flatten(), scores.flatten())
      per_pixel_auroc = compute_roc_score(
          gt_mask.flatten(),
          score_map.flatten(),
          args.eval_threthold_step,
          args.non_partial_AUC,
      )
      # get optimal threshold
      precision, recall, thresholds = precision_recall_curve(
          gt_mask.flatten(), score_map.flatten()
      )
      a = 2 * precision * recall
      b = precision + recall
      f1 = np.divide(a, b, out=np.zeros_like(a), where=b != 0)
      threshold = thresholds[np.argmax(f1)]
      print(f"F1 pixel:{f1.max()} threshold:{max_score}")
  
      # calculate Per-Region-Overlap Score
      total_PRO = (
          compute_pro_score(
              gt_mask,
              score_map,
              args.eval_threthold_step,
              args.non_partial_AUC,
          )
          if args.eval_PRO
          else None
      )
  
      print([class_name, img_auroc, per_pixel_auroc, total_PRO])
      if args.save_pic:
          save_dir = os.path.join(
              args.save_path
              + f"/{args.method}_{args.backbone}_{args.k}",
              class_name,
          )
          os.makedirs(save_dir, exist_ok=True)
          plot_fig(
              test_imgs,
              score_map,
              gt_mask_list,
              threshold,
              save_dir,
              class_name,
          )
      return img_auroc, per_pixel_auroc, total_PRO
  
imports: import os
  import sys
  import random
  import argparse
  import datetime
  import numpy
  import pandas
  import matplotlib.pyplot
  from tqdm import tqdm
  from sklearn.metrics import roc_curve, precision_recall_curve
  import paddle
  from paddle.io import DataLoader
  import qinspector.uad.datasets.mvtec
  from qinspector.uad.models.patchcore import get_model
  from qinspector.uad.utils.utils import *
  from qinspector.cvlib.uad_configs import ConfigParser
label_desc: Use image node_id 3 for calling a global function with example usage: val(args, model, test_dataloader, class_name) and returns: img_auroc,  per_pixel_auroc,  total_PRO