code: def image_map(
      mapcoord: Union[np.ndarray, torch.Tensor],
      dataset: Union[
          torch.utils.data.dataloader.DataLoader,
          torch.utils.data.dataset.Dataset,
      ],
      mapsize: int = 5000,
      imsize: int = 32,
      unnormalize: Optional[Callable] = None,
      snap_to_grid: bool = False,
  ) -> torch.ByteTensor:
      """Constructs a 2D map of images.
  
      The 2D coordinates for each of the images are specified in `mapcoord`, the
      corresponding images are in `dataset`. Optional arguments set the size of
      the map images, the size of the images themselves, the unnormalization
      transform, and whether or not to snap images to a grid.
      """
  
      # assertions:
      if type(mapcoord) == np.ndarray:
          mapcoord = torch.from_numpy(mapcoord)
      assert torch.is_tensor(mapcoord)
      if isinstance(dataset, torch.utils.data.dataloader.DataLoader):
          dataset = dataset.dataset
      assert isinstance(dataset, torch.utils.data.dataset.Dataset)
      assert is_pos_int(mapsize)
      assert is_pos_int(imsize)
      if unnormalize is not None:
          assert callable(unnormalize)
  
      # initialize some variables:
      import torchvision.transforms.functional as F
  
      background = 255
      mapim = torch.ByteTensor(3, mapsize, mapsize).fill_(background)
  
      # normalize map coordinates:
      mapc = mapcoord.add(-(mapcoord.min()))
      mapc.div_(mapc.max())
  
      # loop over images:
      for idx in range(len(dataset)):
          # compute grid location:
          if snap_to_grid:
              y = 1 + int(
                  math.floor(mapc[idx][0] * (mapsize - imsize - 2))
              )
              x = 1 + int(
                  math.floor(mapc[idx][1] * (mapsize - imsize - 2))
              )
          else:
              y = 1 + int(
                  math.floor(
                      mapc[idx][0] * (math.floor(mapsize - imsize) - 2)
                  )
              )
              x = 1 + int(
                  math.floor(
                      mapc[idx][1] * (math.floor(mapsize - imsize) - 2)
                  )
              )
  
          # check whether we can overwrite this location:
          overwrite = not snap_to_grid
          if not overwrite:
              segment = mapim.narrow(1, y, imsize).narrow(2, x, imsize)
              overwrite = segment.eq(background).all()
  
          # draw image:
          if overwrite:
              # load, unnormalize, and resize image:
              image = dataset[idx][0]
              if unnormalize is not None:
                  image = unnormalize(image)
              resized_im = F.to_tensor(
                  F.resize(
                      F.to_pil_image(image), imsize, Image.BILINEAR
                  )
              )
  
              # place image:
              segment = mapim.narrow(1, y, imsize).narrow(2, x, imsize)
              segment.copy_(resized_im.mul_(255.0).byte())
  
      # return map:
      return mapim
  
file_path: ClassyVision/classy_vision/generic/visualize.py
imports: import math
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  import torch.nn.modules
  from classy_vision.generic.util import get_model_dummy_input, is_pos_int
  from classy_vision.models import ClassyModel
  from PIL import Image
label_desc: Use image node_id 6 for calling a global function with example usage: image_map(mapcoord, dataset, mapsize, imsize, unnormalize, snap_to_grid) and returns: mapim