code: def main(args):
      print(args)
  
      torch.backends.cudnn.benchmark = True
  
      if not isinstance(args.workers, int):
          args.workers = min(16, mp.cpu_count())
  
      # Load doctr model
      model = recognition.__dict__[args.arch](
          pretrained=True if args.resume is None else False,
          input_shape=(3, args.input_size, 4 * args.input_size),
          vocab=VOCABS[args.vocab],
      ).eval()
  
      # Resume weights
      if isinstance(args.resume, str):
          print(f"Resuming {args.resume}")
          checkpoint = torch.load(args.resume, map_location="cpu")
          model.load_state_dict(checkpoint)
  
      st = time.time()
      ds = datasets.__dict__[args.dataset](
          train=True,
          download=True,
          recognition_task=True,
          use_polygons=args.regular,
          img_transforms=T.Resize(
              (args.input_size, 4 * args.input_size),
              preserve_aspect_ratio=True,
          ),
      )
  
      _ds = datasets.__dict__[args.dataset](
          train=False,
          download=True,
          recognition_task=True,
          use_polygons=args.regular,
          img_transforms=T.Resize(
              (args.input_size, 4 * args.input_size),
              preserve_aspect_ratio=True,
          ),
      )
      ds.data.extend((np_img, target) for np_img, target in _ds.data)
  
      test_loader = DataLoader(
          ds,
          batch_size=args.batch_size,
          drop_last=False,
          num_workers=args.workers,
          sampler=SequentialSampler(ds),
          pin_memory=torch.cuda.is_available(),
          collate_fn=ds.collate_fn,
      )
      print(
          f"Test set loaded in {time.time() - st:.4}s ({len(ds)} samples in "
          f"{len(test_loader)} batches)"
      )
  
      mean, std = model.cfg["mean"], model.cfg["std"]
      batch_transforms = Normalize(mean=mean, std=std)
  
      # Metrics
      val_metric = TextMatch()
  
      # GPU
      if isinstance(args.device, int):
          if not torch.cuda.is_available():
              raise AssertionError(
                  "PyTorch cannot access your GPU. Please investigate!"
              )
          if args.device >= torch.cuda.device_count():
              raise ValueError("Invalid device index")
      # Silent default switch to GPU if available
      elif torch.cuda.is_available():
          args.device = 0
      else:
          print("No accessible GPU, targe device set to CPU.")
      if torch.cuda.is_available():
          torch.cuda.set_device(args.device)
          model = model.cuda()
  
      print("Running evaluation")
      val_loss, exact_match, partial_match = evaluate(
          model, test_loader, batch_transforms, val_metric, amp=args.amp
      )
      print(
          f"Validation loss: {val_loss:.6} (Exact: {exact_match:.2%} | Partial: {partial_match:.2%})"
      )
  
file_path: doctr/references/recognition/evaluate_pytorch.py
imports: import os
  import multiprocessing
  import time
  import torch
  from torch.utils.data import DataLoader, SequentialSampler
  from torchvision.transforms import Normalize
  from tqdm import tqdm
  from doctr import datasets
  from doctr import transforms
  from doctr.datasets import VOCABS
  from doctr.models import recognition
  from doctr.utils.metrics import TextMatch
label_desc: Use image node_id 2 for calling a global function with example usage: main(args) without return types