code: def rpn_losses(anchor_labels, anchor_boxes, label_logits, box_logits):
      """
      Args:
          anchor_labels: fHxfWxNA
          anchor_boxes: fHxfWxNAx4, encoded
          label_logits:  fHxfWxNA
          box_logits: fHxfWxNAx4
  
      Returns:
          label_loss, box_loss
      """
      with tf.device("/cpu:0"):
          valid_mask = tf.stop_gradient(tf.not_equal(anchor_labels, -1))
          pos_mask = tf.stop_gradient(tf.equal(anchor_labels, 1))
          nr_valid = tf.stop_gradient(
              tfv1.count_nonzero(valid_mask, dtype=tf.int32),
              name="num_valid_anchor",
          )
          nr_pos = tf.identity(
              tfv1.count_nonzero(pos_mask, dtype=tf.int32),
              name="num_pos_anchor",
          )
          # nr_pos is guaranteed >0 in C4. But in FPN. even nr_valid could be 0.
  
          valid_anchor_labels = tf.boolean_mask(
              anchor_labels, valid_mask
          )
      valid_label_logits = tf.boolean_mask(label_logits, valid_mask)
  
      with tf.name_scope("label_metrics"):
          valid_label_prob = tf.nn.sigmoid(valid_label_logits)
          summaries = []
          with tf.device("/cpu:0"):
              for th in [0.5, 0.2, 0.1]:
                  valid_prediction = tf.cast(
                      valid_label_prob > th, tf.int32
                  )
                  nr_pos_prediction = tf.reduce_sum(
                      valid_prediction, name="num_pos_prediction"
                  )
                  pos_prediction_corr = tfv1.count_nonzero(
                      tf.logical_and(
                          valid_label_prob > th,
                          tf.equal(
                              valid_prediction, valid_anchor_labels
                          ),
                      ),
                      dtype=tf.int32,
                  )
                  placeholder = 0.5  # A small value will make summaries appear lower.
                  recall = tf.cast(
                      tf.truediv(pos_prediction_corr, nr_pos),
                      tf.float32,
                  )
                  recall = tf.where(
                      tf.equal(nr_pos, 0),
                      placeholder,
                      recall,
                      name="recall_th{}".format(th),
                  )
                  precision = tf.cast(
                      tf.truediv(
                          pos_prediction_corr, nr_pos_prediction
                      ),
                      tf.float32,
                  )
                  precision = tf.where(
                      tf.equal(nr_pos_prediction, 0),
                      placeholder,
                      precision,
                      name="precision_th{}".format(th),
                  )
                  summaries.extend([precision, recall])
          add_moving_summary(*summaries)
  
      # Per-level loss summaries in FPN may appear lower due to the use of a small placeholder.
      # But the total RPN loss will be fine.  TODO make the summary op smarter
      placeholder = 0.0
      label_loss = tf.nn.sigmoid_cross_entropy_with_logits(
          labels=tf.cast(valid_anchor_labels, tf.float32),
          logits=valid_label_logits,
      )
      label_loss = tf.reduce_sum(label_loss) * (
          1.0 / cfg.RPN.BATCH_PER_IM
      )
      label_loss = tf.where(
          tf.equal(nr_valid, 0),
          placeholder,
          label_loss,
          name="label_loss",
      )
  
      pos_anchor_boxes = tf.boolean_mask(anchor_boxes, pos_mask)
      pos_box_logits = tf.boolean_mask(box_logits, pos_mask)
      delta = 1.0 / 9
      box_loss = (
          tfv1.losses.huber_loss(
              pos_anchor_boxes,
              pos_box_logits,
              delta=delta,
              reduction=tfv1.losses.Reduction.SUM,
          )
          / delta
      )
      box_loss = box_loss * (1.0 / cfg.RPN.BATCH_PER_IM)
      box_loss = tf.where(
          tf.equal(nr_pos, 0), placeholder, box_loss, name="box_loss"
      )
  
      add_moving_summary(label_loss, box_loss, nr_valid, nr_pos)
      return [label_loss, box_loss]
  
file_path: tensorpack/examples/FasterRCNN/modeling/model_rpn.py
imports: import tensorflow
  import numpy
  from tensorpack import tfv1
  from tensorpack.models import Conv2D, layer_register
  from tensorpack.tfutils.argscope import argscope
  from tensorpack.tfutils.scope_utils import auto_reuse_variable_scope, under_name_scope
  from tensorpack.tfutils.summary import add_moving_summary
  from tensorpack.utils.argtools import memoized
  from config import config
  from .model_box import clip_boxes
label_desc: Use image node_id 2 for calling a global function with example usage: rpn_losses(anchor_labels, anchor_boxes, label_logits, box_logits) and returns: list