Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using env.render() along with stable-baselines3 #8

Open
shivakanthsujit opened this issue Feb 14, 2022 · 3 comments
Open

Comments

@shivakanthsujit
Copy link

Hello,

In the gym.Env class, the first argument is defined as the mode, which is either "human" or "rgb_array". In Crafter, the render function takes only one argument, size. This causes errors when using stable-baselines3 to evaluate policies and create videos since their helper functions assume envs follow gym.Env and have the first argument as the mode.

A fix would be to just add a dummy first argument mode to the render function

crafter/crafter/env.py

Lines 120 to 122 in e955b11

def render(self, size=None):
size = size or self._size
unit = size // self._view

@romnatall
Copy link

romnatall commented Jun 21, 2022

its didnt work for me, still this error

@romnatall
Copy link

romnatall commented Jun 21, 2022

to solve this

recorder.py

def reset(self):
    obs = self._env.reset()
    self._frames = [self._env.render( size=self._size)]
    return obs

  def step(self, action):
    obs, reward, done, info = self._env.step(action)
    self._frames.append(self._env.render( size=self._size))
    if done:
      self._save()
    return obs, reward, done, info

env.py

#in __init__
self.metadata = {
        "render_modes": [ "rgb_array"],
        "render_fps": 4,
    }
def render( self ,mode="rgb_array", size=None):
    size = size or self._size
    unit = size // self._view
    canvas = np.zeros(tuple(size) + (3,), np.uint8)
    local_view = self._local_view(self._player, unit)
    item_view = self._item_view(self._player.inventory, unit)
    view = np.concatenate([local_view, item_view], 1)
    border = (size - (size // self._view) * self._view) // 2
    (x, y), (w, h) = border, view.shape[:2]
    canvas[x: x + w, y: y + h] = view

    return canvas.transpose((1, 0, 2))

  def _obs(self):
    return self.render(None)

also delete pycache folder in crafter and gym

@danijar
Copy link
Owner

danijar commented Nov 10, 2022

Thanks! I'll leave this open in case it's helpful for other people. I don't know if updating the env is worth it at this point since OpenAI seems to have already changed the Gym interface in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants