Skip to content

fix(dataset): compare in-memory images of different shapes one by one - #3029

Open
MohammadHijjawi97 wants to merge 3 commits into
roboflow:developfrom
MohammadHijjawi97:fix/dataset-eq-mixed-image-sizes
Open

MohammadHijjawi97 wants to merge 3 commits into
roboflow:developfrom
MohammadHijjawi97:fix/dataset-eq-mixed-image-sizes

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown
Contributor

Description

DetectionDataset.__eq__ and ClassificationDataset.__eq__ compare in-memory images by passing both lists of images to np.array_equal. That stacks each list into one array, which fails when the images differ in size or channel count, and np.array_equal reports the failure as False. A dataset holding such images is therefore unequal even to itself.

Minimal repro:

import numpy as np
import supervision as sv

images = {
    "a.jpg": np.zeros((10, 10, 3), dtype=np.uint8),
    "b.jpg": np.zeros((20, 30, 3), dtype=np.uint8),
}
dataset = sv.DetectionDataset(
    classes=["x"],
    images=images,
    annotations={path: sv.Detections.empty() for path in images},
)
print(dataset == dataset)

Before: False. After: True.

Motivation and Context

Images in a dataset routinely have different resolutions, and grayscale and color images can be mixed. Equality should not depend on whether the images happen to stack.

Changes Made

  • Added _in_memory_images_equal in supervision/dataset/core.py, which checks that both datasets hold the same paths and compares each image pair with np.array_equal.
  • DetectionDataset.__eq__ and ClassificationDataset.__eq__ use it. Datasets without in-memory images, and a dataset with in-memory images compared to one without, behave as before.
  • Changelog entry under Unreleased.

Testing

  • Added TestDatasetEqualityContracts::test_in_memory_images_of_different_shapes, parametrized over both dataset types and over a different-size and a grayscale second image. On develop: 4 failed (assert dataset == dataset). With this change: 4 passed.

  • uv run pytest src/ tests/: 8098 passed, 36 skipped.

  • pre-commit on the changed files: ruff, ruff-format, docformatter, mdformat, codespell and the pre-commit-hooks checks pass; mypy reports no errors in the changed files.

  • Added/updated tests, and the full suite passes locally

  • Updated docs (docstrings / mkdocs entry) for new or changed public API

  • Added a changelog entry in docs/changelog.md under Unreleased (skip for lint/type/format-only or pure doc changes)

Additional Notes

Comparing image by image also avoids building two stacked copies of every in-memory image just to compare them.

DetectionDataset.__eq__ and ClassificationDataset.__eq__ passed the lists of
in-memory images to np.array_equal, which stacks each list into one array.
Images of different sizes or channel counts cannot be stacked, so
np.array_equal returned False and a dataset was unequal even to itself.
Compare the images path by path instead.
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95%. Comparing base (cdb0fa0) to head (98d13f6).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #3029   +/-   ##
=======================================
  Coverage       95%     95%           
=======================================
  Files           84      84           
  Lines        15408   15408           
=======================================
+ Hits         14677   14680    +3     
+ Misses         731     728    -3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda requested a balanced review from Copilot November 19, 2026 10:16
@Borda Borda added the bug Something isn't working label Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the dataset equality fix.

No open issues 🎉

What changed in this PR

Fixes equality checks for detection and classification datasets containing in-memory images with different sizes or channel counts, so mixed-shape datasets can correctly compare equal.

Changes:

  • Adds a shared helper that compares images by path without stacking them into arrays.
  • Adds regression tests for both dataset types, covering mixed image shapes and changed pixel values.
  • Documents the fix in the unreleased changelog.
File Description
tests/​dataset/​test_core.py Tests mixed-size and grayscale image equality for both dataset types.
src/​supervision/​dataset/​core.py Uses path-by-path image comparison in both dataset equality methods.
docs/​changelog.md Records the equality fix and links the PR.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working has conflicts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants