예제 #1
0
def evaluate(existing_model_path,
             num_episodes=100,
             num_hidden_units=(40,),
             starting_alpha=0.1, starting_lamda=0.9,
             min_alpha=0.1, min_lamda=0.7,
             alpha_decay=1, lamda_decay=0.96,
             alpha_decay_interval=1, lamda_decay_interval=3e4,
             hidden_activation=nn.Sigmoid(), num_inputs=198,
             opponent="pubeval"):
    """
    Evaluate a saved model against an opponent and prints out the model's win rate.

    :param existing_model_path: String. Path of the saved model.
    :param num_episodes: Integer. Number of games to play per model.
    :param num_hidden_units: See EvaluationModel class. 
    :param starting_alpha: See EvaluationModel class.
    :param starting_lamda: See EvaluationModel class.
    :param min_alpha: See EvaluationModel class.
    :param min_lamda: See EvaluationModel class.
    :param alpha_decay: See EvaluationModel class.
    :param lamda_decay: See EvaluationModel class.
    :param alpha_decay_interval: See EvaluationModel class.
    :param lamda_decay_interval: See EvaluationModel class.
    :param hidden_activation: See EvaluationModel class.
    :param num_inputs: See EvaluationModel class.
    :param opponent: "pubeval" or "random".
    """

    model = EvaluationModel(num_inputs=num_inputs, num_hidden_units=num_hidden_units,
                            starting_alpha=starting_alpha, starting_lamda=starting_lamda,
                            min_alpha=min_alpha, min_lamda=min_lamda,
                            alpha_decay=alpha_decay, lamda_decay=lamda_decay,
                            alpha_decay_interval=alpha_decay_interval, lamda_decay_interval=lamda_decay_interval,
                            hidden_activation=hidden_activation)

    model.load(checkpoint_path=existing_model_path)

    if opponent == "pubeval":
        opponent_agent = PubevalAgent(0)
    else:
        opponent_agent = RandomAgent(0)
    agents = [opponent_agent, TDAgent(1, model)]
    wins = [0, 0]
    for i in range(num_episodes):
        game = Game(agents)
        wins[game.play()] += 1

    print("\n{}: \t{}".format(existing_model_path, float(wins[1]) / float(sum(wins))))
