Exemple #1
0
def main(args):
    logger = logging.getLogger(__name__)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    cfg_orig = load_cfg(envu.yaml_dump(cfg))
    im = cv2.imread(args.im_file)

    if args.rpn_pkl is not None:
        proposal_boxes, _proposal_scores = get_rpn_box_proposals(im, args)
        workspace.ResetWorkspace()
    else:
        proposal_boxes = None

    cls_boxes, cls_segms, cls_keyps, cls_bodys = None, None, None, None
    for i in range(0, len(args.models_to_run), 2):
        pkl = args.models_to_run[i]
        yml = args.models_to_run[i + 1]
        cfg.immutable(False)
        merge_cfg_from_cfg(cfg_orig)
        merge_cfg_from_file(yml)
        if len(pkl) > 0:
            weights_file = pkl
        else:
            weights_file = cfg.TEST.WEIGHTS
        cfg.NUM_GPUS = 1
        assert_and_infer_cfg(cache_urls=False)
        model = model_engine.initialize_model_from_cfg(weights_file)
        with c2_utils.NamedCudaScope(0):
            cls_boxes_, cls_segms_, cls_keyps_ , cls_bodys_= \
                model_engine.im_detect_all(model, im, proposal_boxes)
        cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes
        cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms
        cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps
        cls_bodys = cls_bodys_ if cls_bodys_ is not None else cls_bodys

        workspace.ResetWorkspace()

    out_name = os.path.join(
        args.output_dir, '{}'.format(os.path.basename(args.im_file) + '.pdf'))
    logger.info('Processing {} -> {}'.format(args.im_file, out_name))

    with open('test_vis.pkl', 'w') as f:
        pickle.dump(
            {
                'im': im,
                'cls_boxes': np.array(cls_boxes),
                'cls_bodys': np.array(cls_bodys)
            }, f)

    vis_utils.vis_one_image(im[:, :, ::-1],
                            args.im_file,
                            args.output_dir,
                            cls_boxes,
                            cls_segms,
                            cls_keyps,
                            cls_bodys,
                            dataset=dummy_coco_dataset,
                            box_alpha=0.3,
                            show_class=True,
                            thresh=0.7,
                            kp_thresh=2)
Exemple #2
0
def main(args):
    logger = logging.getLogger(__name__)
    dummy_nucoco_dataset = dummy_datasets.get_nucoco_dataset()
    cfg_orig = load_cfg(envu.yaml_dump(cfg))
    
    ## Load image
    coco = COCO_PLUS(args.ann_file, args.imgs_dir)
    image_id = coco.dataset['images'][args.im_ind]['id']
    img_path = os.path.join(args.imgs_dir, coco.imgs[image_id]["file_name"])
    im = cv2.imread(img_path)

    ## Get the proposals for this image
    proposals = rrpn_loader(args.rpn_pkl)
    proposal_boxes = proposals[image_id]['boxes']
    _proposal_scores = proposals[image_id]['scores']
    workspace.ResetWorkspace()

    ## run models
    cls_boxes, cls_segms, cls_keyps = None, None, None
    for i in range(0, len(args.models_to_run), 2):
        pkl = args.models_to_run[i]
        yml = args.models_to_run[i + 1]
        cfg.immutable(False)
        merge_cfg_from_cfg(cfg_orig)
        merge_cfg_from_file(yml)
        if len(pkl) > 0:
            weights_file = pkl
        else:
            weights_file = cfg.TEST.WEIGHTS
        cfg.NUM_GPUS = 1
        assert_and_infer_cfg(cache_urls=False)
        model = model_engine.initialize_model_from_cfg(weights_file)
        with c2_utils.NamedCudaScope(0):
            cls_boxes_, cls_segms_, cls_keyps_ = \
                model_engine.im_detect_all(model, im, proposal_boxes)
        cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes
        cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms
        cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps
        workspace.ResetWorkspace()

    out_name = os.path.join(
        args.output_dir, '{}'.format(os.path.basename(img_path) + '.pdf')
    )
    logger.info('Processing {} -> {}'.format(img_path, out_name))

    vis_utils.vis_one_image(
        im[:, :, ::-1],
        img_path,
        args.output_dir,
        cls_boxes,
        cls_segms,
        cls_keyps,
        dataset=dummy_nucoco_dataset,
        box_alpha=0.3,
        show_class=True,
        thresh=0.7,
        kp_thresh=2
    )
 def test_gpu_random_input(self):
     X = np.random.rand(5, 4)
     track_n_rois = [2, 3]
     for output in ['Cosine', 'MatchNet']:
         cfg.immutable(False)
         cfg.TRCNN.OUTPUT = output
         assert_and_infer_cfg(cache_urls=False)
         Y_exp = self._add_track_outputs_np(X.copy(), track_n_rois)
         Y_act = self._add_track_outputs(
             X, np.array(track_n_rois, dtype=np.int32))
         np.testing.assert_allclose(Y_act, Y_exp, rtol=1e-06)
