class_docstr: Region Proposal Network introduced in Faster R-CNN.
  
  This is Region Proposal Network introduced in Faster R-CNN [#]_.
  This takes features extracted from images and propose
  class agnostic bounding boxes around "objects".
  
  .. [#] Shaoqing Ren, Kaiming He, Ross Girshick, Jian Sun.     Faster R-CNN: Towards Real-Time Object Detection with     Region Proposal Networks. NIPS 2015.
  
  Args:
      in_channels (int): The channel size of input.
      mid_channels (int): The channel size of the intermediate tensor.
      ratios (list of floats): This is ratios of width to height of
          the anchors.
      anchor_scales (list of numbers): This is areas of anchors.
          Those areas will be the product of the square of an element in
          :obj:`anchor_scales` and the original area of the reference
          window.
      feat_stride (int): Stride size after extracting features from an
          image.
      initialW (callable): Initial weight value. If :obj:`None` then this
          function uses Gaussian distribution scaled by 0.1 to
          initialize weight.
          May also be a callable that takes an array and edits its values.
      proposal_creator_params (dict): Key valued paramters for
          :class:`~chainercv.links.model.faster_rcnn.ProposalCreator`.
  
  .. seealso::
      :class:`~chainercv.links.model.faster_rcnn.ProposalCreator`
docstr: Forward Region Proposal Network.
  
  Here are notations.
  
  * :math:`N` is batch size.
  * :math:`C` channel size of the input.
  * :math:`H` and :math:`W` are height and witdh of the input feature.
  * :math:`A` is number of anchors assigned to each pixel.
  
  Args:
      x (~chainer.Variable): The Features extracted from images.
          Its shape is :math:`(N, C, H, W)`.
      img_size (tuple of ints): A tuple :obj:`height, width`,
          which contains image size after scaling.
      scales (tuple of floats): The amount of scaling done to each input
          image during preprocessing.
  
  Returns:
      (~chainer.Variable, ~chainer.Variable, array, array, array):
  
      This is a tuple of five following values.
  
      * **rpn_locs**: Predicted bounding box offsets and scales for                 anchors. Its shape is :math:`(N, H W A, 4)`.
      * **rpn_scores**:  Predicted foreground scores for                 anchors. Its shape is :math:`(N, H W A, 2)`.
      * **rois**: A bounding box array containing coordinates of                 proposal boxes.  This is a concatenation of bounding box                 arrays from multiple images in the batch.                 Its shape is :math:`(R', 4)`. Given :math:`R_i` predicted                 bounding boxes from the :math:`i` th image,                 :math:`R' = \sum _{i=1} ^ N R_i`.
      * **roi_indices**: An array containing indices of images to                 which RoIs correspond to. Its shape is :math:`(R',)`.
      * **anchor**: Coordinates of enumerated shifted anchors.                 Its shape is :math:`(H W A, 4)`.
code: def forward(self, x, img_size, scales=None):
      """Forward Region Proposal Network.
  
          Here are notations.
  
          * :math:`N` is batch size.
          * :math:`C` channel size of the input.
          * :math:`H` and :math:`W` are height and witdh of the input feature.
          * :math:`A` is number of anchors assigned to each pixel.
  
          Args:
              x (~chainer.Variable): The Features extracted from images.
                  Its shape is :math:`(N, C, H, W)`.
              img_size (tuple of ints): A tuple :obj:`height, width`,
                  which contains image size after scaling.
              scales (tuple of floats): The amount of scaling done to each input
                  image during preprocessing.
  
          Returns:
              (~chainer.Variable, ~chainer.Variable, array, array, array):
  
              This is a tuple of five following values.
  
              * **rpn_locs**: Predicted bounding box offsets and scales for \
                  anchors. Its shape is :math:`(N, H W A, 4)`.
              * **rpn_scores**:  Predicted foreground scores for \
                  anchors. Its shape is :math:`(N, H W A, 2)`.
              * **rois**: A bounding box array containing coordinates of \
                  proposal boxes.  This is a concatenation of bounding box \
                  arrays from multiple images in the batch. \
                  Its shape is :math:`(R', 4)`. Given :math:`R_i` predicted \
                  bounding boxes from the :math:`i` th image, \
                  :math:`R' = \\sum _{i=1} ^ N R_i`.
              * **roi_indices**: An array containing indices of images to \
                  which RoIs correspond to. Its shape is :math:`(R',)`.
              * **anchor**: Coordinates of enumerated shifted anchors. \
                  Its shape is :math:`(H W A, 4)`.
  
          """
  
      n, _, hh, ww = x.shape
      if scales is None:
          scales = [1.0] * n
      if not isinstance(scales, chainer.utils.collections_abc.Iterable):
          scales = [scales] * n
  
      anchor = _enumerate_shifted_anchor(
          self.xp.array(self.anchor_base), self.feat_stride, hh, ww
      )
      n_anchor = anchor.shape[0] // (hh * ww)
      h = F.relu(self.conv1(x))
  
      rpn_locs = self.loc(h)
      rpn_locs = rpn_locs.transpose((0, 2, 3, 1)).reshape((n, -1, 4))
  
      rpn_scores = self.score(h)
      rpn_scores = rpn_scores.transpose((0, 2, 3, 1))
      rpn_fg_scores = rpn_scores.reshape((n, hh, ww, n_anchor, 2))[
          :, :, :, :, 1
      ]
      rpn_fg_scores = rpn_fg_scores.reshape((n, -1))
      rpn_scores = rpn_scores.reshape((n, -1, 2))
  
      rois = []
      roi_indices = []
      for i in range(n):
          roi = self.proposal_layer(
              rpn_locs[i].array,
              rpn_fg_scores[i].array,
              anchor,
              img_size,
              scale=scales[i],
          )
          batch_index = i * self.xp.ones((len(roi),), dtype=np.int32)
          rois.append(roi)
          roi_indices.append(batch_index)
  
      rois = self.xp.concatenate(rois, axis=0)
      roi_indices = self.xp.concatenate(roi_indices, axis=0)
      return rpn_locs, rpn_scores, rois, roi_indices, anchor
  
file_path: chainercv/chainercv/links/model/faster_rcnn/region_proposal_network.py
imports: import numpy
  import chainer
  from chainer.backends import cuda
  import chainer.functions
  import chainer.links
  from chainercv.links.model.faster_rcnn.utils.generate_anchor_base import generate_anchor_base
  from chainercv.links.model.faster_rcnn.utils.proposal_creator import ProposalCreator
label_desc: Use image node_id 2 for calling the RegionProposalNetwork obj's underlying member method code with example usage: obj.forward(x, img_size, scales) and returns: rpn_locs,  rpn_scores,  rois,  roi_indices,  anchor