PKU-Alignment / safety-gymnasium

NeurIPS 2023: Safety-Gymnasium: A Unified Safe Reinforcement Learning Benchmark
https://safety-gymnasium.readthedocs.io/en/latest/
Apache License 2.0
392 stars 52 forks source link

Can not render mujoco env #105

Closed fardinabbasi closed 5 months ago

fardinabbasi commented 9 months ago

Required prerequisites

What version of Safety-Gymnasium are you using?

1.0.0

System information

sys.version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] sys.platfrom: linux I am runing on colab

Problem description

I am trying to render SafetyRacecarButton2-v0 environment but I face some errors.

Reproducible example code

import safety_gymnasium
from gymnasium.wrappers import RecordVideo
from safety_gymnasium.wrappers import SafeAutoResetWrapper, SafeRescaleAction, SafeUnsqueeze
from safepo.common.wrappers import SafeNormalizeObservation
env = safety_gymnasium.make("SafetyRacecarButton2-v0", max_episode_steps=1000, render_mode="rgb_array", camera_name="fixednear")
env.reset(seed=456)
env = SafeAutoResetWrapper(env)
env = SafeRescaleAction(env, -1.0, 1.0)
env = SafeNormalizeObservation(env)
env = SafeUnsqueeze(env)

trigger = lambda t: t % 300 == 0
env = RecordVideo(env, video_folder=f"./ppo_lag_video", episode_trigger=trigger)

eval_env.reset()
eval_env.render()

Traceback

/usr/local/lib/python3.10/dist-packages/glfw/__init__.py:916: GLFWError: (65544) b'X11: The DISPLAY environment variable is missing'
  warnings.warn(message, GLFWError)
/usr/local/lib/python3.10/dist-packages/glfw/__init__.py:916: GLFWError: (65537) b'The GLFW library is not initialized'
  warnings.warn(message, GLFWError)
FatalError                                Traceback (most recent call last)
<ipython-input-6-b4c042a36a8b> in <cell line: 2>()
      1 eval_env.reset()
----> 2 eval_env.render()

12 frames
/usr/local/lib/python3.10/dist-packages/gymnasium/core.py in render(self)
    416     def render(self) -> RenderFrame | list[RenderFrame] | None:
    417         """Uses the :meth:`render` of the :attr:`env` that can be overwritten to change the returned data."""
--> 418         return self.env.render()
    419 
    420     def close(self):

/usr/local/lib/python3.10/dist-packages/gymnasium/core.py in render(self)
    416     def render(self) -> RenderFrame | list[RenderFrame] | None:
    417         """Uses the :meth:`render` of the :attr:`env` that can be overwritten to change the returned data."""
--> 418         return self.env.render()
    419 
    420     def close(self):

/usr/local/lib/python3.10/dist-packages/gymnasium/core.py in render(self)
    416     def render(self) -> RenderFrame | list[RenderFrame] | None:
    417         """Uses the :meth:`render` of the :attr:`env` that can be overwritten to change the returned data."""
--> 418         return self.env.render()
    419 
    420     def close(self):

/usr/local/lib/python3.10/dist-packages/gymnasium/core.py in render(self)
    416     def render(self) -> RenderFrame | list[RenderFrame] | None:
    417         """Uses the :meth:`render` of the :attr:`env` that can be overwritten to change the returned data."""
--> 418         return self.env.render()
    419 
    420     def close(self):

/usr/local/lib/python3.10/dist-packages/gymnasium/core.py in render(self)
    416     def render(self) -> RenderFrame | list[RenderFrame] | None:
    417         """Uses the :meth:`render` of the :attr:`env` that can be overwritten to change the returned data."""
--> 418         return self.env.render()
    419 
    420     def close(self):

/usr/local/lib/python3.10/dist-packages/gymnasium/wrappers/order_enforcing.py in render(self, *args, **kwargs)
     68                 "set `disable_render_order_enforcing=True` on the OrderEnforcer wrapper."
     69             )
---> 70         return self.env.render(*args, **kwargs)
     71 
     72     @property

