Rendering OpenAI Gym Environments in Google Colab
UPDATE: This package has been updated for compatibility with the new gymnasium
library and is now called renderlab
. Get it here.
I’ve released a module for rendering your gym
environments in Google Colab. Since Colab runs on a VM instance, which doesn’t include any sort of a display, rendering in the notebook is difficult. After looking through the various approaches, I found that using the moviepy
library was best for rendering video in Colab. So I built a wrapper class for this purpose, called colabgymrender
.
Installation
apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1
pip install -U colabgymrender
Example
Output
Usage
Wrap a gym environment in the Recorder
object.
env = gym.make("CartPole-v0")
env = Recorder(env, <directory>, <fps>)
If you specify a frame rate via <fps>, the videos released to <directory> will be of that frame rate. Otherwise, the environment will check for the default frame rate specified by the environment itself in env.metadata['video.frames_per_second']
. If neither is found, the frame rate will default to 30.
You can pause or resume recording with env.pause()
and env.resume()
, but make sure to env.resume()
to record for at least one frame before calling env.play()
. Otherwise, you’ll get an error for trying to play a video in which no frames were recorded.
While recording, each time the environment reaches a terminal state, the videos will automatically be released to <directory>. There is no need to manually release recordings. This provides for ease of use without having to manually control the operations of the recorder. Simply record an episode (or part of an episode), then play the content.
More Examples
Links: