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

gym rendering #48

Closed
christopherhesse opened this issue Jul 1, 2020 · 9 comments
Closed

gym rendering #48

christopherhesse opened this issue Jul 1, 2020 · 9 comments

Comments

@christopherhesse
Copy link
Collaborator

render=True is confusing, should be that render_mode="human" triggers the window

@mtrazzi
Copy link

mtrazzi commented Jul 1, 2020

Hi @christopherhesse,

I'm probably missing something but it's not clear to me how to use render=True from the README.

When I do:
env = gym.make("procgen:procgen-coinrun-v0", start_level=0, num_levels=1, render=True)

I get:

Traceback (most recent call last):
...
  File "~/mtrazzi/procgen/lib/python3.7/site-packages/procgen/gym_registration.py", line 9, in make_env
    env = ProcgenGym3Env(num=1, num_threads=0, **kwargs)
  File "~/mtrazzi/procgen/lib/python3.7/site-packages/procgen/env.py", line 246, in __init__
    super().__init__(num, env_name, options, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'render_human'

And when I do
env = ProcgenGym3Env(num=1, env_name="coinrun", start_level=0, num_levels=1, render=True)

I get:

Traceback (most recent call last):
...
  File "~/mtrazzi/procgen/lib/python3.7/site-packages/procgen/env.py", line 246, in __init__
  super().__init__(num, env_name, options, **kwargs)
  TypeError: __init__() got an unexpected keyword argument 'render'

Would be great to have minimal examples on how to render things in the README.

On a related note, when trying to render with mode=human or mode=rgb_array I don't get anything (maybe because of early reset, as mentioned in the README), and this is still true when using a wrapper from the NeurIPS procgen competion, in particular when running rollouts.py with --video-dir ./outputs (which calls the render() method multiple times inside a Monitor, but then doesn't seem to save any video). Any ideas on why this is hapenning?

Thanks!

@christopherhesse
Copy link
Collaborator Author

@mtrazzi what's the output of pip show procgen and pip show gym3?

@mtrazzi
Copy link

mtrazzi commented Jul 1, 2020

(Note: I changed computer but I get the same tracebacks.)

Version: 0.10.1
Summary: Procedurally Generated Game-Like RL Environments
Home-page: https://github.com/openai/procgen
Author: OpenAI
Author-email: None
License: UNKNOWN
Location: /home/a/procgen/lib/python3.6/site-packages
Requires: gym3, gym, filelock, numpy
Required-by: 
Name: gym3
Version: 0.3.2
Summary: Vectorized Reinforcement Learning Environment Interface
Home-page: https://github.com/openai/gym3
Author: OpenAI
Author-email: None
License: UNKNOWN
Location: /home/a/procgen/lib/python3.6/site-packages
Requires: imageio-ffmpeg, moderngl, glfw, numpy, imageio, cffi
Required-by: procgen

After updating procgen to 0.10.3 I still get the same Traceback for the ProcgenGym3Env command, but the gym.make command seems to run (and render) something!

Also, I got an answer for the NeurIPS competition, and apparently adding a render_mode="rgb_array" in the config should work.

@christopherhesse
Copy link
Collaborator Author

0.10.3 did contain a fix for that error. The docs are a bit ambiguous and partially refer to the previous argument, I'll fix them.

In the future to avoid this sort of stuff, the gym environment will take render_mode="rgb_array" or render_mode="human" and the gym3 will accept only render_mode="rgb_array"

@christopherhesse
Copy link
Collaborator Author

The docs are updated, 0.10.4 will likely contain the fix I mentioned, and I'll leave render=True there for backwards compatibility.

@mtrazzi
Copy link

mtrazzi commented Jul 2, 2020

Hi, thanks for updating the docs.

It seems that passing render_mode='rgb_array' works fine and sets configs correctly.
A slightly modified of the ViewerWrapper demo (cf. PR) render episodes correctly.

Now the question is how to get a video from rollouts, and it seems that gym3's VideoRecorderWrapper should do the trick.

When I replace the identity env in video_recorder_test.py by
env = gym.make("procgen:procgen-coinrun-v0", start_level=0, num_levels=1, render_mode='rgb_array')

I get

Traceback:
...
  super().__init__(ob_space or env.ob_space, ac_space or env.ac_space, env.num)
AttributeError: 'ToGymEnv' object has no attribute 'ob_space'

and when I replace it with env = procgen.ProcgenGym3Env(num=1, env_name="coinrun", render_mode='rgb_array')

I get

  File "script.py", line 54, in test_recorder
    env.act(types_np.zeros(env.ac_space, bshape=(env.num,)))
  File "~/mtrazzi/procgen/lib/python3.7/site-packages/gym3/video_recorder.py", line 101, in act
    self._append_observation()
  File "~/mtrazzi/procgen/lib/python3.7/site-packages/gym3/video_recorder.py", line 77, in _append_observation
    img = ob[self._env_index]
KeyError: 0

Simply using a Monitor doesn't seem to work for gym3/procgen environments so having a demo for video seems useful.
Any ideas on what I'm missing?

Thanks

@mtrazzi
Copy link

mtrazzi commented Jul 2, 2020

Update: problem is solved, code for a video of coinrun here.

@christopherhesse
Copy link
Collaborator Author

That code seems fine, VideoRecorderWrapper will also work, but you have to set info_key="rgb". The error is because by default it expects that the environment returns images in a numpy array. A better error message and/or fallback to "rgb" there would be an improvement.

@christopherhesse
Copy link
Collaborator Author

Here is an example script for future reference:

"""
Example random agent script using the gym3 API to demonstrate that procgen works
"""

from gym3 import types_np
from gym3 import VideoRecorderWrapper
from procgen import ProcgenGym3Env
env = ProcgenGym3Env(num=1, env_name="coinrun", render_mode="rgb_array")
env = VideoRecorderWrapper(env=env, directory=".", info_key="rgb")
step = 0
while True:
    env.act(types_np.sample(env.ac_space, bshape=(env.num,)))
    rew, obs, first = env.observe()
    print(f"step {step} reward {rew} first {first}")
    if step > 0 and first:
        break
    step += 1

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

Successfully merging a pull request may close this issue.

2 participants