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

Integrate with Gym's RecordVideo wrapper. #84

Merged
merged 2 commits into from
Oct 27, 2021

Conversation

vwxyzjn
Copy link
Contributor

@vwxyzjn vwxyzjn commented Oct 27, 2021

This PR adds the meta-data tag that Gym's RecordVideo is looking for.

  metadata = {
    'render.modes': ['human', 'rgb_array'],
    'video.frames_per_second' : 24
  }

Additionally, in the case of a vector environment, the render function would only render the first sub environment for speed reasons.

You can run the following to give it a try:

import gym
import torch

from brax.envs.to_torch import JaxToTorchWrapper
from brax.envs import create_gym_env

dummy_value = torch.ones(1, device="cpu")
envs = create_gym_env("halfcheetah", batch_size=2)
envs.is_vector_env = True
envs = JaxToTorchWrapper(envs, device="cpu")
envs = gym.wrappers.RecordVideo(envs, f"videos/halfcheetah", video_length=50)

obs = envs.reset()
for _ in range(100):
    obs, reward, done, info = envs.step(envs.action_space.sample())
    if done[0]:
        break

Copy link
Collaborator

@erikfrey erikfrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! Two questions for you.

@@ -103,6 +103,10 @@ class GymWrapper(gym.Env):
# Flag that prevents `gym.register` from misinterpreting the `_step` and
# `_reset` as signs of a deprecated gym Env API.
_gym_disable_underscore_compat: ClassVar[bool] = True
metadata = {
'render.modes': ['human', 'rgb_array'],
'video.frames_per_second' : 24
Copy link
Collaborator

@erikfrey erikfrey Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be scaled according to the environment dt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't quite follow, what dt?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you step an environment, a certain amount of time passes, for example in Ant, each step is 0.05 seconds:

https://github.com/google/brax/blob/main/brax/envs/ant.py#L388

For the video speed to match the speed in the world, you would want video.frames_per_second to be 1 / 0.05 = 20

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Thanks for the pointer. Just fixed it in the latest commit.

brax/envs/wrappers.py Show resolved Hide resolved
@erikfrey
Copy link
Collaborator

Looks great - thank you!

@erikfrey erikfrey merged commit d771a99 into google:main Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants