code: def compute_loss(p, targets, model):  # predictions, targets, model
      device = p[0].device
      lcls = torch.zeros(1, device=device)  # Tensor(0)
      lbox = torch.zeros(1, device=device)  # Tensor(0)
      lobj = torch.zeros(1, device=device)  # Tensor(0)
      tcls, tbox, indices, anchors = build_targets(
          p, targets, model
      )  # targets
      h = model.hyp  # hyperparameters
      red = "mean"  # Loss reduction (sum or mean)
  
      # Define criteria
      BCEcls = nn.BCEWithLogitsLoss(
          pos_weight=torch.tensor([h["cls_pw"]], device=device),
          reduction=red,
      )
      BCEobj = nn.BCEWithLogitsLoss(
          pos_weight=torch.tensor([h["obj_pw"]], device=device),
          reduction=red,
      )
  
      # class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3
      cp, cn = smooth_BCE(eps=0.0)
  
      # focal loss
      g = h["fl_gamma"]  # focal loss gamma
      if g > 0:
          BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g)
  
      # per output
      for i, pi in enumerate(p):  # layer index, layer predictions
          b, a, gj, gi = indices[
              i
          ]  # image_idx, anchor_idx, grid_y, grid_x
          tobj = torch.zeros_like(
              pi[..., 0], device=device
          )  # target obj
  
          nb = b.shape[0]  # number of positive samples
          if nb:
              # 对应匹配到正样本的预测信息
              ps = pi[
                  b, a, gj, gi
              ]  # prediction subset corresponding to targets
  
              # GIoU
              pxy = ps[:, :2].sigmoid()
              pwh = ps[:, 2:4].exp().clamp(max=1e3) * anchors[i]
              pbox = torch.cat((pxy, pwh), 1)  # predicted box
              giou = bbox_iou(
                  pbox.t(), tbox[i], x1y1x2y2=False, GIoU=True
              )  # giou(prediction, target)
              lbox += (1.0 - giou).mean()  # giou loss
  
              # Obj
              tobj[b, a, gj, gi] = (
                  1.0 - model.gr
              ) + model.gr * giou.detach().clamp(0).type(
                  tobj.dtype
              )  # giou ratio
  
              # Class
              if model.nc > 1:  # cls loss (only if multiple classes)
                  t = torch.full_like(
                      ps[:, 5:], cn, device=device
                  )  # targets
                  t[range(nb), tcls[i]] = cp
                  lcls += BCEcls(ps[:, 5:], t)  # BCE
  
              # Append targets to text file
              # with open('targets.txt', 'a') as file:
              #     [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)]
  
          lobj += BCEobj(pi[..., 4], tobj)  # obj loss
  
      # 乘上每种损失的对应权重
      lbox *= h["giou"]
      lobj *= h["obj"]
      lcls *= h["cls"]
  
      # loss = lbox + lobj + lcls
      return {"box_loss": lbox, "obj_loss": lobj, "class_loss": lcls}
  
imports: import glob
  import math
  import os
  import random
  import time
  import cv2
  import matplotlib
  import numpy
  import torch
  import torch.nn
  import torchvision
  from tqdm import tqdm
  from build_utils import torch_utils
label_desc: Use image node_id 11 for calling a global function with example usage: compute_loss(p, targets, model) and returns: dict