Exemple #4
0
def get_detectron_result(im):
    """
    功能: 获取传入图像的检测结果

    输入参数列表:
        im: BGR格式图片
    返回参数列表:
        img: 检测结果图片
        detectron_result_info: 检测结果字典
    """
    setup_logging(__name__)
    logger = logging.getLogger(__name__)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    cfg_orig = load_cfg(yaml.dump(cfg))

    if rpn_pkl is not None:
        proposal_boxes, _proposal_scores = get_rpn_box_proposals(im)
        workspace.ResetWorkspace()
    else:
        proposal_boxes = None

    cls_boxes, cls_segms, cls_keyps = None, None, None
    pkl = rpn_pkl
    yml = rpn_cfg
    cfg.immutable(False)
    merge_cfg_from_cfg(cfg_orig)
    merge_cfg_from_file(yml)
    if len(pkl) > 0:
        weights_file = pkl
    else:
        weights_file = cfg.TEST.WEIGHTS
    cfg.NUM_GPUS = 1
    assert_and_infer_cfg(cache_urls=False)
    model = model_engine.initialize_model_from_cfg(weights_file)
    with c2_utils.NamedCudaScope(0):
        cls_boxes_, cls_segms_, cls_keyps_ = \
            model_engine.im_detect_all(model, im, proposal_boxes)
    cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes
    cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms
    cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps
    workspace.ResetWorkspace()

    img, detectron_result_info = vis_utils.vis_one_image_opencv(
        im,
        cls_boxes,
        cls_segms,
        cls_keyps,
        dataset=dummy_coco_dataset,
        show_box=True,
        show_class=True)

    return img, detectron_result_info
Exemple #5
0
def main(args):
    logger = logging.getLogger(__name__)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    cfg_orig = load_cfg(yaml.dump(cfg))
    im = cv2.imread(args.im_file)

    if args.rpn_pkl is not None:
        proposal_boxes, _proposal_scores = get_rpn_box_proposals(im, args)
        workspace.ResetWorkspace()
    else:
        proposal_boxes = None

    cls_boxes, cls_segms, cls_keyps = None, None, None
    for i in range(0, len(args.models_to_run), 2):
        pkl = args.models_to_run[i]
        yml = args.models_to_run[i + 1]
        cfg.immutable(False)
        merge_cfg_from_cfg(cfg_orig)
        merge_cfg_from_file(yml)
        if len(pkl) > 0:
            weights_file = pkl
        else:
            weights_file = cfg.TEST.WEIGHTS
        cfg.NUM_GPUS = 1
        assert_and_infer_cfg(cache_urls=False)
        model = model_engine.initialize_model_from_cfg(weights_file)
        with c2_utils.NamedCudaScope(0):
            cls_boxes_, cls_segms_, cls_keyps_ = \
                model_engine.im_detect_all(model, im, proposal_boxes)
        cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes
        cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms
        cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps
        workspace.ResetWorkspace()

    out_name = os.path.join(
        args.output_dir, '{}'.format(os.path.basename(args.im_file) + '.pdf')
    )
    logger.info('Processing {} -> {}'.format(args.im_file, out_name))

    vis_utils.vis_one_image(
        im[:, :, ::-1],
        args.im_file,
        args.output_dir,
        cls_boxes,
        cls_segms,
        cls_keyps,
        dataset=dummy_coco_dataset,
        box_alpha=0.3,
        show_class=True,
        thresh=0.7,
        kp_thresh=2
    )
Exemple #6
0
def get_rpn_box_proposals(im, args):
    cfg.immutable(False)
    merge_cfg_from_file(args.rpn_cfg)
    cfg.NUM_GPUS = 1
    cfg.MODEL.RPN_ONLY = True
    cfg.TEST.RPN_PRE_NMS_TOP_N = 10000
    cfg.TEST.RPN_POST_NMS_TOP_N = 2000
    assert_and_infer_cfg(cache_urls=False)

    model = model_engine.initialize_model_from_cfg(args.rpn_pkl)
    with c2_utils.NamedCudaScope(0):
        boxes, scores = rpn_engine.im_proposals(model, im)
    return boxes, scores
