Exemplo n.º 1
0
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)
Exemplo n.º 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)
Exemplo n.º 3
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))
Exemplo n.º 4
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))
Exemplo n.º 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)

        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)