Example #1
0
    def _single_gpu_build_func(model):
        """Build the model on a single GPU. Can be called in a loop over GPUs
        with name and device scoping to create a data parallel model.
        """
        # Add the conv body (called "backbone architecture" in papers)
        # E.g., ResNet-50, ResNet-50-FPN, ResNeXt-101-FPN, etc.
        blob_conv, dim_conv, spatial_scale_conv = add_conv_body_func(model)
        if freeze_conv_body:
            for b in c2_utils.BlobReferenceList(blob_conv):
                model.StopGradient(b, b)

        if not model.train:  # == inference
            # Create a net that can be used to execute the conv body on an image
            # (without also executing RPN or any other network heads)
            model.conv_body_net = model.net.Clone('conv_body_net')

        head_loss_gradients = {
            'rpn': None,
            'box': None,
            'mask': None,
            'keypoints': None,
        }

        if cfg.RPN.RPN_ON:
            # Add the RPN head
            head_loss_gradients['rpn'] = rpn_heads.add_generic_rpn_outputs(
                model, blob_conv, dim_conv, spatial_scale_conv)

        if cfg.FPN.FPN_ON:
            # After adding the RPN head, restrict FPN blobs and scales to
            # those used in the RoI heads
            blob_conv, spatial_scale_conv = _narrow_to_fpn_roi_levels(
                blob_conv, spatial_scale_conv)

        if not cfg.MODEL.RPN_ONLY:
            # Add the Fast R-CNN head
            head_loss_gradients['box'] = _add_fast_rcnn_head(
                model, add_roi_box_head_func, blob_conv, dim_conv,
                spatial_scale_conv)

        if cfg.MODEL.MASK_ON:
            # Add the mask head
            head_loss_gradients['mask'] = _add_roi_mask_head(
                model, add_roi_mask_head_func, blob_conv, dim_conv,
                spatial_scale_conv)

        if cfg.MODEL.KEYPOINTS_ON:
            # Add the keypoint head
            head_loss_gradients['keypoint'] = _add_roi_keypoint_head(
                model, add_roi_keypoint_head_func, blob_conv, dim_conv,
                spatial_scale_conv)

        if model.train:
            loss_gradients = {}
            for lg in head_loss_gradients.values():
                if lg is not None:
                    loss_gradients.update(lg)
            return loss_gradients
        else:
            return None
    def _single_gpu_build_func(model):
        """Builds the model on a single GPU. Can be called in a loop over GPUs
        with name and device scoping to create a data parallel model."""
        # For training we define one net that contains all ops
        # For inference, we split the graph into two nets: a standard fast r-cnn
        # net and a mask prediction net; the mask net is only applied to a
        # subset of high-scoring detections
        is_inference = not model.train

        head_loss_gradients = {
            'rpn': None,
            'box': None,
            'mask': None,
            'keypoints': None,
        }

        # Add the conv body
        blob_conv, dim_conv, spatial_scale_conv = add_conv_body_func(model)
        if freeze_conv_body:
            for b in blob_ref_to_list(blob_conv):
                model.StopGradient(b, b)
        backbone_net = copy.deepcopy(model.net.Proto())
        model.backbone = model.net.Clone('backbone')

        if cfg.RPN.RPN_ON:
            # Add the RPN head
            head_loss_gradients['rpn'] = rpn_heads.add_generic_rpn_outputs(
                model, blob_conv, dim_conv, spatial_scale_conv)

        if cfg.FPN.FPN_ON:
            # After adding the RPN head, restrict FPN blobs and scales to
            # those used in the RoI heads
            blob_conv, spatial_scale_conv = model_builder._narrow_to_fpn_roi_levels(
                blob_conv, spatial_scale_conv)

        assert not cfg.MODEL.RPN_ONLY, 'only mask rcnn end2end implemented and tested'
        # Add the Fast R-CNN head
        head_loss_gradients[
            'box'], blob_cls, blob_bbox = _add_fast_rcnn_head_for_feature_extraction(
                model, add_roi_frcn_head_func, blob_conv, dim_conv,
                spatial_scale_conv)

        # Extract the head of faster rcnn net, store it as its own network,
        # then create a new network faster_rcnn_head
        model.faster_rnn_head, [blob_cls,
                                blob_bbox] = SuffixNet('faster_rcnn_head',
                                                       model.net,
                                                       len(backbone_net.op),
                                                       [blob_cls, blob_bbox])

        if cfg.MODEL.MASK_ON:
            head_loss_gradients['mask'] = model_builder._add_roi_mask_head(
                model, add_roi_mask_head_func, blob_conv, dim_conv,
                spatial_scale_conv)

        # Add the keypoint branch
        assert not cfg.MODEL.KEYPOINTS_ON, 'keypoints not implemented, otherwise need to extend here'

        assert not model.train, 'not implemented, should add losses here'
Example #3
0
    def _single_gpu_build_func(model):
        """Build the model on a single GPU. Can be called in a loop over GPUs
        with name and device scoping to create a data parallel model.
        """
        # Add the conv body (called "backbone architecture" in papers)
        # E.g., ResNet-50, ResNet-50-FPN, ResNeXt-101-FPN, etc.
        blob_conv, dim_conv, spatial_scale_conv = add_conv_body_func(model)

        head_loss_gradients = {
            'rpn': None,
            'box': None,
            'mask': None,
            'keypoints': None,
        }

        if cfg.RPN.RPN_ON:
            # Add the RPN head
            head_loss_gradients['rpn'] = rpn_heads.add_generic_rpn_outputs(
                model, blob_conv, dim_conv, spatial_scale_conv
            )

        if cfg.FPN.FPN_ON:
            # After adding the RPN head, restrict FPN blobs and scales to
            # those used in the RoI heads
            blob_conv, spatial_scale_conv = _narrow_to_fpn_roi_levels(
                blob_conv, spatial_scale_conv
            )

        if not cfg.MODEL.RPN_ONLY:
            # Add the Fast R-CNN head
            head_loss_gradients['box'] = _add_fast_rcnn_head(
                model, add_roi_box_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )


        if model.train:
            loss_gradients = {}
            for lg in head_loss_gradients.values():
                if lg is not None:
                    loss_gradients.update(lg)
            return loss_gradients
Example #4
0
 def _single_gpu_build_func(model):
     """Builds the model on a single GPU. Can be called in a loop over GPUs
     with name and device scoping to create a data parallel model."""
     blob_conv, dim_conv, spatial_scale_conv = add_conv_body_func(model)
     head_loss_gradients = {
         'rpn': None,
         'box': None,
         'mask': None,
         'keypoints': None,
         'classification': None
     }
     if freeze_conv_body:
         for b in c2_utils.BlobReferenceList(blob_conv):
             model.StopGradient(b, b)
     if not model.train:
         model.conv_body_net = model.net.Clone('conv_body_net')
     if cfg.RPN.RPN_ON:
         # Add the RPN head
         head_loss_gradients['rpn'] = rpn_heads.add_generic_rpn_outputs(
             model, blob_conv, dim_conv, spatial_scale_conv
         )
     if cfg.FPN.FPN_ON:
         # After adding the RPN head, restrict FPN blobs and scales to
         # those used in the RoI heads
         blob_conv, spatial_scale_conv = _narrow_to_fpn_roi_levels(
             blob_conv, spatial_scale_conv
         )
     head_loss_gradients['box'] = _add_rfcn_head(
             model, add_roi_box_head_func, blob_conv, dim_conv,
             spatial_scale_conv, dim_reduce
         )
     #rfcn_heads.add_rfcn_outputs(model, blob_conv, dim_conv, dim_reduce, spatial_scale_conv)
     #if model.train:
         #head_loss_gradients['box'] = fast_rcnn_heads.add_fast_rcnn_losses(model)
     if model.train:
         loss_gradients = {}
         for lg in head_loss_gradients.values():
             if lg is not None:
                 loss_gradients.update(lg)
         return loss_gradients
     else:
         return None
