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

[Bug Report] Rendering multiple environments in Meta-World doesn't work #736

Closed
1 task done
reginald-mclean opened this issue Oct 11, 2023 · 2 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@reginald-mclean
Copy link
Contributor

Describe the bug

This issue was originally opened in Meta-World by @ManfredStoiber. There are images of the rendering issue in the linked issue.

The issue is with Multi-Task and Meta-RL rendering. The user may want to render in multiple environments in succession and go back to previous environments and render after X steps of training. However it seems that when a user tries to do this, the context of the Mujoco renderer does not make the previously created viewer the current context. In these cases you get rendered frames that look like the ones from the linked issue above.

I think the fix is to change the if statement in this function to >= 1. This forces the viewer to be current context. However, I don't know if this is a fix that will cause issues with single environments. It shouldn't because calling the make context current function on a context that is already current should be a defined behaviour.

Code example

import metaworld
import random
import matplotlib.pyplot as plt

mt50 = metaworld.MT50() # Construct the benchmark, sampling tasks

training_envs = []
for name, env_cls in mt50.train_classes.items():
  env = env_cls(render_mode="rgb_array")
  task = random.choice([task for task in mt50.train_tasks
                        if task.env_name == name])
  env.set_task(task)
  env.camera_name="corner3"
  training_envs.append(env)

# 1. works (image 1)
plt.imshow(training_envs[34].render()) # pick-place-v2
plt.show()

# 2. works (image 2)
plt.imshow(training_envs[1].render()) # basketball-v2
plt.show()

# 3. does not work anymore (image 3)
plt.imshow(training_envs[34].render()) # pick-place-v2
plt.show()

System info

No response

Additional context

No response

Checklist

  • I have checked that there is no similar issue in the repo
@Kallinteris-Andreas
Copy link
Collaborator

Kallinteris-Andreas commented Nov 10, 2023

I tested the performance impact of the proposed change and observed a 1.42% performance degradation of rendering with render_mode="rgb_array" (but can a more complex environment may incur a bigger performance degradation).

import gymnasium
import gymnasium.utils.performance

env = gymnasium.make("Ant-v5", render_mode="rgb_array")
env.reset(seed=1234)
print(f"renders per second: {gymnasium.utils.performance.benchmark_render(env, target_duration=600)}")
$ py test3.py && py test3.py
renders per second: 401.82104986400776 # old version
renders per second: 396.10183664784245 # version with [reginald-mclean](https://github.com/reginald-mclean)'s proposed patch

I believe the proper solution would be to call self.mujoco_renderer.make_context_current() on the Meta-World side, whenever contexts are switched

for reference https://mujoco.readthedocs.io/en/stable/python.html?highlight=make_current

@reginald-mclean
Copy link
Contributor Author

Sounds good to me. Thank you for looking into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants