code: def maskrcnn_loss(mask_logits, fg_labels, fg_target_masks):
      """
      Args:
          mask_logits: #fg x #category xhxw
          fg_labels: #fg, in 1~#class, int64
          fg_target_masks: #fgxhxw, float32
      """
      mask_logits = tf.gather(
          mask_logits, tf.reshape(fg_labels - 1, [-1, 1]), batch_dims=1
      )
      mask_logits = tf.squeeze(mask_logits, axis=1)
  
      mask_probs = tf.sigmoid(mask_logits)
  
      # add some training visualizations to tensorboard
      with tf.name_scope("mask_viz"):
          viz = tf.concat([fg_target_masks, mask_probs], axis=1)
          viz = tf.expand_dims(viz, 3)
          viz = tf.cast(viz * 255, tf.uint8, name="viz")
          tf.summary.image("mask_truth|pred", viz, max_outputs=10)
  
      loss = tf.nn.sigmoid_cross_entropy_with_logits(
          labels=fg_target_masks, logits=mask_logits
      )
      loss = tf.reduce_mean(loss, name="maskrcnn_loss")
  
      pred_label = mask_probs > 0.5
      truth_label = fg_target_masks > 0.5
      accuracy = tf.reduce_mean(
          tf.cast(tf.equal(pred_label, truth_label), tf.float32),
          name="accuracy",
      )
      pos_accuracy = tf.logical_and(
          tf.equal(pred_label, truth_label), tf.equal(truth_label, True)
      )
      pos_accuracy = tf.reduce_mean(
          tf.cast(pos_accuracy, tf.float32), name="pos_accuracy"
      )
      fg_pixel_ratio = tf.reduce_mean(
          tf.cast(truth_label, tf.float32), name="fg_pixel_ratio"
      )
  
      add_moving_summary(loss, accuracy, fg_pixel_ratio, pos_accuracy)
      return loss
  
file_path: tensorpack/examples/FasterRCNN/modeling/model_mrcnn.py
imports: import tensorflow
  from tensorpack import tfv1
  from tensorpack.models import Conv2D, Conv2DTranspose, layer_register
  from tensorpack.tfutils.argscope import argscope
  from tensorpack.tfutils.scope_utils import under_name_scope
  from tensorpack.tfutils.summary import add_moving_summary
  from .backbone import GroupNorm
  from config import config
label_desc: Use image node_id 1 for calling a global function with example usage: maskrcnn_loss(mask_logits, fg_labels, fg_target_masks) and returns: loss