code: def setup_snapshot_image_grid(
      training_set,
      size="1080p",  # '1080p' = to be viewed on 1080p display, '4k' = to be viewed on 4k display.
      layout="random",
  ):  # 'random' = grid contents are selected randomly, 'row_per_class' = each row corresponds to one class label.
      # Select size.
      gw = 1
      gh = 1
      if size == "1080p":
          gw = np.clip(1920 // training_set.shape[2], 3, 32)
          gh = np.clip(1080 // training_set.shape[1], 2, 32)
      if size == "4k":
          gw = np.clip(3840 // training_set.shape[2], 7, 32)
          gh = np.clip(2160 // training_set.shape[1], 4, 32)
      if size == "8k":
          gw = np.clip(7680 // training_set.shape[2], 7, 32)
          gh = np.clip(4320 // training_set.shape[1], 4, 32)
  
      # Initialize data arrays.
      reals = np.zeros(
          [gw * gh] + training_set.shape, dtype=training_set.dtype
      )
      labels = np.zeros(
          [gw * gh, training_set.label_size],
          dtype=training_set.label_dtype,
      )
  
      # Random layout.
      if layout == "random":
          reals[:], labels[:] = training_set.get_minibatch_np(gw * gh)
  
      # Class-conditional layouts.
      class_layouts = dict(
          row_per_class=[gw, 1], col_per_class=[1, gh], class4x4=[4, 4]
      )
      if layout in class_layouts:
          bw, bh = class_layouts[layout]
          nw = (gw - 1) // bw + 1
          nh = (gh - 1) // bh + 1
          blocks = [[] for _i in range(nw * nh)]
          for _iter in range(1000000):
              real, label = training_set.get_minibatch_np(1)
              idx = np.argmax(label[0])
              while idx < len(blocks) and len(blocks[idx]) >= bw * bh:
                  idx += training_set.label_size
              if idx < len(blocks):
                  blocks[idx].append((real, label))
                  if all(len(block) >= bw * bh for block in blocks):
                      break
          for i, block in enumerate(blocks):
              for j, (real, label) in enumerate(block):
                  x = (i % nw) * bw + j % bw
                  y = (i // nw) * bh + j // bw
                  if x < gw and y < gh:
                      reals[x + y * gw] = real[0]
                      labels[x + y * gw] = label[0]
  
      return (gw, gh), reals, labels
  
file_path: insightface/reconstruction/ostec/external/stylegan2/training/misc.py
imports: import os
  import pickle
  import numpy
  import PIL.Image
  import PIL.ImageFont
  import dnnlib
label_desc: Use image node_id 10 for calling a global function with example usage: setup_snapshot_image_grid(training_set, size, layout) and returns: gw,  gh,  reals,  labels