class_docstr: Mask RCNN network.
  
  Parameters
  ----------
  features : gluon.HybridBlock
      Base feature extractor before feature pooling layer.
  top_features : gluon.HybridBlock
      Tail feature extractor after feature pooling layer.
  classes : iterable of str
      Names of categories, its length is ``num_class``.
  mask_channels : int, default is 256
      Number of channels in mask prediction
  rcnn_max_dets : int, default is 1000
      Number of rois to retain in RCNN.
      Upper bounded by min of rpn_test_pre_nms and rpn_test_post_nms.
  rpn_test_pre_nms : int, default is 6000
      Filter top proposals before NMS in testing of RPN.
  rpn_test_post_nms : int, default is 1000
      Return top proposal results after NMS in testing of RPN.
      Will be set to rpn_test_pre_nms if it is larger than rpn_test_pre_nms.
  target_roi_scale : int, default 1
      Ratio of mask output roi / input roi. For model with FPN, this is typically 2.
  num_fcn_convs : int, default 0
      number of convolution blocks before deconv layer. For FPN network this is typically 4.
class_docstr_tok: ["Mask","RCNN","network",".","Parameters","--","--","--","--","--","features",":","gluon.HybridBlock","Base","feature","extractor","before","feature","pooling","layer",".","top_features",":","gluon.HybridBlock","Tail","feature","extractor","after","feature","pooling","layer",".","classes",":","iterable","of","str","Names","of","categories",",","its","length","is","``","num_class","``",".","mask_channels",":","int",",","default","is","256","Number","of","channels","in","mask","prediction","rcnn_max_dets",":","int",",","default","is","1000","Number","of","rois","to","retain","in","RCNN",".","Upper","bounded","by","min","of","rpn_test_pre_nms","and","rpn_test_post_nms",".","rpn_test_pre_nms",":","int",",","default","is","6000","Filter","top","proposals","before","NMS","in","testing","of","RPN",".","rpn_test_post_nms",":","int",",","default","is","1000","Return","top","proposal","results","after","NMS","in","testing","of","RPN",".","Will","be","set","to","rpn_test_pre_nms","if","it","is","larger","than","rpn_test_pre_nms",".","target_roi_scale",":","int",",","default","1","Ratio","of","mask","output","roi","\/","input","roi",".","For","model","with","FPN",",","this","is","typically","2.","num_fcn_convs",":","int",",","default","0","number","of","convolution","blocks","before","deconv","layer",".","For","FPN","network","this","is","typically","4","."]
docstr: Forward Mask RCNN network.
  
  The behavior during training and inference is different.
  
  Parameters
  ----------
  x : mxnet.nd.NDArray or mxnet.symbol
      The network input tensor.
  gt_box : type, only required during training
      The ground-truth bbox tensor with shape (1, N, 4).
  gt_label : type, only required during training
      The ground-truth label tensor with shape (B, 1, 4).
  
  Returns
  -------
  (ids, scores, bboxes, masks)
      During inference, returns final class id, confidence scores, bounding
      boxes, segmentation masks.
