code: def calc_loss(layer_id, gt, preds, debug=False):
      gt_boxes = gt[..., :4]
      gt_labels = tf.cast(gt[..., 4], tf.int32)
      gt_count = tf.shape(gt_labels)[-1]
      gt_mask = tf.where(gt_labels == -1, 0.0, 1.0)
      layer_xywh, layer_obj, layer_cls = utils.decode_layer(
          preds, layer_id
      )
      cls_count = layer_cls.shape[-1]
  
      s = tf.shape(preds)
      batch_size = s[0]
      gw = s[1]
      gh = s[2]
      stride_x = 1 / gw
      stride_y = 1 / gh
      d = s[3]
      truth_mask = tf.zeros((batch_size, gw, gh, 3))
  
      box_loss = 0.0
      cls_loss = 0.0
  
      ix = tf.cast(
          tf.math.floor(tf.cast(gw, tf.float32) * gt_boxes[..., 0]),
          tf.int32,
      )
      iy = tf.cast(
          tf.math.floor(tf.cast(gh, tf.float32) * gt_boxes[..., 1]),
          tf.int32,
      )
      ix = tf.clip_by_value(ix, 0, gw - 1)
      iy = tf.clip_by_value(iy, 0, gh - 1)
  
      box_shape = tf.shape(gt_labels)
      zeros = tf.zeros_like(gt_labels, dtype=tf.float32)
      gt_shift = tf.stack(
          [zeros, zeros, gt_boxes[..., 2], gt_boxes[..., 3]], axis=-1
      )
      gt_shift = tf.stack([gt_shift, gt_shift, gt_shift], axis=1)
  
      anchors_ws = [
          tf.cast(
              tf.fill(box_shape, anchor_sizes[layer_id][ir][0]),
              dtype=tf.float32,
          )
          / 608.0
          for ir in range(3)
      ]
      anchors_hs = [
          tf.cast(
              tf.fill(box_shape, anchor_sizes[layer_id][ir][1]),
              dtype=tf.float32,
          )
          / 608.0
          for ir in range(3)
      ]
      anchors = tf.stack(
          [
              tf.stack(
                  [zeros, zeros, anchors_ws[ir], anchors_hs[ir]],
                  axis=-1,
              )
              for ir in range(3)
          ],
          axis=1,
      )
  
      ious = utils.calc_ious(gt_shift, anchors)
      ious_argmax = tf.cast(tf.argmax(ious, axis=1), dtype=tf.int32)
      batch_idx = tf.tile(
          tf.range(batch_size)[:, tf.newaxis], [1, box_shape[-1]]
      )
  
      indices = tf.stack([batch_idx, iy, ix, ious_argmax], axis=-1)
      pred_boxes = tf.gather_nd(layer_xywh, indices)
      box_loss = tf.math.reduce_sum(
          gt_mask * (1.0 - utils.calc_gious(pred_boxes, gt_boxes))
      )
  
      cls_one_hot = tf.one_hot(gt_labels, cls_count)
      pred_cls = tf.gather_nd(layer_cls, indices)
      cls_diffs = tf.math.reduce_sum(
          tf.math.square(pred_cls - cls_one_hot), axis=-1
      )
      cls_loss = tf.math.reduce_sum(gt_mask * cls_diffs)
  
      indices_not_null = tf.gather_nd(
          indices, tf.where(gt_labels != -1)
      )
      truth_mask = tf.tensor_scatter_nd_update(
          truth_mask,
          indices_not_null,
          tf.ones_like(indices_not_null, dtype=tf.float32)[:, 0],
      )
      inv_truth_mask = 1.0 - truth_mask
  
      obj_loss = tf.math.reduce_sum(
          tf.math.square(1 - layer_obj) * truth_mask
      )
      gt_boxes_exp = tf.tile(
          tf.reshape(gt_boxes, (batch_size, 1, 1, 1, gt_count, 4)),
          [1, gw, gh, 3, 1, 1],
      )
      pred_boxes_exp = tf.tile(
          tf.reshape(layer_xywh, (batch_size, gw, gh, 3, 1, 4)),
          [1, 1, 1, 1, gt_count, 1],
      )
      iou_mask = tf.cast(
          tf.math.reduce_max(
              utils.calc_ious(gt_boxes_exp, pred_boxes_exp), axis=-1
          )
          < 0.7,
          tf.float32,
      )
      obj_loss += tf.math.reduce_sum(
          tf.math.square(layer_obj) * inv_truth_mask * iou_mask
      )
  
      return (
          0.05 * box_loss + 1.0 * obj_loss + 0.5 * cls_loss
      ) / tf.cast(batch_size, dtype=tf.float32)
  
imports: import tensorflow
  import numpy
  from layers import Mish, ScaledRandomUniform
  import utils
label_desc: Use image node_id 1 for calling a global function with example usage: calc_loss(layer_id, gt, preds, debug) and returns: unknown