OpenAI Gym 使用 Grayscale 包装器返回 ValueError:要拆包的值过多(期望 2 个)。

huangapple go评论962阅读模式
英文:

OpenAI gym with Grayscling Wrapper return ValueError: too many values to unpack (expected 2)

问题

由于我从gym.wrappers导入了GrayScaleObservation,所以出现了与gym包相关的问题。
我知道问题出在哪里,因为在日志中提到了,但我不知道如何修复它...

这里是导入部分:

# 导入Frame Stacker Wrapper和GrayScale Wrapper
from gym.wrappers import FrameStack, GrayScaleObservation

这是错误信息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/adhuy/code/led8/AI tools/Nicholas Renotte/Reinforcement Learning/mario_tutotial.ipynb Cell 19 in 5
      2 env = JoypadSpace(env, SIMPLE_MOVEMENT)
      3 env = GrayScaleObservation(env, keep_dim=True) 
----> 5 env.reset()

File ~/.pyenv/versions/3.10.6/envs/lewagon/lib/python3.10/site-packages/gym/core.py:379, in ObservationWrapper.reset(self, **kwargs)
    377 def reset(self, **kwargs):
    378     """Resets the environment, returning a modified observation using :meth:`self.observation`."""
--> 379     obs, info = self.env.reset(**kwargs)
    380     return self.observation(obs), info

ValueError: too many values to unpack (expected 2)

通常情况下,下面的代码应该返回(240, 256, 1),即数组的形状。实际上,当我在没有GrayScaleObservation(env, keep_dim=True)的情况下运行此代码时,一切都正常工作。

这是我的代码和我尝试满足的要求:

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

state = env.reset()
state.shape
英文:

Got issue with the gym package since I imported GrayScaleObservation from gym.wrappers.
I know where the issue is located as it is mentioned in the logs but I don't know how the fix it ...

Here the imports:

# Import Frame Stacker Wrapper and Grayscling Wrapper
from gym.wrappers import FrameStack, GrayScaleObservation

Here is the Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/adhuy/code/led8/AI tools/Nicholas Renotte/Reinforcement Learning/mario_tutotial.ipynb Cell 19 in 5
      2 env = JoypadSpace(env, SIMPLE_MOVEMENT)
      3 env = GrayScaleObservation(env, keep_dim=True) 
----> 5 env.reset()

File ~/.pyenv/versions/3.10.6/envs/lewagon/lib/python3.10/site-packages/gym/core.py:379, in ObservationWrapper.reset(self, **kwargs)
    377 def reset(self, **kwargs):
    378     """Resets the environment, returning a modified observation using :meth:`self.observation`."""
--> 379     obs, info = self.env.reset(**kwargs)
    380     return self.observation(obs), info

ValueError: too many values to unpack (expected 2)

Normally the code below should return (240, 256, 1), the shape of the array. Actually when I ran this code without GrayScaleObservation(env, keep_dim=True) , everything works well.

Here is my code and what I try to meet:

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

state = env.reset()
state.shape

答案1

得分: 1

替换 core.py 中引起错误的行:

def reset(self, **kwargs):
    obs = self.env.reset(**kwargs)
    return self.observation(obs)

在使用更多包装器的情况下,删除信息可能会导致后续包装器中出现类似的错误。我还使用了 DummyVecEnv 进行进一步处理,这导致我还需要替换 dummy_vec_env.py 中的 reset 方法:

def reset(self) -> VecEnvObs:
    for env_idx in range(self.num_envs):
        obs = self.envs[env_idx].reset(seed=self._seeds[env_idx])
        self._save_obs(env_idx, obs)
    # 种子只使用一次
    self._reset_seeds()
    return self._obs_from_buf()

... 替换这原始的行:

obs, self.reset_infos[env_idx] = self.envs[env_idx].reset(seed=self._seeds[env_idx])

按照相似的方式从一个错误到另一个错误来修复它们应该能解决问题。我不确定删除提供的信息是否会在其他项目中引发问题,但是为了使 gym_super_mario_bros 运行起来,似乎可以工作。

英文:

Working through a similar project myself right now and encountered the same error.

Even though I cannot give more insight to why exactly things are causing errors (hopefully someone else will eventually enlighten us) I can give you the fix that seems to have worked for me and produces the expected results:

Replace the lines causing errors within core.py with:

def reset(self, **kwargs):
        obs = self.env.reset(**kwargs)
        return self.observation(obs)

Deleting info might cause similar errors in following wrappers in case you´re using more. I also used DummyVecEnv for further processing, leading me to replace the reset method within dummy_vec_env.py aswell:

def reset(self) -> VecEnvObs:
        for env_idx in range(self.num_envs):
            obs = self.envs[env_idx].reset(seed=self._seeds[env_idx])
            self._save_obs(env_idx, obs)
        # Seeds are only used once
        self._reset_seeds()
        return self._obs_from_buf()

...replacing this original line:

obs, self.reset_infos[env_idx] = self.envs[env_idx].reset(seed=self._seeds[env_idx])

Going from error to error fixing them in similar ways should do the trick. I´m unsure weather removing the info provided might cause problems in other projects, but for getting gym_super_mario_bros to run it seems to work.

huangapple
  • 本文由 发表于 2023年7月31日 23:52:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76805236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定