code: def _assert_approx_equal_tensor_to_pil(
      tensor,
      pil_image,
      tol=1e-5,
      msg=None,
      agg_method="mean",
      allowed_percentage_diff=None,
  ):
      # FIXME: this is handled automatically by `assert_close` below. Let's remove this in favor of it
      # TODO: we could just merge this into _assert_equal_tensor_to_pil
      np_pil_image = np.array(pil_image)
      if np_pil_image.ndim == 2:
          np_pil_image = np_pil_image[:, :, None]
      pil_tensor = torch.as_tensor(
          np_pil_image.transpose((2, 0, 1))
      ).to(tensor)
  
      if allowed_percentage_diff is not None:
          # Assert that less than a given %age of pixels are different
          assert (tensor != pil_tensor).to(
              torch.float
          ).mean() <= allowed_percentage_diff
  
      # error value can be mean absolute error, max abs error
      # Convert to float to avoid underflow when computing absolute difference
      tensor = tensor.to(torch.float)
      pil_tensor = pil_tensor.to(torch.float)
      err = getattr(torch, agg_method)(
          torch.abs(tensor - pil_tensor)
      ).item()
      assert err < tol, f"{err} vs {tol}"
  
imports: import contextlib
  import functools
  import itertools
  import os
  import pathlib
  import random
  import re
  import shutil
  import sys
  import tempfile
  import warnings
  from subprocess import CalledProcessError, check_output, STDOUT
  import numpy
  import PIL.Image
  import pytest
  import torch
  import torch.testing
  from PIL import Image
  from torch.testing._comparison import BooleanPair, NonePair, not_close_error_metas, NumberPair, TensorLikePair
  from torchvision import io, tv_tensors
  from torchvision.transforms._functional_tensor import _max_value
  from torchvision.transforms.v2.functional import to_image, to_pil_image
label_desc: Use image node_id 18 for calling a global function with example usage: _assert_approx_equal_tensor_to_pil(tensor, pil_image, tol, msg, agg_method, allowed_percentage_diff) without return types