code: def run_inference_test(root_dir, device="cuda:0"):
      images = sorted(glob(os.path.join(root_dir, "im*.nii.gz")))
      segs = sorted(glob(os.path.join(root_dir, "seg*.nii.gz")))
      val_files = [
          {"img": img, "seg": seg} for img, seg in zip(images, segs)
      ]
  
      saver = SaveImage(
          output_dir=os.path.join(root_dir, "output"),
          dtype=np.float32,
          output_ext=".nii.gz",
          output_postfix="seg",
          mode="bilinear",
      )
      # define transforms for image and segmentation
      val_transforms = Compose(
          [
              LoadImaged(keys=["img", "seg"]),
              EnsureChannelFirstd(keys=["img", "seg"]),
              # resampling with align_corners=True or dtype=float64 will generate
              # slight different results between PyTorch 1.5 an 1.6
              Spacingd(
                  keys=["img", "seg"],
                  pixdim=[1.2, 0.8, 0.7],
                  mode=["bilinear", "nearest"],
                  dtype=np.float32,
              ),
              ScaleIntensityd(keys="img"),
          ]
      )
      val_ds = monai.data.Dataset(
          data=val_files, transform=val_transforms
      )
      # sliding window inference need to input 1 image in every iteration
      val_loader = monai.data.DataLoader(
          val_ds, batch_size=1, num_workers=4
      )
      val_post_tran = Compose(
          [Activations(sigmoid=True), AsDiscrete(threshold=0.5), saver]
      )
      dice_metric = DiceMetric(
          include_background=True, reduction="mean", get_not_nans=False
      )
  
      model = UNet(
          spatial_dims=3,
          in_channels=1,
          out_channels=1,
          channels=(16, 32, 64, 128, 256),
          strides=(2, 2, 2, 2),
          num_res_units=2,
      ).to(device)
  
      model_filename = os.path.join(root_dir, "best_metric_model.pth")
      model.load_state_dict(torch.load(model_filename))
      with eval_mode(model):
          # resampling with align_corners=True or dtype=float64 will generate
          # slight different results between PyTorch 1.5 an 1.6
          for val_data in val_loader:
              val_images, val_labels = val_data["img"].to(
                  device
              ), val_data["seg"].to(device)
              # define sliding window size and batch size for windows inference
              sw_batch_size, roi_size = 4, (96, 96, 96)
              val_outputs = sliding_window_inference(
                  val_images, roi_size, sw_batch_size, model
              )
              # decollate prediction into a list
              val_outputs = [
                  val_post_tran(i) for i in decollate_batch(val_outputs)
              ]
              # compute metrics
              dice_metric(y_pred=val_outputs, y=val_labels)
  
      return dice_metric.aggregate().item()
  
imports: from __future__ import annotations
  import os
  import shutil
  import tempfile
  import unittest
  from glob import glob
  import nibabel
  import numpy
  import torch
  import monai
  from monai.data import create_test_image_3d, decollate_batch
  from monai.inferers import sliding_window_inference
  from monai.metrics import DiceMetric
  from monai.networks import eval_mode
  from monai.networks.nets import UNet
  from monai.transforms import Activations, AsDiscrete, Compose, EnsureChannelFirstd, LoadImaged, RandCropByPosNegLabeld, RandRotate90d, SaveImage, ScaleIntensityd, Spacingd
  from monai.utils import optional_import, set_determinism
  from monai.visualize import plot_2d_or_3d_image
  from tests.testing_data.integration_answers import test_integration_value
  from tests.utils import DistTestCase, TimedCall, skip_if_quick
label_desc: Use image node_id 2 for calling a global function with example usage: run_inference_test(root_dir, device) and returns: dice_metric