本文介绍了如何在服务器上运行 OpenAI Gym .render()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Jupyter (Ubuntu 14.04) 在 p2.xlarge AWS 服务器上运行 python 2.7 脚本.我希望能够渲染我的模拟.

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.

最小工作示例

import gym
env = gym.make('CartPole-v0')
env.reset()
env.render()

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.

更新一

受到 this 的启发,我尝试了以下操作,而不是 xvfb-run-s "-screen 0 1400x900x24" python <your_script.py>(我无法开始工作).

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

更新二

问题 #154 似乎相关.我尝试禁用弹出窗口,并直接创建 RGB 颜色

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

我收到ImportError: cannot import name gl_info.

更新 III

在@Torxed 的启发下,我尝试创建一个视频文件,然后渲染它(一个完全令人满意的解决方案).

With inspiration from @Torxed I tried creating a video file, and then rendering it (a fully satisfying solution).

使用来自'记录和上传结果'

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

我尝试按照您的建议进行操作,但在运行 env.monitor.start(....

I tried following your suggestions, but got ImportError: cannot import name gl_info from when running env.monitor.start(....

根据我的理解,问题是 OpenAI 使用 pyglet,而 pyglet需要"一个屏幕来计算要渲染的图像的 RGB 颜色.因此有必要欺骗python认为有监视器连接

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

更新四

仅供参考,有一些使用 bumblebee 的在线解决方案似乎有效.如果您可以控制服务器,这应该可以工作,但由于 AWS 在 VM 中运行,我认为您不能使用它.

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.

更新 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:

$ xvfb-run -s "-screen 0 1400x900x24" jupyter notebook

在 Jupyter 中

In Jupyter

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

每一步之后

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

注意:如果您的环境未unwrapped,请将env.env 传递给show_state.

Note: if your environment is not unwrapped, pass env.env to show_state.

这篇关于如何在服务器上运行 OpenAI Gym .render()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 01:50