ScheiklP / sofa_env

Reinforcement learning environments for robot-assisted laparoscopic surgery
MIT License
42 stars 14 forks source link

SOFA_ZOO Observation Space Type Assertion Error #19

Closed KantaphatLeelakunwet closed 7 months ago

KantaphatLeelakunwet commented 7 months ago
python ~/sofa/sofa_zoo/sofa_zoo/envs/reach/ppo.py

I ran the code above and ended up with an Assertion Error.

image

It seemed that type(env.observation_space) = <class 'gymnasium.spaces.box.Box'>, which is not gym.spaces.Box that the code required.

image

Do you know how to fix this problem? I really appreciate any help you can provide.

ScheiklP commented 7 months ago

Hi @KantaphatLeelakunwet , yeah, I have updated sofa_env to Gymnasium, but sofa_zoo is still using Gym. I hope I can find some time in the coming weeks to port sofa_zoo to Gymnasium aswell. Sorry for that.

ScheiklP commented 7 months ago

Hi @KantaphatLeelakunwet , I have just updated sofa_zoo to Gymnasium. Could you try again?

Cheers, Paul

KantaphatLeelakunwet commented 7 months ago

Thanks for the fast response @ScheiklP, unfortunately, the error message is still the same. I have tried git pull and recloned the repo, but both don't work on my side.

I will show the longer part of the error message here, I hope this helps. Thank you in advance.