Example #5
0
    def _single_gpu_build_func(model):
        """Build the model on a single GPU. Can be called in a loop over GPUs
        with name and device scoping to create a data parallel model.
        """
        # Add the conv body (called "backbone architecture" in papers)
        # E.g., ResNet-50, ResNet-50-FPN, ResNeXt-101-FPN, etc.
        blob_conv, dim_conv, spatial_scale_conv = add_conv_body_func(model)
        if freeze_conv_body:
            for b in c2_utils.BlobReferenceList(blob_conv):
                model.StopGradient(b, b)

        if not model.train:  # == inference
            # Create a net that can be used to execute the conv body on an image
            # (without also executing RPN or any other network heads)
            model.conv_body_net = model.net.Clone('conv_body_net')

        head_loss_gradients = {
            'rpn': None,
            'box': None,
            'mask': None,
            'keypoints': None,
        }

        if cfg.RPN.RPN_ON:
            # Add the RPN head
            head_loss_gradients['rpn'] = rpn_heads.add_generic_rpn_outputs(
                model, blob_conv, dim_conv, spatial_scale_conv
            )

        if cfg.FPN.FPN_ON:
            # After adding the RPN head, restrict FPN blobs and scales to
            # those used in the RoI heads
            blob_conv, spatial_scale_conv = _narrow_to_fpn_roi_levels(
                blob_conv, spatial_scale_conv
            )

        if not cfg.MODEL.RPN_ONLY:
            # Add the Fast R-CNN head
            head_loss_gradients['box'] = _add_fast_rcnn_head(
                model, add_roi_box_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )

        if cfg.MODEL.MASK_ON:
            # Add the mask head
            head_loss_gradients['mask'] = _add_roi_mask_head(
                model, add_roi_mask_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )

        if cfg.MODEL.KEYPOINTS_ON:
            # Add the keypoint head
            head_loss_gradients['keypoint'] = _add_roi_keypoint_head(
                model, add_roi_keypoint_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )

        if model.train:
            loss_gradients = {}
            for lg in head_loss_gradients.values():
                if lg is not None:
                    loss_gradients.update(lg)
            return loss_gradients
        else:
            return None
