class_docstr: Calculate losses for Light Head R-CNN and report them.
  
  This is used to train Light Head R-CNN in the joint training scheme
  [#LHRCNN]_.
  
  .. [#LHRCNN] Zeming Li, Chao Peng, Gang Yu, Xiangyu Zhang, Yangdong Deng,     Jian Sun. Light-Head R-CNN: In Defense of Two-Stage Object Detector.     arXiv preprint arXiv:1711.07264.
  
  The losses include:
  
  * :obj:`rpn_loc_loss`: The localization loss for         Region Proposal Network (RPN).
  * :obj:`rpn_cls_loss`: The classification loss for RPN.
  * :obj:`roi_loc_loss`: The localization loss for the head module.
  * :obj:`roi_cls_loss`: The classification loss for the head module.
  
  Args:
      light_head_rcnn (~light_head_rcnn.links.light_head_rcnn.LightHeadRCNN):
          A Light Head R-CNN model that is going to be trained.
      rpn_sigma (float): Sigma parameter for the localization loss
          of Region Proposal Network (RPN). The default value is 3,
          which is the value used in [#LHRCNN]_.
      roi_sigma (float): Sigma paramter for the localization loss of
          the head. The default value is 1, which is the value used
          in [#LHRCNN]_.
      anchor_target_creator: An instantiation of
          :class:`~chainercv.links.model.faster_rcnn.AnchorTargetCreator`.
      proposal_target_creator: An instantiation of
          :class:`~light_head_rcnn.links.model.utils.ProposalTargetCreator`.
class_docstr_tok: ["Calculate","losses","for","Light","Head","R-CNN","and","report","them",".","This","is","used","to","train","Light","Head","R-CNN","in","the","joint","training","scheme","[","#","LHRCNN","]","_",".","..","[","#","LHRCNN","]","Zeming","Li",",","Chao","Peng",",","Gang","Yu",",","Xiangyu","Zhang",",","Yangdong","Deng",",","Jian","Sun",".","Light-Head","R-CNN",":","In","Defense","of","Two-Stage","Object","Detector",".","arXiv","preprint","arXiv:1711.07264",".","The","losses","include",":","*",":","obj",":","`","rpn_loc_loss","`",":","The","localization","loss","for","Region","Proposal","Network","(","RPN",")",".","*",":","obj",":","`","rpn_cls_loss","`",":","The","classification","loss","for","RPN",".","*",":","obj",":","`","roi_loc_loss","`",":","The","localization","loss","for","the","head","module",".","*",":","obj",":","`","roi_cls_loss","`",":","The","classification","loss","for","the","head","module",".","Args",":","light_head_rcnn","(","~light_head_rcnn.links.light_head_rcnn.LightHeadRCNN",")",":","A","Light","Head","R-CNN","model","that","is","going","to","be","trained",".","rpn_sigma","(","float",")",":","Sigma","parameter","for","the","localization","loss","of","Region","Proposal","Network","(","RPN",")",".","The","default","value","is","3",",","which","is","the","value","used","in","[","#","LHRCNN","]","_.","roi_sigma","(","float",")",":","Sigma","paramter","for","the","localization","loss","of","the","head",".","The","default","value","is","1",",","which","is","the","value","used","in","[","#","LHRCNN","]","_.","anchor_target_creator",":","An","instantiation","of",":","class",":","`","~chainercv.links.model.faster_rcnn.AnchorTargetCreator","`",".","proposal_target_creator",":","An","instantiation","of",":","class",":","`","~light_head_rcnn.links.model.utils.ProposalTargetCreator","`","."]
docstr: Forward Faster R-CNN and calculate losses.
  
  Here are notations used.
  
  * :math:`N` is the batch size.
  * :math:`R` is the number of bounding boxes per image.
  
  Currently, only :math:`N=1` is supported.
  
  Args:
      imgs (~chainer.Variable): A variable with a batch of images.
      bboxes (~chainer.Variable): A batch of bounding boxes.
          Its shape is :math:`(N, R, 4)`.
      labels (~chainer.Variable): A batch of labels.
          Its shape is :math:`(N, R)`. The background is excluded from
          the definition, which means that the range of the value
          is :math:`[0, L - 1]`. :math:`L` is the number of foreground
          classes.
      scale (float or ~chainer.Variable): Amount of scaling applied to
          the raw image during preprocessing.
  
  Returns:
      chainer.Variable:
      Scalar loss variable.
      This is the sum of losses for Region Proposal Network and
      the head module.
docstr_tok: ["Forward","Faster","R-CNN","and","calculate","losses",".","Here","are","notations","used",".","*",":","math",":","`","N","`","is","the","batch","size",".","*",":","math",":","`","R","`","is","the","number","of","bounding","boxes","per","image",".","Currently",",","only",":","math",":","`","N=1","`","is","supported",".","Args",":","imgs","(","~chainer.Variable",")",":","A","variable","with","a","batch","of","images",".","bboxes","(","~chainer.Variable",")",":","A","batch","of","bounding","boxes",".","Its","shape","is",":","math",":","`","(","N",",","R",",","4",")","`",".","labels","(","~chainer.Variable",")",":","A","batch","of","labels",".","Its","shape","is",":","math",":","`","(","N",",","R",")","`",".","The","background","is","excluded","from","the","definition",",","which","means","that","the","range","of","the","value","is",":","math",":","`","[","0",",","L","-","1","]","`",".",":","math",":","`","L","`","is","the","number","of","foreground","classes",".","scale","(","float","or","~chainer.Variable",")",":","Amount","of","scaling","applied","to","the","raw","image","during","preprocessing",".","Returns",":","chainer.Variable",":","Scalar","loss","variable",".","This","is","the","sum","of","losses","for","Region","Proposal","Network","and","the","head","module","."]
code: def __call__(self, imgs, bboxes, labels, scales):
      """Forward Faster R-CNN and calculate losses.
  
      Here are notations used.
  
      * :math:`N` is the batch size.
      * :math:`R` is the number of bounding boxes per image.
  
      Currently, only :math:`N=1` is supported.
  
      Args:
          imgs (~chainer.Variable): A variable with a batch of images.
          bboxes (~chainer.Variable): A batch of bounding boxes.
              Its shape is :math:`(N, R, 4)`.
          labels (~chainer.Variable): A batch of labels.
              Its shape is :math:`(N, R)`. The background is excluded from
              the definition, which means that the range of the value
              is :math:`[0, L - 1]`. :math:`L` is the number of foreground
              classes.
          scale (float or ~chainer.Variable): Amount of scaling applied to
              the raw image during preprocessing.
  
      Returns:
          chainer.Variable:
          Scalar loss variable.
          This is the sum of losses for Region Proposal Network and
          the head module.
  
      """
      if isinstance(bboxes, chainer.Variable):
          bboxes = bboxes.array
      if isinstance(labels, chainer.Variable):
          labels = labels.array
      if isinstance(scales, chainer.Variable):
          scales = scales.array
      scales = cuda.to_cpu(scales)
  
      batch_size, _, H, W = imgs.shape
      img_size = (H, W)
  
      rpn_features, roi_features = self.light_head_rcnn.extractor(imgs)
      (
          rpn_locs,
          rpn_scores,
          rois,
          roi_indices,
          anchor,
      ) = self.light_head_rcnn.rpn(rpn_features, img_size, scales)
      rpn_locs = rpn_locs.reshape((-1, rpn_locs.shape[2]))
      rpn_scores = rpn_scores.reshape((-1, rpn_scores.shape[2]))
  
      gt_rpn_locs = []
      gt_rpn_labels = []
      for bbox in bboxes:
          gt_rpn_loc, gt_rpn_label = self.anchor_target_creator(
              bbox, anchor, img_size
          )
          if cuda.get_array_module(rpn_locs.array) != np:
              gt_rpn_loc = cuda.to_gpu(gt_rpn_loc)
              gt_rpn_label = cuda.to_gpu(gt_rpn_label)
          gt_rpn_locs.append(gt_rpn_loc)
          gt_rpn_labels.append(gt_rpn_label)
          del gt_rpn_loc, gt_rpn_label
      gt_rpn_locs = self.xp.concatenate(gt_rpn_locs, axis=0)
      gt_rpn_labels = self.xp.concatenate(gt_rpn_labels, axis=0)
  
      batch_indices = range(batch_size)
      sample_rois = []
      sample_roi_indices = []
      gt_roi_locs = []
      gt_roi_labels = []
  
      for batch_index, bbox, label in zip(
          batch_indices, bboxes, labels
      ):
          roi = rois[roi_indices == batch_index]
          (
              sample_roi,
              gt_roi_loc,
              gt_roi_label,
          ) = self.proposal_target_creator(
              roi,
              bbox,
              label,
              self.loc_normalize_mean,
              self.loc_normalize_std,
          )
          del roi
          sample_roi_index = self.xp.full(
              (len(sample_roi),), batch_index, dtype=np.int32
          )
          sample_rois.append(sample_roi)
          sample_roi_indices.append(sample_roi_index)
          del sample_roi, sample_roi_index
          gt_roi_locs.append(gt_roi_loc)
          gt_roi_labels.append(gt_roi_label)
          del gt_roi_loc, gt_roi_label
      sample_rois = self.xp.concatenate(sample_rois, axis=0)
      sample_roi_indices = self.xp.concatenate(
          sample_roi_indices, axis=0
      )
      gt_roi_locs = self.xp.concatenate(gt_roi_locs, axis=0)
      gt_roi_labels = self.xp.concatenate(gt_roi_labels, axis=0)
  
      roi_cls_locs, roi_scores = self.light_head_rcnn.head(
          roi_features, sample_rois, sample_roi_indices
      )
  
      # RPN losses
      rpn_loc_loss = _fast_rcnn_loc_loss(
          rpn_locs, gt_rpn_locs, gt_rpn_labels, self.rpn_sigma
      )
      rpn_cls_loss = F.softmax_cross_entropy(rpn_scores, gt_rpn_labels)
  
      # Losses for outputs of the head.
      roi_loc_loss, roi_cls_loss = _ohem_loss(
          roi_cls_locs,
          roi_scores,
          gt_roi_locs,
          gt_roi_labels,
          self.n_ohem_sample * batch_size,
          self.roi_sigma,
      )
      roi_loc_loss = 2 * roi_loc_loss
  
      loss = rpn_loc_loss + rpn_cls_loss + roi_loc_loss + roi_cls_loss
      chainer.reporter.report(
          {
              "rpn_loc_loss": rpn_loc_loss,
              "rpn_cls_loss": rpn_cls_loss,
              "roi_loc_loss": roi_loc_loss,
              "roi_cls_loss": roi_cls_loss,
              "loss": loss,
          },
          self,
      )
      return loss
  
file_path: chainercv/chainercv/links/model/light_head_rcnn/light_head_rcnn_train_chain.py
imports: from __future__ import division
  import numpy
  import warnings
  import chainer
  from chainer.backends import cuda
  import chainer.functions
  from chainercv.links.model.faster_rcnn.utils.anchor_target_creator import AnchorTargetCreator
  from chainercv.links.model.faster_rcnn.utils.proposal_target_creator import ProposalTargetCreator
label_desc: Use image node_id 2 for calling the LightHeadRCNNTrainChain obj's underlying member method code with example usage: obj.__call__(imgs, bboxes, labels, scales) and returns: loss