def main():
    global args
    args = parse_args()

    cfg = mmcv.Config.fromfile(args.config)
    # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.data.test.test_mode = True
    dataset = obj_from_dict(cfg.data.test, datasets, dict(test_mode=True))

    if args.out is None or not args.out.endswith(('.pkl', '.pickle')):
        raise ValueError('The output file must be a pkl file.')

    if osp.exists(args.out):
        outputs = mmcv.load(args.out)
    else:
        if args.launcher == 'none':
            raise NotImplementedError(
                "By default, we use distributed testing, so that launcher should be pytorch"
            )
        else:
            distributed = True
            init_dist(args.launcher, **cfg.dist_params)

        model = build_detector(cfg.model,
                               train_cfg=None,
                               test_cfg=cfg.test_cfg)
        data_loader = build_dataloader(dataset,
                                       imgs_per_gpu=1,
                                       workers_per_gpu=1,
                                       dist=distributed,
                                       shuffle=False)

        load_checkpoint(model, args.checkpoint, map_location='cpu')
        find_unused_parameters = cfg.get('find_unused_parameters', False)
        model = MMDistributedDataParallel(
            model.cuda(),
            device_ids=[torch.cuda.current_device()],
            broadcast_buffers=False,
            find_unused_parameters=find_unused_parameters)

        outputs = multiple_test(model, data_loader)

        rank, _ = get_dist_info()
        if rank == 0:
            print('writing results to {}'.format(args.out))
            mmcv.dump(outputs, args.out)

    eval_type = args.eval
    if eval_type:
        print('Starting evaluate {}'.format(eval_type))

        result_file = osp.join(args.out + '.csv')
        results2csv(dataset, outputs, result_file)

        ava_eval(result_file, eval_type, args.label_file, args.ann_file,
                 args.exclude_file)
示例#2
0
def _dist_train(model, dataset, cfg, logger, validate=False):
    # prepare data loaders
    data_loaders = [
        build_dataloader(dataset,
                         cfg.data.videos_per_gpu,
                         cfg.data.workers_per_gpu,
                         dist=True)
    ]
    # put model on gpus
    find_unused_parameters = cfg.get('find_unused_parameters', False)

    # Start: vj changes
    find_unused_parameters = True
    # End:   vj chagnes

    model = MMDistributedDataParallel(
        model.cuda(),
        device_ids=[torch.cuda.current_device()],
        broadcast_buffers=False,
        find_unused_parameters=find_unused_parameters)

    # build runner
    optimizer = build_optimizer(model, cfg.optimizer)
    runner = EpochBasedRunner(model, batch_processor, optimizer, cfg.work_dir,
                              logger)
    # register hooks
    optimizer_config = DistOptimizerHook(**cfg.optimizer_config)
    runner.register_training_hooks(cfg.lr_config, optimizer_config,
                                   cfg.checkpoint_config, cfg.log_config)
    runner.register_hook(DistSamplerSeedHook())
    # register eval hooks
    if validate:
        if cfg.data.val.type in ['RawFramesDataset', 'VideoDataset']:
            runner.register_hook(
                DistEvalTopKAccuracyHook(cfg.data.val, k=(1, 5)))
        if cfg.data.val.type == 'AVADataset':
            runner.register_hook(AVADistEvalmAPHook(cfg.data.val))

    if cfg.resume_from:
        runner.resume(cfg.resume_from)
    elif cfg.load_from:
        runner.load_checkpoint(cfg.load_from)
    runner.run(data_loaders, cfg.workflow, cfg.total_epochs)
def main():

    # get local rank from distributed launcher
    parser = argparse.ArgumentParser()
    parser.add_argument("--local_rank", type=int)
    args = parser.parse_args()
    print('what is the rank of the current program: ')
    print(args.local_rank)

    # initialize dist
    if mp.get_start_method(allow_none=True) is None:
        mp.set_start_method('spawn')
    rank = int(args.local_rank)
    torch.cuda.set_device(rank)
    dist.init_process_group(backend='nccl', init_method='env://')

    # define dataset
    dataset = DentalClassDataset(
        ann_file=ann_file,
        img_prefix=img_prefix,
        img_scale=img_scale,
        img_norm_cfg=img_transform_cfg,
        multiscale_mode='value',   # select a scale, rather than random from a range.
        flip_ratio=flip_ratio,
        with_label=False,
        extra_aug=None,
        test_mode=True,
    )

    # sampler for make number of samples % number of gpu == 0
    rank, world_size = get_dist_info()
    sampler = NewDistributedSampler(
        dataset=dataset,
        num_replicas=world_size,
        images_per_gpu=imgs_per_gpu,
        rank=rank,
        shuffle=False
    )

    # data loader. Note this is the code for one (each) gpu.
    batch_size = imgs_per_gpu
    num_workers = workers_per_gpu
    data_loader = DataLoader(
        dataset=dataset,
        batch_size=batch_size,
        # when sampler is given, shuffle must be False.
        shuffle=False,
        sampler=sampler,
        batch_sampler=None,
        num_workers=num_workers,
        collate_fn=partial(collate, samples_per_gpu=imgs_per_gpu),
        pin_memory=False,
        drop_last=False,
        timeout=0,
        worker_init_fn=None,
    )

    # define the model and restore checkpoint
    model = VGGClassifier(
        with_bn=False,
        num_classes=len(dataset.CLASSES),
        num_stages=5,
        dilations=(1, 1, 1, 1, 1),
        out_indices=(30,),
        frozen_stages=-1,
        bn_eval=True,
        bn_frozen=False,
        ceil_mode=True,
        with_last_pool=True,
        dimension_before_fc=(10, 15),
        dropout_rate=0.5,
        pos_loss_weights=torch.tensor((15, 8), dtype=torch.float32, device=torch.device('cuda', rank)),
    )

    checkpoint = load_checkpoint(
        model=model,
        filename=checkpoint_file,
        map_location='cpu',
        strict=False,
        logger=None
    )

    # define classes
    model.CLASSES = checkpoint['meta']['CLASSES']

    # parallelize model
    model = model.cuda()
    model = MMDistributedDataParallel(
        module=model,
        dim=0,
        broadcast_buffers=True,
        bucket_cap_mb=25
    )
    model.eval()

    # results and progress bar
    results = []
    dataset = data_loader.dataset

    if rank == 0:
        prog_bar = mmcv.ProgressBar(len(dataset))

    # enumerate all data
    for i, data in enumerate(data_loader):
        with torch.no_grad():
            result = model(is_test=True, rescale=True, **data)
        results.extend(result)

        # update program bar only if it is rank 0.
        if rank == 0:
            batch_size = data['img'].size(0)
            for _ in range(batch_size * world_size):
                prog_bar.update()

    # collect results from all gpus
    results = collect_results(
        result_part=results,
        dataset_real_size=len(dataset),
        tmpdir=tmpdir
    )

    # write results to file
    # [Number of images, Number of classes, (k, 5)].
    # 5 for t, l, b, r, and prob.
    if rank == 0:
        print('\nwriting results to {}'.format(out_file))
        mmcv.dump(results, out_file+'.pickle')
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--local_rank', type=int)
    args = parser.parse_args()
    print('what is the rank of the current program: ')
    print(args.local_rank)

    # initialize dist
    if mp.get_start_method(allow_none=True) is None:
        mp.set_start_method('spawn')
    rank = int(args.local_rank)
    torch.cuda.set_device(rank)
    dist.init_process_group(backend='nccl', init_method='env://')

    # init logger before other steps
    logger = logging.getLogger()
    if not logger.hasHandlers():
        logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s',
                            level=log_level)
    if args.local_rank != 0:
        logger.setLevel('ERROR')
    logger.info('Starting Distributed training')

    # set random seeds
    if seed is not None:
        logger.info('Set random seed to {}'.format(seed))
        random.seed(seed)
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)

    # build dataset
    dental_dataset = DentalDataset(
        ann_file=ann_file,
        img_prefix=img_prefix,
        img_scale=img_scale,
        img_norm_cfg=img_norm_cfg,
        multiscale_mode=
        'value',  # select a scale, rather than random from a range.
        flip_ratio=flip_ratio,
        with_label=True,
        extra_aug=extra_aug,
        test_mode=False,
    )

    # build model
    model = SSDDetector(
        pretrained=None,
        # basic
        input_size=img_scale,
        num_classes=4,
        in_channels=(512, 1024, 512, 256, 256, 256),
        # anchor generate
        anchor_ratios=([2], [2, 3], [2, 3], [2, 3], [2], [2]),
        anchor_strides=((8, 8), (16, 16), (32, 32), (60, 64), (80, 106),
                        (120, 320)),
        basesize_ratios=(0.02, 0.05, 0.08, 0.12, 0.15, 0.18),
        allowed_border=-1,
        # regression
        target_means=(.0, .0, .0, .0),
        target_stds=(0.1, 0.1, 0.2, 0.2),
        # box assign
        pos_iou_thr=0.5,
        neg_iou_thr=0.5,
        min_pos_iou=0.,
        gt_max_assign_all=False,
        # sampling
        sampling=False,
        # balancing the loss
        neg_pos_ratio=3,
        # loss
        smoothl1_beta=1.,
        # inference nms
        nms_pre=-1,
        score_thr=0.02,
        nms_cfg=['nms', 0.45, None],
        max_per_img=200,
    )
    model.CLASSES = dental_dataset.CLASSES

    # save class names in
    # checkpoints as meta data
    checkpoint_config['meta'] = dict(CLASSES=dental_dataset.CLASSES)

    # build sampler for shuffling, padding, and mixing.
    _, world_size = get_dist_info()
    sampler = DistributedNonGroupSampler(
        dataset=dental_dataset,
        samples_per_gpu=imgs_per_gpu,
        num_replicas=world_size,
        rank=args.local_rank,
        shuffle=True,
    )

    # build data loader.
    data_loader = DataLoader(
        dataset=dental_dataset,
        batch_size=imgs_per_gpu,
        # shuffle should be False when sampler is given.
        shuffle=False,
        sampler=sampler,
        batch_sampler=None,
        num_workers=workers_per_gpu,
        collate_fn=partial(collate, samples_per_gpu=imgs_per_gpu),
        pin_memory=False,
        drop_last=False,
        timeout=0,
        worker_init_fn=None,
    )

    # put model on gpus
    # AdaptedDistributedDataParallel(
    #   (module): SSDDetector(
    #     (backbone): SSDVGG(
    #       (features): Sequential(
    #         (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (1): ReLU(inplace=True)
    #         (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (3): ReLU(inplace=True)
    #         (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=True)
    #         (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (6): ReLU(inplace=True)
    #         (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (8): ReLU(inplace=True)
    #         (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=True)
    #         (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (11): ReLU(inplace=True)
    #         (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (13): ReLU(inplace=True)
    #         (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (15): ReLU(inplace=True)
    #         (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=True)
    #         (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (18): ReLU(inplace=True)
    #         (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (20): ReLU(inplace=True)
    #         (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (22): ReLU(inplace=True)
    #         (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=True)
    #         (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (25): ReLU(inplace=True)
    #         (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (27): ReLU(inplace=True)
    #         (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (29): ReLU(inplace=True)
    #         (30): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)
    #         (31): Conv2d(512, 1024, kernel_size=(3, 3), stride=(1, 1), padding=(6, 6), dilation=(6, 6))
    #         (32): ReLU(inplace=True)
    #         (33): Conv2d(1024, 1024, kernel_size=(1, 1), stride=(1, 1))
    #         (34): ReLU(inplace=True)
    #       )
    #       (extra): Sequential(
    #         (0): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1))
    #         (1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
    #         (2): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1))
    #         (3): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
    #         (4): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1))
    #         (5): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1))
    #         (6): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1))
    #         (7): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1))
    #       )
    #       (l2_norm): L2Norm()
    #     )
    #     (bbox_head): SSDHead(
    #       (reg_convs): ModuleList(
    #         (0): Conv2d(512, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (1): Conv2d(1024, 24, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (2): Conv2d(512, 24, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (3): Conv2d(256, 24, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (4): Conv2d(256, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (5): Conv2d(256, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #       )
    #       (cls_convs): ModuleList(
    #         (0): Conv2d(512, 324, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (1): Conv2d(1024, 486, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (2): Conv2d(512, 486, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (3): Conv2d(256, 486, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (4): Conv2d(256, 324, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #         (5): Conv2d(256, 324, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    #       )
    #     )
    #   )
    # )

    model = MMDistributedDataParallel(model.cuda())

    # checkpoint
    # {
    #   meta:
    #   {
    #       mmdet_version:
    #       config: []
    #   }
    #   state_dict:
    #       OrderedDict: [
    #           backbone.features.0.weight: tensor([]),
    #           ...
    #       ]
    # }

    # build optimizer
    if hasattr(model, 'module'):
        pure_model = model.module
    else:
        pure_model = model
    for name, param in pure_model.named_parameters():
        if param.requires_grad:
            print(name, param.shape, param.requires_grad)

    if train_head_only:
        train_params = []
        for name, param in pure_model.named_parameters():
            if 'bbox_head' not in name:
                param.requires_grad = False
            else:
                train_params.append(param)

        optimizer = torch.optim.SGD(params=train_params,
                                    lr=lr,
                                    momentum=momentum,
                                    dampening=0,
                                    weight_decay=weight_decay,
                                    nesterov=False)

    else:
        optimizer = torch.optim.SGD(params=pure_model.parameters(),
                                    lr=lr,
                                    momentum=momentum,
                                    dampening=0,
                                    weight_decay=weight_decay,
                                    nesterov=False)

    # build runner: a training helper.
    #   model (:obj:`torch.nn.Module`): The model to be run.
    #   batch_processor (callable): A callable method that process a data
    #       batch. The interface of this method should be
    #       `batch_processor(model, data, train_mode) -> dict`
    #   optimizer (dict or :obj:`torch.optim.Optimizer`).
    #   work_dir (str, optional): The working directory to save checkpoints
    #       and logs.
    #   log_level (int): Logging level.
    #   logger (:obj:`logging.Logger`): Custom logger. If `None`, use the
    #       default logger.
    runner = Runner(
        model=model,
        batch_processor=batch_processor,
        optimizer=optimizer,
        work_dir=work_dir,
        log_level=logging.INFO,
        logger=None,
    )

    # register hooks: optimization after the forward
    optimizer_config = DistOptimizerHook(
        grad_clip=grad_clip,
        coalesce=True,
        bucket_size_mb=-1,
    )
    # register hooks: along with training
    runner.register_training_hooks(lr_config=lr_config,
                                   optimizer_config=optimizer_config,
                                   checkpoint_config=checkpoint_config,
                                   log_config=log_config)
    # register hooks: set sampler seed before each epoch
    runner.register_hook(DistSamplerSeedHook())

    # resume from: epoch and iter to be continued.
    # load from: start as 0.
    if resume_from is not None:
        runner.resume(resume_from)
    elif load_from is not None:
        runner.load_checkpoint(load_from)

    # data_loaders (list[:obj:`DataLoader`]): Dataloaders for training
    #   and validation.
    # workflow (list[tuple]): A list of (phase, epochs) to specify the
    #   running order and epochs. E.g, [('train', 2), ('val', 1)] means
    #   running 2 epochs for training and 1 epoch for validation,
    #   iteratively.
    # max_epochs (int): Total training epochs.
    runner.run(data_loaders=[data_loader],
               workflow=workflow,
               max_epochs=total_epochs)
