HumanCompatibleAI / imitation

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

TypeError: argument of type 'PosixPath' is not iterable #852

Open den-schmidt opened 3 months ago

den-schmidt commented 3 months ago

Bug description

Hi everyone,

I am trying to play with the DAgger example from docs/tutorials/2_train_dagger.ipynb but get the following error when executing the line dagger_trainer.train(2000):

Exception has occurred: TypeError       (note: full exception trace is shown but execution is paused at: _run_module_as_main)
argument of type 'PosixPath' is not iterable
  File "/home/denni/msc/imitation/.venv/lib/python3.8/site-packages/fsspec/core.py", line 323, in _un_chain
    if "::" in path
  File "/home/denni/msc/imitation/.venv/lib/python3.8/site-packages/fsspec/core.py", line 383, in url_to_fs
    chain = _un_chain(url, kwargs)
  File "/home/denni/msc/imitation/.venv/lib/python3.8/site-packages/datasets/arrow_dataset.py", line 1515, in save_to_disk
    fs, _ = url_to_fs(dataset_path, **(storage_options or {}))
  File "/home/denni/msc/imitation/.venv/lib/python3.8/site-packages/imitation/data/serialize.py", line 23, in save
    huggingface_utils.trajectories_to_dataset(trajectories).save_to_disk(p)
  File "/home/denni/msc/imitation/.venv/lib/python3.8/site-packages/imitation/algorithms/dagger.py", line 147, in _save_dagger_demo
    serialize.save(npz_path, [trajectory])
  File "/home/denni/msc/imitation/.venv/lib/python3.8/site-packages/imitation/algorithms/dagger.py", line 285, in step_wait
    _save_dagger_demo(traj, traj_index, self.save_dir, self.rng)
  File "/home/denni/msc/imitation/.venv/lib/python3.8/site-packages/stable_baselines3/common/vec_env/base_vec_env.py", line 206, in step
    return self.step_wait()
  File "/home/denni/msc/imitation/.venv/lib/python3.8/site-packages/imitation/data/rollout.py", line 447, in generate_trajectories
    obs, rews, dones, infos = venv.step(acts)
  File "/home/denni/msc/imitation/.venv/lib/python3.8/site-packages/imitation/algorithms/dagger.py", line 669, in train
    trajectories = rollout.generate_trajectories(
  File "/home/denni/msc/imitation/test.py", line 41, in <module>
    dagger_trainer.train(2000)
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main (Current frame)
    return _run_code(code, main_globals, None,
TypeError: argument of type 'PosixPath' is not iterable

Steps to reproduce

I would be really thankful if someone could try out the steps to see if that is only my issue. To reproduce the bug, please try out the file docs/tutorials/2_train_dagger.ipynb as I did on my personal machine as well as in Google Colab.

Environment

eufrizz commented 2 months ago

I get the same. Here is the full stack trace:

{
    "name": "TypeError",
    "message": "argument of type 'PosixPath' is not iterable",
    "stack": "---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[106], line 11
      2 print(tmpdir)
      3 dagger_trainer = SimpleDAggerTrainer(
      4     venv=env,
      5     scratch_dir=tmpdir,
   (...)
      8     rng=np.random.default_rng(),
      9 )
---> 11 dagger_trainer.train(2000)

File ~/miniconda3/envs/lerobot/lib/python3.10/site-packages/imitation/algorithms/dagger.py:669, in SimpleDAggerTrainer.train(self, total_timesteps, rollout_round_min_episodes, rollout_round_min_timesteps, bc_train_kwargs)
    662 round_timestep_count = 0
    664 sample_until = rollout.make_sample_until(
    665     min_timesteps=max(rollout_round_min_timesteps, self.batch_size),
    666     min_episodes=rollout_round_min_episodes,
    667 )
--> 669 trajectories = rollout.generate_trajectories(
    670     policy=self.expert_policy,
    671     venv=collector,
    672     sample_until=sample_until,
    673     deterministic_policy=True,
    674     rng=collector.rng,
    675 )
    677 for traj in trajectories:
    678     self._logger.record_mean(
    679         \"dagger/mean_episode_reward\",
    680         np.sum(traj.rews),
    681     )

File ~/miniconda3/envs/lerobot/lib/python3.10/site-packages/imitation/data/rollout.py:447, in generate_trajectories(policy, venv, sample_until, rng, deterministic_policy)
    444 while np.any(active):
    445     # policy gets unwrapped observations (eg as dict, not dictobs)
    446     acts, state = get_actions(obs, state, dones)
--> 447     obs, rews, dones, infos = venv.step(acts)
    448     assert isinstance(
    449         obs,
    450         (np.ndarray, dict),
    451     ), \"Tuple observations are not supported.\"
    452     wrapped_obs = types.maybe_wrap_in_dictobs(obs)

File ~/miniconda3/envs/lerobot/lib/python3.10/site-packages/stable_baselines3/common/vec_env/base_vec_env.py:206, in VecEnv.step(self, actions)
    199 \"\"\"
    200 Step the environments with the given action
    201 
    202 :param actions: the action
    203 :return: observation, reward, done, information
    204 \"\"\"
    205 self.step_async(actions)
--> 206 return self.step_wait()

File ~/miniconda3/envs/lerobot/lib/python3.10/site-packages/imitation/algorithms/dagger.py:285, in InteractiveTrajectoryCollector.step_wait(self)
    277 fresh_demos = self.traj_accum.add_steps_and_auto_finish(
    278     obs=next_obs,
    279     acts=self._last_user_actions,
   (...)
    282     dones=dones,
    283 )
    284 for traj_index, traj in enumerate(fresh_demos):
--> 285     _save_dagger_demo(traj, traj_index, self.save_dir, self.rng)
    287 return next_obs, rews, dones, infos

File ~/miniconda3/envs/lerobot/lib/python3.10/site-packages/imitation/algorithms/dagger.py:147, in _save_dagger_demo(trajectory, trajectory_index, save_dir, rng, prefix)
    143 npz_path = save_dir / filename
    144 assert (
    145     not npz_path.exists()
    146 ), \"The following DAgger demonstration path already exists: {0}\".format(npz_path)
--> 147 serialize.save(npz_path, [trajectory])
    148 logging.info(f\"Saved demo at '{npz_path}'\")

File ~/miniconda3/envs/lerobot/lib/python3.10/site-packages/imitation/data/serialize.py:23, in save(path, trajectories)
     16 \"\"\"Save a sequence of Trajectories to disk using HuggingFace's datasets library.
     17 
     18 Args:
     19     path: Trajectories are saved to this path.
     20     trajectories: The trajectories to save.
     21 \"\"\"
     22 p = util.parse_path(path)
---> 23 huggingface_utils.trajectories_to_dataset(trajectories).save_to_disk(p)
     24 logging.info(f\"Dumped demonstrations to {p}.\")

File ~/miniconda3/envs/lerobot/lib/python3.10/site-packages/datasets/arrow_dataset.py:1515, in Dataset.save_to_disk(self, dataset_path, fs, max_shard_size, num_shards, num_proc, storage_options)
   1512 num_shards = num_shards if num_shards is not None else num_proc
   1514 fs: fsspec.AbstractFileSystem
-> 1515 fs, _ = url_to_fs(dataset_path, **(storage_options or {}))
   1517 if not is_remote_filesystem(fs):
   1518     parent_cache_files_paths = {
   1519         Path(cache_filename[\"filename\"]).resolve().parent for cache_filename in self.cache_files
   1520     }

File ~/miniconda3/envs/lerobot/lib/python3.10/site-packages/fsspec/core.py:383, in url_to_fs(url, **kwargs)
    372 known_kwargs = {
    373     \"compression\",
    374     \"encoding\",
   (...)
    380     \"num\",
    381 }
    382 kwargs = {k: v for k, v in kwargs.items() if k not in known_kwargs}
--> 383 chain = _un_chain(url, kwargs)
    384 inkwargs = {}
    385 # Reverse iterate the chain, creating a nested target_* structure

File ~/miniconda3/envs/lerobot/lib/python3.10/site-packages/fsspec/core.py:323, in _un_chain(path, kwargs)
    319 def _un_chain(path, kwargs):
    320     x = re.compile(\".*[^a-z]+.*\")  # test for non protocol-like single word
    321     bits = (
    322         [p if \"://\" in p or x.match(p) else p + \"://\" for p in path.split(\"::\")]
--> 323         if \"::\" in path
    324         else [path]
    325     )
    326     # [[url, protocol, kwargs], ...]
    327     out = []

TypeError: argument of type 'PosixPath' is not iterable"
}