예제 #2
0
def test(dataloader, g_model, d_model, z_dim):
    test_conditions = get_test_conditions(
        os.path.join('dataset/task_1', 'test.json')).to(device)
    print(f'test_conditions: {test_conditions.shape}')
    new_test_conditions = get_test_conditions(
        os.path.join('dataset', 'new_test.json')).to(device)
    ground_test_conditions = get_test_conditions(
        os.path.join('dataset/task_1', 'ground.json')).to(device)
    print(f'ground_test_conditions: {ground_test_conditions.shape}')
    fixed_z = random_z(len(test_conditions), z_dim).to(device)
    evaluation_model = EvaluationModel()
    ground_truth = Image.open(
        os.path.join('dataset/task_1/images',
                     'CLEVR_train_006003_2.png')).convert('RGB')
    transformations = transforms.Compose([
        transforms.Resize((64, 64)),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    ground_truth = transformations(ground_truth)
    ground_truth = torch.unsqueeze(ground_truth, 0)
    ground_truth = ground_truth.to(device)
    print(f'ground_tr.shape: {ground_truth.shape}')

    g_model.eval()
    d_model.eval()
    with torch.no_grad():
        gen_imgs = g_model(fixed_z, test_conditions)
        new_gen_imgs = g_model(fixed_z, new_test_conditions)
        print(f'gen_imgs.shape: {gen_imgs.shape}')
    ground_score = evaluation_model.eval(ground_truth, ground_test_conditions)
    score = evaluation_model.eval(gen_imgs, test_conditions)
    new_score = evaluation_model.eval(new_gen_imgs, new_test_conditions)

    print(f'testing score: {score:.2f}')
    print(f'new_testing score: {new_score:.2f}')
    print(f'ground score: {ground_score:.2f}')
예제 #3
0
def train(name='',
          num_episodes=100000,
          checkpoint_path='', checkpoint_interval=1000,
          num_hidden_units=(40,),
          starting_alpha=0.1, starting_lamda=0.9,
          min_alpha=0.1, min_lamda=0.7,
          alpha_decay=1, lamda_decay=0.96,
          alpha_decay_interval=1, lamda_decay_interval=3e4,
          hidden_activation=nn.Sigmoid(), num_inputs=198,
          existing_model_path=''):
    """
    Evaluates all models in the directory against an opponent and prints out the model's win rate.

    
    :param name: String. Name of the model.
    :param num_episodes: Integer. Number of games to play per model.
    :param checkpoint_path: String. Directory in which to save the model at checkpoints.
    :param checkpoint_interval: Integer. Number of episodes per checkpoint.
    :param num_hidden_units: See EvaluationModel class. 
    :param starting_alpha: See EvaluationModel class.
    :param starting_lamda: See EvaluationModel class.
    :param min_alpha: See EvaluationModel class.
    :param min_lamda: See EvaluationModel class.
    :param alpha_decay: See EvaluationModel class.
    :param lamda_decay: See EvaluationModel class.
    :param alpha_decay_interval: See EvaluationModel class.
    :param lamda_decay_interval: See EvaluationModel class.
    :param hidden_activation: See EvaluationModel class.
    :param num_inputs: See EvaluationModel class.
    :param existing_model_path: 
    """
    model = EvaluationModel(num_inputs=num_inputs, num_hidden_units=num_hidden_units,
                            starting_alpha=starting_alpha, starting_lamda=starting_lamda,
                            min_alpha=min_alpha, min_lamda=min_lamda,
                            alpha_decay=alpha_decay, lamda_decay=lamda_decay,
                            alpha_decay_interval=alpha_decay_interval, lamda_decay_interval=lamda_decay_interval,
                            hidden_activation=hidden_activation)

    if existing_model_path:
        model.load(checkpoint_path=existing_model_path)

    model.train_agent(num_episodes=num_episodes, checkpoint_path=checkpoint_path,
                      checkpoint_interval=checkpoint_interval, name=name)
예제 #4
0
from util import get_test_conditions,save_image
from evaluator import EvaluationModel

device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
z_dim=100
c_dim=200
G_times=4
test_path=os.path.join('dataset','test.json')
generator_path=os.path.join('models',f'c_dim {c_dim} G{G_times}','epoch189_score0.72.pt')

if __name__=='__main__':
    # load testing data conditions
    conditions=get_test_conditions(test_path).to(device)  # (N,24) tensor

    # load generator model
    g_model=Generator(z_dim,c_dim).to(device)
    g_model.load_state_dict(torch.load(generator_path))

    # test
    avg_score=0
    for _ in range(10):
        z = torch.randn(len(conditions), z_dim).to(device)  # (N,100) tensor
        gen_imgs=g_model(z,conditions)
        evaluation_model=EvaluationModel()
        score=evaluation_model.eval(gen_imgs,conditions)
        print(f'score: {score:.2f}')
        avg_score+=score

    save_image(gen_imgs,'eval.png',nrow=8,normalize=True)
    print()
    print(f'avg score: {avg_score/10:.2f}')
예제 #5
0
def train(dataloader, g_model, d_model, z_dim, epochs, lr_g, lr_d):
    """
    z_dim: 100
    """
    Criterion = nn.BCELoss()
    optimizer_g = torch.optim.Adam(g_model.parameters(),
                                   lr_g,
                                   betas=(0.5, 0.99))
    optimizer_d = torch.optim.Adam(d_model.parameters(),
                                   lr_d,
                                   betas=(0.5, 0.99))
    evaluation_model = EvaluationModel()

    test_conditions = get_test_conditions(
        os.path.join('dataset/', 'new_test.json')).to(device)
    new_test_conditions = get_test_conditions(
        os.path.join('dataset', 'new_test.json')).to(device)
    fixed_z = random_z(len(test_conditions), z_dim).to(device)
    best_score = 0

    for epoch in range(1, 1 + epochs):
        total_loss_g = 0
        total_loss_d = 0

        for i, (images, conditions) in enumerate(dataloader):
            g_model.train()
            d_model.train()
            batch_size = len(images)
            images = images.to(device)
            conditions = conditions.to(device)

            real = torch.ones(batch_size).to(device)
            fake = torch.zeros(batch_size).to(device)
            """
            train discriminator
            """
            optimizer_d.zero_grad()

            # for real images
            predicts = d_model(images, conditions)
            loss_real = Criterion(predicts, real)
            # for fake images
            z = random_z(batch_size, z_dim).to(device)
            gen_imgs = g_model(z, conditions)
            predicts = d_model(gen_imgs.detach(), conditions)
            loss_fake = Criterion(predicts, fake)
            # bp
            loss_d = loss_real + loss_fake
            loss_d.backward()
            optimizer_d.step()
            """
            train generator
            """
            for _ in range(4):
                optimizer_g.zero_grad()
                z = random_z(batch_size, z_dim).to(device)
                gen_imgs = g_model(z, conditions)
                predicts = d_model(gen_imgs, conditions)
                loss_g = Criterion(predicts, real)
                # bp
                loss_g.backward()
                optimizer_g.step()

            print(
                f'epoch{epoch} {i}/{len(dataloader)} loss_g: {loss_g.item():.3f} loss_d: {loss_d.item():.3f}'
            )
            total_loss_g += loss_g.item()
            total_loss_d += loss_d.item()

        # evaluate
        g_model.eval()
        d_model.eval()
        with torch.no_grad():
            gen_imgs = g_model(fixed_z, test_conditions)
            new_gen_imgs = g_model(fixed_z, new_test_conditions)
        score = evaluation_model.eval(gen_imgs, test_conditions)
        new_score = evaluation_model.eval(new_gen_imgs, new_test_conditions)
        if score > best_score:
            best_score = score
            best_model_wts = copy.deepcopy(g_model.state_dict())
            torch.save(
                best_model_wts,
                os.path.join('models', f'epoch{epoch}_score{score:.2f}.pt'))
        print(
            f'avg loss_g: {total_loss_g/len(dataloader):.3f}  avg_loss_d: {total_loss_d/len(dataloader):.3f}'
        )
        print(f'testing score: {score:.2f}')
        print(f'new_testing score: {new_score:.2f}')
        print('---------------------------------------------')
        # savefig
        save_image(gen_imgs,
                   os.path.join('results', f'epoch{epoch}.png'),
                   nrow=8,
                   normalize=True)
예제 #6
0
def train(epochs, net, trainloader, device, optimizer, scheduler, loss_fn,
          max_grad_norm):
    global global_step

    net.train()
    loss_meter = util.AverageMeter()
    evaluator = EvaluationModel()
    test_conditions = get_test_conditions(os.path.join('test.json')).to(device)
    new_test_conditions = get_test_conditions(
        os.path.join('new_test.json')).to(device)
    best_score = 0
    new_best_score = 0

    for epoch in range(1, epochs + 1):
        print('\nEpoch: ', epoch)
        with tqdm(total=len(trainloader.dataset)) as progress_bar:
            for x, cond_x in trainloader:
                x, cond_x = x.to(device, dtype=torch.float), cond_x.to(
                    device, dtype=torch.float)
                optimizer.zero_grad()
                z, sldj = net(x, cond_x, reverse=False)
                loss = loss_fn(z, sldj)
                wandb.log({'loss': loss})
                # print('loss: ',loss)
                loss_meter.update(loss.item(), x.size(0))
                # wandb.log({'loss_meter',loss_meter})
                loss.backward()
                if max_grad_norm > 0:
                    util.clip_grad_norm(optimizer, max_grad_norm)
                optimizer.step()
                # scheduler.step(global_step)

                progress_bar.set_postfix(nll=loss_meter.avg,
                                         bpd=util.bits_per_dim(
                                             x, loss_meter.avg),
                                         lr=optimizer.param_groups[0]['lr'])
                progress_bar.update(x.size(0))
                global_step += x.size(0)

        net.eval()
        with torch.no_grad():
            gen_imgs = sample(net, test_conditions, device)
        score = evaluator.eval(gen_imgs, test_conditions)
        wandb.log({'score': score})
        if score > best_score:
            best_score = score
            best_model_wts = copy.deepcopy(net.state_dict())
            torch.save(
                best_model_wts,
                os.path.join('weightings/test',
                             f'epoch{epoch}_score{score:.2f}.pt'))

        with torch.no_grad():
            new_gen_imgs = sample(net, new_test_conditions, device)
        new_score = evaluator.eval(new_gen_imgs, new_test_conditions)
        wandb.log({'new_score': new_score})
        if new_score > new_best_score:
            new_best_score = score
            new_best_model_wts = copy.deepcopy(net.state_dict())
            torch.save(
                best_model_wts,
                os.path.join('weightings/new_test',
                             f'epoch{epoch}_score{score:.2f}.pt'))
        save_image(gen_imgs,
                   os.path.join('results/test', f'epoch{epoch}.png'),
                   nrow=8,
                   normalize=True)
        save_image(new_gen_imgs,
                   os.path.join('results/new_test', f'epoch{epoch}.png'),
                   nrow=8,
                   normalize=True)
예제 #7
0
    def play_real_from_given_state(self, points, bar, agents, starting_player,
                                   starting_roll):
        self.agents = agents
        self.board.set_state(points, bar)
        self.play_real(starting_roll, starting_player)


if __name__ == "__main__":
    from evaluator import EvaluationModel

    model = EvaluationModel(num_hidden_units=(40, ),
                            starting_alpha=0.1,
                            starting_lamda=0.9,
                            min_alpha=0.1,
                            min_lamda=0.7,
                            alpha_decay=1,
                            lamda_decay=0.96,
                            alpha_decay_interval=1,
                            lamda_decay_interval=3e4)
    model.load(
        checkpoint_path=
        "./stored_models/final/final_20200302_0326_50_865023_728001.tar")

    difficulty_input = ""
    easy_inputs = {"e", "easy"}
    medium_inputs = {"m", "med", "medium"}
    hard_inputs = {"h", "hard"}
    while difficulty_input not in easy_inputs.union(medium_inputs).union(
            hard_inputs):
        difficulty_input = input(