code: def roi_heads(self, image, features, proposals, targets):
      image_shape2d = tf.shape(image)[2:]  # h,w
      featuremap = features[0]
  
      gt_boxes, gt_labels, *_ = targets
  
      if self.training:
          # sample proposal boxes in training
          proposals = sample_fast_rcnn_targets(
              proposals.boxes, gt_boxes, gt_labels
          )
      # The boxes to be used to crop RoIs.
      # Use all proposal boxes in inference
  
      boxes_on_featuremap = proposals.boxes * (
          1.0 / cfg.RPN.ANCHOR_STRIDE
      )
      roi_resized = roi_align(featuremap, boxes_on_featuremap, 14)
  
      feature_fastrcnn = resnet_conv5(
          roi_resized, cfg.BACKBONE.RESNET_NUM_BLOCKS[-1]
      )  # nxcx7x7
      # Keep C5 feature to be shared with mask branch
      feature_gap = GlobalAvgPooling(
          "gap", feature_fastrcnn, data_format="channels_first"
      )
      fastrcnn_label_logits, fastrcnn_box_logits = fastrcnn_outputs(
          "fastrcnn", feature_gap, cfg.DATA.NUM_CATEGORY
      )
  
      fastrcnn_head = FastRCNNHead(
          proposals,
          fastrcnn_box_logits,
          fastrcnn_label_logits,
          gt_boxes,
          tf.constant(cfg.FRCNN.BBOX_REG_WEIGHTS, dtype=tf.float32),
      )
  
      if self.training:
          all_losses = fastrcnn_head.losses()
  
          if cfg.MODE_MASK:
              gt_masks = targets[2]
              # maskrcnn loss
              # In training, mask branch shares the same C5 feature.
              fg_feature = tf.gather(
                  feature_fastrcnn, proposals.fg_inds()
              )
              mask_logits = maskrcnn_upXconv_head(
                  "maskrcnn",
                  fg_feature,
                  cfg.DATA.NUM_CATEGORY,
                  num_convs=0,
              )  # #fg x #cat x 14x14
  
              target_masks_for_fg = crop_and_resize(
                  tf.expand_dims(gt_masks, 1),
                  proposals.fg_boxes(),
                  proposals.fg_inds_wrt_gt,
                  14,
                  pad_border=False,
              )  # nfg x 1x14x14
              target_masks_for_fg = tf.squeeze(
                  target_masks_for_fg, 1, "sampled_fg_mask_targets"
              )
              all_losses.append(
                  maskrcnn_loss(
                      mask_logits,
                      proposals.fg_labels(),
                      target_masks_for_fg,
                  )
              )
          return all_losses
      else:
          decoded_boxes = fastrcnn_head.decoded_output_boxes()
          decoded_boxes = clip_boxes(
              decoded_boxes, image_shape2d, name="fastrcnn_all_boxes"
          )
          label_scores = fastrcnn_head.output_scores(
              name="fastrcnn_all_scores"
          )
          (
              final_boxes,
              final_scores,
              final_labels,
          ) = fastrcnn_predictions(
              decoded_boxes, label_scores, name_scope="output"
          )
  
          if cfg.MODE_MASK:
              roi_resized = roi_align(
                  featuremap,
                  final_boxes * (1.0 / cfg.RPN.ANCHOR_STRIDE),
                  14,
              )
              feature_maskrcnn = resnet_conv5(
                  roi_resized, cfg.BACKBONE.RESNET_NUM_BLOCKS[-1]
              )
              mask_logits = maskrcnn_upXconv_head(
                  "maskrcnn", feature_maskrcnn, cfg.DATA.NUM_CATEGORY, 0
              )  # #result x #cat x 14x14
              indices = tf.stack(
                  [
                      tf.range(tf.size(final_labels)),
                      tf.cast(final_labels, tf.int32) - 1,
                  ],
                  axis=1,
              )
              final_mask_logits = tf.gather_nd(
                  mask_logits, indices
              )  # #resultx14x14
              tf.sigmoid(final_mask_logits, name="output/masks")
          return []
  
imports: from tensorpack import tfv1
  from tensorpack import ModelDesc
  from tensorpack.models import GlobalAvgPooling, l2_regularizer, regularize_cost
  from tensorpack.tfutils import optimizer
  from tensorpack.tfutils.summary import add_moving_summary
  from config import config
  from data import get_all_anchors, get_all_anchors_fpn
  from utils.box_ops import area
  from .None import model_frcnn
  from .None import model_mrcnn
  from .backbone import image_preprocess, resnet_c4_backbone, resnet_conv5, resnet_fpn_backbone
  from .model_box import RPNAnchors, clip_boxes, crop_and_resize, roi_align
  from .model_cascade import CascadeRCNNHead
  from .model_fpn import fpn_model, generate_fpn_proposals, multilevel_roi_align, multilevel_rpn_losses
  from .model_frcnn import BoxProposals, FastRCNNHead, fastrcnn_outputs, fastrcnn_predictions, sample_fast_rcnn_targets
  from .model_mrcnn import maskrcnn_loss, maskrcnn_upXconv_head, unpackbits_masks
  from .model_rpn import generate_rpn_proposals, rpn_head, rpn_losses
label_desc: Use image node_id 4 for calling the ResNetC4Model obj's underlying member method code with example usage: obj.roi_heads(image, features, proposals, targets) and returns: all_losses, list