28

I'm trying to use OpenAI gym in google colab. As the Notebook is running on a remote server I can not render gym's environment.

I found some solution for Jupyter notebook, however, these solutions do not work with colab as I don't have access to the remote server.

I wonder if someone knows a workaround for this that works with google Colab?

SiaFahim
  • 411
  • 1
  • 5
  • 7
  • 5
    I've found a better method at this [link](https://colab.research.google.com/drive/1flu31ulJlgiRL1dnN2ir8wGh9p7Zij2t). This method creates a video! – SMA.D Jun 01 '19 at 12:44
  • 1
    now broken at the pacman stage: --------------------------------------------------------------------------- Exception Traceback (most recent call last) in () ----> 1 env = wrap_env(gym.make("MsPacman-v0")) Exception: ROM is missing for ms_pacman, see https://github.com/openai/atari-py#roms for instructions – vwvan Apr 10 '22 at 10:32
  • For anyone that is reading this in 2023, I have a working implementation on my github page using Gymnasium (the current maintained fork of Open AI gym) https://github.com/hom-bahrani/Gymnasium-Colaboratory-Starter – Hom Bahrani Mar 27 '23 at 10:15

4 Answers4

24

Korakot's answer is not correct.

You can indeed render OpenAi Gym in colaboratory, albiet kind of slowly using none other than matplotlib.

Heres how:

Install xvfb & other dependencies (Thanks to Peter for his comment)

!apt-get install x11-utils > /dev/null 2>&1 
!pip install pyglet > /dev/null 2>&1 
!apt-get install -y xvfb python-opengl > /dev/null 2>&1

As well as pyvirtual display:

!pip install gym pyvirtualdisplay > /dev/null 2>&1

then import all your libraries, including matplotlib & ipythondisplay:

import gym
import numpy as np
import matplotlib.pyplot as plt
from IPython import display as ipythondisplay

then you want to import Display from pyvirtual display & initialise your screen size, in this example 400x300... :

from pyvirtualdisplay import Display
display = Display(visible=0, size=(400, 300))
display.start()

last but not least, using gym's "rgb_array" render functionally, render to a "Screen" variable, then plot the screen variable using Matplotlib! (rendered indirectly using Ipython display)

env = gym.make("CartPole-v0")
env.reset()
prev_screen = env.render(mode='rgb_array')
plt.imshow(prev_screen)

for i in range(50):
  action = env.action_space.sample()
  obs, reward, done, info = env.step(action)
  screen = env.render(mode='rgb_array')

  plt.imshow(screen)
  ipythondisplay.clear_output(wait=True)
  ipythondisplay.display(plt.gcf())

  if done:
    break

ipythondisplay.clear_output(wait=True)
env.close()

Link to my working Colaboratory notebook demoing cartpole:

https://colab.research.google.com/drive/16gZuQlwxmxR5ZWYLZvBeq3bTdFfb1r_6

Note: not all Gym Environments support "rgb_array" render mode, but most of the basic ones do.

AsgerHB
  • 153
  • 12
P. Conyngham
  • 259
  • 2
  • 4
10

Try this :-

!apt-get install python-opengl -y

!apt install xvfb -y

!pip install pyvirtualdisplay

!pip install piglet


from pyvirtualdisplay import Display
Display().start()

import gym
from IPython import display
import matplotlib.pyplot as plt
%matplotlib inline

env = gym.make('CartPole-v0')
env.reset()
img = plt.imshow(env.render('rgb_array')) # only call this once
for _ in range(40):
    img.set_data(env.render('rgb_array')) # just update the data
    display.display(plt.gcf())
    display.clear_output(wait=True)
    action = env.action_space.sample()
    env.step(action)

This worked for me so I guess it should also work for you.

Jules G.M.
  • 3,624
  • 1
  • 21
  • 35
Shrawan Agrawal
  • 111
  • 1
  • 3
  • 2
    Not working for me giving the following error " NameError Traceback (most recent call last) ... NameError: name 'base' is not defined" – math_law May 05 '19 at 10:11
5

I recently had to solve the same problem and have written up a blog post with my solution. For ease of reference I am re-posting the TLDR; version here.

Paste this code into a cell in Colab and run it to install all of the dependencies.

%%bash

# install required system dependencies
apt-get install -y xvfb x11-utils

# install required python dependencies (might need to install additional gym extras depending)
pip install gym[box2d]==0.17.* pyvirtualdisplay==0.2.* PyOpenGL==3.1.* PyOpenGL-accelerate==3.1.*

And then start a virtual display in the background.

import pyvirtualdisplay


_display = pyvirtualdisplay.Display(visible=False,  # use False with Xvfb
                                    size=(1400, 900))
_ = _display.start()

In the blog post I also provide a sample simulation demo that demonstrates that the above actually works.

davidrpugh
  • 4,363
  • 5
  • 32
  • 46
  • this worked for me. I visited the blog post and followed the code. – Juan Zamora Jan 15 '22 at 01:10
  • @JuanZamora could you share your notebook where this worked? This does not work for me (see https://imgur.com/a/Irmx2G6). – elexhobby Mar 08 '22 at 01:45
  • 2
    @elexhobby here: https://github.com/drzamoramora/4-Symbolic-AI/blob/main/Otros/1-Lunar-Lander-open-ai-gym.ipynb – Juan Zamora Mar 08 '22 at 04:39
  • The code in the answer only gives you a headless display, it doesn't play back the video. The full extract in the blog post uses `matplotlib` like other answers here (note you'll need to set the render mode when initialising the gym environment, not when calling render). The code linked from @JuanZamora 's comment achieves smoother rendering by collating the frames during simulation then playing them back afterwards with `matplotlib.animation` - I'd encourage Juan to submit this as a separate answer as it's superior to the others here! – RobinGower May 26 '23 at 14:47
2

By far the best solution I found after spending countless hours on this issue is to record & play video. It comes UX wise very close to the real render function.

Here is a Google colab notebook that records & renders video. https://colab.research.google.com/drive/12osEZByXOlGy8J-MSpkl3faObhzPGIrB

enjoy :)

Mathias Asberg
  • 3,562
  • 6
  • 30
  • 46
  • 2
    I'm trying to run [video in Colab](https://stackoverflow.com/questions/60886590/how-to-setup-pyglet-to-work-with-manim-in-colab). Do you know is it possible? – Joys Mar 27 '20 at 13:24