huawei-noah / SMARTS

Scalable Multi-Agent RL Training School for Autonomous Driving
MIT License
952 stars 190 forks source link

gif visualizations for training with SMARTS #1852

Closed yuant95 closed 1 year ago

yuant95 commented 1 year ago

High Level Description I'm training RL agent in SMARTS environment. I'm wondering is there any support to occasionally save a gif visualization from the training? I understand that I can on time visualize the training with Envision server on. However, is there a way to record the episode without a Envision server on? Running a envision server on may be infeasible when I run my training on computing clusters.

Thanks!

Gamenot commented 1 year ago

Hello @yuant95, if an agent has image observations there are some approaches (although I did not realize how incomplete they currently are.) Any of the hiway-v0 or hiway-v1 derived environments can be configured to render by adding "rgb_array" like env.metadata["render.modes"].append("rgb_array"). Then you can get an image returned from render image = env.render(). The gymnasium environment hiway-v1 would require using the smarts.env.gymnasium.wrappers.api_reversion.Api021Reversion to use this.

I believe we have a few utilities as well for saving (although it looks like some potentially unexpected behaviour.)

RecorderWrapper

The first is RecorderWrapper which automatically records a gif while env.recording is true. This almost does what you want, I am slightly surprised that it always sets recording=True on reset() so at current it will always capture the frame at reset. This currently works if running from the repository but not if using the package.

from smarts.env.wrappers.recorder_wrapper import RecorderWrapper
env: gym.Env = env = gym.make("hiway-v0")
env: gym.Env = RecorderWrapper(video_name="agent_cameras")
...
env.reset()
env.step(...)
env.close() # generates `gif`

Direct use of GifRecorder

from pathlib import Path
from smarts.env.wrappers.gif_recorder import GifRecorder
video_path = "./video"
recorder = GifRecorder(video_path)
...
recorder.capture_frame(env.render())
...
recorder.generate_gif()
recorder.close()

Gym's RecordVideo

Alternatively, we have a wrapper for gym that may work depending on the version of gym installed:

from smarts.env.wrappers.record_video import RecordVideo
env: gym.Env = env = gym.make("hiway-v0")
env: gym.Env = RecordVideo(
    env, video_folder="videos", video_length=40
)