Box(-1.0, 1.0, (6,), float32)
<class 'gymnasium.spaces.box.Box'>
False
[ERROR]   [PythonScript] AssertionError: VecFrameStack only works with gym.spaces.Box and gym.spaces.Dict observation spaces
  File "/bd_targaryen/users/kleelakunwet/sofa/sofa_zoo/sofa_zoo/envs/reach/ppo.py", line 76, in <module>
    model, callback = configure_learning_pipeline(
  File "/bd_targaryen/users/jhu/sofa/sofa_zoo/sofa_zoo/common/sb3_setup.py", line 145, in configure_learning_pipeline
    env = VecFrameStack(
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/vec_frame_stack.py", line 22, in __init__
    assert isinstance(

########## SIG 15 - SIGTERM: a termination request was sent to the program ##########
  sofa::helper::BackTrace::sig(int)
  read
  _Py_read
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  PyObject_Call
  _PyEval_EvalFrameDefault
  _PyEval_EvalCodeWithName
  PyEval_EvalCodeEx
  PyEval_EvalCode
  PyRun_StringFlags
  PyRun_SimpleStringFlags
  Py_RunMain
  Py_BytesMain
  __libc_start_main

########## SIG 15 - SIGTERM: a termination request was sent to the program ##########
  sofa::helper::BackTrace::sig(int)
  read
  _Py_read
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  PyObject_Call
  _PyEval_EvalFrameDefault
  _PyEval_EvalCodeWithName
  PyEval_EvalCodeEx
  PyEval_EvalCode
  PyRun_StringFlags
  PyRun_SimpleStringFlags
  Py_RunMain
  Py_BytesMain
  __libc_start_main

########## SIG 15 - SIGTERM: a termination request was sent to the program ##########
  sofa::helper::BackTrace::sig(int)
  read
  _Py_read
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  PyObject_Call
  _PyEval_EvalFrameDefault
  _PyEval_EvalCodeWithName
  PyEval_EvalCodeEx
  PyEval_EvalCode
  PyRun_StringFlags
  PyRun_SimpleStringFlags
  Py_RunMain
  Py_BytesMain
  __libc_start_main

########## SIG 15 - SIGTERM: a termination request was sent to the program ##########
  sofa::helper::BackTrace::sig(int)
  read
  _Py_read
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  PyObject_Call
  _PyEval_EvalFrameDefault
  _PyEval_EvalCodeWithName
  PyEval_EvalCodeEx
  PyEval_EvalCode
  PyRun_StringFlags
  PyRun_SimpleStringFlags
  Py_RunMain
  Py_BytesMain
  __libc_start_main

########## SIG 15 - SIGTERM: a termination request was sent to the program ##########
  sofa::helper::BackTrace::sig(int)
  read
  _Py_read
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  PyObject_Call
  _PyEval_EvalFrameDefault
  _PyEval_EvalCodeWithName
  PyEval_EvalCodeEx
  PyEval_EvalCode
  PyRun_StringFlags
  PyRun_SimpleStringFlags
  Py_RunMain
  Py_BytesMain
  __libc_start_main

########## SIG 15 - SIGTERM: a termination request was sent to the program ##########
  sofa::helper::BackTrace::sig(int)
  read
  _Py_read
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  PyObject_Call
  _PyEval_EvalFrameDefault
  _PyEval_EvalCodeWithName
  PyEval_EvalCodeEx
  PyEval_EvalCode
  PyRun_StringFlags
  PyRun_SimpleStringFlags
  Py_RunMain
  Py_BytesMain
  __libc_start_main

########## SIG 15 - SIGTERM: a termination request was sent to the program ##########
  sofa::helper::BackTrace::sig(int)
  read
  _Py_read
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  PyObject_Call
  _PyEval_EvalFrameDefault
  _PyEval_EvalCodeWithName
  PyEval_EvalCodeEx
  PyEval_EvalCode
  PyRun_StringFlags
  PyRun_SimpleStringFlags
  Py_RunMain
  Py_BytesMain
  __libc_start_main

########## SIG 15 - SIGTERM: a termination request was sent to the program ##########
  sofa::helper::BackTrace::sig(int)
  read
  _Py_read
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyEval_EvalFrameDefault
  _PyFunction_Vectorcall
  PyObject_Call
  _PyEval_EvalFrameDefault
  _PyEval_EvalCodeWithName
  PyEval_EvalCodeEx
  PyEval_EvalCode
  PyRun_StringFlags
  PyRun_SimpleStringFlags
  Py_RunMain
  Py_BytesMain
  __libc_start_main
Exception ignored in: <function VecVideoRecorder.__del__ at 0x7fe854de9040>
Traceback (most recent call last):
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/vec_video_recorder.py", line 112, in __del__
    self.close()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/vec_video_recorder.py", line 108, in close
    VecEnvWrapper.close(self)
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/base_vec_env.py", line 279, in close
    return self.venv.close()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/vec_monitor.py", line 100, in close
    return self.venv.close()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/subproc_vec_env.py", line 146, in close
    remote.send(("close", None))
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/multiprocessing/connection.py", line 206, in send
    self._send_bytes(_ForkingPickler.dumps(obj))
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/multiprocessing/connection.py", line 411, in _send_bytes
    self._send(header + buf)
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe
[WARNING] [Sofa.Simulation.Graph] the library has not been cleaned up (sofa::simulation::graph::cleanup() has never been called, see sofa/helper/init.h)
[WARNING] [SofaSimulationCommon] the library has not been cleaned up (sofa::simulation::common::cleanup() has never been called, see sofa/helper/init.h)
[WARNING] [SofaSimulationCore] the library has not been cleaned up (sofa::simulation::core::cleanup() has never been called, see sofa/helper/init.h)
[WARNING] [SofaCore] the library has not been cleaned up (sofa::core::cleanup() has never been called, see sofa/helper/init.h)
[WARNING] [SofaDefaultType] the library has not been cleaned up (sofa::defaulttype::cleanup() has never been called, see sofa/helper/init.h)
[WARNING] [SofaHelper] the library has not been cleaned up (sofa::helper::cleanup() has never been called, see sofa/helper/init.h)
ScheiklP commented 7 months ago

That looks like you are still using the old version of stable baselines. Could you send me the output of pip list | grep stable?

If that is for example 1.8, do pip uninstall stable-baselines3 and install sofa_zoo again (pip install -e .)

KantaphatLeelakunwet commented 7 months ago

I have stable-baselines3 2.2.1.

Just now I ran the second and it worked! However, there is another broken pipe error.

[ERROR]   [PythonScript] BrokenPipeError: [Errno 32] Broken pipe
  File "/bd_targaryen/users/kleelakunwet/sofa/sofa_zoo/sofa_zoo/envs/reach/ppo.py", line 88, in <module>
    model.learn(
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/ppo/ppo.py", line 315, in learn
    return super().learn(
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/on_policy_algorithm.py", line 277, in learn
    continue_training = self.collect_rollouts(self.env, callback, self.rollout_buffer, n_rollout_steps=self.n_steps)
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/on_policy_algorithm.py", line 200, in collect_rollouts
    if not callback.on_step():
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/callbacks.py", line 114, in on_step
    return self._on_step()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/callbacks.py", line 219, in _on_step
    continue_training = callback.on_step() and continue_training
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/callbacks.py", line 114, in on_step
    return self._on_step()
  File "/bd_targaryen/users/jhu/sofa/sofa_zoo/sofa_zoo/common/callbacks.py", line 65, in _on_step
    self.model._vec_normalize_env.render()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/base_vec_env.py", line 389, in render
    return self.venv.render(mode=mode)
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/base_vec_env.py", line 389, in render
    return self.venv.render(mode=mode)
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/base_vec_env.py", line 389, in render
    return self.venv.render(mode=mode)
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/base_vec_env.py", line 255, in render
    images = self.get_images()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/subproc_vec_env.py", line 164, in get_images
    pipe.send(("render", None))
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/multiprocessing/connection.py", line 206, in send
    self._send_bytes(_ForkingPickler.dumps(obj))
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/multiprocessing/connection.py", line 411, in _send_bytes
    self._send(header + buf)
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)

I don't know whether this problem is a big deal or not because the video is actually already saved in the folder. Thank you for the help!

ScheiklP commented 7 months ago

I'm not really sure what the actual error is here. Could you run that code with a DummyVecEnv and just one instance of the environment? This way there is no subprocess (and thus no pickling) and we can see if there is an underlying error.

KantaphatLeelakunwet commented 7 months ago

In ~/sofa/sofa_zoo/sofa_zoo/envs/reach/ppo.py, I have added dummy_run=True.

image

Now, I encountered GLError instead.

[ERROR]   [SofaRuntime] GLError: GLError(
    err = 1281,
    description = b'invalid value',
    baseOperation = glViewport,
    cArguments = (0, 0, 64, 64)
)
Traceback (most recent call last):
  File "/bd_targaryen/users/kleelakunwet/sofa/sofa_zoo/sofa_zoo/envs/reach/ppo.py", line 89, in <module>
    model.learn(
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/ppo/ppo.py", line 315, in learn
    return super().learn(
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/on_policy_algorithm.py", line 277, in learn
    continue_training = self.collect_rollouts(self.env, callback, self.rollout_buffer, n_rollout_steps=self.n_steps)
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/on_policy_algorithm.py", line 194, in collect_rollouts
    new_obs, rewards, dones, infos = env.step(clipped_actions)
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/base_vec_env.py", line 206, in step
    return self.step_wait()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/vec_frame_stack.py", line 33, in step_wait
    observations, rewards, dones, infos = self.venv.step_wait()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/vec_normalize.py", line 181, in step_wait
    obs, rewards, dones, infos = self.venv.step_wait()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/vec_video_recorder.py", line 88, in step_wait
    obs, rews, dones, infos = self.venv.step_wait()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/vec_monitor.py", line 76, in step_wait
    obs, rewards, dones, infos = self.venv.step_wait()
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/stable_baselines3/common/vec_env/dummy_vec_env.py", line 58, in step_wait
    obs, self.buf_rews[env_idx], terminated, truncated, self.buf_infos[env_idx] = self.envs[env_idx].step(
  File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/gymnasium/wrappers/time_limit.py", line 57, in step
    observation, reward, terminated, truncated, info = self.env.step(action)
  File "/bd_targaryen/users/jhu/sofa/sofa_env/sofa_env/scenes/reach/reach_env.py", line 227, in step
    maybe_rgb_observation = super().step(action)
  File "/bd_targaryen/users/jhu/sofa/sofa_env/sofa_env/base.py", line 172, in step
    return self._maybe_update_rgb_buffer()
  File "/bd_targaryen/users/jhu/sofa/sofa_env/sofa_env/base.py", line 321, in _update_rgb_buffer
    self._update_sofa_visuals()
  File "/bd_targaryen/users/jhu/sofa/sofa_env/sofa_env/base.py", line 402, in _update_sofa_visuals
    self.opengl_gl.glViewport(0, 0, self._camera_object.widthViewport.value, self._camera_object.heightViewport.value)
  File "src/errorchecker.pyx", line 58, in OpenGL_accelerate.errorchecker._ErrorChecker.glCheckError
ScheiklP commented 7 months ago

Hi @KantaphatLeelakunwet , yeah, errors with multiprocessing are mostly just symptoms of an underlying error that is not correctly reported up to the main process. Please also reduce the number of environments to 1 -> https://github.com/ScheiklP/sofa_zoo/blob/main/sofa_zoo/common/lapgym_experiment_parameters.py#L7

KantaphatLeelakunwet commented 7 months ago

Thanks for the prompt response, I did change it but the error is still the same.

ScheiklP commented 7 months ago

Alright. Could you run pip uninstall PyOpenGL-accelerate and try again?

KantaphatLeelakunwet commented 7 months ago

Unfortunately, it's still the same.

ScheiklP commented 7 months ago

That should not be the exact same error as the accelerate module thows the OpenGL_accelerate error, right?

Does the machine that you are working on have a Nvidia GPU and the correct drivers installed?

KantaphatLeelakunwet commented 7 months ago

I apologize, I wasn't paying attention to the last few lines of the error messages.

The below is the only change.

File "/bd_targaryen/users/jhu/anaconda3/envs/sofa/lib/python3.9/site-packages/OpenGL/error.py", line 230, in glCheckError
    raise self._errorClass(

My school cluster has both Nvidia GPU and drivers.

Thank you once again!

ScheiklP commented 7 months ago

Since it is a cluster, I guess you are doing headless rendering, right? Using pyglet for headless rendering further requires EGL, to create a render context without a physical display. My current best guess is that you cluster does not have EGL installed.

Could you try running a minimal EGL-based example? For example https://pyrender.readthedocs.io/en/latest/examples/offscreen.html with the egl option.

KantaphatLeelakunwet commented 7 months ago

Yes, I'm doing headless rendering.

Currently, I think our cluster may not have EGL installed yet. I might need to contact my Ph.D. senior to install it because I am using his sofa_env, which might gonna take some time.

For the minimal EGL-based example, is it the Google Colab link? It seems that the page is no longer accessible.

Thank you once again for the help!

ScheiklP commented 7 months ago

Ah, damn. Maybe you could try the following (after installing pyrender):

import os
os.environ['PYOPENGL_PLATFORM'] = 'egl'
import pyrender
import numpy as np

r = pyrender.OffscreenRenderer(viewport_width=640,viewport_height=480, point_size=1.0)
scene = pyrender.Scene(ambient_light=[0.02, 0.02, 0.02],bg_color=[1.0, 1.0, 1.0])
cam = pyrender.PerspectiveCamera(yfov=np.pi / 3.0, aspectRatio=1.414)
nc = pyrender.Node(camera=cam, matrix=np.eye(4))
scene.add_node(nc)
color, depth = r.render(scene)
KantaphatLeelakunwet commented 7 months ago

I ran the code, and no error was reported.

FYI, the cluster that I'm using doesn't support viewing images. Whenever I need to view an image, I need to scp the image file to my local computer, and look it from there.

ScheiklP commented 7 months ago

Hmmmm... I am not sure how I can provide any further assistance... The problem seems to be specific for your setup on the cluster. :(

KantaphatLeelakunwet commented 6 months ago

Hmmmm... I am not sure how I can provide any further assistance... The problem seems to be specific for your setup on the cluster. :(

Hi Paul, currently sofa_zoo is working fine for me now (except for the magnetic one due to the extra installation of packages), what I did is change the Ubuntu version to 22.05 instead of 18.05. Hope this helps!

ScheiklP commented 6 months ago

Ah, perfect, thank you!