Closed pstansell closed 4 years ago
Thanks for reporting this. Indeed the example code does not work. The issue seems to lie in VecEnv definition where default render mode is not defined but in Gym Env the default is "human", and judging by the examples this should be default for vecenvs too.
However when I tried env.render("human")
on the example something else broke regarding image stuff. That needs bit more time to look into.
I quick work-around is to set the n_envs=1
before running env.render()
.
For example:
import gym
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common import make_vec_env
from stable_baselines import PPO2
# multiprocess environment
env = make_vec_env('CartPole-v1', n_envs=4)
model = PPO2(MlpPolicy, env, verbose=1)
model.learn(total_timesteps=25000)
model.save("ppo2_cartpole")
del model # remove to demonstrate saving and loading
model = PPO2.load("ppo2_cartpole")
# Enjoy trained agent
env = make_vec_env('Pendulum-v0', n_envs=1) # <------ NEW LINE
obs = env.reset()
while True:
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
Yeah the code works for single environments but it should also work for multiple environments (draws a neat tiled image with cv2). I will look into this later today.
~@Miffyli i think i forgot to push that to sb2 : https://github.com/DLR-RM/stable-baselines3/pull/43~
EDIT: i did it here https://github.com/hill-a/stable-baselines/pull/880
@pstansell could you upgrade to master's version (cf doc) ?
@araffin Running with master
it defaults to "human" as expected, but this error still rises:
~\Desktop\stable-baselines\stable_baselines\common\vec_env\dummy_vec_env.py in render(self, mode)
85 return self.envs[0].render(mode=mode)
86 else:
---> 87 return super().render(mode=mode)
88
89 def _save_obs(self, env_idx, obs):
~\Desktop\stable-baselines\stable_baselines\common\vec_env\base_vec_env.py in render(self, mode)
169
170 # Create a big image by tiling images from subprocesses
--> 171 bigimg = tile_images(imgs)
172 if mode == 'human':
173 import cv2 # pytype:disable=import-error
~\Desktop\stable-baselines\stable_baselines\common\tile_images.py in tile_images(img_nhwc)
13 """
14 img_nhwc = np.asarray(img_nhwc)
---> 15 n_images, height, width, n_channels = img_nhwc.shape
16 # new_height was named H before
17 new_height = int(np.ceil(np.sqrt(n_images)))
ValueError: not enough values to unpack (expected 4, got 1)
@Miffyli what version are you using?
The following code works for me (using latest master version):
from stable_baselines.common.cmd_util import make_vec_env
n_envs = 4
env = make_vec_env('CartPole-v1', n_envs=n_envs)
obs = env.reset()
for _ in range(100):
env.step([env.action_space.sample() for _ in range(n_envs)])
env.render()
(it works also with SB3)
Ah yes, I did a derp. I forgot to reset
the environment before trying to render it :'). Everything works as expected.
Thanks for the amazingly quick resolution!
This is possibly quite trivial, but the
evn.render()
lines of the code examples do not run for multiprocess environments unless unlessn_envs=1
inmake_vec_env()
.For example, the code example on this page
I'm using stable-baselines 2.10.0.
The error is