Exemple #7
0
def get_rpn_box_proposals(im, args):
    cfg.immutable(False)
    merge_cfg_from_file(args.rpn_cfg)
    cfg.NUM_GPUS = 1
    cfg.MODEL.RPN_ONLY = True
    cfg.TEST.RPN_PRE_NMS_TOP_N = 10000
    cfg.TEST.RPN_POST_NMS_TOP_N = 2000
    assert_and_infer_cfg(cache_urls=False)

    model = model_engine.initialize_model_from_cfg(args.rpn_pkl)
    with c2_utils.NamedCudaScope(0):
        boxes, scores = rpn_engine.im_proposals(model, im)
    return boxes, scores
 def test_gpu_random_input(self):
     X = np.random.rand(1, 6).astype(np.float32)
     X_gt = np.random.randint(2, size=(1, 6)).astype(np.float32)
     for loss in [
             'Cosine', 'L2', 'L2Balanced', 'CrossEntropy',
             'CrossEntropyBalanced', 'CrossEntropyWeighted'
     ]:
         cfg.immutable(False)
         cfg.TRCNN.LOSS = loss
         assert_and_infer_cfg(cache_urls=False)
         Y_exp = self._add_track_losses_np(X.copy(), X_gt.copy())
         Y_act = self._add_track_losses(X.copy(), X_gt.copy())
         np.testing.assert_allclose(Y_act, Y_exp, rtol=1e-06)
Exemple #9
0
def main():
    # Initialize C2
    workspace.GlobalInit(
        ['caffe2', '--caffe2_log_level=0', '--caffe2_gpu_memory_tracking=1'])
    # Set up logging and load config options
    logger = setup_logging(__name__)
    logging.getLogger('detectron.roi_data.loader').setLevel(logging.INFO)
    args = parse_args()
    logger.info('Called with args:')
    logger.info(args)
    if args.cfg_file is not None:
        merge_cfg_from_file(args.cfg_file)
    if args.opts is not None:
        merge_cfg_from_list(args.opts)
    assert_and_infer_cfg()
    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))
    # Note that while we set the numpy random seed network training will not be
    # deterministic in general. There are sources of non-determinism that cannot
    # be removed with a reasonble execution-speed tradeoff (such as certain
    # non-deterministic cudnn functions).
    np.random.seed(cfg.RNG_SEED)
    # Execute the training run
    checkpoints = detectron.utils.train.train_model()
    # Test the trained model
    if not args.skip_test:
        test_model(checkpoints['final'], args.multi_gpu_testing, args.opts)
        print('reprint snapshot name for the result: ', checkpoints['final'])

        cfg.immutable(False)
        cfg.TEST.BBOX_AUG.ENABLED = False
        cfg.REID.VIS = False
        cfg.immutable(True)

        for snapshot in sorted(checkpoints.iterkeys(), reverse=True):
            test_model(checkpoints[snapshot], args.multi_gpu_testing,
                       args.opts)
            print('reprint snapshot name for the result: ', snapshot,
                  checkpoints[snapshot])