/usr/local/lib/python3.10/dist-packages/gymnasium/wrappers/env_checker.py in render(self, *args, **kwargs)
     61         if self.checked_render is False:
     62             self.checked_render = True
---> 63             return env_render_passive_checker(self.env, *args, **kwargs)
     64         else:
     65             return self.env.render(*args, **kwargs)

/usr/local/lib/python3.10/dist-packages/gymnasium/utils/passive_env_checker.py in env_render_passive_checker(env)
    389             )
    390 
--> 391     result = env.render()
    392     if env.render_mode is not None:
    393         _check_render_return(env.render_mode, result)

/usr/local/lib/python3.10/dist-packages/safety_gymnasium/builder.py in render(self)
    315             not self.task.observe_vision
    316         ), 'When you use vision envs, you should not call this function explicitly.'
--> 317         return self.task.render(cost=self.cost, **asdict(self.render_parameters))
    318 
    319     @property

/usr/local/lib/python3.10/dist-packages/safety_gymnasium/bases/underlying.py in render(self, width, height, mode, camera_id, camera_name, cost)
    513                 )
    514 
--> 515         self._get_viewer(mode)
    516 
    517         # Turn all the geom groups on

/usr/local/lib/python3.10/dist-packages/safety_gymnasium/bases/underlying.py in _get_viewer(self, mode)
    566                 )
    567             elif mode in {'rgb_array', 'depth_array'}:
--> 568                 self.viewer = OffScreenViewer(self.model, self.data)
    569             else:
    570                 raise AttributeError(f'Unexpected mode: {mode}')

/usr/local/lib/python3.10/dist-packages/gymnasium/envs/mujoco/mujoco_rendering.py in __init__(self, model, data)
    142         self._get_opengl_backend(width, height)
    143 
--> 144         super().__init__(model, data, width, height)
    145 
    146         self._init_camera()

/usr/local/lib/python3.10/dist-packages/gymnasium/envs/mujoco/mujoco_rendering.py in __init__(self, model, data, width, height)
     59 
     60         # Keep in Mujoco Context
---> 61         self.con = mujoco.MjrContext(self.model, mujoco.mjtFontScale.mjFONTSCALE_150)
     62 
     63         self._set_mujoco_buffer()

FatalError: gladLoadGL error


### Expected behavior

_No response_

### Additional context

_No response_
muchvo commented 9 months ago

Please refer to this discussion.

fardinabbasi commented 9 months ago

Please refer to this discussion.

Unfortunately, I still get the same error.

Reproducible code

env = safety_gymnasium.make("SafetyRacecarCircle2Vision-v0")
env.reset()
env.step(env.action_space.sample())

Traceback

