code: def roi_heads(self, image, features, proposals, targets):
      image_shape2d = tf.shape(image)[2:]  # h,w
      assert len(features) == 5, "Features have to be P23456!"
      gt_boxes, gt_labels, *_ = targets
  
      if self.training:
          proposals = sample_fast_rcnn_targets(
              proposals.boxes, gt_boxes, gt_labels
          )
  
      fastrcnn_head_func = getattr(model_frcnn, cfg.FPN.FRCNN_HEAD_FUNC)
      if not cfg.FPN.CASCADE:
          roi_feature_fastrcnn = multilevel_roi_align(
              features[:4], proposals.boxes, 7
          )
  
          head_feature = fastrcnn_head_func(
              "fastrcnn", roi_feature_fastrcnn
          )
          fastrcnn_label_logits, fastrcnn_box_logits = fastrcnn_outputs(
              "fastrcnn/outputs", head_feature, 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),
          )
      else:
  
          def roi_func(boxes):
              return multilevel_roi_align(features[:4], boxes, 7)
  
          fastrcnn_head = CascadeRCNNHead(
              proposals,
              roi_func,
              fastrcnn_head_func,
              (gt_boxes, gt_labels),
              image_shape2d,
              cfg.DATA.NUM_CATEGORY,
          )
  
      if self.training:
          all_losses = fastrcnn_head.losses()
  
          if cfg.MODE_MASK:
              gt_masks = targets[2]
              # maskrcnn loss
              roi_feature_maskrcnn = multilevel_roi_align(
                  features[:4],
                  proposals.fg_boxes(),
                  14,
                  name_scope="multilevel_roi_align_mask",
              )
              maskrcnn_head_func = getattr(
                  model_mrcnn, cfg.FPN.MRCNN_HEAD_FUNC
              )
              mask_logits = maskrcnn_head_func(
                  "maskrcnn",
                  roi_feature_maskrcnn,
                  cfg.DATA.NUM_CATEGORY,
              )  # #fg x #cat x 28 x 28
  
              target_masks_for_fg = crop_and_resize(
                  tf.expand_dims(gt_masks, 1),
                  proposals.fg_boxes(),
                  proposals.fg_inds_wrt_gt,
                  28,
                  pad_border=False,
              )  # fg x 1x28x28
              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:
              # Cascade inference needs roi transform with refined boxes.
              roi_feature_maskrcnn = multilevel_roi_align(
                  features[:4], final_boxes, 14
              )
              maskrcnn_head_func = getattr(
                  model_mrcnn, cfg.FPN.MRCNN_HEAD_FUNC
              )
              mask_logits = maskrcnn_head_func(
                  "maskrcnn",
                  roi_feature_maskrcnn,
                  cfg.DATA.NUM_CATEGORY,
              )  # #fg x #cat x 28 x 28
              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
              )  # #resultx28x28
              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 5 for calling the ResNetFPNModel obj's underlying member method code with example usage: obj.roi_heads(image, features, proposals, targets) and returns: all_losses, list, multilevel_roi_align