code: def valid(
      model,
      valloader,
      input_size,
      num_samples,
      dir=None,
      dir_edge=None,
      dir_img=None,
  ):
      height = input_size[0]
      width = input_size[1]
      with torch.autograd.profiler.profile(
          enabled=True,
          use_cuda=True,
          record_shapes=False,
          profile_memory=False,
      ) as prof:
          model.eval()
          parsing_preds = np.zeros(
              (num_samples, height, width), dtype=np.uint8
          )
          scales = np.zeros((num_samples, 2), dtype=np.float32)
          centers = np.zeros((num_samples, 2), dtype=np.int32)
  
          idx = 0
          interp = torch.nn.Upsample(
              size=(height, width), mode="bilinear", align_corners=True
          )
  
          with torch.no_grad():
              for index, batch in enumerate(valloader):
                  image, meta = batch
                  num_images = image.size(0)
                  if index % 10 == 0:
                      print("%d  processd" % (index * num_images))
  
                  c = meta["center"].numpy()
                  s = meta["scale"].numpy()
                  scales[idx : idx + num_images, :] = s[:, :]
                  centers[idx : idx + num_images, :] = c[:, :]
  
                  results = model(image.cuda())
                  outputs = results
  
                  if isinstance(results, list):
                      outputs = results[0]
  
                  if isinstance(outputs, list):
                      for k, output in enumerate(outputs):
                          parsing = output
                          nums = len(parsing)
                          parsing = interp(parsing).data.cpu().numpy()
                          parsing = parsing.transpose(
                              0, 2, 3, 1
                          )  # NCHW NHWC
                          parsing_preds[
                              idx : idx + nums, :, :
                          ] = np.asarray(
                              np.argmax(parsing, axis=3), dtype=np.uint8
                          )
                          idx += nums
                  else:
                      parsing = outputs
                      parsing = interp(parsing).data.cpu().numpy()
                      parsing = parsing.transpose(
                          0, 2, 3, 1
                      )  # NCHW NHWC
                      parsing_preds[
                          idx : idx + num_images, :, :
                      ] = np.asarray(
                          np.argmax(parsing, axis=3), dtype=np.uint8
                      )
  
                      if dir is not None:
                          for i in range(len(meta["name"])):
                              cv2.imwrite(
                                  os.path.join(
                                      dir, meta["name"][i] + ".png"
                                  ),
                                  np.asarray(
                                      np.argmax(parsing, axis=3)
                                  )[i],
                              )
                      idx += num_images
          parsing_preds = parsing_preds[:num_samples, :, :]
  
      return parsing_preds, scales, centers
  
file_path: insightface/parsing/dml_csr/test.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import argparse
  import cv2
  import numpy
  import matplotlib.pyplot
  import os
  import torch
  import torch.backends.cudnn
  import torchvision.transforms
  from copy import deepcopy
  from inplace_abn import InPlaceABN
  from dataset import datasets
  from networks import dml_csr
  from utils import miou
label_desc: Use image node_id 2 for calling a global function with example usage: valid(model, valloader, input_size, num_samples, dir, dir_edge, dir_img) and returns: parsing_preds,  scales,  centers