code: def run_training_test(
      root_dir, device="cuda:0", amp=False, num_workers=4
  ):
      images = sorted(glob(os.path.join(root_dir, "img*.nii.gz")))
      segs = sorted(glob(os.path.join(root_dir, "seg*.nii.gz")))
      train_files = [
          {"image": img, "label": seg}
          for img, seg in zip(images[:20], segs[:20])
      ]
      val_files = [
          {"image": img, "label": seg}
          for img, seg in zip(images[-20:], segs[-20:])
      ]
  
      # define transforms for image and segmentation
      train_transforms = Compose(
          [
              LoadImaged(keys=["image", "label"]),
              EnsureChannelFirstd(
                  keys=["image", "label"], channel_dim=-1
              ),
              ScaleIntensityd(keys=["image", "label"]),
              RandCropByPosNegLabeld(
                  keys=["image", "label"],
                  label_key="label",
                  spatial_size=[96, 96, 96],
                  pos=1,
                  neg=1,
                  num_samples=4,
              ),
              RandRotate90d(
                  keys=["image", "label"], prob=0.5, spatial_axes=[0, 2]
              ),
          ]
      )
      val_transforms = Compose(
          [
              LoadImaged(keys=["image", "label"]),
              EnsureChannelFirstd(
                  keys=["image", "label"], channel_dim=-1
              ),
              ScaleIntensityd(keys=["image", "label"]),
          ]
      )
  
      # create a training data loader
      train_ds = monai.data.CacheDataset(
          data=train_files, transform=train_transforms, cache_rate=0.5
      )
      # use batch_size=2 to load images and use RandCropByPosNegLabeld to generate 2 x 4 images for network training
      train_loader = monai.data.DataLoader(
          train_ds, batch_size=2, shuffle=True, num_workers=num_workers
      )
      # create a validation data loader
      val_ds = monai.data.CacheDataset(
          data=val_files, transform=val_transforms, cache_rate=1.0
      )
      val_loader = monai.data.DataLoader(
          val_ds, batch_size=1, num_workers=num_workers
      )
  
      # create UNet, DiceLoss and Adam optimizer
      net = monai.networks.nets.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)
      loss = monai.losses.DiceLoss(sigmoid=True)
      opt = torch.optim.Adam(net.parameters(), 1e-3)
      lr_scheduler = torch.optim.lr_scheduler.StepLR(
          opt, step_size=2, gamma=0.1
      )
      summary_writer = SummaryWriter(log_dir=root_dir)
  
      val_postprocessing = Compose(
          [
              Activationsd(keys="pred", sigmoid=True),
              AsDiscreted(keys="pred", threshold=0.5),
              KeepLargestConnectedComponentd(
                  keys="pred", applied_labels=[1]
              ),
          ]
      )
  
      class _TestEvalIterEvents:
          def attach(self, engine):
              engine.add_event_handler(
                  IterationEvents.FORWARD_COMPLETED,
                  self._forward_completed,
              )
  
          def _forward_completed(self, engine):
              pass
  
      val_handlers = [
          StatsHandler(iteration_log=False),
          TensorBoardStatsHandler(
              summary_writer=summary_writer, iteration_log=False
          ),
          TensorBoardImageHandler(
              log_dir=root_dir,
              batch_transform=from_engine(["image", "label"]),
              output_transform=from_engine("pred"),
          ),
          CheckpointSaver(
              save_dir=root_dir,
              save_dict={"net": net},
              save_key_metric=True,
          ),
          _TestEvalIterEvents(),
      ]
  
      evaluator = SupervisedEvaluator(
          device=device,
          val_data_loader=val_loader,
          network=net,
          inferer=SlidingWindowInferer(
              roi_size=(96, 96, 96), sw_batch_size=4, overlap=0.5
          ),
          postprocessing=val_postprocessing,
          key_val_metric={
              "val_mean_dice": MeanDice(
                  include_background=True,
                  output_transform=from_engine(["pred", "label"]),
              )
          },
          additional_metrics={
              "val_acc": Accuracy(
                  output_transform=from_engine(["pred", "label"])
              )
          },
          metric_cmp_fn=lambda cur, prev: cur
          >= prev,  # if greater or equal, treat as new best metric
          val_handlers=val_handlers,
          amp=bool(amp),
          to_kwargs={"memory_format": torch.preserve_format},
          amp_kwargs={
              "dtype": torch.float16 if bool(amp) else torch.float32
          }
          if pytorch_after(1, 10, 0)
          else {},
      )
  
      train_postprocessing = Compose(
          [
              Activationsd(keys="pred", sigmoid=True),
              AsDiscreted(keys="pred", threshold=0.5),
              KeepLargestConnectedComponentd(
                  keys="pred", applied_labels=[1]
              ),
          ]
      )
  
      class _TestTrainIterEvents:
          def attach(self, engine):
              engine.add_event_handler(
                  IterationEvents.FORWARD_COMPLETED,
                  self._forward_completed,
              )
              engine.add_event_handler(
                  IterationEvents.LOSS_COMPLETED, self._loss_completed
              )
              engine.add_event_handler(
                  IterationEvents.BACKWARD_COMPLETED,
                  self._backward_completed,
              )
              engine.add_event_handler(
                  IterationEvents.MODEL_COMPLETED, self._model_completed
              )
  
          def _forward_completed(self, engine):
              pass
  
          def _loss_completed(self, engine):
              pass
  
          def _backward_completed(self, engine):
              pass
  
          def _model_completed(self, engine):
              pass
  
      train_handlers = [
          LrScheduleHandler(lr_scheduler=lr_scheduler, print_lr=True),
          ValidationHandler(
              validator=evaluator, interval=2, epoch_level=True
          ),
          StatsHandler(
              tag_name="train_loss",
              output_transform=from_engine("loss", first=True),
          ),
          TensorBoardStatsHandler(
              summary_writer=summary_writer,
              tag_name="train_loss",
              output_transform=from_engine("loss", first=True),
          ),
          CheckpointSaver(
              save_dir=root_dir,
              save_dict={"net": net, "opt": opt},
              save_interval=2,
              epoch_level=True,
          ),
          _TestTrainIterEvents(),
      ]
  
      trainer = SupervisedTrainer(
          device=device,
          max_epochs=5,
          train_data_loader=train_loader,
          network=net,
          optimizer=opt,
          loss_function=loss,
          inferer=SimpleInferer(),
          postprocessing=train_postprocessing,
          key_train_metric={
              "train_acc": Accuracy(
                  output_transform=from_engine(["pred", "label"])
              )
          },
          train_handlers=train_handlers,
          amp=bool(amp),
          optim_set_to_none=True,
          to_kwargs={"memory_format": torch.preserve_format},
          amp_kwargs={
              "dtype": torch.float16 if bool(amp) else torch.float32
          }
          if pytorch_after(1, 10, 0)
          else {},
      )
      trainer.run()
  
      # test train and validation stats
      train_stats = trainer.get_stats("output")
      assert_allclose(
          train_stats["output"][0]["loss"],
          trainer.state.output[0]["loss"],
      )
      val_stats = evaluator.get_stats("metrics")
  
      return val_stats["best_validation_metric"]
  
