예제 #1
0
def main(args):
    # Create dir
    if not os.path.exists("./logs"):
        os.makedirs("./logs")
    if not os.path.exists("./pytorch_models"):
        os.makedirs("./pytorch_models")

    # Set logs
    tb_writer = SummaryWriter('./logs/tb_{0}'.format(args.log_name))
    log = set_log(args)

    # Create env
    env = make_env(args)

    # Set seeds
    env.seed(args.seed)
    torch.manual_seed(args.seed)
    np.random.seed(args.seed)

    # Initialize policy
    student_n = [
        set_policy(env, log, args, name="student", i_agent=i_agent)
        for i_agent in range(args.n_student)
    ]

    # Start train
    train(student_n=student_n,
          env=env,
          log=log,
          tb_writer=tb_writer,
          args=args)

    if not os.path.exists("./saved_model"):
        os.makedirs("./saved_model")
예제 #2
0
파일: main.py 프로젝트: jdariasl/COVIDNET
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())
예제 #3
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())
예제 #4
0
def main(args):
    if args.central_train is True:
        raise NotImplementedError("todo")

    # Create directories
    if not os.path.exists("./logs"):
        os.makedirs("./logs")
    if not os.path.exists("./pytorch_models"):
        os.makedirs("./pytorch_models")

    # Set logs
    tb_writer = SummaryWriter('./logs/tb_{0}'.format(args.log_name))
    log = set_log(args)

    # Create env
    env = make_env(args)

    # Set seeds
    env.seed(args.seed)
    torch.manual_seed(args.seed)
    np.random.seed(args.seed)

    # Initialize policy
    agents = [
        set_policy(env, tb_writer, log, args, name="agent", i_agent=i_agent)
        for i_agent in range(args.n_agent)
    ]

    # Start train
    train(agents=agents, env=env, log=log, tb_writer=tb_writer, args=args)
예제 #5
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
예제 #6
0
def main(args):
    # Create directories
    if not os.path.exists("./logs"):
        os.makedirs("./logs")
    if not os.path.exists("./pytorch_models"):
        os.makedirs("./pytorch_models")

    # Set logging
    log = set_log(args)
    tb_writer = SummaryWriter('./logs/tb_{0}'.format(args.log_name))

    # Create env
    env = make_env(args)

    # Set seeds 0 seed is odd
    env.seed(args.seed)
    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)

    # Initialize policy
    agent = set_policy(env, args.n_hidden, tb_writer, log, args)

    # load agent
    if args.mode == "test":
        agent.load_weight("pytorch_models/", args.test_model)
        test(agent=agent, env=env, log=log, tb_writer=tb_writer, args=args)
    else:
        train(agent=agent,
              env=env,
              log=log,
              tb_writer=tb_writer,
              num_samples=args.num_samples,
              args=args)
예제 #7
0
def main(args):
    try:
        config = init_configs(flags=FLAGS)

        if config.type == 'train':
            train(config)
            pass
        elif config.type == 'predict':
            pass

    except ValueError as e:
        print('Run is finished. Reason:')
        print(e)
        exit()
    except Exception as e:
        print('Run is finished. Reason:')
        print(e)
        exit()
예제 #8
0
def main(args):
    # Create directories
    if not os.path.exists("./logs"):
        os.makedirs("./logs")
    if not os.path.exists("./pytorch_models"):
        os.makedirs("./pytorch_models")

    # Set logs
    tb_writer = SummaryWriter('./logs/tb_{0}'.format(args.log_name))
    log = set_log(args)

    # Create env
    env = make_env(args)

    # Set seeds
    env.seed(args.seed)
    torch.manual_seed(args.seed)
    np.random.seed(args.seed)

    # Initialize policy
    # Note that only one teacher is considered in the one box push domain 
    # to transfer knowledge from agent $i$ to agent $k$ (Section 6.1)
    workers = [
        set_policy(env, tb_writer, log, args, name="worker", i_agent=i_agent)
        for i_agent in range(args.n_worker)]
    managers = [
        set_policy(env, tb_writer, log, args, name="manager", i_agent=i_agent)
        for i_agent in range(args.n_manager)]
    temp_managers = [
        set_policy(env, tb_writer, log, args, name="temp_manager", i_agent=i_agent)
        for i_agent in range(args.n_manager)]
    teacher = set_policy(env, tb_writer, log, args, name="teacher", i_agent=0)

    assert len(workers) == len(managers), "The two number must be same"
    assert len(managers) == len(temp_managers), "The two number must be same"
    
    # Start train
    train(
        workers=workers, managers=managers, 
        temp_managers=temp_managers, teacher=teacher,
        env=env, log=log, tb_writer=tb_writer, args=args)
예제 #9
0
def main(args):
    # Create dir
    if not os.path.exists("./logs"):
        os.makedirs("./logs")
    if not os.path.exists("./pytorch_models"):
        os.makedirs("./pytorch_models")

    # Set logs
    tb_writer = SummaryWriter('./logs/tb_{0}'.format(args.log_name))
    log = set_log(args)

    # Create env
    env = make_env(args)

    # Set seeds
    env.seed(args.seed)
    torch.manual_seed(args.seed)
    np.random.seed(args.seed)

    # Initialize policy
    predator_agents = [
        set_policy(env, tb_writer, log, args, name="predator", i_agent=i_agent)
        for i_agent in range(args.n_predator)
    ]

    prey_agents = [
        set_policy(env, tb_writer, log, args, name="prey", i_agent=i_agent)
        for i_agent in range(args.n_prey)
    ]

    # Start training
    train(predator_agents=predator_agents,
          prey_agents=prey_agents,
          env=env,
          log=log,
          tb_writer=tb_writer,
          args=args)
