code: def build_targets(p, targets, loss_layers, device="cpu"):
      # Build targets for compute_loss(), input targets(image,class,x,y,w,h)
      na, nt = (
          len(loss_layers[0].anchors),
          targets.shape[0],
      )  # number of anchors, targets
      tcls, tbox, indices, anch = [], [], [], []
      gain = torch.ones(
          7, device=device
      )  # normalized to gridspace gain
      # Make a tensor that iterates 0-2 for 3 anchors and repeat that as many times as we have target boxes
      ai = (
          torch.arange(na, device=device)
          .float()
          .view(na, 1)
          .repeat(1, nt)
      )
      # Copy target boxes anchor size times and append an anchor index to each copy the anchor index is also expressed by the new first dimension
      targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2)
  
      g = 0.5
      off = (
          torch.tensor(
              [[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]], device=device
          ).float()
          * g
      )  # offsets
  
      for i, yolo_layer in enumerate(loss_layers):
          # Scale anchors by the yolo grid cell size so that an anchor with the size of the cell would result in 1
          anchors = yolo_layer.anchors / yolo_layer.stride
          # Add the number of yolo cells in this layer the gain tensor
          # The gain tensor matches the collums of our targets (img id, class, x, y, w, h, anchor id)
          gain[2:6] = torch.tensor(p[i].shape)[
              [3, 2, 3, 2]
          ]  # xyxy gain
          # Scale targets by the number of yolo layer cells, they are now in the yolo cell coordinate system
          t = targets * gain
          # Check if we have targets
          if nt:
              # Calculate ration between anchor and target box for both width and height
              r = t[:, :, 4:6] / anchors[:, None]
              # Select the ratios that have the highest divergence in any axis and check if the ratio is less than 4
              j = torch.max(r, 1.0 / r).max(2)[0] < 4.0  # compare
              # Only use targets that have the correct ratios for their anchors
              # That means we only keep ones that have a matching anchor and we loose the anchor dimension
              # The anchor id is still saved in the 7th value of each target
              t = t[j]
  
              # offsets
              gxy = t[:, 2:4]  # grid xy
              gxi = gain[[2, 3]] - gxy
              j, k = ((gxy % 1 < g) & (gxy > 1)).T
              l, m = ((gxi % 1 < g) & (gxi > 1)).T
              j = torch.stack((torch.ones_like(j), j, k, l, m))
              t = t.repeat((5, 1, 1))[j]
              offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
          else:
              t = targets[0]
              offsets = 0
  
          # Extract image id in batch and class id
          b, c = t[:, :2].long().T
          # We isolate the target cell associations.
          # x, y, w, h are allready in the cell coordinate system meaning an x = 1.2 would be 1.2 times cellwidth
          gxy = t[:, 2:4]  # grid xy
          gwh = t[:, 4:6]  # grid wh
          # Cast to int to get an cell index e.g. 1.2 gets associated to cell 1
          gij = (gxy - offsets).long()
          # Isolate x and y index dimensions
          gi, gj = gij.T  # grid xy indices
  
          # Convert anchor indexes to int
          a = t[:, 6].long()
          # Add target tensors for this yolo layer to the output lists
          # Add to index list and limit index range to prevent out of bounds
          indices.append(
              (
                  b,
                  a,
                  gj.clamp_(0, int(gain[3] - 1)),
                  gi.clamp_(0, int(gain[2] - 1)),
              )
          )
          # Add to target box list and convert box coordinates from global grid coordinates to local offsets in the grid cell
          tbox.append(torch.cat((gxy - gij, gwh), 1))  # box
          # Add correct anchor for each target to the list
          anch.append(anchors[a])
          # Add class for each target to the list
          tcls.append(c)
  
      return tcls, tbox, indices, anch
  
imports: import math
  import torch
  import torch.nn
label_desc: Use image node_id 3 for calling a global function with example usage: build_targets(p, targets, loss_layers, device) and returns: tcls,  tbox,  indices,  anch