Exemple #10
0
def main(args):
    logger = logging.getLogger(__name__)

    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    if len(args.datasets):
        cfg.TRAIN.DATASETS = tuple(args.datasets)
    assert_and_infer_cfg(cache_urls=False)
    cfg.immutable(False)
    cfg.TRAIN.IMS_PER_BATCH = 1
    cfg.immutable(True)

    model = create_model(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    # Iterate through all images
    # TODO: find a proper way to stop iteration
    for i in xrange(1000):
        logger.info("Processing {}".format(i +))
        with c2_utils.NamedCudaScope(0):
            try:
                workspace.RunNet(model.net.Proto().name)
            except:
                pass
            # Fetch specified blobs
            blobs = [(blob_name, workspace.FetchBlob(core.ScopedName(blob_name))) \
                for blob_name in args.blobs]
        invalid_blobs = [blob for blob in blobs if not hasattr(blob[1],
            'shape')]
        assert len(invalid_blobs) == 0, "Blobs not found: {}".format([blob[0] \
            for blob in invalid_blobs])
        # Save blobs
        save_file = os.path.join(args.output_dir, str(i) + ".npz")
        logger.info("Saving to {}".format(save_file))
        to_save = [blob[1] for blob in blobs]
        np.savez(open(save_file, "w+"), *to_save)
Exemple #11
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.')
Exemple #12
0
def get_rpn_box_proposals(im):
    """
    功能: 获取传入图像的region proposal

    输入参数列表:
        im: BGR格式图片
    返回参数列表:
        boxes: 建议boxes
        scores: 对应的分数
    """
    cfg.immutable(False)
    merge_cfg_from_file(rpn_cfg)
    cfg.NUM_GPUS = 1
    cfg.MODEL.RPN_ONLY = True
    cfg.TEST.RPN_PRE_NMS_TOP_N = 10000
    cfg.TEST.RPN_POST_NMS_TOP_N = 2000
    assert_and_infer_cfg(cache_urls=False)

    model = model_engine.initialize_model_from_cfg(rpn_pkl)
    with c2_utils.NamedCudaScope(0):
        boxes, scores = rpn_engine.im_proposals(model, im)
    return boxes, scores
Exemple #13
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.'
        )
Exemple #14
0
    for scoped_name, blob in restored_all_params.items():
        unscoped_name = c2_utils.UnscopeName(scoped_name)
        np.testing.assert_array_equal(blob, orig_gpu_0_params[unscoped_name])


if __name__ == '__main__':
    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    logger = setup_logging(__name__)
    logger.setLevel(logging.DEBUG)
    logging.getLogger('Detectron-ZZnet.roi_data.loader').setLevel(logging.INFO)
    np.random.seed(cfg.RNG_SEED)
    output_dir = tempfile.mkdtemp()
    # Generate config for test
    cfg.MODEL.TYPE = 'generalized_rcnn'
    cfg.MODEL.CONV_BODY = 'FPN.add_fpn_ResNet50_conv5_body'
    cfg.MODEL.NUM_CLASSES = 81
    cfg.MODEL.FASTER_RCNN = True
    cfg.FPN.FPN_ON = True
    cfg.FPN.MULTILEVEL_ROIS = True
    cfg.FPN.MULTILEVEL_RPN = True
    cfg.FAST_RCNN.ROI_BOX_HEAD = 'fast_rcnn_heads.add_roi_2mlp_head'
    cfg.FAST_RCNN.ROI_XFORM_METHOD = 'RoIAlign'
    cfg.OUTPUT_DIR = output_dir
    cfg.TRAIN.DATASETS = ('coco_2014_minival', )
    cfg.TRAIN.WEIGHTS = b''
    for num_gpu in range(workspace.NumCudaDevices()):
        cfg.immutable(False)
        cfg.NUM_GPUS = num_gpu + 1
        assert_and_infer_cfg()
        test_restore_checkpoint()