imports: from __future__ import annotations
  import os
  import shutil
  import tempfile
  import unittest
  import warnings
  from glob import glob
  import nibabel
  import numpy
  import torch
  from ignite.engine import Events
  from ignite.metrics import Accuracy
  import monai
  from monai.data import create_test_image_3d
  from monai.engines import IterationEvents, SupervisedEvaluator, SupervisedTrainer
  from monai.handlers import CheckpointLoader, CheckpointSaver, LrScheduleHandler, MeanDice, StatsHandler, TensorBoardImageHandler, TensorBoardStatsHandler, ValidationHandler, from_engine
  from monai.inferers import SimpleInferer, SlidingWindowInferer
  from monai.transforms import Activationsd, AsDiscreted, Compose, EnsureChannelFirstd, KeepLargestConnectedComponentd, LoadImaged, RandCropByPosNegLabeld, RandRotate90d, SaveImage, SaveImaged, ScaleIntensityd
  from monai.utils import optional_import, set_determinism
  from tests.testing_data.integration_answers import test_integration_value
  from tests.utils import DistTestCase, TimedCall, assert_allclose, pytorch_after, skip_if_quick
label_desc: Use image node_id 1 for calling a global function with example usage: run_training_test(root_dir, device, amp, num_workers) and returns: val_stats