-
Notifications
You must be signed in to change notification settings - Fork 221
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
Approximate proportional sampling in BucketingSampler; remaining_duration, remaining_cuts, num_cuts properties for samplers. #372
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a0e4da7
Approximate proportional sampling in BucketingSampler; remaining_dura…
pzelasko cd3fed6
Add a test for the new properties
pzelasko 7918904
Remove commented code
pzelasko 7c1aa93
Add a flag to disable the proportional sampling of buckets
pzelasko f0c08a3
Merge branch 'master' into feature/approximate-proportional-sampling
pzelasko 4d98d1d
easier to ask for forgiveness than permission
pzelasko 317fdca
Merge remote-tracking branch 'origin/feature/approximate-proportional…
pzelasko d687706
Merge branch 'master' into feature/approximate-proportional-sampling
pzelasko 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 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import random | ||
from collections import deque | ||
from typing import Generator, Iterable | ||
from typing import Generator, Iterable, Optional | ||
|
||
from lhotse import CutSet | ||
from lhotse.cut import Cut | ||
|
@@ -18,6 +18,27 @@ def __init__(self, items: CutSet): | |
self._shuffled_items = self._orig_items | ||
self._iter = None | ||
self._reusable = deque() | ||
# Add duration tracking for non-lazy CutSets | ||
if not self.is_lazy: | ||
self._total_duration = sum(c.duration for c in self._orig_items) | ||
self._total_cuts = len(self._orig_items) | ||
else: | ||
self._total_duration = None | ||
self._total_cuts = None | ||
self._remaining_duration = self._total_duration | ||
self.remaining_cuts = self._total_cuts | ||
|
||
@property | ||
def is_lazy(self) -> bool: | ||
return self._orig_items.is_lazy | ||
|
||
@property | ||
def remaining_duration(self) -> Optional[float]: | ||
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. Is there a reason why this is a property and not a function? E.g. does it indicate that it's expected to be fast to compute? 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. Yeah, that's the reason. |
||
# Paranoia mode: float arithmetic is imprecise, so we'll make sure | ||
# that the returned duration is non-negative. | ||
if self._remaining_duration is None: | ||
return None | ||
return max(0, self._remaining_duration) | ||
|
||
def shuffle(self, seed: int) -> "DataSource": | ||
""" | ||
|
@@ -45,11 +66,16 @@ def sort_like(self, other: "DataSource") -> "DataSource": | |
def take_back(self, cut: Cut) -> None: | ||
"""Push the cut in front of other cuts to be sampled again.""" | ||
self._reusable.append(cut) | ||
if not self.is_lazy: | ||
self._remaining_duration += cut.duration | ||
self.remaining_cuts += 1 | ||
|
||
def reset(self) -> None: | ||
"""Reset the iterable state of DataSource.""" | ||
self._iter = None | ||
self._reusable.clear() | ||
self._remaining_duration = self._total_duration | ||
self.remaining_cuts = self._total_cuts | ||
|
||
def __iter__(self) -> "DataSource": | ||
self.reset() | ||
|
@@ -58,8 +84,13 @@ def __iter__(self) -> "DataSource": | |
|
||
def __next__(self) -> Cut: | ||
if self._reusable: | ||
return self._reusable.popleft() | ||
return next(self._iter) | ||
next_cut = self._reusable.popleft() | ||
else: | ||
next_cut = next(self._iter) | ||
if not self.is_lazy: | ||
self._remaining_duration -= next_cut.duration | ||
self.remaining_cuts -= 1 | ||
return next_cut | ||
|
||
def __len__(self) -> int: | ||
return len(self._shuffled_items) | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.
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.
BTW you could make this more efficient with try-except.