Minari icon indicating copy to clipboard operation
Minari copied to clipboard

[Bug Report] Serialization failed for BabyAI MissionSpace

Open helenlu66 opened this issue 1 year ago • 5 comments

Describe the bug When creating a BabyAI dataset, I get the following NotImplementedError No serialization method available for MissionSpace(<function BabyAIMissionSpace._gen_mission at 0x7f7a93fcdb90>, None)

Code example

import gymnasium as gym
import minari
from minari import DataCollectorV0

dataset_id = 'BabyAI-GoToLocal-v0'
env = gym.make(dataset_id)
env = DataCollectorV0(env, record_infos=True, max_buffer_steps=100000)

total_episodes = 100

for _ in range(total_episodes):
    env.reset(seed=123)
    while True:
        # random action policy
        action = env.action_space.sample()
        obs, rew, terminated, truncated, info = env.step(action)
        
        if terminated or truncated:
            break

dataset = minari.create_dataset_from_collector_env(dataset_id=dataset_id, 
                                               collector_env=env,
                                               algorithm_name="Random-Policy"
                                               )

System Info Describe the characteristic of your environment:

  • Describe how Gymnasium was installed: pip
  • Operating system: MacOs
  • Python version: 3.7.16

Additional context BabyAI is part of MiniGrid: BabyAI GoToLocal

Checklist

  • [x] I have checked that there is no similar issue in the repo (required)

helenlu66 avatar Jul 20 '23 07:07 helenlu66

Thanks for your report. It looks like minigrid envs have MissionSpace spaces in their observation spaces https://github.com/Farama-Foundation/Minigrid/blob/546c040a9424f339b7ba69e911e4126cee216e64/minigrid/minigrid_env.py#L78. Minari only supports the spaces listed here: https://minari.farama.org/content/dataset_standards/#supported-spaces. The recommended solution would be to use a StepDataCallback like in this tutorial: https://minari.farama.org/tutorials/dataset_creation/observation_space_subseting/ Crucially, the dataset will have to have a different observation space than the actual env.

balisujohn avatar Jul 20 '23 12:07 balisujohn

Also, to use this feature, you will need to use minari 0.4.1 which supports python3.8 and later. If need to use python3.7, then I would recommend creating a wrapper env for the GoToLocal env with a minari-compliant observation space.

balisujohn avatar Jul 20 '23 12:07 balisujohn

Hey @helenlu66 . As @balisujohn mentioned we only support the spaces that are listed in the docs. However, for the Minigrid envrionments you can still store the full observation with a minor hack.

import gymnasium as gym
from gymnasium.spaces.text import alphanumeric, Text
import minari
from minari import DataCollectorV0
import copy

dataset_id = 'BabyAI-GoToLocal-v0'
env = gym.make(dataset_id)
dataset_observation_space = copy.deepcopy(env.observation_space)

# include all alphanumeric characters plus space
charset = " "
for character in alphanumeric:
    charset += character

dataset_observation_space["mission"] = Text(max_length=200, charset=charset)
env = DataCollectorV0(env, record_infos=True, max_buffer_steps=100000, observation_space=dataset_observation_space)

total_episodes = 100

for _ in range(total_episodes):
    env.reset(seed=123)
    while True:
        # random action policy
        action = env.action_space.sample()
        obs, rew, terminated, truncated, info = env.step(action)
        
        if terminated or truncated:
            break

dataset = minari.create_dataset_from_collector_env(dataset_id=dataset_id, 
                                               collector_env=env,
                                               algorithm_name="Random-Policy"
                                               )

The differences from the code that you shared is that I substitute the MissionSpace in the observation space for a Gymnasium Text space. This will be your new dataset_observation_space which you can set in the DataCollectorV0.

The reason why this works is because MissionSpace is basically bounding a Text space to contain viable missions of the current minigrid environment so that you can sample valid observations from it. A text space should contain all of the possible mission values.

And also, as @balisujohn already said I strongly suggest updating to python >=3.8 and using the latest minari release 0.4.1

rodrigodelazcano avatar Jul 20 '23 13:07 rodrigodelazcano

@balisujohn @younik this could be a cool use case for custom dataset spaces to include in the tutorials. What do you think?

rodrigodelazcano avatar Jul 20 '23 14:07 rodrigodelazcano

@rodrigodelazcano I don't think it would hurt to add a tutorial for using StepDataCallback with this environment. I think it would also be good to make this error message No serialization method available for space) explicitly say "Minari does not support this space"

balisujohn avatar Jul 20 '23 14:07 balisujohn