def show_agents(grids,
                agent_list,
                agent_names,
                grid_names,
                filename='AgentComparison',
                figtitle=''):
    """Shows how agents perform on a gridworld

    grid - list of gridworlds (see examples in earlier part of file)
    agent_list - list of agent (objects)
    agent_names - names of agents (strings)
    """
    num_ex = len(agent_list)
    num_grids = len(grids)
    fig, axes_grid = plt.subplots(num_grids, num_ex, figsize=(14.0, 4.5))

    if num_grids == 1:
        axes_grid = [axes_grid]

    for i, axes in enumerate(axes_grid):
        # Give each gridworld a name (uncomment to do so)
        # ax.set_ylabel(grid_names[i])
        # Generate MDP
        grid = grids[i]
        mdp = GridworldMdp(grid, noise=0.2)
        walls, reward, start = mdp.convert_to_numpy_input()

        for idx, agent in enumerate(agent_list):
            ax = axes[idx]
            ax.set_aspect('equal')

            plot_reward(reward, walls, '', fig=fig, ax=ax)
            plot_trajectory(walls,
                            reward,
                            start,
                            agent,
                            arrow_width=0.35,
                            fig=fig,
                            ax=ax)
            # Only write Agent names if it's the first row
            if i == 0:
                ax.set_title(agent_names[idx],
                             fontname='Times New Roman',
                             fontsize=16)

            print('Agent {} is {}'.format(agent_names[idx], agent))

    # Increase vertical space btwn subplots
    # fig.subplots_adjust(hspace=0.2)
    # fig.suptitle(figtitle)
    fig.savefig(filename, bbox_inches='tight', dpi=500)
    print("Saved figure to {}.png".format(filename))
Exemplo n.º 2
0
def save_imgs(batch,
              img_path,
              idx,
              n_neighbors=5,
              xlim=[-150, 150],
              ylim=[-150, 150]):
    global obs, use_speeds, batch_size
    if use_speeds:
        batch_for_network = torch.cat(batch[1], 0).transpose(0, 1)
    else:
        batch_for_network = torch.cat(batch[0], 0).transpose(0, 1)

    batch_for_network = Variable(batch_for_network.squeeze(0))
    # Forward through network
    first_pos = torch.cat(batch[0], 0)[:, 7, :][:, [2, 3]]
    results, speeds = net.forward(batch_for_network[:, :, [2, 3]][:obs, :, :])

    if use_speeds:
        first_pos = torch.cat(batch[0], 0)[:, 7, :][:, [2, 3]]
        speeds[0, :, :] = torch.add(first_pos, speeds[0, :, :])
        pts_predicted = torch.cumsum(speeds, 0)
    else:
        pts_predicted = speeds
    idx__ = batch_size * idx
    for x in range(len(batch[1])):
        try:
            utils.plot_trajectory(batch[0][x].squeeze(0),
                                  pts_predicted[:, x, :],
                                  batch[2][x],
                                  n_neighbors,
                                  img_path + '_' + str(idx__ + x),
                                  xlim=xlim,
                                  ylim=ylim)
        except:
            continue
    return batch[0], pts_predicted, batch[2]
