Exemple #1
0
    def __init__(
        self,
        cf='/detectron/configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml',
        weights='https://dl.fbaipublicfiles.com/detectron/35861858/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml.02_32_51.SgT4y1cO/output/train/coco_2014_train%3Acoco_2014_valminusminival/generalized_rcnn/model_final.pkl'
    ):

        workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
        c2_utils.import_detectron_ops()
        # OpenCL may be enabled by default in OpenCV3; disable it because it's not
        # thread safe and causes unwanted GPU memory allocations.
        cv2.ocl.setUseOpenCL(False)
        # cfg path
        self.cfg = cf
        # weights path
        self.weights = weights
        self.weights = cache_url(self.weights, cfg.DOWNLOAD_CACHE)
        merge_cfg_from_file(self.cfg)
        cfg.NUM_GPUS = 1
        assert_and_infer_cfg(cache_urls=False)

        assert not cfg.MODEL.RPN_ONLY, \
            'RPN models are not supported'
        assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
            'Models that require precomputed proposals are not supported'

        self.model = infer_engine.initialize_model_from_cfg(self.weights)
        dummy_coco_dataset = dummy_datasets.get_coco_dataset()
def main():
    c2_utils.import_contrib_ops()
    c2_utils.import_detectron_ops()
    cv2.ocl.setUseOpenCL(False)
    args = parse_args()
    input_file = args.input_file
    workspace.GlobalInit(
        ['caffe2', '--caffe2_log_level=0', '--caffe2_gpu_memory_tracking=1'])
    merge_cfg_from_file(
        '/home/LAB/wusj/exp/KL-Loss/configs/e2e_faster_rcnn_R-50-FPN_2x_entropy.yaml'
    )
    assert_and_infer_cfg(cache_urls=False)
    smi_output, cuda_ver, cudnn_ver = c2_utils.get_nvidia_info()
    logger.info("cuda version : {}".format(cuda_ver))
    logger.info("cudnn version: {}".format(cudnn_ver))
    logger.info("nvidia-smi output:\n{}".format(smi_output))
    logger.info('Training with config:')
    logger.info(pprint.pformat(cfg))
    workspace.ResetWorkspace()

    np.random.seed(cfg.RNG_SEED)
    with open(input_file, 'r') as f:
        config = json.load(f)

        parameter = config['parameter']
        inference_id = parameter['inferenceId']

        dataset = parameter['dataSetName']
        model_file = parameter['modelPkl']
        task_id = inference_id
        image_id_list = parameter['imageIdList']

        load_data(dataset, image_id_list)
        roidb, result = test_net(model_file, dataset)

        tmp = []
        for i, entry in enumerate(roidb):
            tmp.append(entry)
        tmp.sort(cmp=compare)

        selectNum = parameter['selectNum']
        output = {}
        # time.sleep(3)
        output['annotationList'] = []
        output['selectImageIdList'] = []
        output['remainImageIdList'] = []
        for i in range(selectNum):
            output['annotationList'].append(result[str(tmp[i]['id'])])
            output['selectImageIdList'].append(str(tmp[i]['id']))
        for i in range(parameter['selectNum'], len(parameter['imageIdList'])):
            output['remainImageIdList'].append(str(tmp[i]['id']))
        output['remainImageNum'] = len(output['remainImageIdList'])
        output['inferenceId'] = inference_id
        output['dataSetName'] = dataset

        result_output_dir = '/home/LAB/wusj/fastwash_tmp/inference/'
        with open(result_output_dir + 'result_' + task_id, 'wt') as f2:
            json.dump(output, f2)
    def __init__(self, config_file, weights_file, gpu_id):
        c2_utils.import_detectron_ops()

        workspace.GlobalInit(['caffe2', '--caffe2_log_level=3'])

        merge_cfg_from_file(config_file)
        cfg.NUM_GPUS = 1
        assert_and_infer_cfg(cache_urls=False)
        self.__model = infer_engine.initialize_model_from_cfg(weights_file,
                                                              gpu_id=gpu_id)
        self.gpu_id = gpu_id
Exemple #4
0
 def setup(self):
     c2_utils.import_detectron_ops()
     cv2.ocl.setUseOpenCL(False)
     merge_cfg_from_file(
         '/DensePose/configs/DensePose_ResNet101_FPN_s1x-e2e.yaml')
     cfg.NUM_GPUS = 1
     weights = cache_url(
         'https://s3.amazonaws.com/densepose/DensePose_ResNet101_FPN_s1x-e2e.pkl',
         cfg.DOWNLOAD_CACHE)
     assert_and_infer_cfg(cache_urls=False)
     model = infer_engine.initialize_model_from_cfg(weights, 1)
     dummy_coco_dataset = dummy_datasets.get_coco_dataset()
     return model