Example #6
0
    def _single_gpu_build_func(model):
        """Build the model on a single GPU. Can be called in a loop over GPUs
        with name and device scoping to create a data parallel model.
        """
        # Add the conv body (called "backbone architecture" in papers)
        # E.g., ResNet-50, ResNet-50-FPN, ResNeXt-101-FPN, etc.
        blob_conv, dim_conv, spatial_scale_conv = add_conv_body_func(model)
        if freeze_conv_body:
            for b in c2_utils.BlobReferenceList(blob_conv):
                model.StopGradient(b, b)

        if not model.train:  # == inference
            # Create a net that can be used to execute the conv body on an image
            # (without also executing RPN or any other network heads)
            model.conv_body_net = model.net.Clone('conv_body_net')

        head_loss_gradients = {
            'rpn': None,
            'box': None,
            'mask': None,
            'keypoints': None,
            'classification': None
        }

        if cfg.RPN.RPN_ON:
            # Add the RPN head
            head_loss_gradients['rpn'] = rpn_heads.add_generic_rpn_outputs(
                model, blob_conv, dim_conv, spatial_scale_conv
            )

        if cfg.FPN.FPN_ON:
            # After adding the RPN head, restrict FPN blobs and scales to
            # those used in the RoI heads
            blob_conv, spatial_scale_conv = _narrow_to_fpn_roi_levels(
                blob_conv, spatial_scale_conv
            )

        if not cfg.MODEL.RPN_ONLY and not cfg.MODEL.CLASSIFICATION and not cfg.MODEL.FR_FUSED:
            # Add the Fast R-CNN head
            head_loss_gradients['box'] = _add_fast_rcnn_head(
                model, add_roi_box_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )
            if cfg.MODEL.CASCADE_ON:
                # Add the Cascade R-CNN head
                num_stage = cfg.CASCADE_RCNN.NUM_STAGE
                _check_for_cascade_rcnn()
                for stage in range(2, num_stage + 1):
                    stage_name = '_{}'.format(stage)
                    head_loss_gradients['box' + stage_name] = _add_cascade_rcnn_head(
                        model, add_roi_cascade_head_func,
                        blob_conv, dim_conv, spatial_scale_conv, stage
                    )

        if cfg.MODEL.FR_FUSED:
            blob_frcnn, dim_frcnn = add_roi_box_head_func(
                model, blob_conv, dim_conv, spatial_scale_conv
            )
            get_func(cfg.RFCN.ROI_BOX_HEAD)(
                model, blob_conv, dim_conv, spatial_scale_conv, dim_reduce=None
            )
            fr_fused_heads.add_fr_fused_outputs(model, blob_frcnn, dim_frcnn)
            if model.train:
                head_loss_gradients['box'] = fr_fused_heads.add_fast_rcnn_losses(model)

        if cfg.MODEL.MASK_ON:
            # Add the mask head
            head_loss_gradients['mask'] = _add_roi_mask_head(
                model, add_roi_mask_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )

        if cfg.MODEL.KEYPOINTS_ON:
            # Add the keypoint head
            head_loss_gradients['keypoint'] = _add_roi_keypoint_head(
                model, add_roi_keypoint_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )

        if cfg.MODEL.CLASSIFICATION:
            # Add classification head
            head_loss_gradients['classification'] = _add_classification_head(
                model, add_classification_head_func, blob_conv, dim_conv)

        if model.train:
            loss_gradients = {}
            for lg in head_loss_gradients.values():
                if lg is not None:
                    loss_gradients.update(lg)
            return loss_gradients
        else:
            return None
    def _single_gpu_build_func(model):
        ### 在单块gpu上建立模型,产生数据并行的模型
        """Build the model on a single GPU. Can be called in a loop over GPUs
        with name and device scoping to create a data parallel model.
        """
        # Add the conv body (called "backbone architecture" in papers)
        # E.g., ResNet-50, ResNet-50-FPN, ResNeXt-101-FPN, etc.
        ###  添加卷积骨架网络

        #### 下面这几句都是啥意思???
        blob_conv, dim_conv, spatial_scale_conv = add_conv_body_func(model)
        if freeze_conv_body:
            for b in c2_utils.BlobReferenceList(blob_conv):
                model.StopGradient(b, b)

        if not model.train:  # == inference

            ### 创建推断时执行的卷积网络结构
            # Create a net that can be used to execute the conv body on an image
            # (without also executing RPN or any other network heads)
            model.conv_body_net = model.net.Clone('conv_body_net')

        head_loss_gradients = {
            'rpn': None,
            'box': None,
            'mask': None,
            'keypoints': None,
        }

        if cfg.RPN.RPN_ON:
            # Add the RPN head
            head_loss_gradients['rpn'] = rpn_heads.add_generic_rpn_outputs(
                model, blob_conv, dim_conv, spatial_scale_conv
            )

        if cfg.FPN.FPN_ON:
            # After adding the RPN head, restrict FPN blobs and scales to
            # those used in the RoI heads
            blob_conv, spatial_scale_conv = _narrow_to_fpn_roi_levels(
                blob_conv, spatial_scale_conv
            )

        if not cfg.MODEL.RPN_ONLY:
            # Add the Fast R-CNN head
            head_loss_gradients['box'] = _add_fast_rcnn_head(
                model, add_roi_box_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )

            ### 如果使用cascade rcnn网络
            if cfg.MODEL.CASCADE_ON:
                # Add the Cascade R-CNN head
                num_stage = cfg.CASCADE_RCNN.NUM_STAGE
                _check_for_cascade_rcnn()

                ### 这里是对每一个stage获得

                for stage in range(2, num_stage + 1):
                    stage_name = '_{}'.format(stage)
                    head_loss_gradients['box' + stage_name] = _add_cascade_rcnn_head(
                        model, add_roi_cascade_head_func,
                        blob_conv, dim_conv, spatial_scale_conv, stage
                    )

        if cfg.MODEL.MASK_ON:
            # Add the mask head
            head_loss_gradients['mask'] = _add_roi_mask_head(
                model, add_roi_mask_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )

        if cfg.MODEL.KEYPOINTS_ON:
            # Add the keypoint head
            head_loss_gradients['keypoint'] = _add_roi_keypoint_head(
                model, add_roi_keypoint_head_func, blob_conv, dim_conv,
                spatial_scale_conv
            )

        ### 训练阶段根据head_loss_gradients更新参数
        if model.train:
            loss_gradients = {}
            for lg in head_loss_gradients.values():
                if lg is not None:
                    ### 用lg中的参数更新loss_gradients
                    loss_gradients.update(lg)
            return loss_gradients
        else:
            return None