docstr_tok: ["Forward","Mask","RCNN","network",".","The","behavior","during","training","and","inference","is","different",".","Parameters","--","--","--","--","--","x",":","mxnet.nd.NDArray","or","mxnet.symbol","The","network","input","tensor",".","gt_box",":","type",",","only","required","during","training","The","ground-truth","bbox","tensor","with","shape","(","1",",","N",",","4",")",".","gt_label",":","type",",","only","required","during","training","The","ground-truth","label","tensor","with","shape","(","B",",","1",",","4",")",".","Returns","--","--","--","-","(","ids",",","scores",",","bboxes",",","masks",")","During","inference",",","returns","final","class","id",",","confidence","scores",",","bounding","boxes",",","segmentation","masks","."]
code: def hybrid_forward(self, F, x, gt_box=None, gt_label=None):
      """Forward Mask RCNN network.
  
      The behavior during training and inference is different.
  
      Parameters
      ----------
      x : mxnet.nd.NDArray or mxnet.symbol
          The network input tensor.
      gt_box : type, only required during training
          The ground-truth bbox tensor with shape (1, N, 4).
      gt_label : type, only required during training
          The ground-truth label tensor with shape (B, 1, 4).
  
      Returns
      -------
      (ids, scores, bboxes, masks)
          During inference, returns final class id, confidence scores, bounding
          boxes, segmentation masks.
  
      """
      if autograd.is_training():
          (
              cls_pred,
              box_pred,
              rpn_box,
              samples,
              matches,
              raw_rpn_score,
              raw_rpn_box,
              anchors,
              cls_targets,
              box_targets,
              box_masks,
              top_feat,
              indices,
          ) = super(MaskRCNN, self).hybrid_forward(
              F, x, gt_box, gt_label
          )
          top_feat = F.reshape(
              top_feat.expand_dims(0), (self._batch_size, -1, 0, 0, 0)
          )
          top_feat = F.concat(
              *[
                  F.take(
                      F.slice_axis(
                          top_feat, axis=0, begin=i, end=i + 1
                      ).squeeze(),
                      F.slice_axis(
                          indices, axis=0, begin=i, end=i + 1
                      ).squeeze(),
                  )
                  for i in range(self._batch_size)
              ],
              dim=0,
          )
          mask_pred = self.mask(top_feat)
  
          return (
              cls_pred,
              box_pred,
              mask_pred,
              rpn_box,
              samples,
              matches,
              raw_rpn_score,
              raw_rpn_box,
              anchors,
              cls_targets,
              box_targets,
              box_masks,
              indices,
          )
      else:
          batch_size = 1
          ids, scores, boxes, feat = super(
              MaskRCNN, self
          ).hybrid_forward(F, x)
  
          # (B, N * (C - 1), 1) -> (B, N * (C - 1)) -> (B, topk)
          num_rois = self._rcnn_max_dets
          order = F.argsort(
              scores.squeeze(axis=-1), axis=1, is_ascend=False
          )
          topk = F.slice_axis(order, axis=1, begin=0, end=num_rois)
  
          # pick from (B, N * (C - 1), X) to (B * topk, X) -> (B, topk, X)
          # roi_batch_id = F.arange(0, self._max_batch, repeat=num_rois)
          roi_batch_id = F.arange(0, batch_size)
          roi_batch_id = F.repeat(roi_batch_id, num_rois)
          indices = F.stack(roi_batch_id, topk.reshape((-1,)), axis=0)
          ids = F.gather_nd(ids, indices).reshape(
              (-4, batch_size, num_rois, 1)
          )
          scores = F.gather_nd(scores, indices).reshape(
              (-4, batch_size, num_rois, 1)
          )
          boxes = F.gather_nd(boxes, indices).reshape(
              (-4, batch_size, num_rois, 4)
          )
  
          # create batch id and reshape for roi pooling
          padded_rois = F.concat(
              roi_batch_id.reshape((-1, 1)),
              boxes.reshape((-3, 0)),
              dim=-1,
          )
          padded_rois = F.stop_gradient(padded_rois)
  
          # pool to roi features
          if self.num_stages > 1:
              # using FPN
              pooled_feat = self._pyramid_roi_feats(
                  F,
                  feat,
                  padded_rois,
                  self._roi_size,
                  self._strides,
                  roi_mode=self._roi_mode,
              )
          else:
              if self._roi_mode == "pool":
                  pooled_feat = F.ROIPooling(
                      feat[0],
                      padded_rois,
                      self._roi_size,
                      1.0 / self._strides,
                  )
              elif self._roi_mode == "align":
                  pooled_feat = F.contrib.ROIAlign(
                      feat[0],
                      padded_rois,
                      self._roi_size,
                      1.0 / self._strides,
                      sample_ratio=2,
                  )
              else:
                  raise ValueError(
                      "Invalid roi mode: {}".format(self._roi_mode)
                  )
  
          # run top_features again
          if self.top_features is not None:
              top_feat = self.top_features(pooled_feat)
          else:
              top_feat = pooled_feat
          # (B, N, C, pooled_size * 2, pooled_size * 2)
          rcnn_mask = self.mask(top_feat)
          # index the B dimension (B * N,)
          # batch_ids = F.arange(0, self._max_batch, repeat=num_rois)
          batch_ids = F.arange(0, batch_size)
          batch_ids = F.repeat(batch_ids, num_rois)
          # index the N dimension (B * N,)
          roi_ids = F.tile(F.arange(0, num_rois), reps=batch_size)
          # index the C dimension (B * N,)
          class_ids = ids.reshape((-1,))
          # clip to 0 to max class
          class_ids = F.clip(class_ids, 0, self.num_class)
          # pick from (B, N, C, PS*2, PS*2) -> (B * N, PS*2, PS*2)
          indices = F.stack(batch_ids, roi_ids, class_ids, axis=0)
          masks = F.gather_nd(rcnn_mask, indices)
          # (B * N, PS*2, PS*2) -> (B, N, PS*2, PS*2)
          masks = masks.reshape((-4, batch_size, num_rois, 0, 0))
          # output prob
          masks = F.sigmoid(masks)
  
          # ids (B, N, 1), scores (B, N, 1), boxes (B, N, 4), masks (B, N, PS*2, PS*2)
          return ids, scores, boxes, masks
  
imports: from __future__ import absolute_import
  import os
  import warnings
  import mxnet
  from mxnet import autograd
  from mxnet.gluon import nn
  from mxnet.gluon.contrib.nn import SyncBatchNorm
  from .rcnn_target import MaskTargetGenerator
  from ..faster_rcnn import FasterRCNN
  from ..rcnn import custom_rcnn_fpn
label_desc: Use image node_id 2 for calling the MaskRCNN obj's underlying member method code with example usage: obj.hybrid_forward(F, x, gt_box, gt_label) and returns: cls_pred,  box_pred,  mask_pred,  rpn_box,  samples,  matches,  raw_rpn_score,  raw_rpn_box,  anchors,  cls_targets,  box_targets,  box_masks,  indices, ids,  scores,  boxes,  masks