Exemple #5
0
def load_model():
    c2_utils.import_detectron_ops()

    # OpenCL may be enabled by default in OpenCV3; disable it because it's not
    # thread safe and causes unwanted GPU memory allocations.
    cv2.ocl.setUseOpenCL(False)

    workspace.GlobalInit(['caffe2', '--caffe2_log_level=4'])
    merge_cfg_from_file(CONFIG_PATH)
    cfg.NUM_GPUS = 1

    print("Loading weights")
    weights = cache_url(MODEl_URL, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(weights, gpu_id=GPU_ID)
    print("Model loaded")

    return model
def main():
    c2_utils.import_contrib_ops()
    c2_utils.import_detectron_ops()
    cv2.ocl.setUseOpenCL(False)
    workspace.GlobalInit(
        ['caffe2', '--caffe2_log_level=0', '--caffe2_gpu_memory_tracking=1'])
    merge_cfg_from_file(
        '/home/LAB/wusj/exp/KL-Loss/configs/e2e_faster_rcnn_R-50-FPN_2x_entropy.yaml'
    )
    assert_and_infer_cfg(cache_urls=False)
    smi_output, cuda_ver, cudnn_ver = c2_utils.get_nvidia_info()
    logger.info("cuda version : {}".format(cuda_ver))
    logger.info("cudnn version: {}".format(cudnn_ver))
    logger.info("nvidia-smi output:\n{}".format(smi_output))
    logger.info('Training with config:')
    logger.info(pprint.pformat(cfg))
    workspace.ResetWorkspace()

    np.random.seed(cfg.RNG_SEED)
    args = parse_args()
    input_file = args.input_file
    result_output_dir = args.output_dir

    with open(input_file, 'r') as f:
        config = json.load(f)

        dataset = config['dataSetName']
        model_file = args.model_file
        task_id = config['id']
        image_id_list = config['imageIdList']

        load_data(dataset, image_id_list)
        roidb, result = test_net(model_file, dataset)
        config['inferenceResult'] = result
        with open(result_output_dir + 'result_' + task_id, 'wt') as f2:
            json.dump(config, f2)
Exemple #7
0
import sys
import time
import re

from copy import deepcopy
from caffe2.python import workspace

from detectron.core.config import assert_and_infer_cfg
from detectron.core.config import cfg
from detectron.core.config import merge_cfg_from_file
from detectron.core.config import merge_cfg_from_list
from detectron.core.test_engine import run_inference
from detectron.utils.logging import setup_logging
import detectron.utils.c2 as c2_utils

c2_utils.import_detectron_ops()

# OpenCL may be enabled by default in OpenCV3; disable it because it's not
# thread safe and causes unwanted GPU memory allocations.
cv2.ocl.setUseOpenCL(False)

# gloabl value
file_processed = []

def parse_args():
    parser = argparse.ArgumentParser(description='Test a Fast R-CNN network')
    parser.add_argument(
        '--cfg',
        dest='cfg_file',
        help='optional config file',
        default=None,
        A = np.random.randn(2, 3, 5, 7).astype(np.float32)
        I = np.array([0, 1], dtype=np.int32)
        self._run_op_test(A, I, check_grad=True)

        A = np.random.randn(2, 3, 5, 7).astype(np.float32)
        I = np.array([1, 0], dtype=np.int32)
        self._run_op_test(A, I, check_grad=True)

        A = np.random.randn(10, 3, 5, 7).astype(np.float32)
        I = np.array(np.random.permutation(10), dtype=np.int32)
        self._run_op_test(A, I, check_grad=True)

    def test_size_exceptions(self):
        A = np.random.randn(2, 256, 42, 86).astype(np.float32)
        I = np.array(np.random.permutation(10), dtype=np.int32)
        with self.assertRaises(RuntimeError):
            self._run_op_test(A, I)

    # See doc string in _run_speed_test
    # def test_perf(self):
    #     with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
    #         self._run_speed_test()


if __name__ == '__main__':
    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    c2_utils.import_detectron_ops()
    assert 'BatchPermutation' in workspace.RegisteredOperators()
    logging_utils.setup_logging(__name__)
    unittest.main()
Exemple #9
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))