Skip to content

Commit

Permalink
Fix image stats when no image info available (#29)
Browse files Browse the repository at this point in the history
* Fix image stats when no image info available

* Update changelog
  • Loading branch information
zhiltsov-max committed Nov 20, 2023
1 parent f8a24d4 commit 6b65cf8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
(<https://github.com/cvat-ai/datumaro/pull/9>, <https://github.com/cvat-ai/datumaro/pull/16>)
- Missing comparison of the base class attributes in the `Mask` class
(<https://github.com/cvat-ai/datumaro/pull/28>)
- Image stats when no image info available for some images in the dataset
(<https://github.com/cvat-ai/datumaro/pull/29>)

### Security
- TBD
Expand Down
9 changes: 8 additions & 1 deletion datumaro/components/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,8 +1320,15 @@ def __init__(self):
self._stats = {} # (id, subset) -> (pixel count, mean vec, std vec)

def accumulate(self, item: DatasetItem):
if not isinstance(item.media, Image):
log.warning(
"Item %s: has no image info, the image will be skipped from pixel statistics",
item.id,
)
return

size = item.media.size
if size is None:
if size is None or not item.media.has_data:
log.warning(
"Item %s: can't detect image size, "
"the image will be skipped from pixel statistics",
Expand Down
24 changes: 24 additions & 0 deletions tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ def test_image_stats(self):
for estd, astd in zip(expected_std, actual_std):
self.assertAlmostEqual(estd, astd, places=0)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_image_stats_with_no_image_infos(self):
dataset = Dataset.from_iterable(
[
DatasetItem(id=0, media=Image(size=(10, 10))),
DatasetItem(id=1, media=Image(path="inexistent.path")),
DatasetItem(id=2),
]
)

actual = compute_image_statistics(dataset)

self.assertEqual(
actual["dataset"],
{
"images count": 3,
"unique images count": 3,
"repeated images count": 0,
"repeated images": [],
},
)
self.assertEqual("n/a", actual["subsets"]["default"]["image mean"])
self.assertEqual("n/a", actual["subsets"]["default"]["image std"])

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_stats(self):
dataset = Dataset.from_iterable(
Expand Down

0 comments on commit 6b65cf8

Please sign in to comment.