def grid_search():
    dataset_name, proposal_file = get_inference_dataset(0, is_parent=False)
    roidb, dataset, start_ind, end_ind, total_num_images = get_roidb_and_dataset(
        dataset_name, proposal_file, None 
    )
    num_images = len(roidb)
    num_classes = cfg.MODEL.NUM_CLASSES
    subinds = np.array_split(range(num_images), cfg.NUM_GPUS)

    tag = 'detection'
    output_dir = get_output_dir(cfg.TEST.DATASETS, training=False)

    det_file = os.path.join(output_dir, 'detections.pkl')
    outputs = load_object(det_file)

    print(len(outputs))
    all_dets_cache = outputs['all_boxes']
    print(len(all_dets_cache))

    all_boxes_cache = []
    all_scores_cache = []
    for i, entry in enumerate(roidb):
        print(i)
        max_det = all_dets_cache[1][i].shape[0]
        print(max_det, num_classes)
        
        boxes = np.zeros((max_det, 4), dtype=np.float32)
        scores = np.zeros((max_det, num_classes), dtype=np.float32)
        boxes[:] = -1
        scores[:] = -1
        for j in range(num_classes):
            if len(all_dets_cache[j]) > 0:
                pass
            else:
                continue
            scores[:, j] = all_dets_cache[j][i][:, 4]
        boxes[:, 0:4] = all_dets_cache[1][i][:, :4]
        boxes = np.tile(boxes, (1, scores.shape[1]))
        print(scores.shape, boxes.shape)
        all_boxes_cache.append(boxes)
        all_scores_cache.append(scores)

    timers = defaultdict(Timer)
    resultss = []
    nmses = [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]
    threshs = [1e-10, 1e-9, 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1]
    max_per_images = [10000, 1000, 100, 10, 1]

    for nms in nmses:
        for thresh in threshs:
            for max_per_image in max_per_images:
                print("----------------------------------------------------")
                print('NUM: ', nms, ' Thresh: ', thresh, ' MAX_PER_IM: ', max_per_image)
                cfg.immutable(False)
                cfg.TEST.NMS = nms
                cfg.TEST.SCORE_THRESH = thresh
                cfg.TEST.DETECTIONS_PER_IM = max_per_image
                cfg.immutable(True)

                all_boxes, all_segms, all_keyps = empty_results(num_classes, num_images)
                for i, entry in enumerate(roidb):
                    # print(i)
                    timers['im_detect_bbox'].tic()
                    scores = all_scores_cache[i]
                    boxes = all_boxes_cache[i]
                    # print(scores.shape, boxes.shape)

                    timers['im_detect_bbox'].toc()

                    timers['misc_bbox'].tic()
                    scores, boxes, cls_boxes_i = box_results_with_nms_and_limit(scores, boxes)
                    timers['misc_bbox'].toc()

                    extend_results(i, all_boxes, cls_boxes_i)

                results = task_evaluation.evaluate_all(
                    dataset, all_boxes, all_segms, all_keyps, output_dir
                )
                print(results)

    print(resultss)
    f = open('grid_search.csv', 'wb')
    wr = csv.writer(f, dialect='excel')
    wr.writerows(resultss)
Exemple #16
0
def create_cpg_net(train=True):
    logger = logging.getLogger(__name__)

    FREEZE_CONV_BODY = cfg.TRAIN.FREEZE_CONV_BODY
    FREEZE_AT = cfg.TRAIN.FREEZE_AT
    WSL_CSC = cfg.WSL.CSC
    CENTER_LOSS = cfg.WSL.CENTER_LOSS
    MIN_ENTROPY_LOSS = cfg.WSL.MIN_ENTROPY_LOSS
    MASK_ON = cfg.MODEL.MASK_ON
    EXECUTION_TYPE = cfg.MODEL.EXECUTION_TYPE

    cfg.immutable(False)
    cfg.TRAIN.FREEZE_CONV_BODY = False
    cfg.TRAIN.FREEZE_AT = 0
    cfg.WSL.CSC = False
    cfg.WSL.CPG = False
    cfg.WSL.CENTER_LOSS = False
    cfg.WSL.MIN_ENTROPY_LOSS = False
    cfg.MODEL.MASK_ON = False
    cfg.MODEL.EXECUTION_TYPE = b'simple'
    cfg.immutable(True)

    output_dir = get_output_dir(cfg.TRAIN.DATASETS, training=True)
    for gpu_id in range(cfg.NUM_GPUS):
        logger.info('Building model: {}'.format('gpu_' + str(gpu_id) + '_' +
                                                cfg.MODEL.TYPE + '_cpg'))
        model = model_builder_wsl.create('gpu_' + str(gpu_id) + '_' +
                                         cfg.MODEL.TYPE + '_cpg',
                                         train=train)
        # workspace.RunNetOnce(model.param_init_net)

        #-----------------------------------------------------------------------------------
        # logger.info(
        # 'Outputs saved to: {:s}'.format(os.path.abspath(output_dir)))
        # dump_proto_files(model, output_dir)

        if cfg.MEMONGER and False:
            start_op, end_op = OP_surgery_head(model, gpu_id)
            optimize_memory_cpg(model, gpu_id)
            OP_surgery_back(model, gpu_id, start_op, end_op)
            namescope = 'gpu_{}/'.format(gpu_id)
            model.net._net.op[0].input[
                0] = namescope + cfg.WSL.CPG_PRE_BLOB + '_grad'
            model.net._net.op[-1].output[
                -1] = namescope + cfg.WSL.CPG_DATA_BLOB + '_grad'
            # share_surgery(model, gpu_id)
        else:
            OP_surgery(model, gpu_id)
        Input_surgery(model, gpu_id)
        workspace.CreateBlob('gpu_' + str(gpu_id) + '/' +
                             cfg.WSL.CPG_PRE_BLOB + '_grad')
        optimize_memory_cpg(model, gpu_id)
        #-----------------------------------------------------------------------------------

        # nu.broadcast_parameters(model)
        workspace.CreateNet(model.net)

        logger.info('Outputs saved to: {:s}'.format(
            os.path.abspath(output_dir)))
        dump_proto_files(model, output_dir)

    cfg.immutable(False)
    cfg.TRAIN.FREEZE_CONV_BODY = FREEZE_CONV_BODY
    cfg.TRAIN.FREEZE_AT = FREEZE_AT
    cfg.WSL.CSC = WSL_CSC
    cfg.WSL.CENTER_LOSS = CENTER_LOSS
    cfg.WSL.MIN_ENTROPY_LOSS = MIN_ENTROPY_LOSS
    cfg.MODEL.MASK_ON = MASK_ON
    cfg.MODEL.EXECUTION_TYPE = EXECUTION_TYPE
    cfg.immutable(True)
