code: def validate(val_loader, pose_model, epoch, config, factor=0.6451607):
      # parse the config
      num_frames = config.DATA.NUM_FRAMES
      use_same_norm_3d = config.DATA.USE_SAME_NORM_3D
      prefix = "mpi" if config.DATA.DATASET_NAME == "mpi" else "h36m"
      pose_model.eval()
      prefix = config.DATA.DATASET_NAME
      # for 10 w
      # factor = 0.680019 if prefix == "h36m" else 0.7577316
      if prefix == "surreal":
          factor = 0.56071004
      else:
          factor = 0.680019 if prefix == "h36m" else 0.7577316
      root_idx = 13 if config.DATA.NUM_JOINTS == 17 else 12
      if not use_same_norm_3d:
          with open(f"../data/{prefix}_test_factor_3d.pkl", "rb") as f:
              factor = torch.from_numpy(pkl.load(f)).cuda()
      center_idx = int((num_frames - 1) / 2)
      valid_cnt = 0
      p1_mpjpe, p2_mpjpe = 0.0, 0.0
      pck_3d = 0.0
      all_scale_mids_pre = []
      pred_3ds, gt_3ds = [], []
      with torch.no_grad():
          t = tqdm(val_loader, desc=f"Epoch: {epoch}, testing...")
          for kp2ds, kp3ds, rot, _, _, _ in t:
              if valid_cnt == 0:
                  bs = kp2ds.size(0)
              kp2ds = kp2ds.cuda()
              kp3ds = kp3ds.cuda()
              rot = rot.cuda()
              pred_3d, _, scale_mids = pose_model(
                  kp2ds, is_train=False, rot=rot
              )
              root3d, root3d_pred = (
                  kp3ds[:, root_idx : root_idx + 1].clone(),
                  pred_3d[:, root_idx : root_idx + 1].clone(),
              )
              kp3ds, pred_3d = kp3ds - root3d, pred_3d - root3d_pred
              # rescale back to original scale from 1 unit
              if not config.TRAIN.GENERIC_BASELINE:
                  if not use_same_norm_3d:
                      pred_3d = (
                          pred_3d
                          * factor[
                              int(bs * valid_cnt) : int(
                                  bs * (valid_cnt + 1)
                              )
                          ]
                      )
                  else:
                      pred_3d = pred_3d * factor
              # all_scale_mids_pre.extend(scale_mids.cpu().numpy().tolist())
              p1_err = (pred_3d - kp3ds).pow(2).sum(
                  dim=-1
              ).sqrt().mean().item() * 1000.0
              p2_err = (
                  p_mpjpe(pred_3d.cpu().numpy(), kp3ds.cpu().numpy())
                  * 1000.0
              )
              p1_mpjpe += p1_err
              p2_mpjpe += p2_err
              if prefix == "mpi":
                  pred_3ds.append(pred_3d.cpu().numpy())
                  gt_3ds.append(kp3ds.cpu().numpy())
              valid_cnt += 1
          # do some visualization
          val_iter = iter(val_loader)
          num = np.random.randint(0, len(val_loader) - 1)
          for i in range(num):
              _, _, _, _, _, _ = next(val_iter)
          kp2ds, kp3ds, rot, _, _, _ = next(val_iter)
          kp2ds = kp2ds.cuda()
          kp3ds = kp3ds.cuda()
          kp3ds = kp3ds.cuda()
          pred_3d, _, _ = pose_model(kp2ds, is_train=False, rot=rot)
          joints = {
              "2d input": kp2ds.cpu().numpy(),
              "lift 3d": pred_3d.cpu().numpy(),
              "gt 3d": kp3ds.cpu().numpy(),
          }
          vis_image = (
              vis_joints(joints)
              if config.DATA.NUM_JOINTS > 15
              else np.zeros((200, 200, 3), dtype=np.uint8)
          )
          vis_image = (
              torch.from_numpy(vis_image)
              .type(torch.float32)
              .permute(2, 0, 1)
              / 255
          )
      p1_mpjpe /= valid_cnt
      p2_mpjpe /= valid_cnt
      if prefix == "mpi":
          auc_val, pckh5 = calc_auc(
              np.concatenate(pred_3ds, axis=0),
              np.concatenate(gt_3ds, axis=0),
          )
          auc_val_p2, pckh5_p2 = calc_auc_aligned(
              np.concatenate(pred_3ds, axis=0),
              np.concatenate(gt_3ds, axis=0),
          )
          extra_dict = {
              "auc": auc_val,
              "pckh5": pckh5,
              "auc_p2": auc_val_p2,
              "pckh5_p2": pckh5_p2,
          }
          del pred_3ds, gt_3ds
      else:
          extra_dict = None
      return (
          p1_mpjpe,
          p2_mpjpe,
          vis_image,
          all_scale_mids_pre,
          extra_dict,
      )
  
file_path: insightface/body/human_pose/ambiguity_aware/lib/core/function1.py
imports: import time
  import pickle
  import os.path
  import numpy
  import torch
  import torch.nn.functional
  import os
  from tqdm import tqdm
  from lib.core.loss import *
  from lib.utils.vis import vis_joints
  from lib.utils.utils import p_mpjpe, get_pck3d, calc_auc, calc_auc_aligned
label_desc: Use image node_id 2 for calling a global function with example usage: validate(val_loader, pose_model, epoch, config, factor) and returns: p1_mpjpe,  p2_mpjpe,  vis_image,  all_scale_mids_pre,  extra_dict