Exemplo n.º 3
0
def main():
    global args, output_directory, train_csv, test_csvs, mm_scaler
    # MinMax-Scaler!
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
    # evaluation mode
    start_epoch = 0
    if args.evaluate:
        assert os.path.isfile(args.evaluate), \
        "=> no best model found at '{}'".format(args.evaluate)
        print("=> loading best model '{}'".format(args.evaluate))
        checkpoint = torch.load(args.evaluate)
        output_directory = os.path.dirname(args.evaluate)
        args = checkpoint['args']
        start_epoch = checkpoint['epoch'] + 1
        model = checkpoint['model']
        print("=> loaded best model (epoch {})".format(checkpoint['epoch']))
        _, val_loader = create_data_loaders(args, mm_scaler)
        args.evaluate = True
        validate(val_loader, model, checkpoint['epoch'], write_to_file=False)
        return

    # optionally resume from a checkpoint
    elif args.resume:
        chkpt_path = args.resume
        assert os.path.isfile(chkpt_path), \
            "=> no checkpoint found at '{}'".format(chkpt_path)
        print("=> loading checkpoint '{}'".format(chkpt_path))
        checkpoint = torch.load(chkpt_path)
        args = checkpoint['args']
        start_epoch = checkpoint['epoch'] + 1
        best_result = checkpoint['best_result']
        model = checkpoint['model']
        optimizer = checkpoint['optimizer']
        output_directory = os.path.dirname(os.path.abspath(chkpt_path))
        print("=> loaded checkpoint (epoch {})".format(checkpoint['epoch']))
        train_loader, val_loader = create_data_loaders(args, mm_scaler)
        args.resume = True

    # create new model
    else:
        train_loader, val_loader = create_data_loaders(args, mm_scaler)
        print("=> creating Model ({}) ...".format(args.arch))
        from models.rnn_model import Model
        if args.arch == 'LSTM':
            model = Model(input_dim=args.x_dim,
                          hidden_dim=args.hidden_size,
                          Y_target=args.y_target,
                          model_type="lstm")
        elif args.arch == 'GRU':
            model = Model(input_dim=args.x_dim,
                          hidden_dim=args.hidden_size,
                          Y_target=args.y_target,
                          model_type="gru")
        if args.arch == 'RNN':
            model = Model(input_dim=args.x_dim,
                          hidden_dim=args.hidden_size,
                          Y_target=args.y_target,
                          model_type="rnn")
        print("=> model created.")

        model_parameters = list(model.parameters())
        params = sum([np.prod(p.size()) for p in model_parameters])
        print("Num. of parameters: ", params)

        optimizer = torch.optim.Adam(model.parameters(),
                                     args.lr,
                                     weight_decay=args.weight_decay)

        # model = torch.nn.DataParallel(model).cuda() # for multi-gpu training
        model = model.cuda()

    criterion = nn.MSELoss().cuda()
    # create results folder, if not already exists
    output_directory = utils.get_output_directory(args)
    if not os.path.exists(output_directory):
        os.makedirs(output_directory)
    train_csv = os.path.join(output_directory, 'train.csv')
    test_csvs = []
    for i in range(NUM_VAL_CSVS):
        test_csv_name = 'test_' + str(i) + '.csv'
        test_csv_each = os.path.join(output_directory, test_csv_name)
        test_csvs.append(test_csv_each)
    test_csv_total = os.path.join(output_directory, 'test.csv')
    test_csvs.append(test_csv_total)

    # 1 indicates total
    assert NUM_VAL_CSVS + 1 == len(test_csvs), "Something's wrong!"

    # create new csv files with only header
    if not args.resume:
        with open(train_csv, 'w') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=[])
            writer.writeheader()
        for test_csv in test_csvs:
            with open(test_csv, 'w') as csvfile:
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                writer.writeheader()

    best_rmse = 1000000000

    print("=> Learning start.")
    for epoch in range(start_epoch, args.epochs):
        utils.adjust_learning_rate(optimizer, epoch, args.lr, args.decay_rate,
                                   args.decay_step)
        print("=> On training...")
        train(train_loader, model, criterion, optimizer,
              epoch)  # train for one epoch
        if epoch % args.validation_interval == 0:
            print("=> On validating...")
            result_rmse, results_list = validate(
                val_loader, model, epoch)  # evaluate on validation set
            # Save validation results
            print("=> On drawing results...")
            pngname = os.path.join(
                output_directory,
                str(epoch).zfill(2) + "_" + str(round(result_rmse, 5)) +
                ".png")
            utils.plot_trajectory(pngname, results_list[:-1])
            is_best = best_rmse > result_rmse
            if is_best:
                best_rmse = result_rmse
                best_name = os.path.join(output_directory, "best.csv")
                with open(best_name, 'w', newline='') as csvfile:
                    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                    writer.writeheader()
                    for result_container in results_list:
                        avg = result_container.result
                        writer.writerow({
                            'rmse': avg.rmse,
                            'mean': avg.mean,
                            'median': avg.median,
                            'var': avg.var,
                            'max': avg.error_max
                        })

                    writer.writerow({
                        'rmse': epoch,
                        'mean': 0,
                        'median': 0,
                        'var': 0,
                        'max': 0
                    })

                utils.save_output(results_list, epoch, output_directory)
                utils.save_checkpoint(
                    {
                        'args': args,
                        'epoch': epoch,
                        'arch': args.arch,
                        'model': model,
                        'optimizer': optimizer,
                        'scaler': mm_scaler
                    }, is_best, epoch, output_directory)
