题意:怎样在服务器上运行 OpenAI Gym 的 .render()
方法
问题背景:
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.
通过 Jupyter(在 Ubuntu 14.04 系统上)在 AWS 的 p2.xlarge 服务器上运行一个 Python 2.7 脚本,并希望能够在其中渲染模拟
Minimal working example 最小工作示例
import gym
env = gym.make('CartPole-v0')
env.reset()
env.render()
env.render()
makes (among other things) the following errors:
env.render()
方法在调用时(除了其他可能的行为外)产生以下错误:
...
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.
我希望能够以某种方式看到模拟结果。如果能够在内联(inline)中查看那就最理想了,但任何显示方法都会很不错。
Edit: This is only an issue with some environments, like classic control.
编辑: 这个问题只出现在某些环境中,比如经典控制(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 -s "-screen 0 1400x900x24" python <your_script.py>
(我无法使其正常工作)。
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
问题 #154 似乎与之相关。我尝试禁用了弹出窗口,并直接创建了 RGB 颜色。
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
.
Python 在尝试从某个模块中导入名为 gl_info
的内容时失败了
Update III
With inspiration from @Torxed I tried creating a video file, and then rendering it (a fully satisfying solution).
受到 @Torxed 的启发,我尝试创建了一个视频文件,并成功渲染了它(这是一个完全令人满意的解决方案)。
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(...
.
我尝试按照你的建议操作,但在运行 env.monitor.start(...)
时遇到了 ImportError: cannot import name gl_info
的错误
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
据我理解,问题在于 OpenAI 使用的是 pyglet
,而 pyglet
需要一个屏幕来计算将要渲染的图像的 RGB 颜色。因此,有必要欺骗 Python,让它认为有一个监视器是连接着的。
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.
仅供参考,网上有一些使用 Bumblebee 的解决方案,看起来是有效的。如果你对服务器有控制权,这个方法应该是可行的,但由于 AWS 运行在虚拟机上,我认为你可能无法使用它。
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.
如果你也遇到这个问题,并且不知道该怎么办(就像我一样),那么大多数环境的状态都足够简单,以至于你可以创建自己的渲染机制。虽然不是很令人满意,但……你懂的。
问题解决:
Got a simple solution working: 找到了一个简单的解决方案:
If on a linux server, open jupyter with
如果在 Linux 服务器上,使用以下命令打开 Jupyter:
$ xvfb-run -s "-screen 0 1400x900x24" jupyter notebook
In Jupyter 在 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
.
注意:如果你的环境没有“展开”(即没有直接暴露所有底层属性),那么将 env.env
传递给 show_state
函数