Example #8
0
    def _single_gpu_build_func(model):
        """Build the model on a single GPU. Can be called in a loop over GPUs
        with name and device scoping to create a data parallel model.
        """
        # Add the conv body (called "backbone architecture" in papers)
        # E.g., ResNet-50, ResNet-50-FPN, ResNeXt-101-FPN, etc.

        blob_conv, dim_conv, spatial_scale_conv = add_conv_body_func(model)
        if freeze_conv_body:
            for b in c2_utils.BlobReferenceList(blob_conv):
                model.StopGradient(b, b)

        if not model.train:  # == inference
            # Create a net that can be used to execute the conv body on an image
            # (without also executing RPN or any other network heads)
            model.conv_body_net = model.net.Clone('conv_body_net')

        blob_feature_map = blob_conv
        dim_feature_map = dim_conv
        spatial_scale_feature_map = spatial_scale_conv

        head_loss_gradients = {
            'rpn': None,
            'box': None,
            'mask': None,
            'keypoints': None,
            'image': None,
            'instance': None,
            'consistency': None,
        }

        if cfg.RPN.RPN_ON:
            # Add the RPN head
            head_loss_gradients['rpn'] = rpn_heads.add_generic_rpn_outputs(
                model, blob_conv, dim_conv, spatial_scale_conv)

        if cfg.FPN.FPN_ON:
            # After adding the RPN head, restrict FPN blobs and scales to
            # those used in the RoI heads
            blob_conv, spatial_scale_conv = _narrow_to_fpn_roi_levels(
                blob_conv, spatial_scale_conv)

        if not cfg.MODEL.RPN_ONLY:
            # Add the Fast R-CNN head
            head_loss_gradients[
                'box'], blob_feats_rois_all, dim_feats_rois_all = _add_fast_rcnn_head(
                    model, add_roi_box_head_func, blob_conv, dim_conv,
                    spatial_scale_conv)
            # rois = workspace.FetchBlob(blob_feats_rois_all)
            # print(type(rois))

        if cfg.MODEL.MASK_ON:
            # Add the mask head
            head_loss_gradients['mask'] = _add_roi_mask_head(
                model, add_roi_mask_head_func, blob_conv, dim_conv,
                spatial_scale_conv)

        if cfg.MODEL.KEYPOINTS_ON:
            # Add the keypoint head
            head_loss_gradients['keypoint'] = _add_roi_keypoint_head(
                model, add_roi_keypoint_head_func, blob_conv, dim_conv,
                spatial_scale_conv)
        if cfg.TRAIN.DOMAIN_ADAPTATION:
            # Add Image-level loss
            head_loss_gradients['image'], _ = _add_image_level_classifier(
                model, blob_feature_map, dim_feature_map,
                spatial_scale_feature_map)
            # Add Instance-level loss
            head_loss_gradients[
                'instance'], _ = _add_instance_level_classifier(
                    model, blob_feats_rois_all, dim_feats_rois_all)
            # Add consistency regularization
            #head_loss_gradients['consistency'] = _add_consistency_loss(model)

        if model.train:
            loss_gradients = {}
            for lg in head_loss_gradients.values():
                if lg is not None:
                    loss_gradients.update(lg)
            return loss_gradients
        else:
            return None