Skip to content

[Bug Report] Unwanted assumption that uint8 is an image #324

@rparac

Description

@rparac

Describe the bug
Minari's automatic image compression heuristic assumes that any array with a uint8 data type is an image, regardless of its shape. If a user passes a 1D numerical array (e.g., a state vector of shape (3,)) or a generic labels array with a uint8 dtype into an EpisodeBuffer, Minari aggressively routes it through the JPEG encoder.

When the dataset is loaded back, the data is severely corrupted. Instead of the original 1D arrays, the loaded dataset returns a jagged object array where each element contains the raw byte stream of a compressed JPEG (starting with 255, 216, which is the FF D8 JPEG signature).
This seems to be related to #275.
It was really painful to debug

Code example

import gymnasium as gym
import numpy as np
import minari
from minari.utils import EpisodeBuffer

# 1. Define a space with a non-image 1D uint8 array
observation_space = gym.spaces.Dict({
    "vector_state": gym.spaces.Box(low=0, high=255, shape=(3,), dtype=np.uint8)
})
action_space = gym.spaces.Discrete(2)

# 2. Create dummy data for 10 steps
num_steps = 10
observations = {
    # Shape is (10, 3) representing 10 steps of a 3-element vector
    "vector_state": np.ones((num_steps, 3), dtype=np.uint8) 
}
actions = np.zeros(num_steps, dtype=np.int64)
rewards = np.zeros(num_steps, dtype=np.float64)
terminations = np.zeros(num_steps, dtype=bool)
truncations = np.zeros(num_steps, dtype=bool)

# 3. Create EpisodeBuffer
episode = EpisodeBuffer(
    observations=observations,
    actions=actions,
    rewards=rewards,
    terminations=terminations,
    truncations=truncations,
    infos={}
)

# 4. Save dataset
dataset_id = "test-uint8-bug-v0"
minari.create_dataset_from_buffers(
    dataset_id=dataset_id,
    buffer=[episode],
    observation_space=observation_space,
    action_space=action_space
)

# 5. Load and inspect the corruption
dataset = minari.load_dataset(dataset_id)
ep = list(dataset.iterate_episodes())[0]

print("Expected shape for an episode: (10, 3)")
print("Actual loaded shape:", ep.observations["vector_state"].shape)
print("First element shape:", ep.observations["vector_state"][0].shape)
print("First bytes (JPEG Signature):", ep.observations["vector_state"][0][:2])

Output of the script:
Expected shape per step: (10,3)
Actual loaded shape: (10, 332)
First element shape: (332,)
First bytes (JPEG Signature): [255 216]

System Info
(Note: Please adjust these to match your actual machine!)

Describe how Minari was installed: pip install minari[all]

Operating system: Ubuntu 24.04

Python version: 3.10.19


Checklist

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions