Пример #1
0
    def __init__(self,
                 config,
                 im_list,
                 model=None,
                 gpu_id=0):  # im_list passed from cityscapes dataset
        self.nb_features = config['nb_features']
        self.split = config['split']
        self.im_list = im_list

        workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
        if not cfg.is_immutable(
        ):  # just in case feature extractor has not been set up already
            dset = b'cityscapes_fine_instanceonly_seg_' + self.split
            args = Namespace(
                cfg_file=MASK_RCNN_CONFIG,
                wait=True,
                multi_gpu_testing=False,
                range=None,  #[0, 3],
                opts=['OUTPUT_DIR', config['save']])

            merge_cfg_from_file(args.cfg_file)
            if args.opts is not None:
                merge_cfg_from_list(args.opts)
                assert_and_infer_cfg()

        if model is None or model == False:
            self.model = initialize_model_from_cfg(instanciate_head_also=True)
        else:
            self.model = model

        gpu_dev = caffe2_core.DeviceOption(caffe2_pb2.CUDA, gpu_id)
        name_scope = 'gpu_{}'.format(gpu_id)
        # Subsampler - originally inside the FPN network. But we don't want to predict the subsampled features.
        # Instead, we want to predict the features, and then use the same subsampling operator to obtain the subsampled features
        with caffe2_core.NameScope(name_scope):
            with caffe2_core.DeviceScope(gpu_dev):
                self.subsampler = caffe2_core.CreateOperator(
                    "MaxPool",  # operator
                    ["predicted_fpn_res5_2_sum"],  #input blobs
                    ["predicted_fpn_res5_2_sum_subsampled_2x"],  #output blobs
                    kernel=1,
                    pad=0,
                    stride=2,
                    deterministic=1)

        self.timers = {
            k: Timer()
            for k in [
                'im_detect_bbox', 'im_detect_mask', 'misc_bbox', 'misc_mask',
                'im_forward_backbone'
            ]
        }

        # For evaluation with respect to the dataset's gt, we save the prediction of the annotated frame for each sequence
        self.num_classes = cfg.MODEL.NUM_CLASSES
        self.num_images = len(self.im_list)
        self.all_boxes_ann_frame, self.all_segms_ann_frame, _ = empty_results(
            self.num_classes, self.num_images)
        self.id_sequences = []
        self.gpu_id = gpu_id
Пример #2
0
def configure_bbox_reg_weights(model, saved_cfg):
    """Compatibility for old models trained with bounding box regression
    mean/std normalization (instead of fixed weights).
    """
    if 'MODEL' not in saved_cfg or 'BBOX_REG_WEIGHTS' not in saved_cfg.MODEL:
        logger.warning('Model from weights file was trained before config key '
                       'MODEL.BBOX_REG_WEIGHTS was added. Forcing '
                       'MODEL.BBOX_REG_WEIGHTS = (1., 1., 1., 1.) to ensure '
                       'correct **inference** behavior.')
        # Generally we don't allow modifying the config, but this is a one-off
        # hack to support some very old models
        is_immutable = cfg.is_immutable()
        cfg.immutable(False)
        cfg.MODEL.BBOX_REG_WEIGHTS = (1., 1., 1., 1.)
        cfg.immutable(is_immutable)
        logger.info('New config:')
        logger.info(pprint.pformat(cfg))
        assert not model.train, (
            'This model was trained with an older version of the code that '
            'used bounding box regression mean/std normalization. It can no '
            'longer be used for training. To upgrade it to a trainable model '
            'please use fb/compat/convert_bbox_reg_normalized_model.py.')