FatalError                                Traceback (most recent call last)
[<ipython-input-28-e2c2c37d7088>](https://localhost:8080/#) in <cell line: 1>()
----> 1 env.reset()
      2 env.step(env.action_space.sample())

10 frames
[/usr/local/lib/python3.10/dist-packages/gymnasium/wrappers/time_limit.py](https://localhost:8080/#) in reset(self, **kwargs)
     73         """
     74         self._elapsed_steps = 0
---> 75         return self.env.reset(**kwargs)
     76 
     77     @property

[/usr/local/lib/python3.10/dist-packages/gymnasium/wrappers/order_enforcing.py](https://localhost:8080/#) in reset(self, **kwargs)
     59         """Resets the environment with `kwargs`."""
     60         self._has_reset = True
---> 61         return self.env.reset(**kwargs)
     62 
     63     def render(self, *args, **kwargs):

[/usr/local/lib/python3.10/dist-packages/gymnasium/wrappers/env_checker.py](https://localhost:8080/#) in reset(self, **kwargs)
     53         if self.checked_reset is False:
     54             self.checked_reset = True
---> 55             return env_reset_passive_checker(self.env, **kwargs)
     56         else:
     57             return self.env.reset(**kwargs)

[/usr/local/lib/python3.10/dist-packages/gymnasium/utils/passive_env_checker.py](https://localhost:8080/#) in env_reset_passive_checker(env, **kwargs)
    213 
    214     # Checks the result of env.reset with kwargs
--> 215     result = env.reset(**kwargs)
    216 
    217     if not isinstance(result, tuple):

[/usr/local/lib/python3.10/dist-packages/safety_gymnasium/builder.py](https://localhost:8080/#) in reset(self, seed, options)
    188 
    189         # Return an observation
--> 190         return (self.task.obs(), info)
    191 
    192     def step(self, action: np.ndarray) -> tuple[np.ndarray, float, float, bool, bool, dict]:

[/usr/local/lib/python3.10/dist-packages/safety_gymnasium/bases/base_task.py](https://localhost:8080/#) in obs(self)
    400 
    401         if self.observe_vision:
--> 402             obs['vision'] = self._obs_vision()
    403 
    404         assert self.obs_info.obs_space_dict.contains(

[/usr/local/lib/python3.10/dist-packages/safety_gymnasium/bases/base_task.py](https://localhost:8080/#) in _obs_vision(self)
    536         rows, cols = self.vision_env_conf.vision_size
    537         width, height = cols, rows
--> 538         return self.render(width, height, mode='rgb_array', camera_name='vision', cost={})
    539 
    540     def _ego_xy(self, pos: np.ndarray) -> np.ndarray:

[/usr/local/lib/python3.10/dist-packages/safety_gymnasium/bases/underlying.py](https://localhost:8080/#) in render(self, width, height, mode, camera_id, camera_name, cost)
    513                 )
    514 
--> 515         self._get_viewer(mode)
    516 
    517         # Turn all the geom groups on

[/usr/local/lib/python3.10/dist-packages/safety_gymnasium/bases/underlying.py](https://localhost:8080/#) in _get_viewer(self, mode)
    566                 )
    567             elif mode in {'rgb_array', 'depth_array'}:
--> 568                 self.viewer = OffScreenViewer(self.model, self.data)
    569             else:
    570                 raise AttributeError(f'Unexpected mode: {mode}')

[/usr/local/lib/python3.10/dist-packages/gymnasium/envs/mujoco/mujoco_rendering.py](https://localhost:8080/#) in __init__(self, model, data)
    142         self._get_opengl_backend(width, height)
    143 
--> 144         super().__init__(model, data, width, height)
    145 
    146         self._init_camera()

[/usr/local/lib/python3.10/dist-packages/gymnasium/envs/mujoco/mujoco_rendering.py](https://localhost:8080/#) in __init__(self, model, data, width, height)
     59 
     60         # Keep in Mujoco Context
---> 61         self.con = mujoco.MjrContext(self.model, mujoco.mjtFontScale.mjFONTSCALE_150)
     62 
     63         self._set_mujoco_buffer()

FatalError: gladLoadGL error
muchvo commented 8 months ago

Please see this issue.

fardinabbasi commented 8 months ago

Please see this issue.

Sorry, but I tried all these solutions in colab and the same error still remains.


!echo 'export MUJOCO_GL=egl' >> ~/.bashrc

# Source .bashrc to apply changes
!source ~/.bashrc

or

bashrc_path = os.path.expanduser('~/.bashrc')

# Write the export command to .bashrc
with open(bashrc_path, 'a') as bashrc_file:
    bashrc_file.write("export MUJOCO_GL=egl\n")

# Reload the environment to apply changes
os.system("source ~/.bashrc")

or

export MUJOCO_GL=osmesa
export MJLIB_PATH=$HOME/.mujoco/mujoco200/bin/libmujoco200.so
export MJKEY_PATH=$HOME/.mujoco/mujoco200/mjkey.txt
export LD_LIBRARY_PATH=$HOME/.mujoco/mujoco200/bin:$LD_LIBRARY_PATH
export MUJOCO_PY_MJPRO_PATH=$HOME/.mujoco/mujoco200/
export MUJOCO_PY_MJKEY_PATH=$HOME/.mujoco/mujoco200/mjkey.txt
muchvo commented 8 months ago

Could you please provide specific environment details and the latest reproducible source code? It would be helpful to include information about your system, hardware configuration, and a list of conda dependencies.

muchvo commented 8 months ago

In this Colab notebook, you'll find code for rendering that can be executed successfully. You can also cross-reference your local environment with the environment in the Cloud Colab to troubleshoot any issues.