-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
62 lines (57 loc) · 1.93 KB
/
Copy pathdataset.py
File metadata and controls
62 lines (57 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import torch
import soundfile as sf
import yaml
from itertools import groupby
from torch.utils.data import Dataset
class MuSTC(Dataset):
def __init__(
self, root: str,
audio_processor,
tokenizer,
sample_rate,
validation=False
):
split = 'dev' if validation else 'train'
root = f'{root}/en-ru/data/split'
wav_root, txt_root = (f'{root}/{p}' for p in ('wav', 'txt'))
assert root.is_dir() and wav_root.is_dir() and txt_root.is_dir()
with open(f'{txt_root}/{split}.yaml') as f:
segments = yaml.load(f, Loader=yaml.BaseLoader)
for lang in ('en', 'ru'):
with open(f'{txt_root}/{split}.{lang}') as f:
utterances = [r.strip() for r in f]
for i, u in enumerate(utterances):
segments[i][lang] = u
self.data = []
for wav_filename, _seg_group in groupby(segments, lambda x: x['wav']):
wav_path = f'{wav_root}/{wav_filename}'
sample_rate = sf.info(wav_path.as_posix()).samplerate
seg_group = sorted(_seg_group, key=lambda x: x['offset'])
for i, segment in enumerate(seg_group):
offset = int(float(segment['offset']) * sample_rate)
n_frames = int(float(segment['duration']) * sample_rate)
self.data.append((
wav_path.as_posix(),
offset,
n_frames,
sample_rate,
segment['en'],
segment['ru'],
))
self.audio_processor = audio_processor
self.tokenizer = tokenizer
def __getitem__(self, n: int):
wav_path, offset, n_frames, sample_rate, src, tgt = self.data[n]
waveform, _ = sf.read(
wav_path,
dtype='float32',
samplerate=sample_rate,
always_2d=True,
frames=n_frames,
start=offset
)
waveform = torch.from_numpy(waveform.T)
src, tgt = (self.tokenizer(x) for x in (src, tgt))
return self.audio_processor(waveform), src, tgt, sample_rate
def __len__(self):
return len(self.data)