code: def _evaluate(
      model,
      args,
      val_dataset,
      *,
      padder_mode,
      num_flow_updates=None,
      batch_size=None,
      header=None,
  ):
      """Helper function to compute various metrics (epe, etc.) for a model on a given dataset.
  
      We process as many samples as possible with ddp, and process the rest on a single worker.
      """
      batch_size = batch_size or args.batch_size
      device = torch.device(args.device)
  
      model.eval()
  
      if args.distributed:
          sampler = torch.utils.data.distributed.DistributedSampler(
              val_dataset, shuffle=False, drop_last=True
          )
      else:
          sampler = torch.utils.data.SequentialSampler(val_dataset)
  
      val_loader = torch.utils.data.DataLoader(
          val_dataset,
          sampler=sampler,
          batch_size=batch_size,
          pin_memory=True,
          num_workers=args.workers,
      )
  
      num_flow_updates = num_flow_updates or args.num_flow_updates
  
      def inner_loop(blob):
          if blob[0].dim() == 3:
              # input is not batched, so we add an extra dim for consistency
              blob = [
                  x[None, :, :, :] if x is not None else None
                  for x in blob
              ]
  
          image1, image2, flow_gt = blob[:3]
          valid_flow_mask = None if len(blob) == 3 else blob[-1]
  
          image1, image2 = image1.to(device), image2.to(device)
  
          padder = utils.InputPadder(image1.shape, mode=padder_mode)
          image1, image2 = padder.pad(image1, image2)
  
          flow_predictions = model(
              image1, image2, num_flow_updates=num_flow_updates
          )
          flow_pred = flow_predictions[-1]
          flow_pred = padder.unpad(flow_pred).cpu()
  
          metrics, num_pixels_tot = utils.compute_metrics(
              flow_pred, flow_gt, valid_flow_mask
          )
  
          # We compute per-pixel epe (epe) and per-image epe (called f1-epe in RAFT paper).
          # per-pixel epe: average epe of all pixels of all images
          # per-image epe: average epe on each image independently, then average over images
          for name in (
              "epe",
              "1px",
              "3px",
              "5px",
              "f1",
          ):  # f1 is called f1-all in paper
              logger.meters[name].update(
                  metrics[name], n=num_pixels_tot
              )
          logger.meters["per_image_epe"].update(
              metrics["epe"], n=batch_size
          )
  
      logger = utils.MetricLogger()
      for meter_name in (
          "epe",
          "1px",
          "3px",
          "5px",
          "per_image_epe",
          "f1",
      ):
          logger.add_meter(meter_name, fmt="{global_avg:.4f}")
  
      num_processed_samples = 0
      for blob in logger.log_every(
          val_loader, header=header, print_freq=None
      ):
          inner_loop(blob)
          num_processed_samples += blob[0].shape[0]  # batch size
  
      if args.distributed:
          num_processed_samples = utils.reduce_across_processes(
              num_processed_samples
          )
          print(
              f"Batch-processed {num_processed_samples} / {len(val_dataset)} samples. "
              "Going to process the remaining samples individually, if any."
          )
          if (
              args.rank == 0
          ):  # we only need to process the rest on a single worker
              for i in range(num_processed_samples, len(val_dataset)):
                  inner_loop(val_dataset[i])
  
          logger.synchronize_between_processes()
  
      print(header, logger)
  
file_path: vision/references/optical_flow/train.py
imports: import argparse
  import warnings
  from math import ceil
  from pathlib import Path
  import torch
  import torchvision.models.optical_flow
  import utils
  from presets import OpticalFlowPresetEval, OpticalFlowPresetTrain
  from torchvision.datasets import FlyingChairs, FlyingThings3D, HD1K, KittiFlow, Sintel
label_desc: Use image node_id 2 for calling a global function with example usage: _evaluate(model, args, val_dataset) without return types