Exemplo n.º 1
0
def main():
    args = get_arguments()
    SEED = args.seed
    torch.manual_seed(SEED)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    np.random.seed(SEED)
    if (args.cuda):
        torch.cuda.manual_seed(SEED)
    model, optimizer, training_generator, val_generator, test_generator = initialize(
        args)

    print(model)

    best_pred_loss = 1000.0
    scheduler = ReduceLROnPlateau(optimizer,
                                  factor=0.5,
                                  patience=2,
                                  min_lr=1e-5,
                                  verbose=True)
    print('Checkpoint folder ', args.save)
    if args.tensorboard:
        writer = SummaryWriter('./runs/' + util.datestr())
    else:
        writer = None
    for epoch in range(1, args.nEpochs + 1):
        train(args, model, training_generator, optimizer, epoch, writer)
        val_metrics, confusion_matrix = validation(args, model, val_generator,
                                                   epoch, writer)

        best_pred_loss = util.save_model(model, optimizer, args, val_metrics,
                                         epoch, best_pred_loss,
                                         confusion_matrix)

        scheduler.step(val_metrics.avg_loss())
Exemplo n.º 2
0
def main():
    args = get_arguments()
    SEED = args.seed
    torch.manual_seed(SEED)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    np.random.seed(SEED)
    if (args.cuda):
        torch.cuda.manual_seed(SEED)
    if args.new_training:
        model, optimizer, training_generator, val_generator, class_weight, Last_epoch = initialize_from_saved_model(args)
    else:
        model, optimizer, training_generator, val_generator, class_weight = initialize(args)
        Last_epoch = 0

    #print(model)

    best_pred_loss = 0#lo cambie por balanced accuracy
    scheduler = ReduceLROnPlateau(optimizer, factor=0.5, patience=3, min_lr=1e-5, verbose=True)
    print('Checkpoint folder ', args.save)
    # writer = SummaryWriter(log_dir='../runs/' + args.model, comment=args.model)
    for epoch in range(1, args.nEpochs + 1):
        train(args, model, training_generator, optimizer, Last_epoch+epoch, class_weight)
        val_metrics, confusion_matrix = validation(args, model, val_generator, Last_epoch+epoch, class_weight)
        BACC = BalancedAccuray(confusion_matrix.numpy())
        val_metrics.replace({'bacc': BACC})
        best_pred_loss = util.save_model(model, optimizer, args, val_metrics, Last_epoch+epoch, best_pred_loss, confusion_matrix)

        print(confusion_matrix)
        scheduler.step(val_metrics.avg_loss())
Exemplo n.º 3
0
def objective(trial):

    info = nvmlDeviceGetMemoryInfo(handle)
    print("Total memory:", info.total)
    print("Free memory:", info.free)
    print("Used memory:", info.used)

    model, training_generator, val_generator, test_generator = initialize(ARGS)

    optim_name = trial.suggest_categorical("optimizer",
                                           ["Adam", "RMSprop", "SGD"])
    weight_decay = trial.suggest_float("weight_decay", 1e-5, 1e-1, log=True)
    lr = trial.suggest_float("learning_rate", 1e-7, 1e-5, log=True)
    trial.set_user_attr("worker_id", WORKER_ID)

    optimizer = util.select_optimizer(optim_name, model, lr, weight_decay)
    scheduler = ReduceLROnPlateau(optimizer,
                                  factor=0.5,
                                  patience=2,
                                  min_lr=1e-5,
                                  verbose=True)

    best_pred_loss = 1000.0

    for epoch in range(1, EPOCHS + 1):

        train(ARGS, model, training_generator, optimizer, epoch)
        val_metrics, confusion_matrix = validation(ARGS, model, val_generator,
                                                   epoch)
        scheduler.step(val_metrics._data.average.loss)

    return val_metrics._data.average.recall_mean