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

ValueError: Output array has wrong dimensionality #119

Closed
Yefanov opened this issue Jul 28, 2023 · 2 comments · Fixed by #76
Closed

ValueError: Output array has wrong dimensionality #119

Yefanov opened this issue Jul 28, 2023 · 2 comments · Fixed by #76
Assignees

Comments

@Yefanov
Copy link
Contributor

Yefanov commented Jul 28, 2023

Hello, I have encountered a problem running vector environments for TurnFaucet-v0 and ms1 tasks, even with sparse reward.

python -m mani_skill2.examples.demo_vec_env -e TurnFaucet-v0 --reward-mode sparse
2023-07-28 15:02:12,107 - mani_skill2 - INFO - Monkey patching gym.spaces.Box.__repr__
opts: []
env_kwargs: {}
[2023-07-28 15:02:13.568] [svulkan2] [error] GLFW error: X11: The DISPLAY environment variable is missing
[2023-07-28 15:02:13.568] [svulkan2] [warning] Continue without GLFW.
2023-07-28 15:02:15,828 - mani_skill2 - INFO - RenderServer is running at: localhost:43115
env VecEnv(<TimeLimit<TurnFaucetEnv<TurnFaucet-v0>>>)
Observation space Dict(agent:Dict(qpos:Box(-inf, inf, (9,), float32), qvel:Box(-inf, inf, (9,), float32), base_pose:Box(-inf, inf, (7,), float32)), extra:Dict(tcp_pose:Box(-inf, inf, (7,), float32), target_angle_diff:Box(-inf, inf, (1,), float32), target_joint_axis:Box(-inf, inf, (3,), float32), target_link_pos:Box(-inf, inf, (3,), float32)), camera_param:Dict(base_camera:Dict(extrinsic_cv:Box(-inf, inf, (4, 4), float32), cam2world_gl:Box(-inf, inf, (4, 4), float32), intrinsic_cv:Box(-inf, inf, (3, 3), float32)), hand_camera:Dict(extrinsic_cv:Box(-inf, inf, (4, 4), float32), cam2world_gl:Box(-inf, inf, (4, 4), float32), intrinsic_cv:Box(-inf, inf, (3, 3), float32))), image:Dict(base_camera:Dict(Color:Box(0.0, 1.0, (128, 128, 4), float32), Position:Box(-inf, inf, (128, 128, 4), float32)), hand_camera:Dict(Color:Box(0.0, 1.0, (128, 128, 4), float32), Position:Box(-inf, inf, (128, 128, 4), float32))))
Action space Box(-1.0, 1.0, (8,), float32)
Episode 0
Traceback (most recent call last):
  File "/opt/conda/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/conda/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/opt/conda/lib/python3.9/site-packages/mani_skill2/examples/demo_vec_env.py", line 111, in <module>
    main()
  File "/opt/conda/lib/python3.9/site-packages/mani_skill2/examples/demo_vec_env.py", line 62, in main
    obs = env.reset()
  File "/opt/conda/lib/python3.9/site-packages/mani_skill2/vector/vec_env.py", line 279, in reset
    return self.reset_wait(indices=indices)
  File "/opt/conda/lib/python3.9/site-packages/mani_skill2/vector/vec_env.py", line 273, in reset_wait
    self._update_np_buffer(results, indices)
  File "/opt/conda/lib/python3.9/site-packages/mani_skill2/vector/vec_env.py", line 226, in _update_np_buffer
    return stack_obs(
  File "/opt/conda/lib/python3.9/site-packages/mani_skill2/vector/vec_env.py", line 425, in stack_obs
    ret[key] = stack_obs(_obs, space[key], buffer=_buffer)
  File "/opt/conda/lib/python3.9/site-packages/mani_skill2/vector/vec_env.py", line 425, in stack_obs
    ret[key] = stack_obs(_obs, space[key], buffer=_buffer)
  File "/opt/conda/lib/python3.9/site-packages/mani_skill2/vector/vec_env.py", line 428, in stack_obs
    return np.stack(obs, out=buffer)
  File "<__array_function__ internals>", line 180, in stack
  File "/opt/conda/lib/python3.9/site-packages/numpy/core/shape_base.py", line 433, in stack
    return _nx.concatenate(expanded_arrays, axis=axis, out=out)
  File "<__array_function__ internals>", line 180, in concatenate
ValueError: Output array has wrong dimensionality

The same error replicates in the RL Colab after running this cell (modified only env_id and reward_mode):

from mani_skill2.vector import VecEnv, make as make_vec_env
num_envs = 2 # recommended value for Google Colab. If you have more cores and a more powerful GPU you can increase this
env: VecEnv = make_vec_env(
    "TurnFaucet-v0",
    num_envs,
    obs_mode=obs_mode,
    reward_mode="sparse",
    control_mode=control_mode,
)
env.seed(0)
obs = env.reset()
print("Base Camera RGB:", obs['image']['base_camera']['rgb'].shape)
print("Base Camera RGB device:", obs['image']['base_camera']['rgb'].device)
env.close()
2023-07-28 14:46:18,376 - mani_skill2 - INFO - RenderServer is running at: localhost:51711

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

[<ipython-input-18-6567b89c585b>](https://localhost:8080/#) in <cell line: 11>()
      9 )
     10 env.seed(0)
---> 11 obs = env.reset()
     12 print("Base Camera RGB:", obs['image']['base_camera']['rgb'].shape)
     13 print("Base Camera RGB device:", obs['image']['base_camera']['rgb'].device)

8 frames

[/usr/local/lib/python3.10/dist-packages/mani_skill2/vector/vec_env.py](https://localhost:8080/#) in reset(self, indices)
    277     def reset(self, indices=None):
    278         self.reset_async(indices=indices)
--> 279         return self.reset_wait(indices=indices)
    280 
    281     def step_async(self, actions: np.ndarray) -> None:

[/usr/local/lib/python3.10/dist-packages/mani_skill2/vector/vec_env.py](https://localhost:8080/#) in reset_wait(self, indices)
    271         self.waiting = False
    272         vec_obs = self._get_torch_observations()
--> 273         self._update_np_buffer(results, indices)
    274         vec_obs.update(deepcopy(self._obs_np_buffer))
    275         return vec_obs

[/usr/local/lib/python3.10/dist-packages/mani_skill2/vector/vec_env.py](https://localhost:8080/#) in _update_np_buffer(self, obs_list, indices)
    224         for i, obs in zip(indices, obs_list):
    225             self._last_obs_np[i] = obs
--> 226         return stack_obs(
    227             self._last_obs_np, self.non_image_obs_space, self._obs_np_buffer
    228         )

[/usr/local/lib/python3.10/dist-packages/mani_skill2/vector/vec_env.py](https://localhost:8080/#) in stack_obs(obs, space, buffer)
    423             _obs = [o[key] for o in obs]
    424             _buffer = None if buffer is None else buffer[key]
--> 425             ret[key] = stack_obs(_obs, space[key], buffer=_buffer)
    426         return ret
    427     elif isinstance(space, spaces.Box):

[/usr/local/lib/python3.10/dist-packages/mani_skill2/vector/vec_env.py](https://localhost:8080/#) in stack_obs(obs, space, buffer)
    423             _obs = [o[key] for o in obs]
    424             _buffer = None if buffer is None else buffer[key]
--> 425             ret[key] = stack_obs(_obs, space[key], buffer=_buffer)
    426         return ret
    427     elif isinstance(space, spaces.Box):

[/usr/local/lib/python3.10/dist-packages/mani_skill2/vector/vec_env.py](https://localhost:8080/#) in stack_obs(obs, space, buffer)
    426         return ret
    427     elif isinstance(space, spaces.Box):
--> 428         return np.stack(obs, out=buffer)
    429     else:
    430         raise NotImplementedError(type(space))

/usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in stack(*args, **kwargs)

[/usr/local/lib/python3.10/dist-packages/numpy/core/shape_base.py](https://localhost:8080/#) in stack(arrays, axis, out)
    431     sl = (slice(None),) * axis + (_nx.newaxis,)
    432     expanded_arrays = [arr[sl] for arr in arrays]
--> 433     return _nx.concatenate(expanded_arrays, axis=axis, out=out)
    434 
    435 

/usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in concatenate(*args, **kwargs)

ValueError: Output array has wrong dimensionality
@Yefanov
Copy link
Contributor Author

Yefanov commented Jul 31, 2023

Issue arises when the observation space contains a 0-dimensional parameter like target_angle_diff. In this case, this parameter is represented as either a 0-dimensional numpy array or just a float number. However, the stack_obs function expects a 1-dimensional array with shape (1,). A quick fix can be applied to the mani_skill2/vector/vec_env.py file by adding two lines:

def stack_obs(obs: Sequence, space: spaces.Space, buffer: Optional[np.ndarray] = None):
    if isinstance(space, spaces.Dict):
        ret = {}
        for key in space:
            _obs = [o[key] for o in obs]
            _buffer = None if buffer is None else buffer[key]
            ret[key] = stack_obs(_obs, space[key], buffer=_buffer)
        return ret
    elif isinstance(space, spaces.Box):
        if len(np.array(obs[0]).shape) == 0: # check shape
            obs = [np.array(o)[None] for o in obs] # add dimension
        return np.stack(obs, out=buffer)
    else:
        raise NotImplementedError(type(space))

Yefanov added a commit to Yefanov/mani-skill2 that referenced this issue Jul 31, 2023
Yefanov added a commit to Yefanov/mani-skill2 that referenced this issue Jul 31, 2023
Yefanov added a commit to Yefanov/mani-skill2 that referenced this issue Jul 31, 2023
@Yefanov Yefanov mentioned this issue Jul 31, 2023
@xuanlinli17
Copy link
Collaborator

@StoneT2000

@StoneT2000 StoneT2000 self-assigned this Jul 31, 2023
StoneT2000 pushed a commit that referenced this issue Jul 31, 2023
* [BC] Add base_pose and  tcp_pose in MS1 envs' observations

* Fix softbody installation instructions in installation.md (#99)

* Fix softbody installation instructions in installation.md

* Fix build_lib import issue

* Update the doc

---------

Co-authored-by: Jiayuan-Gu <[email protected]>

* Update gh-pages.yml

The issue is caused by `gym` and `wheel`. Follow the solution in HumanCompatibleAI/imitation#707.

* fix #119 issue

---------

Co-authored-by: Jiayuan-Gu <[email protected]>
Co-authored-by: Xuanlin (Simon) Li <[email protected]>
Co-authored-by: Jiayuan Gu <[email protected]>
@Yefanov Yefanov closed this as completed Aug 1, 2023
@StoneT2000 StoneT2000 mentioned this issue Aug 4, 2023
9 tasks
StoneT2000 added a commit that referenced this issue Aug 16, 2023
* init

* init

* update reset funcs

* render api

* Update record.py

* update vec env to gymnasium vec env implementation and updated sb3 wrapper for it

* formatting

* use single observation space per new API

* tune rgbd baseline

* fix some bugs with envs closing incorrectly

* bug with terminated signal

* Update record.py

* Fix for gymnasium migration

- workaround for `env.render`
- remove `make_box_space_readable`
- fix `VecRobotSegmentationObservationWrapper`

* python local matrix test with docker

* Update run.sh

* Update run.sh

* Modify PandaAvoidObstacles reward to clip the collision penalty

* add normalized dense reward mode

* fix original venv tests

* fix robot_seg obs wrappers

* fix docs using old reset API

* more tests and init contributing doc and pytest-xdist

* Update run.sh

* coverage tests

* wrappers tests

* fix bug with recordepisode wrapper used on robot-seg obs modes

* more tests

* control modes tests

* code formatting

* ms2 venv tests

* default reward is normalized, tuned sb3 state liftcube hyperparams

* match vec make env closer to gym make and finish sb3 rgbd test

* remove outdated gym code

* update rl tutorial

* update quickstart.md to factor in gymnasium change

* Created using Colaboratory

* update python min req to 3.8 (per gymnasium support)

* update replay_Trajectory and fix bc code

* Update bc_liftcube_state.py

* modify evaluation/evaluator.py to account for gymnasium api

* Update observation.py

* modify docs/source/concepts/observation.md to account for gymnasium api

* remove render_mode in mpm/base_env.py

* fix evaluator renderer

* Created using Colaboratory

* Created using Colaboratory

* remove a print statement

* fix some bugs

* fix order tracking bug with dictionary observations

* better tuning, fix bug with record.py where obs group created twice

* fixes

* Update CONTRIBUTING.md

* Update evaluator.py to remove print statement

* Update test_venv.py

* use options now

* fix some bugs with options being None

* Update test_wrappers.py

* Update test_venv.py

* fix #119 issue (#123)

* [BC] Add base_pose and  tcp_pose in MS1 envs' observations

* Fix softbody installation instructions in installation.md (#99)

* Fix softbody installation instructions in installation.md

* Fix build_lib import issue

* Update the doc

---------

Co-authored-by: Jiayuan-Gu <[email protected]>

* Update gh-pages.yml

The issue is caused by `gym` and `wheel`. Follow the solution in HumanCompatibleAI/imitation#707.

* fix #119 issue

---------

Co-authored-by: Jiayuan-Gu <[email protected]>
Co-authored-by: Xuanlin (Simon) Li <[email protected]>
Co-authored-by: Jiayuan Gu <[email protected]>

* Update vec_env.py

* Update test_venv.py

* update vec env demo to test both MS2 and Gymnasium vec envs

* nicer demo code

* Update README.md

* hide ms2 challenge

* Update quickstart.md

* update venv tests to also compare gym observations and enable gym make_vec_env to work

* Update test_venv.py

* tests

* add render_mode=human upon env creation in replay_trajectory.py

* adapt replay_trajectory.py with the new env.reset(seed, options) api

* fix PegInsertionSide reset function

* fix environment reset: always pop out options specific to an environment

* clean up errors when env fails on start and add some types

* use fixed model scales and ensure obs use np array floats instead of python floats for gymnasium

* Fix TurnFaucet-v0 mesh sampling to be deterministic with episode seed, so reward function is deterministic as well.

* add demo and asset download tests, and example code tests

* add render_cameras option, fix replay_trajectory to show viewer iff --vis is true,  fix goal visual to now show up for rgb_array and human

* basic replay trajectory tests and allow to replay up to a max of episodes

* fix goal visuals for PandaAvoidObstacles-v0

* fix goal visuals for PickClutterYCB-v0

* explicitly specify the  max_episode_steps keyword argument in make_vec_env

* fix bug where max_episode_steps wasnt processed in make_vec_env

* versioned demo download from HF

* fix missing arg

* Fix bug where main rng was not set on reset(seed=...) calls

* remove old test and minor fix to sb3 vector wrapper where repeated calls to reset used same seed.

* enable back env check

* improve comment and fix test name to be different from file name.

* black formatting.

* isort

* Update registration.py

---------

Co-authored-by: Jiayuan-Gu <[email protected]>
Co-authored-by: Xuanlin (Simon) Li <[email protected]>
Co-authored-by: xuanlinli17 <[email protected]>
Co-authored-by: Yefanov <[email protected]>
Co-authored-by: Jiayuan Gu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants