Esempio n. 1
0
def main():
    args = parse_args()

    with open(args.config) as f:
        cfg = yaml.load(f, Loader=yaml.FullLoader)
    
    # We will set all the seeds we can, in vain ...
    set_reproducibility(cfg['seed'])
    # Set GPU
    torch.cuda.set_device(args.gpu)

    if args.num_workers > 0:
        if 'transform' not in cfg.keys():
            cfg['transform'] = {}
        cfg['transform']['num_workers'] = args.num_workers

    cfg = set_inference_batch_size(cfg)

    if args.mode == 'predict':
        predict(args, cfg)
        return
    elif args.mode == 'predict_ensemble':
        predict_ensemble(args, cfg)
        return

    if 'mixup' not in cfg['train']['params'].keys():
        cfg['train']['params']['mixup'] = None

    # Make directory to save checkpoints
    if not osp.exists(cfg['evaluation']['params']['save_checkpoint_dir']): 
        os.makedirs(cfg['evaluation']['params']['save_checkpoint_dir'])

    # Load in labels with CV splits
    df = pd.read_csv(cfg['dataset']['csv_filename'])
    if 'chromosome' in cfg['dataset'].keys():
        if cfg['dataset']['chromosome'].lower() == 'xy': 
            df = df[df['male']]
        elif cfg['dataset']['chromosome'].lower() == 'xx':
            df = df[~df['male']]
        else:
            raise Exception('`chromosome` must be one of [`XY`,`XX`]')

    ofold = cfg['dataset']['outer_fold']
    ifold = cfg['dataset']['inner_fold']

    train_df, valid_df, test_df = get_train_valid_test(cfg, df, ofold, ifold)

    logger = create_logger(cfg, args.mode)
    logger.info('Saving to {} ...'.format(cfg['evaluation']['params']['save_checkpoint_dir']))

    if args.mode == 'find_lr':
        cfg['optimizer']['params']['lr'] = cfg['find_lr']['params']['start_lr']
        find_lr(args, cfg, train_df, valid_df)
    elif args.mode == 'train':
        train(args, cfg, train_df, valid_df)
    elif args.mode == 'test':
        test(args, cfg, test_df)
Esempio n. 2
0
def main():
    args = parse_args()

    with open(args.config) as f:
        cfg = yaml.load(f, Loader=yaml.FullLoader)

    if args.num_workers > 0:
        cfg['transform']['num_workers'] = args.num_workers

    cfg = set_inference_batch_size(cfg)

    # We will set all the seeds we can, in vain ...
    set_reproducibility(cfg['seed'])
    # Set GPU
    torch.cuda.set_device(args.gpu)

    # Make directory to save checkpoints
    if not osp.exists(cfg['evaluation']['params']['save_checkpoint_dir']):
        os.makedirs(cfg['evaluation']['params']['save_checkpoint_dir'])

    # Load in labels with CV splits
    with open(cfg['dataset']['pickled'], 'rb') as f:
        ann = pickle.load(f)

    if 'positives_only' in cfg['dataset'].keys(
    ) and cfg['dataset']['positives_only']:
        print('Filtering out negative images ...')
        ann = [a for a in ann if a['ann']['bboxes'].shape[0] > 0]

    ofold = cfg['dataset']['outer_fold']
    ifold = cfg['dataset']['inner_fold']

    if args.mode != 'predict':
        train_ann, valid_ann, test_ann = get_train_valid_test(
            cfg, ann, ofold, ifold, args.mode)
    else:
        pred_ann = ann

    logger = create_logger(cfg, args.mode)
    logger.info('Saving to {} ...'.format(
        cfg['evaluation']['params']['save_checkpoint_dir']))

    if args.mode == 'find_lr':
        cfg['optimizer']['params']['lr'] = cfg['find_lr']['params']['start_lr']
        if 'grad_clip' in cfg['train']['params'].keys():
            cfg['find_lr']['params']['grad_clip'] = cfg['train']['params'][
                'grad_clip']
        find_lr(args, cfg, train_ann, valid_ann)
    elif args.mode == 'train':
        train(args, cfg, train_ann, valid_ann)
    elif args.mode == 'test':
        test()
