How to run OpenAI Gym .render() over a server
jupyter-notebook, openai-gym, pyglet, python, xvfb
Solution
Got a simple solution working:
If on a linux server, open jupyter with
$ xvfb-run -s "-screen 0 1400x900x24" jupyter notebook
In Jupyter
import matplotlib.pyplot as plt
%matplotlib inline
from IPython import display
After each step
def show_state(env, step=0, info=""):
plt.figure(3)
plt.clf()
plt.imshow(env.render(mode='rgb_array'))
plt.title("%s | Step: %d %s" % (env._spec.id,step, info))
plt.axis('off')
display.clear_output(wait=True)
display.display(plt.gcf())
Note: if your environment is not `unwrapped`, pass `env.env` to `show_state`.
Problem
I am running a python 2.7 script on a p2.xlarge AWS server through Jupyter (Ubuntu 14.04). I would like to be able to render my simulations. Minimal working example ``` import gym env = gym.make('CartPole-v0') env.reset() env.render() ``` `env.render()` makes (among other things) the following errors: ``` ... HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'") ... NoSuchDisplayException: Cannot connect to "None" ``` I would like to some how be able to see the simulations. It would be ideal if I could get it inline, but any display method would be nice. Edit: This is only an issue with some environments, like classic control. Update I Inspired by this I tried the following, instead of the `xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>` (which I couldn't get to work). ``` xvfb-run -a jupyter notebook ``` Running the original script I now get instead ``` GLXInfoException: pyglet requires an X server with GLX ``` Update II Issue #154 seems relevant. I tried disabling the pop-up, and directly creating the RGB colors ``` import gym env = gym.make('CartPole-v0') env.reset() img = env.render(mode='rgb_array', close=True) print(type(img)) # <--- <type 'NoneType'> img = env.render(mode='rgb_array', close=False) # <--- ERROR print(type(img)) ``` I get `ImportError: cannot import name gl_info`. Update III With inspiration from @Torxed I tried creating a video file, and then rendering it (a fully satisfying solution). Using the code from 'Recording and uploading results' ``` import gym env = gym.make('CartPole-v0') env.monitor.start('/tmp/cartpole-experiment-1', force=True) observation = env.reset() for t in range(100): # env.render() print(observation) action = env.action_space.sample() observation, reward, done, info = env.step(action) if done: print("Episode finished after {} timesteps".format(t+1)) break env.monitor.close() ``` I tried following your suggestions, but got `ImportError: cannot import name gl_info` from when running `env.monitor.start(...`. From my understanding the problem is that OpenAI uses `pyglet`, and `pyglet` 'needs' a screen in order to compute the RGB colors of the image that is to be rendered. It is therefore necessary to trick python to think that there is a monitor connected Update IV FYI there are solutions online using bumblebee that seem to work. This should work if you have control over the server, but since AWS run in a VM I don't think you can use this. Update V Just if you have this problem, and don't know what to do (like me) the state of most environments are simple enough that you can create your own rendering mechanism. Not very satisfying, but.. you know.