def main(): parser = argparse.ArgumentParser(description='Benchmark dataloading') parser.add_argument('config', help='train config file path') args = parser.parse_args() cfg = Config.fromfile(args.config) # init logger before other steps logger = get_root_logger() logger.info(f'Config: {cfg.text}') dataset = build_dataset(cfg.data.train) data_loaders = [ build_dataloader(ds, cfg.data.samples_per_gpu, cfg.data.workers_per_gpu, dist=False, drop_last=cfg.data.get('drop_last', False), seed=0) for ds in dataset ] # Start progress bar after first 5 batches prog_bar = mmcv.ProgressBar(len(dataset) - 5 * cfg.data.samples_per_gpu, start=False) for data_loader in data_loaders: for i, data in enumerate(data_loader): if i == 5: prog_bar.start() for _ in data['imgs']: if i < 5: continue prog_bar.update()
def main(): args = parse_args() cfg = Config.fromfile(args.config) if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) # init distributed env first, since logger depends on the dist info. distributed = False # build the dataloader dataset = build_dataset(cfg.data.test) loader_cfg = { **dict((k, cfg.data[k]) for k in ['workers_per_gpu'] if k in cfg.data), **dict( samples_per_gpu=1, drop_last=False, shuffle=False, dist=distributed), **cfg.data.get('test_dataloader', {}) } data_loader = build_dataloader(dataset, **loader_cfg) # build the model if args.backend == 'onnxruntime': model = ONNXRuntimeEditing(args.model, cfg=cfg, device_id=0) elif args.backend == 'tensorrt': model = TensorRTEditing(args.model, cfg=cfg, device_id=0) args.save_image = args.save_path is not None model = MMDataParallel(model, device_ids=[0]) outputs = single_gpu_test( model, data_loader, save_path=args.save_path, save_image=args.save_image) print() # print metrics stats = dataset.evaluate(outputs) for stat in stats: print('Eval-{}: {}'.format(stat, stats[stat])) # save result pickle if args.out: print('writing results to {}'.format(args.out)) mmcv.dump(outputs, args.out)
def test_build_dataloader(): dataset = ToyDataset() samples_per_gpu = 3 # dist=True, shuffle=True, 1GPU dataloader = build_dataloader(dataset, samples_per_gpu=samples_per_gpu, workers_per_gpu=2) assert dataloader.batch_size == samples_per_gpu assert len(dataloader) == int(math.ceil(len(dataset) / samples_per_gpu)) assert isinstance(dataloader.sampler, DistributedSampler) assert dataloader.sampler.shuffle # dist=True, shuffle=False, 1GPU dataloader = build_dataloader(dataset, samples_per_gpu=samples_per_gpu, workers_per_gpu=2, shuffle=False) assert dataloader.batch_size == samples_per_gpu assert len(dataloader) == int(math.ceil(len(dataset) / samples_per_gpu)) assert isinstance(dataloader.sampler, DistributedSampler) assert not dataloader.sampler.shuffle # dist=True, shuffle=True, 8GPU dataloader = build_dataloader(dataset, samples_per_gpu=samples_per_gpu, workers_per_gpu=2, num_gpus=8) assert dataloader.batch_size == samples_per_gpu assert len(dataloader) == int(math.ceil(len(dataset) / samples_per_gpu)) assert dataloader.num_workers == 2 # dist=False, shuffle=True, 1GPU dataloader = build_dataloader(dataset, samples_per_gpu=samples_per_gpu, workers_per_gpu=2, dist=False) assert dataloader.batch_size == samples_per_gpu assert len(dataloader) == int(math.ceil(len(dataset) / samples_per_gpu)) assert isinstance(dataloader.sampler, RandomSampler) assert dataloader.num_workers == 2 # dist=False, shuffle=False, 1GPU dataloader = build_dataloader(dataset, samples_per_gpu=3, workers_per_gpu=2, shuffle=False, dist=False) assert dataloader.batch_size == samples_per_gpu assert len(dataloader) == int(math.ceil(len(dataset) / samples_per_gpu)) assert isinstance(dataloader.sampler, SequentialSampler) assert dataloader.num_workers == 2 # dist=False, shuffle=True, 8GPU dataloader = build_dataloader(dataset, samples_per_gpu=3, workers_per_gpu=2, num_gpus=8, dist=False) assert dataloader.batch_size == samples_per_gpu * 8 assert len(dataloader) == int(math.ceil( len(dataset) / samples_per_gpu / 8)) assert isinstance(dataloader.sampler, RandomSampler) assert dataloader.num_workers == 16
def main(): args = parse_args() cfg = mmcv.Config.fromfile(args.config) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.model.pretrained = None # init distributed env first, since logger depends on the dist info. if args.launcher == 'none': distributed = False else: distributed = True init_dist(args.launcher, **cfg.dist_params) rank, _ = get_dist_info() # set random seeds if args.seed is not None: if rank == 0: print('set random seed to', args.seed) set_random_seed(args.seed, deterministic=args.deterministic) # build the dataloader # TODO: support multiple images per gpu (only minor changes are needed) dataset = build_dataset(cfg.data.test) loader_cfg = { **dict((k, cfg.data[k]) for k in ['workers_per_gpu'] if k in cfg.data), **dict(samples_per_gpu=1, drop_last=False, shuffle=False, dist=distributed), **cfg.data.get('test_dataloader', {}) } data_loader = build_dataloader(dataset, **loader_cfg) # build the model and load checkpoint model = build_model(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg) args.save_image = args.save_path is not None empty_cache = cfg.get('empty_cache', False) if not distributed: _ = load_checkpoint(model, args.checkpoint, map_location='cpu') model = MMDataParallel(model, device_ids=[0]) outputs = single_gpu_test(model, data_loader, save_path=args.save_path, save_image=args.save_image) else: find_unused_parameters = cfg.get('find_unused_parameters', False) model = DistributedDataParallelWrapper( model, device_ids=[torch.cuda.current_device()], broadcast_buffers=False, find_unused_parameters=find_unused_parameters) device_id = torch.cuda.current_device() _ = load_checkpoint( model, args.checkpoint, map_location=lambda storage, loc: storage.cuda(device_id)) outputs = multi_gpu_test(model, data_loader, args.tmpdir, args.gpu_collect, save_path=args.save_path, save_image=args.save_image, empty_cache=empty_cache) if rank == 0: print('') # print metrics stats = dataset.evaluate(outputs) for stat in stats: print('Eval-{}: {}'.format(stat, stats[stat])) # save result pickle if args.out: print('writing results to {}'.format(args.out)) mmcv.dump(outputs, args.out)
def main(): args = parse_args() checkpoint_list = os.listdir(args.checkpoint_dir) print(checkpoint_list) for checkpoint in checkpoint_list: if '.pth' in checkpoint: cfg = mmcv.Config.fromfile(args.config) # set cudnn_benchmark if cfg.get('cudnn_benchmark', False): torch.backends.cudnn.benchmark = True cfg.model.pretrained = None # init distributed env first, since logger depends on the dist info. if args.launcher == 'none': distributed = False else: distributed = True init_dist(args.launcher, **cfg.dist_params) rank, _ = get_dist_info() # set random seeds if args.seed is not None: if rank == 0: print('set random seed to', args.seed) set_random_seed(args.seed, deterministic=args.deterministic) # build the dataloader # TODO: support multiple images per gpu (only minor changes are needed) dataset = build_dataset(cfg.data.test) data_loader = build_dataloader(dataset, samples_per_gpu=1, workers_per_gpu=cfg.data.get( 'val_workers_per_gpu', cfg.data.workers_per_gpu), dist=distributed, shuffle=False) # build the model and load checkpoint model = build_model(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg) args.save_image = args.save_path is not None # distributed test find_unused_parameters = cfg.get('find_unused_parameters', False) model = DistributedDataParallelWrapper( model, device_ids=[torch.cuda.current_device()], broadcast_buffers=False, find_unused_parameters=find_unused_parameters) device_id = torch.cuda.current_device() _ = load_checkpoint( model, os.path.join(args.checkpoint_dir, checkpoint), map_location=lambda storage, loc: storage.cuda(device_id)) outputs = multi_gpu_test(model, data_loader, args.tmpdir, args.gpu_collect, save_path=args.save_path, save_image=args.save_image) if rank == 0: # print metrics stats = dataset.evaluate(outputs) write_file = open( os.path.join(args.checkpoint_dir, 'eval_result_new.txt'), 'a') for stat in stats: print('{}: Eval-{}: {}'.format(checkpoint, stat, stats[stat])) write_file.write('{}: Eval-{}: {} '.format( checkpoint, stat, stats[stat])) write_file.write('\n') write_file.close() # save result pickle if args.out: print('writing results to {}'.format(args.out)) mmcv.dump(outputs, args.out)