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

Support both gym and gymnasium, and retain dict obs in wrapper #497

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

youliangtan
Copy link

Summary

The previously provided GymWrapper() only supports the new gymnasium environment. Since there are still many projects that use the classic gym interface, this PR opens up the option for users to use the OpenAI gym interface with robosuite, along with some minor changes.

  • The gymnasium and gym interfaces are the same. A runtime check will ensure gym>=0.26.0 (which uses terminate and truncate).
  • The previous implementation flattens all return observations to a single float array. Users can choose to specify GymWrapper(..., flatten_obs=False) to retain the dictionary structure in the observation.
  • Ensured that the .observation_space specifies the correct dtype and shape.

Quick run

import robosuite as suite
from robosuite.wrappers.gym_wrapper import GymWrapper
import cv2

if __name__ == '__main__':
    # create environment instance
    env = suite.make(
        env_name="Lift",  # try with other tasks like "Stack" and "Door"
        robots="Panda",   # try with other robots like "Sawyer" and "Jaco"
        has_renderer=True,
        has_offscreen_renderer=True,
        use_camera_obs=True,
    )

    # convert to gym/gymnasium environment
    env = GymWrapper(
        env,
        keys=["robot0_proprio-state", "object-state", "agentview_image"],
        flatten_obs=False, # default is True, we will not flatten the observation
    )
    print(" -> action space:", env.action_space)
    print(" -> observation space:", env.observation_space)
    obs, info = env.reset()

    for i in range(1000):
        # action = np.random.randn(env.robots[0].dof)  # sample random action
        action = env.action_space.sample()
        obs, reward, done, trunc, info = env.step(action)  # take action in the environment
        print(obs.keys(), obs["agentview_image"].shape)

        # Optional camera view viz
        cv2.imshow("Agent View", cv2.cvtColor(obs["agentview_image"], cv2.COLOR_RGB2BGR))
        if cv2.waitKey(1) & 0xFF == ord('q'): break

        env.render()  # render on display

Signed-off-by: youliang <tan_you_liang@hotmail.com>
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 this pull request may close these issues.

1 participant