Beispiel #1
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("folder_path", type=str)
    # parser.add_argument("--num_iters", type=int)
    args = parser.parse_args()
    base = Path(os.getcwd())
    base = base / args.folder_path

    path_and_iter = get_path_and_iters(base)

    resolution = 20
    x_bounds = (-1, 1)
    y_bounds = (-1, 1)

    report = HTMLReport(str(base / 'report.html'), images_per_row=1)

    # for path, itr in takespread(path_and_iter, args):
    for path, itr in [path_and_iter[-1]]:
        report.add_text("Path: %s" % path)
        print("Loading: %s" % path)
        data = joblib.load(str(path))
        qf = data['qf']
        env = data['env']
        qf.train(False)

        start_state = env.reset()
        report.add_text("Start State = {}".format(start_state))
        report.add_text("Start XY = {}".format(
            position_from_angles(np.expand_dims(start_state, 0))))
        goal_states = [start_state]
        goal_states += [env.sample_goal_for_rollout() for _ in range(5)]
        for goal_state in goal_states:
            qf_eval = create_qf_eval_fnct(qf, start_state, goal_state)
            qf_heatmap = vu.make_heat_map(
                qf_eval,
                x_bounds=x_bounds,
                y_bounds=y_bounds,
                resolution=resolution,
            )

            fig = create_figure(
                ['Estimated'],
                [qf_heatmap],
            )
            img = vu.save_image(fig)
            report.add_image(
                img, "Goal State = {}\nGoal XY = {}".format(
                    goal_state,
                    position_from_angles(np.expand_dims(goal_state, 0))))

    abs_path = osp.abspath(report.path)
    print("Report saved to: {}".format(abs_path))
    report.save()
    open_report = query_yes_no("Open report?", default="yes")
    if open_report:
        cmd = "xdg-open {}".format(abs_path)
        print(cmd)
        subprocess.call(cmd, shell=True)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("folder_path", type=str)
    args = parser.parse_args()
    base = Path(os.getcwd())
    base = base / args.folder_path

    path_and_iter = get_path_and_iters(base)

    resolution = 10
    discount_factor = 0.99
    state_bounds = (-WaterMaze.BOUNDARY_DIST, WaterMaze.BOUNDARY_DIST)
    action_bounds = (-1, 1)

    for path, iter_number in path_and_iter:
        data = joblib.load(str(path))
        qf = data['qf']
        print("QF loaded from iteration %d" % iter_number)

        vector_fields_t0 = []
        optimal_heatmaps = []
        target_poses = np.linspace(-5, 5, num=5)
        for target_pos in target_poses:
            qf_vector_field_eval = create_qf_derivative_eval_fnct(qf, target_pos, 0)
            vector_fields_t0.append(vu.make_vector_field(
                qf_vector_field_eval,
                x_bounds=state_bounds,
                y_bounds=action_bounds,
                resolution=resolution,
            ))
            optimal_qf_eval = create_optimal_qf(
                target_pos,
                state_bounds=state_bounds,
                action_bounds=action_bounds,
                resolution=resolution,
                discount_factor=discount_factor,
            )
            optimal_heatmaps.append(vu.make_heat_map(
                optimal_qf_eval,
                x_bounds=state_bounds,
                y_bounds=action_bounds,
                resolution=resolution,
            ))

        fig = create_figure(
            'Estimated T=0',
            vector_fields_t0,
            target_poses,
            iter_number,
        )
        save_dir = base / "images"
        if not save_dir.exists():
            save_dir.mkdir()
        save_file = save_dir / 'iter_{}.png'.format(iter_number)
        fig.savefig(str(save_file), bbox_inches='tight')
Beispiel #3
0
def show_prob_heatmap(
    vae,
    xlim,
    ylim,
    resolution=20,
):
    def get_prob_batch(batch):
        return vae.compute_density(batch)

    heat_map = vu.make_heat_map(get_prob_batch,
                                xlim,
                                ylim,
                                resolution=resolution,
                                batch=True)
    vu.plot_heatmap(heat_map)
Beispiel #4
0
def show_weight_heatmap(
    vae,
    skew_config,
    xlim,
    ylim,
    resolution=20,
):
    def get_prob_batch(batch):
        prob = vae.compute_density(batch)
        return prob_to_weight(prob, skew_config)

    heat_map = vu.make_heat_map(get_prob_batch,
                                xlim,
                                ylim,
                                resolution=resolution,
                                batch=True)
    vu.plot_heatmap(heat_map)
def debug_q(ensemble_qs, policy, show_mean=True, show_std=True):
    global fig_v_mean, fig_v_std, axes_v_mean, axes_v_std

    if fig_v_mean is None:
        if show_mean:
            fig_v_mean, axes_v_mean = plt.subplots(3, 3, sharex='all', sharey='all', figsize=(9, 9))
            fig_v_mean.canvas.set_window_title('Q Mean')
            # plt.suptitle("V Mean")
        if show_std:
            fig_v_std, axes_v_std = plt.subplots(3, 3, sharex='all', sharey='all', figsize=(9, 9))
            fig_v_std.canvas.set_window_title('Q Std')
            # plt.suptitle("V Std")

    # obss = [(0, 0), (0, 0.75), (0, 1.75), (0, 1.25), (0, 4), (-4, 4), (4, -4), (4, 4), (-4, 4)]
    obss = []
    for x in [-3, 0, 3]:
        for y in [3, 0, -3]:
            obss.append((x, y))

    def create_eval_function(q, obs, ):
        def beta_eval(goals):
            # goals = np.array([[
            #     *goal
            # ]])
            N = len(goals)
            observations = np.tile(obs, (N, 1))
            new_obs = np.hstack((observations, goals))
            actions = torch_ify(policy.get_action(new_obs)[0])
            return np_ify(q(torch_ify(new_obs), actions)).flatten()
        return beta_eval

    for o in range(9):
        i = o % 3
        j = o // 3
        H = []
        for b in range(5):
            q = ensemble_qs[b]

            rng = [-4, 4]
            resolution = 30

            obs = obss[o]

            heatmap = vu.make_heat_map(create_eval_function(q, obs, ), rng, rng, resolution=resolution, batch=True)
            H.append(heatmap.values)
        p, x, y, _ = heatmap
        if show_mean:
            h1 = vu.HeatMap(np.mean(H, axis=0), x, y, _)
            vu.plot_heatmap(h1, ax=axes_v_mean[i, j])
            axes_v_mean[i, j].set_title("pos " + str(obs))

        if show_std:
            h2 = vu.HeatMap(np.std(H, axis=0), x, y, _)
            vu.plot_heatmap(h2, ax=axes_v_std[i, j])
            axes_v_std[i, j].set_title("pos " + str(obs))

        # axes_v_mean[i, j].plot(range(100), range(100))
        # axes_v_std[i, j].plot(range(100), range(100))

    plt.draw()
    plt.pause(0.01)