SforAiDl / genrl

A PyTorch reinforcement learning library for generalizable and reproducible algorithm implementations with an aim to improve accessibility in RL
https://genrl.readthedocs.io
MIT License
403 stars 59 forks source link

save_to_gif argument in Trainer #291

Open Sharad24 opened 4 years ago

Sharad24 commented 4 years ago

Save a GIF file based on this argument in trainer.

To-do:

  1. Check tensorboard saving in video
sampreet-arthi commented 4 years ago

This might be useful here.

AdityaKapoor74 commented 4 years ago

An example code snippet for this issue:

# ignore this
from simple_spread_test import make_env
from maa2c import MAA2C

import imageio
import numpy as np
import os
import torch

if __name__ == '__main__':
    env = make_env(scenario_name="simple_spread")
    ma_controller = MAA2C(env ,gif = True)

    # Number of images to capture
    n_images = 10000

    images = []

    # init a new episode
    obs = env.reset()
    # init the img var with the starting state of the env
    img = env.render(mode='rgb_array')[0]

    for i in range(n_images):
      # At each step, append an image to list
      images.append(img)

      # Advance a step and render a new image
      with torch.no_grad():
        action = ma_controller.get_actions(obs)
      obs, _, _ ,_ = env.step(action)
      img = env.render(mode='rgb_array')[0]

     imageio.mimwrite('./simple_spread.gif',
                     [np.array(img) for i, img in enumerate(images) if i%2 == 0],
                     fps=50)
github-actions[bot] commented 3 years ago

Stale issue message