示例#5
0
def main():
    global args
    args = parse_args()

    if args.out is not None and not args.out.endswith(('.pkl', '.pickle')):
        raise ValueError('The output file must be a pkl file.')

    cfg = mmcv.Config.fromfile(args.config)
    # must use fcn testing
    cfg.model.update({'fcn_testing': True})
    cfg.model['cls_head'].update({'fcn_testing': True})

    # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.data.test.test_mode = True

    if args.testfile != '':
        cfg.data.test.ann_file = args.testfile

    dataset = obj_from_dict(cfg.data.test, datasets, dict(test_mode=True))

    if args.launcher == 'none':
        distributed = False
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)

    assert distributed, "We only support distributed testing"

    model = build_recognizer(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    data_loader = build_dataloader(
        dataset,
        imgs_per_gpu=1,
        workers_per_gpu=1,
        dist=distributed,
        shuffle=False)

    load_checkpoint(model, args.checkpoint, map_location='cpu')

    find_unused_parameters = cfg.get('find_unused_parameters', False)
    model = MMDistributedDataParallel(
        model.cuda(),
        device_ids=[torch.cuda.current_device()],
        broadcast_buffers=False,
        find_unused_parameters=find_unused_parameters)

    outputs = multi_test(model, data_loader)

    rank, _ = get_dist_info()
    if args.out and rank == 0:
        print('writing results to {}'.format(args.out))
        mmcv.dump(outputs, args.out)

        gt_labels = []
        for i in range(len(dataset)):
            ann = dataset.get_ann_info(i)
            gt_labels.append(ann['label'])

        if args.use_softmax:
            print("Averaging score over {} clips with softmax".format(outputs[0].shape[0]))
            results = [softmax(res, dim=1).mean(axis=0) for res in outputs]
        else:
            print("Averaging score over {} clips without softmax (ie, raw)".format(outputs[0].shape[0]))
            results = [res.mean(axis=0) for res in outputs]
        top1, top5 = top_k_accuracy(results, gt_labels, k=(1, 5))
        mean_acc = mean_class_accuracy(results, gt_labels)
        print("Mean Class Accuracy = {:.02f}".format(mean_acc * 100))
        print("Top-1 Accuracy = {:.02f}".format(top1 * 100))
        print("Top-5 Accuracy = {:.02f}".format(top5 * 100))
示例#6
0
def main():

    # get local rank from distributed launcher
    parser = argparse.ArgumentParser()
    parser.add_argument("--local_rank", type=int)
    args = parser.parse_args()
    print('what is the rank of the current program: ')
    print(args.local_rank)

    # initialize dist
    if mp.get_start_method(allow_none=True) is None:
        mp.set_start_method('spawn')
    rank = int(args.local_rank)
    torch.cuda.set_device(rank)
    dist.init_process_group(backend='nccl', init_method='env://')

    # define dataset
    dataset = CocoDataset(
        ann_file=ann_file,
        img_prefix=img_prefix,
        img_scale=img_scale,
        img_norm_cfg=img_transform_cfg,
        multiscale_mode='value',
        flip_ratio=flip_ratio,
        with_ignore=False,
        with_label=False,
        extra_aug=None,
        test_mode=True,
    )

    # sampler for make number of samples % number of gpu == 0
    rank, world_size = get_dist_info()
    sampler = NewDistributedSampler(dataset=dataset,
                                    num_replicas=world_size,
                                    images_per_gpu=imgs_per_gpu,
                                    rank=rank,
                                    shuffle=False)

    # data loader. Note this is the code for one (each) gpu.
    batch_size = imgs_per_gpu
    num_workers = workers_per_gpu
    data_loader = DataLoader(
        dataset=dataset,
        batch_size=batch_size,
        # when sampler is given, shuffle must be False.
        shuffle=False,
        sampler=sampler,
        batch_sampler=None,
        num_workers=num_workers,
        collate_fn=partial(collate, samples_per_gpu=imgs_per_gpu),
        pin_memory=False,
        drop_last=False,
        timeout=0,
        worker_init_fn=None,
    )

    # define the model and restore checkpoint
    model = SSDDetector(pretrained=None)
    checkpoint = load_checkpoint(model=model,
                                 filename=checkpoint_file,
                                 map_location='cpu',
                                 strict=False,
                                 logger=None)

    # define classes
    if 'CLASSES' in checkpoint['meta']:
        model.CLASSES = checkpoint['meta']['CLASSES']
    else:
        model.CLASSES = dataset.CLASSES

    # parallelize model
    model = model.cuda()
    model = MMDistributedDataParallel(module=model,
                                      dim=0,
                                      broadcast_buffers=True,
                                      bucket_cap_mb=25)
    model.eval()

    # results and progress bar
    results = []
    dataset = data_loader.dataset

    if rank == 0:
        prog_bar = mmcv.ProgressBar(len(dataset))

    # enumerate all data
    for i, data in enumerate(data_loader):

        print(data['img_meta'])

        with torch.no_grad():
            result = model(is_test=True, rescale=True, **data)
        results.extend(result)

        # update program bar only if it is rank 0.
        if rank == 0:
            batch_size = data['img'].size(0)
            for _ in range(batch_size * world_size):
                prog_bar.update()

    # collect results from all gpus
    results = collect_results(result_part=results,
                              dataset_real_size=len(dataset),
                              tmpdir=tmpdir)

    # write results to file
    # [Number of images, Number of classes, (k, 5)].
    # 5 for t, l, b, r, and prob.
    if rank == 0:
        print('\nwriting results to {}'.format(out_file))
        mmcv.dump(results, out_file + '.pickle')

        if not isinstance(results[0], dict):
            # result file that of coco format. save it to result.bbox.json and result.proposal.json.
            # jason format:
            #   [Number_of_bboxes,
            #       dict(
            #           image_id:
            #           bbox: [x, y, w, h]
            #           score:
            #           category_id:
            #       )]
            results2json(dataset, results, out_file)

            #  Average Precision  (AP) @[ IoU=0.50      | maxDets=  1 ] = 0.310
            #  Average Precision  (AP) @[ IoU=0.50      | maxDets= 10 ] = 0.431
            #  Average Precision  (AP) @[ IoU=0.50      | maxDets=100 ] = 0.443
            #  Average Recall     (AR) @[ IoU=0.50      | maxDets=  1 ] = 0.368
            #  Average Recall     (AR) @[ IoU=0.50      | maxDets= 10 ] = 0.582
            #  Average Recall     (AR) @[ IoU=0.50      | maxDets=100 ] = 0.637
            # coco_eval(
            #     result_file=out_file+'.bbox.json',
            #     result_type='bbox',
            #     coco=dataset.coco,
            #     iou_thrs=[0.5],
            #     max_dets=[1, 10, 100]
            # )

            #  Average Precision  (AP) @[ IoU=0.50      | maxDets=  1 ] = 0.310
            #  Average Precision  (AP) @[ IoU=0.50      | maxDets= 10 ] = 0.431
            #  Average Precision  (AP) @[ IoU=0.50      | maxDets=100 ] = 0.443
            #  Average Precision  (AP) @[ IoU=0.55      | maxDets=  1 ] = 0.298
            #  Average Precision  (AP) @[ IoU=0.55      | maxDets= 10 ] = 0.411
            #  Average Precision  (AP) @[ IoU=0.55      | maxDets=100 ] = 0.421
            #  Average Precision  (AP) @[ IoU=0.60      | maxDets=  1 ] = 0.281
            #  Average Precision  (AP) @[ IoU=0.60      | maxDets= 10 ] = 0.382
            #  Average Precision  (AP) @[ IoU=0.60      | maxDets=100 ] = 0.390
            #  Average Precision  (AP) @[ IoU=0.65      | maxDets=  1 ] = 0.263
            #  Average Precision  (AP) @[ IoU=0.65      | maxDets= 10 ] = 0.350
            #  Average Precision  (AP) @[ IoU=0.65      | maxDets=100 ] = 0.355
            #  Average Precision  (AP) @[ IoU=0.70      | maxDets=  1 ] = 0.238
            #  Average Precision  (AP) @[ IoU=0.70      | maxDets= 10 ] = 0.308
            #  Average Precision  (AP) @[ IoU=0.70      | maxDets=100 ] = 0.312
            #  Average Precision  (AP) @[ IoU=0.75      | maxDets=  1 ] = 0.206
            #  Average Precision  (AP) @[ IoU=0.75      | maxDets= 10 ] = 0.260
            #  Average Precision  (AP) @[ IoU=0.75      | maxDets=100 ] = 0.262
            #  Average Precision  (AP) @[ IoU=0.80      | maxDets=  1 ] = 0.165
            #  Average Precision  (AP) @[ IoU=0.80      | maxDets= 10 ] = 0.201
            #  Average Precision  (AP) @[ IoU=0.80      | maxDets=100 ] = 0.202
            #  Average Precision  (AP) @[ IoU=0.85      | maxDets=  1 ] = 0.111
            #  Average Precision  (AP) @[ IoU=0.85      | maxDets= 10 ] = 0.130
            #  Average Precision  (AP) @[ IoU=0.85      | maxDets=100 ] = 0.130
            #  Average Precision  (AP) @[ IoU=0.90      | maxDets=  1 ] = 0.048
            #  Average Precision  (AP) @[ IoU=0.90      | maxDets= 10 ] = 0.053
            #  Average Precision  (AP) @[ IoU=0.90      | maxDets=100 ] = 0.053
            #  Average Precision  (AP) @[ IoU=0.95      | maxDets=  1 ] = 0.005
            #  Average Precision  (AP) @[ IoU=0.95      | maxDets= 10 ] = 0.005
            #  Average Precision  (AP) @[ IoU=0.95      | maxDets=100 ] = 0.005
            #  Average Recall     (AR) @[ IoU=0.50      | maxDets=  1 ] = 0.368
            #  Average Recall     (AR) @[ IoU=0.50      | maxDets= 10 ] = 0.582
            #  Average Recall     (AR) @[ IoU=0.50      | maxDets=100 ] = 0.637
            #  Average Recall     (AR) @[ IoU=0.55      | maxDets=  1 ] = 0.354
            #  Average Recall     (AR) @[ IoU=0.55      | maxDets= 10 ] = 0.557
            #  Average Recall     (AR) @[ IoU=0.55      | maxDets=100 ] = 0.606
            #  Average Recall     (AR) @[ IoU=0.60      | maxDets=  1 ] = 0.338
            #  Average Recall     (AR) @[ IoU=0.60      | maxDets= 10 ] = 0.523
            #  Average Recall     (AR) @[ IoU=0.60      | maxDets=100 ] = 0.565
            #  Average Recall     (AR) @[ IoU=0.65      | maxDets=  1 ] = 0.319
            #  Average Recall     (AR) @[ IoU=0.65      | maxDets= 10 ] = 0.478
            #  Average Recall     (AR) @[ IoU=0.65      | maxDets=100 ] = 0.509
            #  Average Recall     (AR) @[ IoU=0.70      | maxDets=  1 ] = 0.291
            #  Average Recall     (AR) @[ IoU=0.70      | maxDets= 10 ] = 0.421
            #  Average Recall     (AR) @[ IoU=0.70      | maxDets=100 ] = 0.443
            #  Average Recall     (AR) @[ IoU=0.75      | maxDets=  1 ] = 0.258
            #  Average Recall     (AR) @[ IoU=0.75      | maxDets= 10 ] = 0.360
            #  Average Recall     (AR) @[ IoU=0.75      | maxDets=100 ] = 0.373
            #  Average Recall     (AR) @[ IoU=0.80      | maxDets=  1 ] = 0.215
            #  Average Recall     (AR) @[ IoU=0.80      | maxDets= 10 ] = 0.287
            #  Average Recall     (AR) @[ IoU=0.80      | maxDets=100 ] = 0.295
            #  Average Recall     (AR) @[ IoU=0.85      | maxDets=  1 ] = 0.158
            #  Average Recall     (AR) @[ IoU=0.85      | maxDets= 10 ] = 0.203
            #  Average Recall     (AR) @[ IoU=0.85      | maxDets=100 ] = 0.207
            #  Average Recall     (AR) @[ IoU=0.90      | maxDets=  1 ] = 0.084
            #  Average Recall     (AR) @[ IoU=0.90      | maxDets= 10 ] = 0.101
            #  Average Recall     (AR) @[ IoU=0.90      | maxDets=100 ] = 0.103
            #  Average Recall     (AR) @[ IoU=0.95      | maxDets=  1 ] = 0.014
            #  Average Recall     (AR) @[ IoU=0.95      | maxDets= 10 ] = 0.017
            #  Average Recall     (AR) @[ IoU=0.95      | maxDets=100 ] = 0.017
            coco_eval(result_file=out_file + '.bbox.json',
                      result_type='bbox',
                      coco=ann_file,
                      iou_thrs=[
                          0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95
                      ],
                      max_dets=[1, 10, 100])
示例#7
0
def main():

    # get local rank from distributed launcher
    parser = argparse.ArgumentParser()
    parser.add_argument("--local_rank", type=int)
    args = parser.parse_args()
    print('what is the rank of the current program: ')
    print(args.local_rank)

    # initialize dist
    if mp.get_start_method(allow_none=True) is None:
        mp.set_start_method('spawn')
    rank = int(args.local_rank)
    torch.cuda.set_device(rank)
    dist.init_process_group(backend='nccl', init_method='env://')

    # define dataset
    dataset = DentalDataset(
        ann_file=ann_file,
        img_prefix=img_prefix,
        img_scale=img_scale,
        img_norm_cfg=img_transform_cfg,
        multiscale_mode='value',
        flip_ratio=flip_ratio,
        with_label=False,
        extra_aug=None,
        test_mode=True,
    )

    # sampler for make number of samples % number of gpu == 0
    rank, world_size = get_dist_info()
    sampler = NewDistributedSampler(
        dataset=dataset,
        num_replicas=world_size,
        images_per_gpu=imgs_per_gpu,
        rank=rank,
        shuffle=False
    )

    # data loader. Note this is the code for one (each) gpu.
    batch_size = imgs_per_gpu
    num_workers = workers_per_gpu
    data_loader = DataLoader(
        dataset=dataset,
        batch_size=batch_size,
        # when sampler is given, shuffle must be False.
        shuffle=False,
        sampler=sampler,
        batch_sampler=None,
        num_workers=num_workers,
        collate_fn=partial(collate, samples_per_gpu=imgs_per_gpu),
        pin_memory=False,
        drop_last=False,
        timeout=0,
        worker_init_fn=None,
    )

    # define the model and restore checkpoint
    model = SSDDetector(
        pretrained=None,
        # basic
        input_size=(480, 320),
        num_classes=4,
        in_channels=(512, 1024, 512, 256, 256, 256),
        # anchor generate
        anchor_ratios=([2], [2, 3], [2, 3], [2, 3], [2], [2]),
        anchor_strides=((8, 8), (16, 16), (32, 32), (60, 64), (80, 106), (120, 320)),
        basesize_ratios=(0.02, 0.05, 0.08, 0.12, 0.15, 0.18),
        allowed_border=-1,
        # regression
        target_means=(.0, .0, .0, .0),
        target_stds=(0.1, 0.1, 0.2, 0.2),
        # box assign
        pos_iou_thr=0.5,
        neg_iou_thr=0.5,
        min_pos_iou=0.,
        gt_max_assign_all=False,
        # sampling
        sampling=False,
        # balancing the loss
        neg_pos_ratio=3,
        # loss
        smoothl1_beta=1.,
        # inference nms
        nms_pre=-1,
        score_thr=0.02,
        nms_cfg=['nms', 0.45, None],
        max_per_img=200,
    )
    checkpoint = load_checkpoint(
        model=model,
        filename=checkpoint_file,
        map_location='cpu',
        strict=False,
        logger=None
    )

    # define classes
    model.CLASSES = checkpoint['meta']['CLASSES']

    # parallelize model
    model = model.cuda()
    model = MMDistributedDataParallel(
        module=model,
        dim=0,
        broadcast_buffers=True,
        bucket_cap_mb=25
    )
    model.eval()

    # results and progress bar
    results = []
    dataset = data_loader.dataset

    if rank == 0:
        prog_bar = mmcv.ProgressBar(len(dataset))

    # enumerate all data
    for i, data in enumerate(data_loader):
        with torch.no_grad():
            result = model(is_test=True, rescale=True, **data)
        results.extend(result)

        # update program bar only if it is rank 0.
        if rank == 0:
            batch_size = data['img'].size(0)
            for _ in range(batch_size * world_size):
                prog_bar.update()

    # collect results from all gpus
    results = collect_results(
        result_part=results,
        dataset_real_size=len(dataset),
        tmpdir=tmpdir
    )

    # write results to file
    # [Number of images, Number of classes, (k, 5)].
    # 5 for t, l, b, r, and prob.
    if rank == 0:
        print('\nwriting results to {}'.format(out_file))
        mmcv.dump(results, out_file+'.pickle')
示例#8
0
def main():
    global args
    args = parse_args()

    if args.out is not None and not args.out.endswith(('.pkl', '.pickle')):
        raise ValueError('The output file must be a pkl file.')
    cfg = mmcv.Config.fromfile(args.config)
    if args.checkpoint == None:
        args.checkpoint = os.path.join(cfg.work_dir, 'latest.pth')
    else:
        args.checkpoint = os.path.join(cfg.work_dir,
                                       'epoch_%d.pth' % (int(args.checkpoint)))

    cfg = mmcv.Config.fromfile(args.config)
    # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.data.test.test_mode = True

    # pass arg of fcn testing
    if args.fcn_testing:
        cfg.model.update({'fcn_testing': True})
        cfg.model['cls_head'].update({'fcn_testing': True})

    # for regular testing
    if cfg.data.test.oversample == 'three_crop':
        cfg.model.spatial_temporal_module.spatial_size = 8

    dataset = obj_from_dict(cfg.data.test, datasets, dict(test_mode=True))

    if args.launcher == 'none':
        raise NotImplementedError(
            "By default, we use distributed testing, so that launcher should be pytorch"
        )
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)

    model = build_recognizer(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    data_loader = build_dataloader(dataset,
                                   imgs_per_gpu=1,
                                   workers_per_gpu=1,
                                   dist=distributed,
                                   shuffle=False)

    load_checkpoint(model, args.checkpoint, map_location='cpu')

    find_unused_parameters = cfg.get('find_unused_parameters', False)
    model = MMDistributedDataParallel(
        model.cuda(),
        device_ids=[torch.cuda.current_device()],
        broadcast_buffers=False,
        find_unused_parameters=find_unused_parameters)

    outputs = multi_test(model, data_loader, cfg.work_dir)

    rank, _ = get_dist_info()
    if args.out and rank == 0:
        print('writing results to {}'.format(args.out))
        mmcv.dump(outputs, args.out)

        data_path = []
        gt_labels = []
        pre = []
        for i in range(len(dataset)):
            ann = dataset.get_ann_info(i)
            gt_labels.append(ann['label'])
            data_path.append(ann['path'])
            pre.append(outputs[i].mean(axis=0))

        save_data = {
            path: [p, g]
            for path, p, g in zip(data_path, pre, gt_labels)
        }
        with open(os.path.join(cfg.work_dir, 'test.pkl'), 'wb') as f:
            pickle.dump(save_data, f)

        if args.use_softmax:
            print("Averaging score over {} clips with softmax".format(
                outputs[0].shape[0]))
            results = [softmax(res, dim=1).mean(axis=0) for res in outputs]
        else:

            print("Averaging score over {} clips without softmax (ie, raw)".
                  format(outputs[0].shape[0]))
            results = [res.mean(axis=0) for res in outputs]
        top1, top5 = top_k_accuracy(results, gt_labels, k=(1, 5))
        mean_acc = mean_class_accuracy(results, gt_labels)
        with open(os.path.join(cfg.work_dir, 'test_result.txt'), 'w') as f:
            f.writelines('model is :' + args.checkpoint + '\n')
            f.writelines("Mean Class Accuracy = {:.04f}".format(mean_acc *
                                                                100) + '\n')
            f.writelines("Top-1 Accuracy = {:.04f}".format(top1 * 100) + '\n')
            f.writelines("Top-5 Accuracy = {:.04f}".format(top5 * 100) + '\n')

        print("Mean Class Accuracy = {:.04f}".format(mean_acc * 100))
        print("Top-1 Accuracy = {:.04f}".format(top1 * 100))
        print("Top-5 Accuracy = {:.04f}".format(top5 * 100))
示例#9
0
def main():
    global args
    args = parse_args()

    # if args.out is not None and not args.out.endswith(('.pkl', '.pickle')):
    #     raise ValueError('The output file must be a pkl file.')

    cfg = mmcv.Config.fromfile(args.config)
    # must use fcn testing
    cfg.model.update({'fcn_testing': True})
    cfg.model['cls_head'].update({'fcn_testing': True})

    # set cudnn_benchmark
    if cfg.get('cudnn_benchmark', False):
        torch.backends.cudnn.benchmark = True
    cfg.data.test.test_mode = True

    if args.testfile != '':
        cfg.data.test.ann_file = args.testfile

    dataset = obj_from_dict(cfg.data.test, datasets, dict(test_mode=True))

    if args.launcher == 'none':
        distributed = False
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)

    assert distributed, "We only support distributed testing"

    model = build_recognizer(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    data_loader = build_dataloader(
        dataset,
        imgs_per_gpu=1,
        workers_per_gpu=1,
        dist=distributed,
        shuffle=False)

    load_checkpoint(model, args.checkpoint, map_location='cpu')

    find_unused_parameters = cfg.get('find_unused_parameters', False)
    model = MMDistributedDataParallel(
        model.cuda(),
        device_ids=[torch.cuda.current_device()],
        broadcast_buffers=False,
        find_unused_parameters=find_unused_parameters)

    outputs = multi_test(model, data_loader)

    rank, _ = get_dist_info()
    if args.out and rank == 0:
        print('writing results to {}'.format(args.out))
        # mmcv.dump(outputs, args.out)

        results_numpyformat = np.zeros((1,226))
        for i in range(len(outputs)):
            results_numpyformat = np.row_stack((results_numpyformat,outputs[i]))
        np.save("res",results_numpyformat[1:,:])

        # gt_labels = []
        image_name = []
        for i in range(len(dataset)):
            ann = dataset.get_ann_info(i)
            image_name.append(ann['path'])
            # gt_labels.append(ann['label'])
        print(image_name[0])
        if args.use_softmax:
            print("Averaging score over {} clips with softmax".format(outputs[0].shape[0]))
            results = [softmax(res, dim=1).mean(axis=0) for res in outputs]
        else:
            print("Averaging score over {} clips without softmax (ie, raw)".format(outputs[0].shape[0]))
            results = [res.mean(axis=0) for res in outputs]

        prediction = pd.read_csv('predictions.csv',header=None)
        if not os.path.exists(args.out):
            os.mkdir(args.out)
        pbar = tqdm(total=len(image_name))
        for i in range(len(results)):
            pred_class = np.argsort(results[i])[-1:].item()
            # index = prediction[(prediction[0]==image_name[i].replace("_depth",""))].index.item()
            index = prediction[(prediction[0]==image_name[i].replace("_color",""))].index.item()
            # print("{}:{}".format(index,pred_class))
            prediction.iloc[index,1] = pred_class
            pbar.update(1)
            # break
        prediction.to_csv(os.path.join(args.out,'predictions.csv'),header=False,index=False)