openai / mujoco-py

MuJoCo is a physics engine for detailed, efficient rigid body simulations with contacts. mujoco-py allows using MuJoCo from Python 3.
Other
2.86k stars 816 forks source link

Failed to initialize OpenGL #187

Open richardrl opened 6 years ago

richardrl commented 6 years ago

I want to use raw image observations, so I am trying to call the _read_pixels_as_in_window(self) in mjviewer.py. However, getting this error:

LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libGLEW.so:/usr/lib/nvidia-384/libGL.so python trpo_run_mujoco.py --env 'Reacher-Pixel-v0'
Logging to /tmp/openai-2018-01-28-23-59-19-815938
2018-01-28 23:59:19.816385: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Device mapping: no known devices.
2018-01-28 23:59:19.816722: I tensorflow/core/common_runtime/direct_session.cc:297] Device mapping:

[2018-01-28 23:59:19,816] Making new env: Reacher-Pixel-v0
Compiling /home/richard/lib/mujoco-py/mujoco_py/cymj.pyx because it changed.
[1/1] Cythonizing /home/richard/lib/mujoco-py/mujoco_py/cymj.pyx
running build_ext
building 'mujoco_py.cymj' extension
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/gl
gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/richard/lib/mujoco-py/mujoco_py -I/home/richard/.mujoco/mjpro150/include -I/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/numpy/core/include -I/home/richard/lib/mujoco-py/mujoco_py/vendor/egl -I/home/richard/anaconda3/envs/py35/include/python3.5m -c /home/richard/lib/mujoco-py/mujoco_py/cymj.c -o /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/cymj.o -fopenmp -w
gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/richard/lib/mujoco-py/mujoco_py -I/home/richard/.mujoco/mjpro150/include -I/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/numpy/core/include -I/home/richard/lib/mujoco-py/mujoco_py/vendor/egl -I/home/richard/anaconda3/envs/py35/include/python3.5m -c /home/richard/lib/mujoco-py/mujoco_py/gl/eglshim.c -o /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/gl/eglshim.o -fopenmp -w
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/lib.linux-x86_64-3.5
creating /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/lib.linux-x86_64-3.5/mujoco_py
gcc -pthread -shared -L/home/richard/anaconda3/envs/py35/lib -Wl,-rpath=/home/richard/anaconda3/envs/py35/lib,--no-as-needed /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/cymj.o /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/temp.linux-x86_64-3.5/home/richard/lib/mujoco-py/mujoco_py/gl/eglshim.o -L/home/richard/.mujoco/mjpro150/bin -L/home/richard/anaconda3/envs/py35/lib -Wl,--enable-new-dtags,-R/home/richard/.mujoco/mjpro150/bin -lmujoco150 -lglewegl -lpython3.5m -o /home/richard/lib/mujoco-py/mujoco_py/generated/_pyxbld_LinuxExtensionBuilder/lib.linux-x86_64-3.5/mujoco_py/cymj.cpython-35m-x86_64-linux-gnu.so -fopenmp
get_obs
Creating window glfw
Found 1 GPUs for rendering. Using device 0.
Could not make EGL context current
Traceback (most recent call last):
  File "trpo_run_mujoco.py", line 35, in <module>
    main()
  File "trpo_run_mujoco.py", line 31, in main
    train(args.env, num_timesteps=args.num_timesteps, seed=args.seed)
  File "trpo_run_mujoco.py", line 21, in train
    env = make_mujoco_env(env_id, workerseed)
  File "/home/richard/baselines/baselines/common/cmd_util.py", line 34, in make_mujoco_env
    env = gym.make(env_id)
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/registration.py", line 164, in make
    return registry.make(id)
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/registration.py", line 122, in make
    env = spec.make()
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/registration.py", line 89, in make
    env = cls(**self._kwargs)
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/mujoco/reacher_pixel.py", line 10, in __init__
    mujoco_env.MujocoEnv.__init__(self, 'reacher.xml', 2)
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/mujoco/mujoco_env.py", line 39, in __init__
    observation, _reward, done, _info = self._step(np.zeros(self.model.nu))
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/mujoco/reacher_pixel.py", line 18, in _step
    ob = self._get_obs()
  File "/home/richard/anaconda3/envs/py35/lib/python3.5/site-packages/gym/envs/mujoco/reacher_pixel.py", line 40, in _get_obs
    print(np.shape(self._get_viewer()._read_pixels_as_in_window()))
  File "/home/richard/lib/mujoco-py/mujoco_py/mjviewer.py", line 218, in _read_pixels_as_in_window
    self.sim.render(resolution[0], resolution[1])
  File "mjsim.pyx", line 149, in mujoco_py.cymj.MjSim.render
  File "mjsim.pyx", line 151, in mujoco_py.cymj.MjSim.render
  File "mjrendercontext.pyx", line 43, in mujoco_py.cymj.MjRenderContext.__init__
  File "mjrendercontext.pyx", line 99, in mujoco_py.cymj.MjRenderContext._setup_opengl_context
  File "opengl_context.pyx", line 128, in mujoco_py.cymj.OffscreenOpenGLContext.__init__