Esempio n. 3
0
def main():
    args = parse_args()

    with open(args.config) as f:
        cfg = yaml.load(f, Loader=yaml.FullLoader)
    
    if args.num_workers > 0:
        if 'transform' not in cfg.keys():
            cfg['transform'] = {}
        cfg['transform']['num_workers'] = args.num_workers

    if args.mode != 'predict_kfold':
        if args.backbone:
            cfg['model']['params']['backbone'] = args.backbone
            cfg['evaluation']['params']['save_checkpoint_dir'] = osp.join(cfg['evaluation']['params']['save_checkpoint_dir'], args.backbone)

    if args.batch_size:
        cfg['train']['batch_size'] = args.batch_size
        cfg['evaluation']['params']['save_checkpoint_dir'] = osp.join(cfg['evaluation']['params']['save_checkpoint_dir'], f'bs{args.batch_size}')

    if args.fold >= 0:
        if args.mode == 'train':
            cfg['dataset']['outer_only'] = True
            cfg['dataset']['outer_fold'] = args.fold
            cfg['evaluation']['params']['save_checkpoint_dir'] = osp.join(cfg['evaluation']['params']['save_checkpoint_dir'], f'fold{args.fold}')
            cfg['seed'] = int('{}{}'.format(cfg['seed'], args.fold))
        elif args.mode == 'test':
            cfg['dataset']['outer_only'] = True
            cfg['dataset']['outer_fold'] = args.fold
            cfg['test']['savefile'] = osp.join(osp.dirname(cfg['test']['savefile']), f'fold{args.fold}', cfg['test']['savefile'].split('/')[-1]) 

    if args.checkpoint:
        cfg['test']['checkpoint'] = args.checkpoint

    if args.load_previous:
        cfg['train']['load_previous'] = args.load_previous

    if args.eps:
        cfg['optimizer']['params']['eps'] = args.eps
        
    cfg = set_inference_batch_size(cfg)

    # We will set all the seeds we can, in vain ...
    set_reproducibility(cfg['seed'])
    # Set GPU
    if len(args.gpu) == 1:
        torch.cuda.set_device(args.gpu[0])

    if 'predict' in args.mode:
        eval(args.mode)(args, cfg)
        return

    if cfg['transform']['augment'] == 'grid_mask':
        assert 'grid_mask' in cfg['train']['params']

    if 'mixup' not in cfg['train']['params'].keys():
        cfg['train']['params']['mixup'] = None

    if 'cutmix' not in cfg['train']['params'].keys():
        cfg['train']['params']['cutmix'] = None

    # Make directory to save checkpoints
    if not osp.exists(cfg['evaluation']['params']['save_checkpoint_dir']): 
        os.makedirs(cfg['evaluation']['params']['save_checkpoint_dir'])

    # Load in labels with CV splits
    df = pd.read_csv(cfg['dataset']['csv_filename'])
    if cfg['dataset'].pop('exclude_unknown', False):
        df = df[df['diagnosis'] != 'unknown']
    
    if cfg['dataset'].pop('isic2020', False):
        df = df[df['isic'] == 2020]

    ofold = cfg['dataset']['outer_fold']
    ifold = cfg['dataset']['inner_fold']

    train_df, valid_df, test_df = get_train_valid_test(cfg, df, ofold, ifold)

    logger = create_logger(cfg, args.mode)
    logger.info('Saving to {} ...'.format(cfg['evaluation']['params']['save_checkpoint_dir']))

    if args.mode == 'find_lr':
        cfg['optimizer']['params']['lr'] = cfg['find_lr']['params']['start_lr']
        find_lr(args, cfg, train_df, valid_df)
    elif args.mode == 'train':
        train(args, cfg, train_df, valid_df)
    elif args.mode == 'test':
        test(args, cfg, train_df, test_df)