Exemplo n.º 1
0
    print(colored('==> ', 'blue') + 'GLOBAL-Net created.')

    # Optimizer
    optimizer = \
        optim.Adam(filter(lambda p: p.requires_grad, model.parameters()),
                   lr=args.lr,
                   weight_decay=args.weight_decay)
    # Scheduler
    weights_loss_coeffs = [0.32, 0.08, 0.02]
    scheduler = lr_scheduler.MultiStepLR(optimizer,
                                         milestones=[3, 30, 40, 50, 60],
                                         gamma=0.1)

    if args.pretrained:
        # reload from pre_trained_model
        model, optimizer, scheduler, start_epoch, best_val = load_checkpoint(model, optimizer, scheduler,
                                                                 filename=args.pretrained)
        # now individually transfer the optimizer parts...
        for state in optimizer.state.values():
            for k, v in state.items():
                if isinstance(v, torch.Tensor):
                    state[k] = v.to(device)
        cur_snapshot = os.path.basename(os.path.dirname(args.pretrained))
    else:
        if not os.path.isdir(args.snapshots):
            os.mkdir(args.snapshots)

        cur_snapshot = args.name_exp
        if not osp.isdir(osp.join(args.snapshots, cur_snapshot)):
            os.makedirs(osp.join(args.snapshots, cur_snapshot))

        with open(osp.join(args.snapshots, cur_snapshot, 'args.pkl'), 'wb') as f:
               lr=cfg['training']['learning_rate'],
               weight_decay=0)
'''
torch with weight decay is equivelent to tensorflow tfa.optimizers.AdamW
we used tf.train.AdamOptimizer, which does not have weight decay. however, i dont know what is eauivelent to
the regularisation function in layers of tensorflow
'''

if cfg['training']['load_pre_training']:
    path_to_pretrained_model = cfg['training']['pre_trained_weights']
    if not os.path.isfile(cfg['training']['pre_trained_weights']):
        raise ValueError('The path to the model pre-trained weights you indicated does not exist !')

    # reload from pre_trained_model
    print('path to pretrained model is {}'.format(path_to_pretrained_model))
    model, optimizer, start_epoch, best_val = load_checkpoint(model, optimizer, filename=path_to_pretrained_model)
    # now individually transfer the optimizer parts...
    for state in optimizer.state.values():
        for k, v in state.items():
            if isinstance(v, torch.Tensor):
                state[k] = v.to(device)
    save_path = os.path.join(cfg['write_dir'], cfg['name_exp'])

else:
    save_path = os.path.join(cfg['write_dir'], cfg['name_exp'])
    # creates the directory to save the images and checkpoints
    if not osp.isdir(save_path):
        os.mkdir(save_path)

    with open(osp.join(save_path, 'args.pkl'), 'wb') as f:
        pickle.dump(args, f)