Пример #3
0
def configure_bbox_reg_weights(model, saved_cfg):
    """Compatibility for old models trained with bounding box regression
    mean/std normalization (instead of fixed weights).
    """
    if 'MODEL' not in saved_cfg or 'BBOX_REG_WEIGHTS' not in saved_cfg.MODEL:
        logger.warning('Model from weights file was trained before config key '
                       'MODEL.BBOX_REG_WEIGHTS was added. Forcing '
                       'MODEL.BBOX_REG_WEIGHTS = (1., 1., 1., 1.) to ensure '
                       'correct **inference** behavior.')
        # Generally we don't allow modifying the config, but this is a one-off
        # hack to support some very old models
        is_immutable = cfg.is_immutable()
        cfg.immutable(False)
        cfg.MODEL.BBOX_REG_WEIGHTS = (1., 1., 1., 1.)
        cfg.immutable(is_immutable)
        logger.info('New config:')
        logger.info(pprint.pformat(cfg))
        assert not model.train, (
            'This model was trained with an older version of the code that '
            'used bounding box regression mean/std normalization. It can no '
            'longer be used for training. To upgrade it to a trainable model '
            'please use fb/compat/convert_bbox_reg_normalized_model.py.'
        )
Пример #4
0
    def __init__(self,
                 split,
                 frame_ss,
                 nSeq,
                 features,
                 savedir,
                 size=None,
                 loaded_model=None):
        super(CityscapesDatasetAndFeatures, self).__init__()

        self.split = split
        self.frame_ss = frame_ss
        self.nSeq = nSeq or 1
        self.features = features
        self.FPNfeatures = u'fpn_res5_2_sum' in self.features \
            or u'fpn_res4_5_sum' in self.features \
            or u'fpn_res3_3_sum' in self.features \
            or u'fpn_res2_2_sum' in self.features
        self.limitSize = size

        # Check which features have been precomputed and load them if they have been found
        logger.info('Searching for precomputed features...')
        self.precompute_features_dir = PRECOMPUTED_FEATURES_DIR

        self.potential_precomputed_feature_types = [
            u'fpn_res5_2_sum', u'fpn_res4_5_sum', u'fpn_res3_3_sum',
            u'fpn_res2_2_sum'
        ]
        self.load_precomputed = self.check_requested_features_that_could_be_precomputed_were(
        )
        if self.load_precomputed:
            self.precomputed_features_index, self.precomputed_features = \
                self.load_requested_precomputed_features()

        self.requested_fpn_features = [] + \
            ([u'fpn_res5_2_sum'] if 'fpn_res5_2_sum' in self.features else []) + \
            ([u'fpn_res4_5_sum'] if 'fpn_res4_5_sum' in self.features else []) + \
            ([u'fpn_res3_3_sum'] if 'fpn_res3_3_sum' in self.features else []) + \
            ([u'fpn_res2_2_sum'] if 'fpn_res2_2_sum' in self.features else [])

        import detectron.utils.c2 as c2_utils
        c2_utils.import_detectron_ops()

        workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
        dset = b'cityscapes_fine_instanceonly_seg_sequences_' + self.split
        if not cfg.is_immutable(
        ):  # just in case feature extractor has not been set up already
            # Preparing where to load data from and how using coco api

            args = Namespace(cfg_file=MASK_RCNN_CONFIG,
                             wait=True,
                             multi_gpu_testing=False,
                             opts=['OUTPUT_DIR', savedir])

            merge_cfg_from_file(args.cfg_file)
            if args.opts is not None:
                merge_cfg_from_list(args.opts)
                assert_and_infer_cfg()

        assert os.path.exists(cfg.TEST.WEIGHTS), \
            'need path to pretrained instance segmentation model'
        assert not cfg.MODEL.RPN_ONLY, 'end to end model required'
        dataset = JsonDataset(dset)

        self.dataset = dataset
        self.im_list = self.dataset.get_roidb()
        # Preparing the model from which we obtain the features
        if not self.load_precomputed:
            if loaded_model is None:
                model = initialize_model_from_cfg()
                self.model = model
            else:
                self.model = loaded_model
        else:
            self.model = False

        # Store config for further use in running the Mask RCNN head
        logger.info('Cityscapes dataset, size : %d' % len(self))