DLR-RM / stable-baselines3

PyTorch version of Stable Baselines, reliable implementations of reinforcement learning algorithms.
https://stable-baselines3.readthedocs.io
MIT License
8.37k stars 1.61k forks source link

AttributeError: 'SuperMarioBrosEnv' object has no attribute 'render_mode' #1532

Closed Asjad22 closed 1 year ago

Asjad22 commented 1 year ago

❓ Question

from nes_py.wrappers import JoypadSpace import gym_super_mario_bros from gym_super_mario_bros.actions import SIMPLE_MOVEMENT import gym

Import Frame Stacker Wrapper and GrayScaling Wrapper

from gym.wrappers import GrayScaleObservation

Import Vectorization Wrappers

from stable_baselines3.common.vec_env import VecFrameStack, DummyVecEnv

Import Matplotlib to show the impact of frame stacking

from matplotlib import pyplot as plt

from nes_py.wrappers import JoypadSpace import gym_super_mario_bros from gym_super_mario_bros.actions import SIMPLE_MOVEMENT

env = gym_super_mario_bros.make('SuperMarioBros-v0') env = JoypadSpace(env, SIMPLE_MOVEMENT) env = GrayScaleObservation(env, keep_dim=True)

when i execute the line mentioned below:

4. Wrap inside the Dummy Environment

env = DummyVecEnv([lambda: env])

i got this error

C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\stable_baselines3\common\vec_env\patch_gym.py:49: UserWarning: You provided an OpenAI Gym environment. We strongly recommend transitioning to Gymnasium environments. Stable-Baselines3 is automatically wrapping your environments in a compatibility layer, which could potentially cause issues. warnings.warn(

AttributeError Traceback (most recent call last) Cell In[5], line 2 1 # 4. Wrap inside the Dummy Environment ----> 2 env = DummyVecEnv([lambda: env])

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\stable_baselines3\common\vec_env\dummy_vec_env.py:30, in DummyVecEnv.init(self, env_fns) 29 def init(self, env_fns: List[Callable[[], gym.Env]]): ---> 30 self.envs = [_patch_env(fn()) for fn in env_fns] 31 if len(set([id(env.unwrapped) for env in self.envs])) != len(self.envs): 32 raise ValueError( 33 "You tried to create multiple environments, but the function to create them returned the same instance " 34 "instead of creating different objects. " (...) 39 "Please read https://github.com/DLR-RM/stable-baselines3/issues/1151 for more information." 40 )

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\stable_baselines3\common\vec_env\dummy_vec_env.py:30, in (.0) 29 def init(self, env_fns: List[Callable[[], gym.Env]]): ---> 30 self.envs = [_patch_env(fn()) for fn in env_fns] 31 if len(set([id(env.unwrapped) for env in self.envs])) != len(self.envs): 32 raise ValueError( 33 "You tried to create multiple environments, but the function to create them returned the same instance " 34 "instead of creating different objects. " (...) 39 "Please read https://github.com/DLR-RM/stable-baselines3/issues/1151 for more information." 40 )

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\stable_baselines3\common\vec_env\patch_gym.py:58, in _patch_env(env) 49 warnings.warn( 50 "You provided an OpenAI Gym environment. " 51 "We strongly recommend transitioning to Gymnasium environments. " 52 "Stable-Baselines3 is automatically wrapping your environments in a compatibility " 53 "layer, which could potentially cause issues." 54 ) 56 if "seed" in signature(env.unwrapped.reset).parameters: 57 # Gym 0.26+ env ---> 58 return shimmy.GymV26CompatibilityV0(env=env) 59 # Gym 0.21 env 60 return shimmy.GymV21CompatibilityV0(env=env)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\shimmy\openai_gym_compatibility.py:90, in GymV26CompatibilityV0.init(self, env_id, make_kwargs, env) 87 self.action_space = _convert_space(self.gym_env.action_space) 89 self.metadata = getattr(self.gym_env, "metadata", {"render_modes": []}) ---> 90 self.render_mode = self.gym_env.render_mode 91 self.reward_range = getattr(self.gym_env, "reward_range", None) 92 self.spec = getattr(self.gym_env, "spec", None)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\gym\core.py:229, in Wrapper.getattr(self, name) 227 if name.startswith("_"): 228 raise AttributeError(f"attempted to get missing private attribute '{name}'") --> 229 return getattr(self.env, name)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\gym\core.py:229, in Wrapper.getattr(self, name) 227 if name.startswith("_"): 228 raise AttributeError(f"attempted to get missing private attribute '{name}'") --> 229 return getattr(self.env, name)

[... skipping similar frames: Wrapper.__getattr__ at line 229 (1 times)]

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\gym\core.py:229, in Wrapper.getattr(self, name) 227 if name.startswith("_"): 228 raise AttributeError(f"attempted to get missing private attribute '{name}'") --> 229 return getattr(self.env, name)

AttributeError: 'SuperMarioBrosEnv' object has no attribute 'render_mode'

Checklist

qgallouedec commented 1 year ago

It seems like you're using a custom env. So, please fill the custom env issue template completely.

  • [x] If code there is, it is formatted using the markdown code blocks for both code and stack traces.

Also, please check that you have completed the item before checking a box.

Asjad22 commented 1 year ago

I'm new to this and I really didn't get it. Im using virtual environment with python3.11. I saw a tutorial on the web I followed along.

araffin commented 1 year ago

Closing as the issue template was not filled properly, also seems to be related to a custom env, not SB3.

BIT-Gs commented 11 months ago

Bro I just solved this problem by reinstalling an old version of stable-baselines3. You can try: pip install stable-baselines3[extra]==1.4.0

fanshuaiyao commented 6 months ago

pip install stable-baselines3[extra]==1.4.0

very good bro