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

Fix Dataset.get when there are transforms in the dataset #45

Merged
merged 5 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
(<https://github.com/cvat-ai/datumaro/pull/34>)
- Incorrect computation of binary mask bbox (missed 1 pixel of the size)
(<https://github.com/cvat-ai/datumaro/pull/41>)
- `Dataset.get()` could ignore existing transforms in the dataset
(<https://github.com/cvat-ai/datumaro/pull/45>)

### Security
- TBD
Expand Down
5 changes: 3 additions & 2 deletions datumaro/components/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,9 @@ def _update_status(item_id, new_status: ItemStatus):
source = self._source or DatasetItemStorageDatasetView(
self._storage, categories=self._categories, media_type=media_type
)
transform = None

transform = None
old_ids = None
if self._transforms:
transform = _StackedTransform(source, self._transforms)
if transform.is_local:
Expand Down Expand Up @@ -588,7 +589,7 @@ def get(self, id, subset=None) -> Optional[DatasetItem]:

item = self._storage.get(id, subset)
if item is None and not self.is_cache_initialized():
if self._source.get.__func__ == Extractor.get:
if self._source.get.__func__ == Extractor.get or self._transforms:
# can be improved if IDataset is ABC
self.init_cache()
item = self._storage.get(id, subset)
Expand Down
71 changes: 71 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,77 @@ def __iter__(self):

self.assertEqual(iter_called, 2)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_can_get_item_after_local_transforms(self):
iter_called = 0

class TestExtractor(Extractor):
def __iter__(self):
nonlocal iter_called
iter_called += 1
yield from [
DatasetItem(1),
DatasetItem(2),
DatasetItem(3),
DatasetItem(4),
]

dataset = Dataset.from_extractors(TestExtractor())

class TestTransform(ItemTransform):
def transform_item(self, item):
return self.wrap_item(item, id=int(item.id) + 1)

dataset.transform(TestTransform)
dataset.transform(TestTransform)

self.assertEqual(iter_called, 0)

self.assertIsNone(dataset.get(1))
self.assertIsNone(dataset.get(2))
self.assertIsNotNone(dataset.get(3))
self.assertIsNotNone(dataset.get(4))
self.assertIsNotNone(dataset.get(5))
self.assertIsNotNone(dataset.get(6))

self.assertEqual(iter_called, 1)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_can_get_item_after_nonlocal_transforms(self):
iter_called = 0

class TestExtractor(Extractor):
def __iter__(self):
nonlocal iter_called
iter_called += 1
yield from [
DatasetItem(1),
DatasetItem(2),
DatasetItem(3),
DatasetItem(4),
]

dataset = Dataset.from_extractors(TestExtractor())

class TestTransform(Transform):
def __iter__(self):
for item in self._extractor:
yield self.wrap_item(item, id=int(item.id) + 1)

dataset.transform(TestTransform)
dataset.transform(TestTransform)

self.assertEqual(iter_called, 0)

self.assertIsNone(dataset.get(1))
self.assertIsNone(dataset.get(2))
self.assertIsNotNone(dataset.get(3))
self.assertIsNotNone(dataset.get(4))
self.assertIsNotNone(dataset.get(5))
self.assertIsNotNone(dataset.get(6))

self.assertEqual(iter_called, 2)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_can_get_subsets_after_local_transforms(self):
iter_called = 0
Expand Down
Loading