-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[Datumaro] Add random split transform #1213
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
import logging as log | ||
import os.path as osp | ||
import random | ||
|
||
import pycocotools.mask as mask_utils | ||
|
||
|
@@ -295,6 +296,66 @@ def transform_item(self, item): | |
return self.wrap_item(item, | ||
subset=self._mapping.get(item.subset, item.subset)) | ||
|
||
class RandomSplit(Transform, CliPlugin): | ||
""" | ||
Joins all subsets into one and splits the result into few parts. | ||
It is expected that item ids are unique and subset ratios sum up to 1.|n | ||
|n | ||
Example:|n | ||
|s|s%(prog)s --subset train:.67 --subset test:.33 | ||
""" | ||
|
||
@staticmethod | ||
def _split_arg(s): | ||
parts = s.split(':') | ||
if len(parts) != 2: | ||
import argparse | ||
raise argparse.ArgumentTypeError() | ||
return (parts[0], float(parts[1])) | ||
|
||
@classmethod | ||
def build_cmdline_parser(cls, **kwargs): | ||
parser = super().build_cmdline_parser(**kwargs) | ||
parser.add_argument('-s', '--subset', action='append', | ||
type=cls._split_arg, dest='splits', | ||
help="Subsets in the form of: '<subset>:<ratio>' (repeatable)") | ||
parser.add_argument('--seed', type=int, help="Random seed") | ||
return parser | ||
|
||
def __init__(self, extractor, splits, seed=None): | ||
super().__init__(extractor) | ||
|
||
total_ratio = sum((s[1] for s in splits), 0) | ||
if not total_ratio == 1: | ||
raise Exception( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to throw |
||
"Sum of ratios is expected to be 1, got %s, which is %s" % | ||
(splits, total_ratio)) | ||
|
||
dataset_size = len(extractor) | ||
indices = list(range(dataset_size)) | ||
|
||
random.seed(seed) | ||
random.shuffle(indices) | ||
|
||
parts = [] | ||
s = 0 | ||
for subset, ratio in splits: | ||
s += ratio | ||
boundary = int(s * dataset_size) | ||
parts.append((boundary, subset)) | ||
|
||
self._parts = parts | ||
|
||
def _find_split(self, index): | ||
for boundary, subset in self._parts: | ||
if index < boundary: | ||
return subset | ||
return subset | ||
|
||
def __iter__(self): | ||
for i, item in enumerate(self._extractor): | ||
yield self.wrap_item(item, subset=self._find_split(i)) | ||
|
||
class IdFromImageName(Transform, CliPlugin): | ||
def transform_item(self, item): | ||
name = item.id | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -320,3 +320,40 @@ def __iter__(self): | |
|
||
actual = transforms.BoxesToMasks(SrcExtractor()) | ||
compare_datasets(self, DstExtractor(), actual) | ||
|
||
def test_random_split(self): | ||
class SrcExtractor(Extractor): | ||
def __iter__(self): | ||
return iter([ | ||
DatasetItem(id=1, subset="a"), | ||
DatasetItem(id=2, subset="a"), | ||
DatasetItem(id=3, subset="b"), | ||
DatasetItem(id=4, subset="b"), | ||
DatasetItem(id=5, subset="b"), | ||
DatasetItem(id=6, subset=""), | ||
DatasetItem(id=7, subset=""), | ||
]) | ||
|
||
actual = transforms.RandomSplit(SrcExtractor(), splits=[ | ||
('train', 4.0 / 7.0), | ||
('test', 3.0 / 7.0), | ||
]) | ||
|
||
self.assertEqual(4, len(actual.get_subset('train'))) | ||
self.assertEqual(3, len(actual.get_subset('test'))) | ||
|
||
def test_random_split_gives_error_on_non1_ratios(self): | ||
class SrcExtractor(Extractor): | ||
def __iter__(self): | ||
return iter([DatasetItem(id=1)]) | ||
|
||
has_error = False | ||
try: | ||
transforms.RandomSplit(SrcExtractor(), splits=[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably you can use |
||
('train', 0.5), | ||
('test', 0.7), | ||
]) | ||
except Exception: | ||
has_error = True | ||
|
||
self.assertTrue(has_error) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you have problems here with fp precision? https://docs.python.org/3/library/math.html#math.isclose