code: def _evaluate(
      model,
      args,
      val_loader,
      *,
      padder_mode,
      print_freq=10,
      writer=None,
      step=None,
      iterations=10,
      cascades=1,
      batch_size=None,
      header=None,
      save_images=False,
      save_path="",
  ):
      """Helper function to compute various metrics (epe, etc.) for a model on a given dataset.
      We process as many samples as possible with ddp.
      """
      model.eval()
      header = header or "Test:"
      device = torch.device(args.device)
      metric_logger = utils.MetricLogger(delimiter="  ")
  
      iterations = iterations or args.recurrent_updates
  
      logger = utils.MetricLogger()
      for meter_name in args.metrics:
          logger.add_meter(meter_name, fmt="{global_avg:.4f}")
      if "fl-all" not in args.metrics:
          logger.add_meter("fl-all", fmt="{global_avg:.4f}")
  
      num_processed_samples = 0
      with torch.cuda.amp.autocast(
          enabled=args.mixed_precision, dtype=torch.float16
      ):
          batch_idx = 0
          for blob in metric_logger.log_every(
              val_loader, print_freq, header
          ):
              image_left, image_right, disp_gt, valid_disp_mask = (
                  x.to(device) for x in blob
              )
              padder = utils.InputPadder(
                  image_left.shape, mode=padder_mode
              )
              image_left, image_right = padder.pad(
                  image_left, image_right
              )
  
              disp_pred = cascade_inference(
                  model, image_left, image_right, iterations, cascades
              )
              disp_pred = disp_pred[:, :1, :, :]
              disp_pred = padder.unpad(disp_pred)
  
              if save_images:
                  if args.distributed:
                      rank_prefix = args.rank
                  else:
                      rank_prefix = 0
                  make_prediction_image_side_to_side(
                      disp_pred,
                      disp_gt,
                      valid_disp_mask,
                      save_path,
                      prefix=f"batch_{rank_prefix}_{batch_idx}",
                  )
  
              metrics, _ = utils.compute_metrics(
                  disp_pred,
                  disp_gt,
                  valid_disp_mask,
                  metrics=logger.meters.keys(),
              )
              num_processed_samples += image_left.shape[0]
              for name in metrics:
                  logger.meters[name].update(metrics[name], n=1)
  
              batch_idx += 1
  
      num_processed_samples = (
          utils.reduce_across_processes(num_processed_samples)
          / args.world_size
      )
  
      print("Num_processed_samples: ", num_processed_samples)
      if (
          hasattr(val_loader.dataset, "__len__")
          and len(val_loader.dataset) != num_processed_samples
          and torch.distributed.get_rank() == 0
      ):
          warnings.warn(
              f"Number of processed samples {num_processed_samples} is different"
              f"from the dataset size {len(val_loader.dataset)}. This may happen if"
              "the dataset is not divisible by the batch size. Try lowering the batch size for more accurate results."
          )
  
      if writer is not None and args.rank == 0:
          for meter_name, meter_value in logger.meters.items():
              scalar_name = f"{meter_name} {header}"
              writer.add_scalar(scalar_name, meter_value.avg, step)
  
      logger.synchronize_between_processes()
      print(header, logger)
  
      logger_metrics = {
          k: v.global_avg for k, v in logger.meters.items()
      }
      return logger_metrics
  
file_path: vision/references/depth/stereo/cascade_evaluation.py
imports: import os
  import warnings
  import torch
  import torchvision
  import torchvision.prototype.models.depth.stereo
  import utils
  from torch.nn import functional
  from train import make_eval_loader
  from utils.metrics import AVAILABLE_METRICS
  from visualization import make_prediction_image_side_to_side
label_desc: Use image node_id 3 for calling a global function with example usage: _evaluate(model, args, val_loader) and returns: logger_metrics