Esempio n. 1
0
    def _plot(self, updater, rollouts):
        plt.ion()

        if updater.dataset.gym_dataset.image_obs:
            obs = rollouts.obs
        else:
            obs = rollouts.image

        fig, axes = square_subplots(rollouts.batch_size, figsize=(5, 5))
        plt.subplots_adjust(top=0.95, bottom=0, left=0, right=1, wspace=0.1, hspace=0.1)

        images = []
        for i, ax in enumerate(axes.flatten()):
            ax.set_aspect("equal")
            ax.set_axis_off()
            image = ax.imshow(np.zeros(obs.shape[2:]))
            images.append(image)

        def animate(t):
            for i in range(rollouts.batch_size):
                images[i].set_array(obs[t, i, :, :, :])

        anim = animation.FuncAnimation(fig, animate, frames=len(rollouts), interval=500)

        path = updater.exp_dir.path_for('plots', '{}_animation.gif'.format(self.name))
        anim.save(path, writer='imagemagick')

        plt.close(fig)
Esempio n. 2
0
File: game.py Progetto: alcinos/dps
    def __call__(self, updater):
        plt.ion()
        for learner in updater.learners:
            with learner:
                rollouts = updater.env.do_rollouts(policy=learner.pi,
                                                   n_rollouts=self.N,
                                                   T=cfg.T,
                                                   mode='val')

        if updater.env.gym_env.image_obs:
            obs = rollouts.obs
        else:
            obs = rollouts.image

        fig, axes = square_subplots(self.N, figsize=(5, 5))
        plt.subplots_adjust(top=0.95,
                            bottom=0,
                            left=0,
                            right=1,
                            wspace=0.1,
                            hspace=0.1)

        images = []
        for i, ax in enumerate(axes.flatten()):
            ax.set_aspect("equal")
            ax.set_axis_off()
            image = ax.imshow(np.zeros(obs.shape[2:]))
            images.append(image)

        def animate(t):
            for i in range(self.N):
                images[i].set_array(obs[t, i, :, :, :])

        anim = animation.FuncAnimation(fig,
                                       animate,
                                       frames=len(rollouts),
                                       interval=500)

        path = updater.exp_dir.path_for(
            'plots', 'animation.gif')  # Don't open gifs with VLC
        anim.save(path, writer='imagemagick')

        if cfg.show_plots:
            plt.show()

        plt.close(fig)
Esempio n. 3
0
            d = {}
            d.update({ph: a for ph, a in zip(sprites_ph, sprites)})
            d.update({ph: a for ph, a in zip(scales_ph, scales)})
            d.update({ph: a for ph, a in zip(offsets_ph, offsets)})
            d[backgrounds_ph] = backgrounds
            result = sess.run(images, feed_dict=d)

        from dps.utils import image_to_string
        print(image_to_string(result[0, ..., 0]))
        print()
        print(image_to_string(result[0, ..., 1]))
        print()
        print(image_to_string(result[0, ..., 2]))
        print()

        print(result)

        print("Done running.")

        # Sometimes we get values like 1.0001, nothing really bad.
        result = np.clip(result, 1e-6, 1 - 1e-6)

        import matplotlib.pyplot as plt
        from dps.utils import square_subplots
        fig, axes = square_subplots(len(sprites[0]))
        fig.suptitle(device)
        for img, ax in zip(result, axes.flatten()):
            ax.imshow(img)
        plt.show()