forked from SeanNaren/deepspeech.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e048430
commit 6441435
Showing
2 changed files
with
44 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from torch.utils.data.sampler import Sampler | ||
import numpy as np | ||
from data.data_loader import SpectrogramDataset, load_audio | ||
from collections import defaultdict | ||
|
||
class SpectrogramDatasetWithLength(SpectrogramDataset): | ||
def __init__(self, *args, **kwargs): | ||
super(SpectrogramDatasetWithLength, self).__init__(*args, **kwargs) | ||
audio_paths = [path for (path, _) in self.ids] | ||
audio_lengths = [len(load_audio(path)) for path in audio_paths ] | ||
hist, bin_edges = np.histogram(audio_lengths, bins="auto") | ||
audio_samples_indices = np.digitize(audio_lengths, bins=bin_edges) | ||
self.bins_to_samples = defaultdict(list) | ||
for idx, bin_id in enumerate(audio_samples_indices): | ||
self.bins_to_samples[bin_id].append(idx) | ||
|
||
class BucketingSampler(Sampler): | ||
""" | ||
""" | ||
|
||
def __init__(self, data_source): | ||
self.data_source = data_source | ||
assert hasattr(self.data_source, 'bins_to_samples') | ||
|
||
def __iter__(self): | ||
for bin, sample_idx in self.data_source.bins_to_samples.items(): | ||
np.random.shuffle(sample_idx) | ||
for s in sample_idx: | ||
yield s | ||
|
||
def __len__(self): | ||
return len(self.data_source) | ||
|
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