(self, imgs, bboxes, labels, scales)
return loss
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.
Forward Faster R-CNN and calculate losses.
Forward Faster R - CNN and calculate losses .
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
def __call__ ( self , imgs , bboxes , labels , scales ) : 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