datumaro icon indicating copy to clipboard operation
datumaro copied to clipboard

Error on Dataset export - dataTypeClass not JSON serializable

Open leoll2 opened this issue 1 year ago • 1 comments

Describe the bug

When I try to export my dataset with the following script, the export_dataset function throws an error.

import numpy as np
import polars as pl
from datumaro.experimental import *
from datumaro.experimental.categories import LabelCategories
from datumaro.experimental.export_import import *

class DetectionSample(Sample):
    image: str = image_path_field()
    image_info: ImageInfo = image_info_field()
    bboxes: np.ndarray = bbox_field(dtype=pl.Int32)
    label: np.ndarray = label_field(dtype=pl.Int32, is_list=True)
    confidence: np.ndarray | None = score_field(dtype=pl.Float32, is_list=True)

dataset = Dataset(DetectionSample, categories={"label": LabelCategories(labels=tuple(["car", "truck", "motorbike"]))})
img_info = ImageInfo(width=100, height=100)
samples = (
    DetectionSample(
        image="/dummy/path/A.jpg",
        image_info=img_info,
        bboxes=np.array([[10, 20, 30, 40]]),
        label=np.array([1]),
        confidence=np.array([0.8]),
    ),
    DetectionSample(
        image="/dummy/path/B.jpg",
        image_info=img_info,
        bboxes=np.array([[5, 5, 20, 20], [25, 30, 50, 60]]),
        label=np.array([0, 1]),
        confidence=np.array([0.9, 0.7]),
    ),
    DetectionSample(
        image="/dummy/path/C.jpg",
        image_info=img_info,
        bboxes=np.array([[0, 0, 15, 15]]),
        label=np.array([1]),
        confidence=np.array([0.6]),
    ),
)
for sample in samples:
    dataset.append(sample)

export_dataset(
    dataset=dataset,
    output_path="/tmp/dm-dataset.zip", 
    export_images=False,
    as_zip=False,
)

Error

Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    export_dataset(
    ~~~~~~~~~~~~~~^
        dataset=dataset,
        ^^^^^^^^^^^^^^^^
    ...<2 lines>...
        as_zip=False,
        ^^^^^^^^^^^^^
    )
    ^
  File "/home/leoll2/training_extensions/application/backend/.venv/lib/python3.13/site-packages/datumaro/experimental/export_import.py", line 277, in export_dataset
    json.dump(metadata, f, indent=2)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/leoll2/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/__init__.py", line 179, in dump
    for chunk in iterable:
                 ^^^^^^^^
  File "/home/leoll2/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 433, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/home/leoll2/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 407, in _iterencode_dict
    yield from chunks
  File "/home/leoll2/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 407, in _iterencode_dict
    yield from chunks
  File "/home/leoll2/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 407, in _iterencode_dict
    yield from chunks
  [Previous line repeated 2 more times]
  File "/home/leoll2/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 440, in _iterencode
    o = _default(o)
  File "/home/leoll2/.local/share/uv/python/cpython-3.13.7-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
                    f'is not JSON serializable')
TypeError: Object of type DataTypeClass is not JSON serializable

Environment:

  • OS: Ubuntu 24.04 WSL
  • Datumaro version: develop@e7e3b42f76ccd4c6bdfc5330e8d4d6bb71f9ed2a
  • Python version: 3.13

leoll2 avatar Nov 07 '25 10:11 leoll2

Samples should define fields with dtype=pl.Int32(), not dtype=pl.Int32. Will be enforced through https://github.com/open-edge-platform/datumaro/issues/1855

AlbertvanHouten avatar Nov 07 '25 10:11 AlbertvanHouten