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

JPEG encoding and decoding if the observation is an image #275

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

gabrielemaraglino
Copy link

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue), Depends on # (pull request)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Screenshots

Please attach before and after screenshots of the change if applicable.
To upload images to a PR -- simply drag and drop or copy paste.

Checklist:

  • I have run the pre-commit checks with pre-commit run --all-files (see CONTRIBUTING.md instructions to set it up)
  • I have run pytest -v and no errors are present.
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I solved any possible warnings that pytest -v has generated that are related to my code to the best of my knowledge.
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Member

@younik younik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this feature!

Can you add the line .idea in the .gitignore file?
Also, can you add some tests? You can make a fake env that generates images as obs, and check that everything is okay (serialization works and deserialization returns the same obs). Check the dataset_creation tests for the examples

@@ -157,4 +157,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm, maybe you should keep the '/'
The point of this is that the idea files in the code diff should disappear

Comment on lines +13 to +28
class TestEnv:
def __init__(self):
self.observation_space = spaces.Box(low=0, high=255, shape=(4, 84, 84), dtype=np.uint8)
self.action_space = spaces.Discrete(2)

def reset(self):
obs = np.random.randint(0, 256, (4, 84, 84), dtype=np.uint8)
return obs, {}

def step(self, action):
obs = np.random.randint(0, 256, (4, 84, 84), dtype=np.uint8)
reward = 1.0
terminated = False
truncated = False
info = {}
return obs, reward, terminated, truncated, info
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in common.py in tests

And I would call it something like "ImageObsEnv"

Comment on lines +30 to +45
def generate_episode_buffer_from_env(env: TestEnv, length=3) -> EpisodeBuffer:
initial_obs , _ = env.reset()
buffer = EpisodeBuffer(observations=initial_obs)
for i in range(length):
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action)
step_data = {
"observation": obs,
"action": action,
"reward": reward,
"terminated": terminated,
"truncated": truncated,
"info": info
}
buffer = buffer.add_step_data(step_data)
return buffer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is already present in common.py, just use that one

Comment on lines +47 to +93
def test_arrow_storage_serialization():
env = TestEnv()
episode = generate_episode_buffer_from_env(env, length=3)
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = pathlib.Path(tmpdir)

METADATA_FILE_NAME = "metadata.json"
default_metadata = {
"total_steps": 0,
"total_episodes": 0,
"data_format": "arrow",
"observation_space": str(env.observation_space),
"action_space": str(env.action_space)
}
metadata_path = tmp_path.joinpath(METADATA_FILE_NAME)
metadata_path.write_text(json.dumps(default_metadata))

from minari.dataset._storages.arrow_storage import ArrowStorage
storage = ArrowStorage(tmp_path, env.observation_space, env.action_space)
storage.update_episodes([episode])
loaded_episode = list(storage.get_episodes([0]))
loaded_obs = loaded_episode[0]["observations"]
np.testing.assert_array_equal(episode.observations, loaded_obs)

def test_hdf5_serialization():
env = TestEnv()
episode = generate_episode_buffer_from_env(env, length=3)
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = pathlib.Path(tmpdir)

METADATA_FILE_NAME = "metadata.json"
default_metadata = {
"total_steps": 0,
"total_episodes": 0,
"data_format": "hdf5",
"observation_space": str(env.observation_space),
"action_space": str(env.action_space)
}
metadata_path = tmp_path.joinpath(METADATA_FILE_NAME)
metadata_path.write_text(json.dumps(default_metadata))

from minari.dataset._storages.hdf5_storage import HDF5Storage
storage = HDF5Storage._create(tmp_path, env.observation_space, env.action_space)
storage.update_episodes([episode])
loaded_episodes = list(storage.get_episodes([0]))
loaded_obs = loaded_episodes[0]["observations"]
np.testing.assert_array_equal(episode.observations, loaded_obs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests can be merged in a single function, and use pytest.mark.parametrize to test both storages.

Also, in the end, I believe that if you add your test env in the list of envs as mentioned above, you can rely on previous written tests that should already check equality, and this file can be removed

values = np.pad(values, ((0, pad), (0, 0)))
dtype = pa.list_(pa.from_numpy_dtype(space.dtype), list_size=values.shape[1])
return pa.FixedSizeListArray.from_arrays(values.reshape(-1), type=dtype)
if values.shape == (4, 84, 84) and values.dtype == np.uint8: # check for image observation (4 stacked greyscale images)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not constrain ourselves with 84 x 84 images, but we should accept any size.

Same reasoning for the 4 dimension.

I think the discriminant here is to have Box, with at least 2 dim, and with type uint8. Then you can put a logging.warn saying you are considering it as image, and if this is not intended to disable it via a flag "image_observation" which is defaulted to None. Then you can compute the value of the flag during init (and warn just once). I will clarify later in our meeting.

if values.type == pa.binary(): # check for binary data (JPEG)
jpeg_images = []
for jpeg_bytes in values:
image = Image.open(io.BytesIO(jpeg_bytes)).convert("L") # decode JPEG and convert to greyscale
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should work with non-grayscale images as well.

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.

2 participants