code: def mask_head_loss_pre(
      rois, roi_indices, gt_masks, gt_bboxes, gt_head_labels, segm_size
  ):
      """Loss function for Mask Head (pre).
  
      This function processes RoIs for :func:`mask_head_loss_post` by
      selecting RoIs for mask loss calculation and
      preparing ground truth network output.
  
      Args:
          rois (iterable of arrays): An iterable of arrays of
              shape :math:`(R_l, 4)`, where :math:`R_l` is the number
              of RoIs in the :math:`l`-th feature map.
          roi_indices (iterable of arrays): An iterable of arrays of
              shape :math:`(R_l,)`.
          gt_masks (iterable of arrays): An iterable of arrays whose shape is
              :math:`(R_n, H, W)`, where :math:`R_n` is the number of
              ground truth objects.
          gt_head_labels (iterable of arrays): An iterable of arrays of
              shape :math:`(R_l,)`. This is a collection of ground-truth
              labels assigned to :obj:`rois` during bounding box localization
              stage. The range of value is :math:`(0, n\_class - 1)`.
          segm_size (int): Size of the ground truth network output.
  
      Returns:
          tuple of four lists:
          :obj:`mask_rois`, :obj:`mask_roi_indices`,
          :obj:`gt_segms`, and :obj:`gt_mask_labels`.
  
          * **rois**: A list of arrays of shape :math:`(R'_l, 4)`, \
              where :math:`R'_l` is the number of RoIs in the :math:`l`-th \
              feature map.
          * **roi_indices**: A list of arrays of shape :math:`(R'_l,)`.
          * **gt_segms**: A list of arrays of shape :math:`(R'_l, M, M). \
              :math:`M` is the argument :obj:`segm_size`.
          * **gt_mask_labels**: A list of arrays of shape :math:`(R'_l,)` \
              indicating the classes of ground truth.
      """
  
      xp = cuda.get_array_module(*rois)
  
      n_level = len(rois)
  
      roi_levels = xp.hstack(
          xp.array((l,) * len(rois[l])) for l in range(n_level)
      ).astype(np.int32)
      rois = xp.vstack(rois).astype(np.float32)
      roi_indices = xp.hstack(roi_indices).astype(np.int32)
      gt_head_labels = xp.hstack(gt_head_labels)
  
      index = (gt_head_labels > 0).nonzero()[0]
      mask_roi_levels = roi_levels[index]
      mask_rois = rois[index]
      mask_roi_indices = roi_indices[index]
      gt_mask_labels = gt_head_labels[index]
  
      gt_segms = xp.empty(
          (len(mask_rois), segm_size, segm_size), dtype=np.float32
      )
      for i in np.unique(cuda.to_cpu(mask_roi_indices)):
          gt_mask = gt_masks[i]
          gt_bbox = gt_bboxes[i]
  
          index = (mask_roi_indices == i).nonzero()[0]
          mask_roi = mask_rois[index]
          iou = bbox_iou(mask_roi, gt_bbox)
          gt_index = iou.argmax(axis=1)
          gt_segms[index] = xp.array(
              mask_to_segm(gt_mask, mask_roi, segm_size, gt_index)
          )
  
      flag_masks = [mask_roi_levels == l for l in range(n_level)]
      mask_rois = [mask_rois[m] for m in flag_masks]
      mask_roi_indices = [mask_roi_indices[m] for m in flag_masks]
      gt_segms = [gt_segms[m] for m in flag_masks]
      gt_mask_labels = [gt_mask_labels[m] for m in flag_masks]
      return mask_rois, mask_roi_indices, gt_segms, gt_mask_labels
  
file_path: chainercv/chainercv/links/model/fpn/mask_head.py
imports: from __future__ import division
  import numpy
  import chainer
  from chainer.backends import cuda
  import chainer.functions
  from chainer.initializers import HeNormal
  import chainer.links
  from chainercv.links import Conv2DActiv
  from chainercv.utils.bbox.bbox_iou import bbox_iou
  from chainercv.links.model.fpn.mask_utils import mask_to_segm
  from chainercv.links.model.fpn.mask_utils import segm_to_mask
label_desc: Use image node_id 1 for calling a global function with example usage: mask_head_loss_pre(rois, roi_indices, gt_masks, gt_bboxes, gt_head_labels, segm_size) and returns: mask_rois,  mask_roi_indices,  gt_segms,  gt_mask_labels