-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmls_collate_fn.py
140 lines (116 loc) · 4.87 KB
/
mls_collate_fn.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import torch
from omegaconf import DictConfig
from torch.nn.utils.rnn import pad_sequence
Lang_to_id = {'dutch' : 0,
'french' : 1,
'german' : 2,
'italian' : 3,
'polish' : 4,
'portuguese' : 5,
'spanish' : 6}
id_to_Lang = {0: 'dutch',
1: 'french',
2: 'german',
3: 'italian',
4: 'polish',
5: 'portuguese',
6: 'spanish'}
class DataCollator:
def __init__(self, conf: DictConfig):
pass
def __call__(self,batch):
tokens = []
token_lengths = []
specs = []
spec_lengths = []
waveform = []
cropped_waveform = []
mels = []
language_ids = []
speaker_ids = []
for item in batch:
token, token_length, wav, cropped_wav, spec, spec_length, mel, aux_input = item
spec = torch.transpose(spec.squeeze(0),0,1) # [t, spec_dim]
wav = wav.squeeze() # [L]
mel = torch.transpose(mel.squeeze(0),0,1) # [t, mel_dim]
tokens.append(token)
token_lengths.append(token_length)
specs.append(spec)
spec_lengths.append(spec_length)
waveform.append(wav)
cropped_waveform.append(cropped_wav)
mels.append(mel)
language_ids.append(aux_input['language_id'])
speaker_ids.append(aux_input['speaker_id'])
tokens = pad_sequence(tokens, batch_first= True) # [B, T]
token_lengths = torch.LongTensor(token_lengths) # [B]
specs = pad_sequence(specs, batch_first=True) # [B,t,spec_dim]
specs = torch.transpose(specs, 1,2) # [B,spec_dim,t]
spec_lengths = torch.LongTensor(spec_lengths) # [B]
waveform = pad_sequence(waveform, batch_first=True) # [B,L]
waveform = waveform.unsqueeze(1) #[B,1,L]
cropped_waveform = torch.stack(cropped_waveform, dim=0) #[B, crop_len]
mels = pad_sequence(mels, batch_first=True) # [B, t, mel_dim]
mels = torch.transpose(mels, 1,2) # [B, mel_dim, t]
language_ids = torch.LongTensor(language_ids) #[B]
speaker_ids = torch.LongTensor(speaker_ids) # [B]
result = {}
result['tokens'] = tokens
result['token_lengths'] = token_lengths
result['specs'] = specs
result['spec_lengths'] = spec_lengths
result['waveform'] = waveform
result['cropped_waveform'] = cropped_waveform
result['mel'] = mels
result['aux_input'] = {"d_vectors": None, "speaker_ids": speaker_ids, "language_ids": language_ids}
return result
class DataCollator2:
def __init__(self, conf: DictConfig):
pass
def __call__(self,batch):
tokens = []
token_lengths = []
specs = []
spec_lengths = []
waveform = []
cropped_waveform = []
mels = []
language_ids = []
speaker_ids = []
for item in batch:
for id in range(len(id_to_Lang)):
token, token_length, wav, cropped_wav, spec, spec_length, mel, aux_input = item[id_to_Lang[id]]
spec = torch.transpose(spec.squeeze(0),0,1) # [t, spec_dim]
wav = wav.squeeze() # [L]
mel = torch.transpose(mel.squeeze(0),0,1) # [t, mel_dim]
tokens.append(token)
token_lengths.append(token_length)
specs.append(spec)
spec_lengths.append(spec_length)
waveform.append(wav)
cropped_waveform.append(cropped_wav)
mels.append(mel)
language_ids.append(aux_input['language_id'])
speaker_ids.append(aux_input['speaker_id'])
tokens = pad_sequence(tokens, batch_first= True) # [B, T]
token_lengths = torch.LongTensor(token_lengths) # [B]
specs = pad_sequence(specs, batch_first=True) # [B,t,spec_dim]
specs = torch.transpose(specs, 1,2) # [B,spec_dim,t]
spec_lengths = torch.LongTensor(spec_lengths) # [B]
waveform = pad_sequence(waveform, batch_first=True) # [B,L]
waveform = waveform.unsqueeze(1) #[B,1,L]
cropped_waveform = torch.stack(cropped_waveform, dim=0) #[B, crop_len]
mels = pad_sequence(mels, batch_first=True) # [B, t, mel_dim]
mels = torch.transpose(mels, 1,2) # [B, mel_dim, t]
language_ids = torch.LongTensor(language_ids) #[B]
speaker_ids = torch.LongTensor(speaker_ids) # [B]
result = {}
result['tokens'] = tokens
result['token_lengths'] = token_lengths
result['specs'] = specs
result['spec_lengths'] = spec_lengths
result['waveform'] = waveform
result['cropped_waveform'] = cropped_waveform
result['mel'] = mels
result['aux_input'] = {"d_vectors": None, "speaker_ids": speaker_ids, "language_ids": language_ids}
return result