RuntimeError: Failed to initialize OpenGL

My setup works for training and rendering video of the non-pixel Mujoco environment on my screen, but this '_read_pixels_as_in_window(self)', which I am using to access raw image data, fails. And actually even this method was working until I tried installing CUDA (which overwrote my nvidia-384 driver with nvidia-390') and installing CUDA broke everything, so now I think I rolled everything back to nvidia-384 properly and yet this read_pixels is still not working.

febert commented 6 years ago

Are there any updates on this?

adonishong commented 6 years ago

met same issue here, track into this file: ./mujoco_py/gl/eglshim.c call eglGetError() with the place this issue happens(after eglMakeCurrent(eglDpy, EGL_NO_SURFACE, EGL_NO_SURFACE, eglCtx)), eglGetError returns 0x3002, EGL_BAD_ACCESS, no idea from this place...

mjpro150/sample/record with same logic works fine ...

HailinRen commented 6 years ago

Did anyone solve this problem?

vitchyr commented 6 years ago

I've found that if I want to call env.render() then I need to set LD_PRELOAD to /usr/lib/x86_64-linux-gnu/libGLEW.so:/usr/lib/nvidia-384/libGL.so.

However, if I want to call env.sim.render(w, h), then I need to not set LD_PRELOAD (e.g. run unset LD_PRELOAD).

lutein commented 5 years ago

@vitchyr Thx a lot!

chenyiwen97 commented 5 years ago

I got the same problem,too.How to fix it?

vitchyr commented 5 years ago

@chenyiwen97 have you tried the solution posted above?

chenyiwen97 commented 5 years ago

@chenyiwen97 have you tried the solution posted above?

Yes.I add the 'export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libGLEW.so:/usr/lib/nvidia-415/libGL.so' to the bashrc, but it still doesn't work.

chenyiwen97 commented 5 years ago

@chenyiwen97 have you tried the solution posted above?

Sorry, I misunderstood what you said. Now it works.Thanks a lot.

beark007 commented 5 years ago

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia-384 This works for me.

beark007 commented 5 years ago

when training, the following has to be commented, unless do testing. export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libGLEW.so

kracon7 commented 5 years ago

Is there a solution to this (can't use env.render and env.sim.render in the same environment) yet? I'm experiencing the same issue. And my current work around is to set and unset environment variable "LD_PRELOAD" everytime in python scripts. Basically there is no way to call env.render() and env.sim.render(w, h) in the same environment because the first one requires export LD_PRELOAD=/usr/lib/...../libGLEW.so but the second one requires this line commented.

luizmarao commented 5 years ago

After lots of trying and lots of reading, with no success, I tried (for no reason at all) something that worked! Using the mode as rgb_array in env.sim.render(), I received a warning that mode should be offscreen or window. Offscreen was exactly the mode in that the error was happening... so I changed to window. At first, the only change was that, immediately after loading the env, a render screen has shown up, without the error from offscreen mode. Then, without closing the render window, I decided to run the command env.sim.render() with offscreen mode on the python shell, and it worked! I received the image without any error! So I included in my code a line calling env.sim.render() with mode window before the line with mode offscreen, which gets the camera image.

my code lines (inside my gym environment): self.sim.render(mode='window', camera_name='first-person', width=16, height=16, depth=False) img = self.sim.render(mode='offscreen', camera_name='first-person', width=16, height=16, depth=False)

huangjiancong1 commented 4 years ago

The most import is to check the usage inside env.render():

viewer =mujoco_py.MjRenderContextOffscreen(sim, dveice_id=)

or

viewer = mujoco_py.MjViewer(sim)

The second one should has a LD_PRELOAD= to /usr/lib/x86_64-linux-gnu/libGLEW.so:/usr/lib/nvidia-384/libGL.so as mentioned above.

madhan001 commented 4 years ago

undsetting LD_PRELOAD does't work for me, is there a workaround that doesn't involve it? edit : This is an issue related to openGL and ubuntu nvidia drivers, move to a non ubuntu distro to fix this issue

arthur801031 commented 3 years ago

I also had this problem, but it had to do with incorrect Ubuntu version. I had this error message using env.sim.render with Ubuntu 20.04, but once I reinstalled my machine with Ubuntu 18.04, the program ran perfectly.

drozzy commented 3 years ago

Here is a better solution I found: https://github.com/openai/mujoco-py/issues/390#issuecomment-525385434

geyang commented 2 years ago

Here is a full-writing targeting our HPC cluster: https://github.com/geyang/jaynes-starter-kit/tree/master/07_supercloud_setup

axmav commented 2 years ago

I don't even have a /usr/lib/nvidia-384 folder. Only nvidia folder without any .so files inside. But actually CUDA is installed and working properly. Normal rendering in widow is working but in offscreen context not. it fails on: data = self.sim.render(width=width, height=height, camera_name=camera_name) unset LD_PRELOAD helped

zichunxx commented 2 years ago

I don't even have a /usr/lib/nvidia-384 folder. Only nvidia folder without any .so files inside. But actually CUDA is installed and working properly. Normal rendering in widow is working but in offscreen context not. it fails on: data = self.sim.render(width=width, height=height, camera_name=camera_name) unset LD_PRELOAD helped

I have the same issue as you, but unset LD_PRELOAD is useless for me.

axmav commented 2 years ago

@QUIlToT as far as I understand you can not use the gym for rendering in window as well as in background at the same time on Ubuntu. You need to switch between using LD_PRELOAD and not.

zichunxx commented 2 years ago

@QUIlToT as far as I understand you can not use the gym for rendering in window as well as in background at the same time on Ubuntu. You need to switch between using LD_PRELOAD and not.

Thx! You made my day!

Irismoon commented 2 years ago

I don't even have a /usr/lib/nvidia-384 folder. Only nvidia folder without any .so files inside. But actually CUDA is installed and working properly. Normal rendering in widow is working but in offscreen context not. it fails on: data = self.sim.render(width=width, height=height, camera_name=camera_name) unset LD_PRELOAD helped

Hi, I'm running code below,

image

which throws RunTimeError at env.render(mode='rgb_array') like the figure below:

image

Would you like to give any advice?

Thanks, Mango

geng-haoran commented 9 months ago

I was rendering on a remote server and met the same problem. I tried to open an VNC server with port 9 and export DISPLAY=:9 in the terminal, and then the problem was fixed.

albee commented 6 months ago

@huangjiancong1's and @vitchyr's answer guided me to the right solution on an ARM M1 Mac running an Ubuntu 20.04 VM. The NVIDIA drivers don't exist on this system (which has an Apple GPU), but making libGLX_mesa available for linking fixed things for me.

export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libGLEW.so:/usr/lib/aarch64-linux-gnu/libGLX_mesa.so.0

Ghiara commented 1 week ago

For rgb array mode, this code works for me:

self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, None, device_id=-1)

img = self.viewer.read_pixels(512, 512, depth=False)[::-1,:,:]

by default my cuda visible device is 0.