Exemplo n.º 4
0
def main(input_file, input_no):
    other_params = get_other_params()
    data = np.load(input_file, allow_pickle=True)
    data = data.item()
    counter = 5
    for trajcount in range(6):
        q_init = data[trajcount]['q_init']
        q_fin = data[trajcount]['q_fin'][0]
        x_guess = utils.get_real_guess(data, trajcount, 0)
        xs = stack_x(q_init, q_fin)
        q, comp_time_1, msg = utils.solver_solution(x_guess, xs, other_params,
                                                    cost_fun)
        print("Initial problem solved")
        for i in range(1, 7):
            n = data[trajcount]['q_fin'][i].shape[0]
            for j in range(n):
                folder = OUTPUT_FOLDER + "/" + input_no + "/traj" + str(
                    trajcount) + '/' + str((i - 1) * counter + j)
                if not os.path.exists(folder):
                    os.makedirs(folder)

                q_init_new = q_init.copy()
                q_fin_new = data[trajcount]['q_fin'][i][j, :]

                ys = q.copy()
                x_guess = ys.copy()
                xs_new = stack_x(q_init_new, q_fin_new)
                q_new, comp_time_2, msg = utils.solver_solution(
                    x_guess, xs_new, other_params, cost_fun)
                print("Perturbed problem solved by solver")

                solver_sol = {}
                solver_sol['q'] = q
                solver_sol['q_new'] = q_new
                solver_sol['comp_time_1'] = comp_time_1
                solver_sol['comp_time_2'] = comp_time_2
                sfolder = SOLVER + "/" + input_no + "/traj" + str(
                    trajcount) + '/' + str((i - 1) * counter + j)
                if not os.path.exists(sfolder):
                    os.makedirs(sfolder)
                with open(sfolder + '/data.npy', 'wb') as f:
                    np.save(f, solver_sol)

                print("Minimal cost from solver intial ",
                      cost_fun(q, xs, other_params))
                print("Minimal cost from solver perturbed ",
                      cost_fun(q_new, xs_new, other_params))
                etas = np.arange(0.05, 1.05, 0.05)
                niters = 30
                q_pred, comp_time_3, saved_cost, saved_eta, saved_ys_pred = utils.argmin_solution(ys, xs, xs_new, other_params, \
                                                                          etas, niters, cost_fun_jax, vmap_batched_update_ys, F_YY_fn, \
                                                                          F_XY_fn, get_qfin_jax, stack_x, folder)
                print("Perturbed problem solved by argmin")
                print("Time taken : ", comp_time_3)

                save_problem_data(q_init, q_fin, q_init_new, q_fin_new, q, q_new, q_pred, \
                                  comp_time_1, comp_time_2, comp_time_3, saved_cost, saved_eta, saved_ys_pred, other_params, folder)

                # plotting
                utils.plot_trajectory(q, q_new, q_pred, q_init, q_fin,
                                      q_init_new, q_fin_new, folder)
                utils.plot_end_effector_angles(q_new, q_pred, folder)
                utils.plot_joint_angles(q_new, q_pred, folder)
                print("-" * 50)
            print("*" * 50)
        print("=" * 80)
    print('done')
