envs.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. import numpy
  3. import gym
  4. from gym.spaces.box import Box
  5. from baselines import bench
  6. from baselines.common.atari_wrappers import make_atari, wrap_deepmind
  7. try:
  8. import pybullet_envs
  9. except ImportError:
  10. pass
  11. try:
  12. import gym_minigrid
  13. except:
  14. pass
  15. def make_env(env_id, seed, rank, log_dir):
  16. def _thunk():
  17. env = gym.make(env_id)
  18. is_atari = hasattr(gym.envs, 'atari') and isinstance(env.unwrapped, gym.envs.atari.atari_env.AtariEnv)
  19. if is_atari:
  20. env = make_atari(env_id)
  21. env.seed(seed + rank)
  22. if log_dir is not None:
  23. env = bench.Monitor(env, os.path.join(log_dir, str(rank)))
  24. if is_atari:
  25. env = wrap_deepmind(env)
  26. # If the input has shape (W,H,3), wrap for PyTorch convolutions
  27. obs_shape = env.observation_space.shape
  28. if len(obs_shape) == 3 and obs_shape[2] == 3:
  29. env = WrapPyTorch(env)
  30. return env
  31. return _thunk
  32. class WrapPyTorch(gym.ObservationWrapper):
  33. def __init__(self, env=None):
  34. super(WrapPyTorch, self).__init__(env)
  35. obs_shape = self.observation_space.shape
  36. self.observation_space = Box(
  37. self.observation_space.low[0,0,0],
  38. self.observation_space.high[0,0,0],
  39. [obs_shape[2], obs_shape[1], obs_shape[0]]
  40. )
  41. def _observation(self, observation):
  42. return observation.transpose(2, 0, 1)