How to render OpenAI gym in google Colab?

给你一囗甜甜゛ 提交于 2019-12-09 10:58:58

问题


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?


回答1:


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 dependancies

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

& install 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.




回答2:


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.




回答3:


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 :)



来源:https://stackoverflow.com/questions/50107530/how-to-render-openai-gym-in-google-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!