Exemple #17
0
def main(args):
    logger = logging.getLogger(__name__)
      
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    if "mot-classes" in args.opts:
        dummy_dataset = dummy_datasets.get_mot_dataset()
        cfg.NUM_CLASSES = 14
    else:
        dummy_dataset = dummy_datasets.get_coco_dataset()
        cfg.NUM_CLASSES = 81
    for i, weights_file in enumerate(args.weights_pre_list):
        args.weights_pre_list[i] = cache_url(weights_file, cfg.DOWNLOAD_CACHE)
    for i, weights_file in enumerate(args.weights_post_list):
        args.weights_post_list[i] = cache_url(weights_file, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    logger.info('Testing with config:')
    logger.info(pprint.pformat(cfg))

    # If True: Evaluation with respect to specified parameters (model from
    # specifig training iterations or inference hyper-parameters)
    # If False: Infer test sequence for evaluation on the MOT benchmark server
    EVAL = "eval" in args.opts 

    train_dir = get_output_dir(cfg.TRAIN.DATASETS, training=True)
    train_dir_split = train_dir.split("/")
    train_dir = os.path.join("/".join(train_dir_split[:-1]) + \
        args.model_suffix, train_dir_split[-1])

    model_list = []
    files = os.listdir(train_dir)

    if EVAL:
        test_dir = "/".join(get_output_dir(cfg.TEST.DATASETS,
            training=False).split("/")[:-1]) + args.model_suffix
        # Evaluation with respect to inference hyper-parameters
        if HYPER_PARAM is not None:
            test_dir = os.path.join(test_dir, HYPER_PARAM.lower())
            model_param = ((args.model, param) for param in PARAM_RANGE)
        # Evaluation with respect to weights from specific training iterations
        else:
            model_param = []
            for f in files:
                if f.startswith("model_") and f.endswith(".pkl"):
                    iter_string = re.findall(r'(?<=model_iter)\d+(?=\.pkl)', f)
                    if len(iter_string) > 0:
                        model_param.append((f, int(iter_string[0])))
            model_param.sort(key=lambda tup: tup[1])
            if "model_final.pkl" in files:
                model_param.append(("model_final.pkl", "final"))
        # Tracking evaluation by interface to matlab engine
        seq_map_path = os.path.join(test_dir, "seq_map.txt")
        if not os.path.exists(test_dir):
            os.makedirs(test_dir)
        with open(seq_map_path, "w+") as seq_map:
            seq_map.write("name\n")
            for dataset in cfg.TEST.DATASETS:
                seq_map.write(get_im_dir(dataset).split("/")[-2] + '\n')
        seq_map_path = os.path.relpath(os.path.abspath(seq_map_path),
            os.path.expanduser(os.path.join(args.devkit_path, "seqmaps")))
        matlab_eng = get_matlab_engine(args.devkit_path)
        eval_datections = lambda res_dir, gt_dir: eval_detections_matlab(
            matlab_eng, seq_map_path, res_dir, gt_dir, 'MOT17')
    else:
        if args.model is not None:
            model_param = ((args.model, None),)
        else:
            model_param = (("model_final.pkl", "final"),)

    # Iterate through (model, parameter) tuples
    for i, (model, param) in enumerate(model_param):
        if EVAL and (i + 1 + args.offset) % (args.skip + 1) != 0:
            logger.info("Skipping {}".format(model))
            continue
        # Hyper parameter inference
        elif HYPER_PARAM is not None:
                cfg.immutable(False)
                setattr(cfg.TRCNN, HYPER_PARAM, param)
                assert_and_infer_cfg(cache_urls=False)
                print(cfg.TRCNN)
        if not EVAL or param >= args.start_at:
            weights_list = args.weights_pre_list + [os.path.join(train_dir, model)] + \
                args.weights_post_list
            preffix_list = args.preffix_list if len(args.preffix_list) \
                else [""] * (len(args.weights_pre_list) + len(args.weights_post_list) + 1)
            workspace.ResetWorkspace()
            model = infer_engine.initialize_mixed_model_from_cfg(weights_list,
                preffix_list=preffix_list)
            logger.info("Processing {}".format(param))
            timing = []
            # iterate through test sequences
            for dataset in cfg.TEST.DATASETS:
                tracking = Tracking(args.thresh, cfg.TRCNN.MAX_BACK_TRACK)
                logger.info("Processing dataset {}".format(dataset))
                im_dir = get_im_dir(dataset)
                vis = None
                if EVAL:
                    output_file = os.path.join(test_dir, str(param),
                        im_dir.split("/")[-2] + ".txt")
                # Visualize detections along with tracking detection file creation
                else:
                    output_dir = os.path.join("outputs/MOT17", im_dir.split("/")[-2])
                    if "vis" in args.opts:
                        vis = {
                            "output-dir": output_dir,
                            "dummy-dataset": dummy_dataset,
                            "show-class": "show-class" in args.opts,
                            "show-track": "show-track" in args.opts,
                            "thresh": args.thresh,
                            "track-thresh": cfg.TRCNN.DETECTION_THRESH,
                            "n-colors": 15,
                        }
                    output_file = os.path.join("outputs/MOT17",
                        im_dir.split("/")[-2] + ".txt")
                # Use custom proposals if provided
                if "proposals" in args.opts:
                    proposals = pickle.load(open(os.path.join('/', *(im_dir.split("/")[:-1] + \
                        ["det/proposals.pkl"])), 'r'))
                else:
                    proposals = None
                head, tail = os.path.split(output_file) 
                if not os.path.exists(head):
                    os.makedirs(head)
                start = time.time()
                # Run inference
                infer_track_sequence(model, im_dir, tracking, proposals=proposals,
                    vis=vis, det_file=output_file)
                delta = time.time() - start
                freq = float(len(os.listdir(im_dir))) / delta
                timing.append(freq)

            # Save evaluation results
            if EVAL:
                val_directory = os.path.abspath(head) + "/"
                eval_datections(val_directory,
                    os.path.abspath(os.path.join(*im_dir.split("/")[:-2])) + "/")
                with open(val_directory + "eval.txt", "r") as f:
                    temp = f.readline().strip()
                with open(val_directory + "eval.txt", "w+") as f:
                    f.write("{},{}".format(temp, np.average(timing)))
    for scoped_name, blob in restored_all_params.items():
        unscoped_name = c2_utils.UnscopeName(scoped_name)
        np.testing.assert_array_equal(blob, orig_gpu_0_params[unscoped_name])


if __name__ == '__main__':
    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    logger = setup_logging(__name__)
    logger.setLevel(logging.DEBUG)
    logging.getLogger('detectron.roi_data.loader').setLevel(logging.INFO)
    np.random.seed(cfg.RNG_SEED)
    output_dir = tempfile.mkdtemp()
    # Generate config for test
    cfg.MODEL.TYPE = 'generalized_rcnn'
    cfg.MODEL.CONV_BODY = 'FPN.add_fpn_ResNet50_conv5_body'
    cfg.MODEL.NUM_CLASSES = 81
    cfg.MODEL.FASTER_RCNN = True
    cfg.FPN.FPN_ON = True
    cfg.FPN.MULTILEVEL_ROIS = True
    cfg.FPN.MULTILEVEL_RPN = True
    cfg.FAST_RCNN.ROI_BOX_HEAD = 'fast_rcnn_heads.add_roi_2mlp_head'
    cfg.FAST_RCNN.ROI_XFORM_METHOD = 'RoIAlign'
    cfg.OUTPUT_DIR = output_dir
    cfg.TRAIN.DATASETS = ('coco_2014_minival',)
    cfg.TRAIN.WEIGHTS = b''
    for num_gpu in range(workspace.NumCudaDevices()):
        cfg.immutable(False)
        cfg.NUM_GPUS = num_gpu + 1
        assert_and_infer_cfg()
        test_restore_checkpoint()