远程环境无法渲染
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import gymnasium as gym
from stable_baselines3 import A2C
env = gym.make("CartPole-v1", render_mode="rgb_array")
model = A2C("MlpPolicy", env, verbose=1) model.learn(total_timesteps=10_000)
vec_env = model.get_env() obs = vec_env.reset() for i in range(1000): action, _state = model.predict(obs, deterministic=True) obs, reward, done, info = vec_env.step(action) vec_env.render("human") # VecEnv resets automatically # if done: # obs = vec_env.reset()
|
会得到报错NoSuchDisplayException: Cannot connect to "None"
,原因是远程服务器没有接受显示的媒体。那么解决的第一个方案就是通过一个虚拟桌面接受来使其不报错,然后通过逐帧绘制图像,将过程展示在画板上。
XVFB + Matplotlib
XVFB
Xvfb是一个实现了X11显示服务协议的显示服务器。
安装
运行代码
1
| xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>
|
运行jupyter
1
| xvfb-run -s "-screen 0 1400x900x24" jupyter-notebook
|
通过matplotlib渲染过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from IPython import display from matplotlib import pyplot as plt %matplotlib inline # turn on in cell
env = gym.make(..., render_mode="rgb_array") # must set `render_mode` to "rgb_array"
...
img = plt.imshow(vec_env.render()) for i in range(n): ... img.set_data(vec_env.render()) display.display(plt.gcf()) display.clear_output(wait=True)
|
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import gymnasium as gym
from stable_baselines3 import A2C from IPython import display from matplotlib import pyplot as plt %matplotlib inline
env = gym.make("CartPole-v1", render_mode="rgb_array")
model = A2C("MlpPolicy", env, verbose=1) model.learn(total_timesteps=10_000)
vec_env = model.get_env() obs = vec_env.reset() img = plt.imshow(vec_env.render()) for i in range(1000): action, _state = model.predict(obs, deterministic=True) obs, reward, done, info = vec_env.step(action) img.set_data(vec_env.render()) display.display(plt.gcf()) display.clear_output(wait=True) # VecEnv resets automatically # if done: # obs = vec_env.reset()
|
其他方案
参考 StackOverflow