Exemplo n.º 5
0
def test_function(model,
                  test_data,
                  save_plot,
                  epoch,
                  it,
                  save_dir=('imgs_vlstm_nr/', 'checkpoints_vlstm_nr/'),
                  num=12,
                  plot_loss=False,
                  comment=''):
    global writer, use_speeds
    # Variables
    saved_for_plots = []
    obs = 8
    loss_display_test, rmse_display, l2_display, final_l2_display = 0, 0, 0, 0

    # Sample plots to save
    saved_plots = random.sample(range(len(test_data)), 3)
    saved_plots = dict([(int(math.floor(x / test_data.batch_size)),
                         x % test_data.batch_size) for x in saved_plots])

    for idx, batch in enumerate(test_data):
        batch_for_network = None
        if use_speeds:
            batch_for_network = Variable(
                torch.cat(batch[1], 0).transpose(0, 1))
        else:
            batch_for_network = Variable(
                torch.cat(batch[0], 0).transpose(0, 1))

        # Forward through network
        results, speeds = net.forward(batch_for_network[:, :,
                                                        [2, 3]][:obs, :, :])

        # Compute loss
        loss = utils.get_lossfunc(results[:, :, 0], results[:, :, 1],
                                  results[:, :, 2], results[:, :, 3],
                                  results[:, :, 4], batch_for_network[8:, :,
                                                                      2],
                                  batch_for_network[8:, :, 3])
        pts_predicted = None
        if use_speeds:
            first_pos = torch.cat(batch[0], 0)[:, 7, :][:, [2, 3]]
            speeds[0, :, :] = torch.add(first_pos, speeds[0, :, :])
            pts_predicted = torch.cumsum(speeds, 0)
        else:
            pts_predicted = speeds
        # Compute accuracies
        tmp_true_pos = torch.cat(batch[0], 0).transpose(0, 1)
        acc = utils.get_avg_displacement(tmp_true_pos[8:, :, :][:, :, [2, 3]],
                                         pts_predicted)
        acc_l2, acc_final = utils.get_accuracy(
            tmp_true_pos[8:, :, :][:, :, [2, 3]], pts_predicted)

        if len(saved_for_plots) < 50 and random.choice([True, False]):
            saved_for_plots.append((batch, pts_predicted))

        loss_display_test += loss.data[0]
        acc = (torch.sum(acc))
        acc_l2 = torch.sum(acc_l2)
        rmse_display += acc
        l2_display += acc_l2
        final_l2_display += torch.sum(acc_final)

        if (idx in saved_plots) and save_plot:
            plot_true, plot_pred = batch, pts_predicted
            x = saved_plots[idx]
            utils.plot_trajectory(
                batch[0][x].squeeze(0), plot_pred[:, x, :], batch[2][x], 5,
                "{}vanilla_lstm_it_{}_i_{}_b_{}_test_{}".format(
                    save_dir[0], epoch, idx, x, comment))

    if save_plot:
        utils.save_checkpoint(
            {
                'true_pred': saved_for_plots,
            }, save_dir[1] +
            'test_data_plot_o_lstm_{}_{}.pth.tar'.format(epoch, comment))

    print('[Loss_Test_{}] {}'.format(comment,
                                     loss_display_test / len(test_data)))
    print('[Accuracy_mse_Test_{}] {}'.format(comment,
                                             rmse_display / len(test_data)))
    print('[Accuracy_l2_Test_{}] {}'.format(comment,
                                            l2_display / len(test_data)))
    print('[Accuracy_final_Test_{}] {}'.format(
        comment, final_l2_display / len(test_data)))

    print('-------------------------------------------------------------')
    if (plot_loss) and (writer is not None):
        writer.add_scalar('Loss Test {}'.format(comment),
                          (loss_display_test / len(test_data)), it)
        writer.add_scalar('acc_l2 Test {}'.format(comment),
                          (l2_display / len(test_data)), it)
Exemplo n.º 6
0
            loss_display += loss.data[0]
            acc = (torch.sum(acc))
            acc_l2 = torch.sum(acc_l2)
            rmse_display += acc
            l2_display += acc_l2
            final_l2_display += torch.sum(acc_final)
            loss_display_mid += loss.data[0]
            acc_display_mid += acc_l2
            loss.backward()
            optimizer.step()
            if (idx == saved_plots[0][0]) and (epoch % 2 == 0):
                plot_true, plot_pred = batch, pts_predicted
                x = saved_plots[0][1]
                utils.plot_trajectory(
                    batch[0][x].squeeze(0), plot_pred[:, x, :], batch[2][x], 5,
                    "{}/vanilla_lstm_it_{}_i_{}_b_{}".format(
                        save_dir[0], args_input.plot_save, epoch,
                        saved_plots[0][0], x))
            it += 1
            if (it % 2 == 0):
                writer.add_scalar('Loss', loss.data[0] / len(batch[0]), it)
                writer.add_scalar('Accuracy', acc / len(batch[0]), it)
            if (cnt > 500):
                writer.add_scalar('Loss_epoch', loss_display_mid / cnt, it)
                writer.add_scalar('Accuracy_l2_epoch', acc_display_mid / cnt,
                                  it)
                loss_display_mid, acc_display_mid, cnt = 0, 0, 0

        print('iteration {}'.format(epoch))
        print('[Loss] {}'.format(loss_display / len(train_data)))
        print('[Accuracy_mse] {}'.format(rmse_display / len(train_data)))