HumanCompatibleAI / imitation

Clean PyTorch implementations of imitation and reward learning algorithms
https://imitation.readthedocs.io/
MIT License
1.26k stars 239 forks source link

Example notebook "1_train_bc.ipynb" gives "Namespace not found error" for "seals" module #816

Closed Rajesh-Siraskar closed 9 months ago

Rajesh-Siraskar commented 10 months ago

Bug description

Example notebook 1_train_bc.ipynb gives Namespace not found error for seals module

Versions: Python: 3.9.1, imitation: 0.4.0, seals: 0.2.1, gymnasium: 0.29.1

Steps to reproduce

Fails to run env = make_vec_env() code in the first cell and produces error NamespaceNotFound: Namespace seals:seals not found. Have you installed the proper package for seals:seals?

Code where error occurs:

env = make_vec_env(
    "seals:seals/CartPole-v0",
    rng=np.random.default_rng(),
    post_wrappers=[
        lambda env, _: RolloutInfoWrapper(env)
    ],  # needed for computing rollouts later
)

Error output:

---------------------------------------------------------------------------
NamespaceNotFound                         Traceback (most recent call last)
Cell In[1], line 7
      4 from imitation.util.util import make_vec_env
      5 from imitation.data.wrappers import RolloutInfoWrapper
----> 7 env = make_vec_env(
      8     "seals:seals/CartPole-v0",
      9     rng=np.random.default_rng(),
     10     post_wrappers=[
     11         lambda env, _: RolloutInfoWrapper(env)
     12     ],  # needed for computing rollouts later
     13 )
     14 expert = load_policy(
     15     "ppo-huggingface",
     16     organization="HumanCompatibleAI",
     17     env_name="seals/CartPole-v0",
     18     venv=env,
     19 )

File c:\users\rajeshs\appdata\local\programs\python\python39\lib\site-packages\imitation\util\util.py:107, in make_vec_env(env_name, rng, n_envs, parallel, log_dir, max_episode_steps, post_wrappers, env_make_kwargs)
     82 """Makes a vectorized environment.
     83 
     84 Args:
   (...)
    103     A VecEnv initialized with `n_envs` environments.
    104 """
    105 # Resolve the spec outside of the subprocess first, so that it is available to
    106 # subprocesses running `make_env` via automatic pickling.
--> 107 spec = gym.spec(env_name)
    108 env_make_kwargs = env_make_kwargs or {}
    110 def make_env(i: int, this_seed: int) -> gym.Env:
    111     # Previously, we directly called `gym.make(env_name)`, but running
    112     # `imitation.scripts.train_adversarial` within `imitation.scripts.parallel`
   (...)
    117     # work. For more discussion and hypotheses on this issue see PR #160:
    118     # https://github.com/HumanCompatibleAI/imitation/pull/160.

File c:\users\rajeshs\appdata\local\programs\python\python39\lib\site-packages\gym\envs\registration.py:699, in spec(env_id)
    697 if spec_ is None:
    698     ns, name, version = parse_env_id(env_id)
--> 699     _check_version_exists(ns, name, version)
    700     raise error.Error(f"No registered env with id: {env_id}")
    701 else:

File c:\users\rajeshs\appdata\local\programs\python\python39\lib\site-packages\gym\envs\registration.py:219, in _check_version_exists(ns, name, version)
    216 if get_env_id(ns, name, version) in registry:
    217     return
--> 219 _check_name_exists(ns, name)
    220 if version is None:
    221     return

File c:\users\rajeshs\appdata\local\programs\python\python39\lib\site-packages\gym\envs\registration.py:187, in _check_name_exists(ns, name)
    185 def _check_name_exists(ns: Optional[str], name: str):
    186     """Check if an env exists in a namespace. If it doesn't, print a helpful error message."""
--> 187     _check_namespace_exists(ns)
    188     names = {spec_.name for spec_ in registry.values() if spec_.namespace == ns}
    190     if name in names:

File c:\users\rajeshs\appdata\local\programs\python\python39\lib\site-packages\gym\envs\registration.py:182, in _check_namespace_exists(ns)
    173 suggestion = (
    174     difflib.get_close_matches(ns, namespaces, n=1) if len(namespaces) > 0 else None
    175 )
    176 suggestion_msg = (
    177     f"Did you mean: `{suggestion[0]}`?"
    178     if suggestion
    179     else f"Have you installed the proper package for {ns}?"
    180 )
--> 182 raise error.NamespaceNotFound(f"Namespace {ns} not found. {suggestion_msg}")

NamespaceNotFound: Namespace seals:seals not found. Have you installed the proper package for seals:seals?

Environment

Rajesh-Siraskar commented 9 months ago

Hi - Just wanted to mention that in my issue - I do use gymnasium: 0.29.1, and not gym.

I noticed that a similar Issue was closed today -- Examples Don't work #815 - mentioning that imitation does not support gym. It is interesting then that I get the same issue, when in fact I use gymnasium.

Code snippet:

import numpy as np
import gymnasium as gym
from imitation.policies.serialize import load_policy
from imitation.util.util import make_vec_env
from imitation.data.wrappers import RolloutInfoWrapper

env = make_vec_env(
    "seals:seals/CartPole-v0",
    rng=np.random.default_rng(),
    post_wrappers=[
        lambda env, _: RolloutInfoWrapper(env)
    ],  # needed for computing rollouts later
)
expert = load_policy(
    "ppo-huggingface",
    organization="HumanCompatibleAI",
    env_name="seals/CartPole-v0",
    venv=env,
)
ernestum commented 9 months ago

I could not reproduce this in imitation==1.0.0. Feel free to reopen if it persists for you.

Rajesh-Siraskar commented 9 months ago

Thanks @ernestum - worked like a charm with imitation==1.0.0!

I tested 3 tutorials and they all work flawlessly now: 1_train_bc.ipynb 2_train_dagger.ipynb 3_train_gail.ipynb

Thanks!