code: def generate_anchors(
      dataset, n=9, img_size=416, thr=4.0, gen=1000, verbose=True
  ):
      """Creates kmeans-evolved anchors from training dataset
  
      Arguments:
          dataset: a loaded dataset i.e. subclass of torch.utils.data.Dataset
          n: number of anchors
          img_size: image size used for training
          thr: anchor-label wh ratio threshold used for training, default=4.0
          gen: generations to evolve anchors using genetic algorithm
          verbose: print all results
  
      Return:
          k: kmeans evolved anchors
      """
      thr = 1 / thr
  
      def metric(k, wh):  # compute metrics
          r = wh[:, None] / k[None]
          x = torch.min(r, 1 / r).min(2)[0]  # ratio metric
          return x, x.max(1)[0]  # x, best_x
  
      def anchor_fitness(k):  # mutation fitness
          _, best = metric(torch.tensor(k, dtype=torch.float32), wh)
          return (best * (best > thr).float()).mean()  # fitness
  
      def print_results(k, verbose=True):
          k = k[np.argsort(k.prod(1))]  # sort small to large
          if verbose:
              x, best = metric(k, wh0)
              bpr, aat = (best > thr).float().mean(), (
                  x > thr
              ).float().mean() * n  # best possible recall, anch > thr
              s = (
                  f"thr={thr:.2f}: {bpr:.4f} best possible recall, {aat:.2f} anchors past thr\n"
                  f"n={n}, img_size={img_size}, metric_all={x.mean():.3f}/{best.mean():.3f}-mean/best, "
                  f"past_thr={x[x > thr].mean():.3f}-mean: "
              )
              print(s)
          return k
  
      # Get label wh
      shapes = (
          img_size
          * dataset.shapes
          / dataset.shapes.max(1, keepdims=True)
      )
      wh0 = np.concatenate(
          [l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]
      )  # wh
  
      # Filter
      i = (wh0 < 3.0).any(1).sum()
      if i and verbose:
          print(
              f"WARNING: Extremely small objects found. {i} of {len(wh0)} labels are < 3 pixels in size."
          )
      wh = wh0[(wh0 >= 2.0).any(1)]  # filter > 2 pixels
      # wh = wh * (np.random.rand(wh.shape[0], 1) * 0.9 + 0.1)  # multiply by random scale 0-1
  
      # Kmeans calculation
      s = wh.std(0)  # sigmas for whitening
      k, dist = kmeans(wh / s, n, iter=30)  # points, mean distance
      assert (
          len(k) == n
      ), f"ERROR: scipy.cluster.vq.kmeans requested {n} points but returned only {len(k)}"
      k *= s
      wh = torch.tensor(wh, dtype=torch.float32)  # filtered
      wh0 = torch.tensor(wh0, dtype=torch.float32)  # unfiltered
      k = print_results(k, verbose=False)
  
      # Evolve
      npr = np.random
      f, sh, mp, s = (
          anchor_fitness(k),
          k.shape,
          0.9,
          0.1,
      )  # fitness, generations, mutation prob, sigma
      if verbose:
          print("Generating anchor boxes for training images...")
      for _ in range(gen):
          v = np.ones(sh)
          while (
              v == 1
          ).all():  # mutate until a change occurs (prevent duplicates)
              v = (
                  (npr.random(sh) < mp)
                  * random.random()
                  * npr.randn(*sh)
                  * s
                  + 1
              ).clip(0.3, 3.0)
          kg = (k.copy() * v).clip(min=2.0)
          fg = anchor_fitness(kg)
          if fg > f:
              f, k = fg, kg.copy()
  
      return print_results(k)
  
imports: import random
  import torch
  import numpy
  from scipy.cluster.vq import kmeans
label_desc: Use image node_id 1 for calling a global function with example usage: generate_anchors(dataset, n, img_size, thr, gen, verbose) and returns: print_results, x,  x, unknown, k