예제 #10
0
def main(args):
    # Create directories
    if not os.path.exists("./logs"):
        os.makedirs("./logs")
    if not os.path.exists("./pytorch_models"):
        os.makedirs("./pytorch_models")

    # Set logs
    tb_writer = SummaryWriter('./logs/tb_{0}'.format(args.log_name))
    log = set_log(args)

    # Create env
    env = make_env(args)

    # Set seeds
    env.seed(args.seed)
    torch.manual_seed(args.seed)
    np.random.seed(args.seed)

    # Initialize policy
    agent = set_policy(env, tb_writer, log, args, name="agent")

    # Start train
    train(agent=agent, env=env, log=log, tb_writer=tb_writer, args=args)
    path = Path.cwd().absolute() / 'data' / dataset_name
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    transform = T.Compose([
        T.RandomTranslate(0.01),
        T.RandomRotate(15, axis=0),
        T.RandomRotate(15, axis=1),
        T.RandomRotate(15, axis=2)
    ])
    pre_transform = T.NormalizeScale()

    train_dataset, test_dataset = load_dataset(path,
                                               transform,
                                               pre_transform,
                                               category=category)
    train_loader, test_loader = load_dataloader(train_dataset, test_dataset)

    model = eval(model_name)(train_dataset.num_classes, k=30).to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                step_size=20,
                                                gamma=0.8)

    writer = SummaryWriter()

    pbar = trange(1, 31, desc='Epoch', unit='epoch')
    for epoch in pbar:
        train(train_loader, model, optimizer, scheduler, epoch, device, writer)
        iou = test(test_loader, model, device)
        pbar.set_postfix_str(f'test_IoU={iou}')
예제 #12
0
def main(_):
    train.train(FLAGS)
예제 #13
0
def main():
    # Training settings
    parser = argparse.ArgumentParser(description='Image Classification')
    parser.add_argument('--batch-size',
                        type=int,
                        default=64,
                        metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--epochs',
                        type=int,
                        default=10,
                        metavar='N',
                        help='number of epochs to train (default: 20)')
    parser.add_argument('--lr',
                        type=float,
                        default=1e-5,
                        metavar='LR',
                        help='learning rate (default: 1.0)')
    parser.add_argument('--gamma',
                        type=float,
                        default=0.1,
                        metavar='M',
                        help='Learning rate step gamma (default: 0.1)')
    parser.add_argument('--seed',
                        type=int,
                        default=123,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--save-model',
                        action='store_true',
                        default=True,
                        help='For Saving the current Model')
    parser.add_argument('--retrain-base',
                        type=bool,
                        default=True,
                        help='whether retrain the base classifier')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=4,
        metavar='N',
        help='how many batches to wait before logging training status')
    args = parser.parse_args()

    torch.manual_seed(args.seed)

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    kwargs = {'batch_size': args.batch_size, 'shuffle': True}
    if torch.cuda.is_available():
        kwargs.update({'num_workers': 1, 'pin_memory': True}, )

    # load train/test data
    train_set = HornetDataset()
    test_set = HornetTestDataset()
    train_loader = DataLoader(train_set, **kwargs)
    test_loader = DataLoader(test_set, shuffle=False)
    print("Finish loading training data.")
    # imgshow(test_loader)
    # train base model
    if args.retrain_base:
        model = models.alexnet(pretrained=True).to(device)
        model.classifier[6] = nn.Linear(4096, 2).to(device)
        optimizer = optim.Adam(model.parameters(), lr=args.lr)
        # scheduler = StepLR(optimizer, step_size=1, gamma=args.gamma)
        # scheduler = ReduceLROnPlateau(optimizer)

        for epoch in range(1, args.epochs + 1):
            train(args.log_interval, model, device, train_loader, optimizer,
                  epoch)
            # val_loss = test(model, device, test_loader)
            # scheduler.step(val_loss)

        torch.save(model.state_dict(), "saved/model/alex_hornet.pt")
        print("Model saved.")

    # load weights and do experiments
    model = models.alexnet(pretrained=True).to(device)
    model.classifier[6] = nn.Linear(4096, 2).to(device)
    model.load_state_dict(torch.load("saved/model/alex_hornet.pt"))
    model.eval()
    print("Finish loading weights.")

    # directly test on photos
    print("Directly test on photos")
    predict(model, device, test_loader, test_set)
예제 #14
0
파일: train.py 프로젝트: jiangqn/seq2seq
import yaml
import os
from trainer.train import train

config = yaml.load(open('config.yml'))
os.environ["CUDA_VISIBLE_DEVICES"] = str(config[config['task']]['gpu'])
train(config)
예제 #15
0
if __name__ == "__main__":
    """ 主函数,解析参数并启动 """
    parser = argparse.ArgumentParser(description='shoeprint recognition')
    parser.add_argument('action',
                        choices=['train', 'test', 'docs'],
                        help='action type (train/test)')
    parser.add_argument('--resume', action='store_true', help='恢复已有模型继续训练')
    parser.add_argument('--no-gpu', action='store_true', help='不使用 GPU')
    parser.add_argument('--use-cache',
                        action='store_true',
                        help='使用已有的 sample cache')

    args = parser.parse_args()

    CONFIG.train.resume = args.resume or CONFIG.train.resume
    CONFIG.test.use_cache = args.use_cache or CONFIG.test.use_cache
    CONFIG.gpu.enable = not args.no_gpu or not CONFIG.gpu.enable

    if args.action == "train":
        from trainer.train import train
        train()
    elif args.action == "test":
        from infer.test import test
        test()
    elif args.action == "docs":
        from docs import docs_dev
        docs_dev()
    else:
        print("can not parse arg {}".format(args.action))