-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_loader.py
69 lines (57 loc) · 1.94 KB
/
data_loader.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
import numpy as np
import random
import os
import torch
from torch.utils.data import Dataset, DataLoader
class VCCDataset(Dataset):
def __init__(self, path, batch_size):
super().__init__()
self.path = path
self.batch_size = batch_size
# load speaker embed
self.embed = {}
for fname in os.listdir('./embeddings'):
if not fname.endswith('.npy'):
continue
spk = fname.split('.')[0]
self.embed[spk] = np.load(os.path.join('./embeddings', fname))
# load data
self.data = []
for fname in os.listdir(path):
if not fname.endswith('.npy'):
continue
src = fname.split('.')[0]
if src not in self.embed:
continue
data = np.load(os.path.join(path, fname))
np.random.shuffle(data)
# split to batches
num = data.shape[0]
batches = []
i = 0
while i + batch_size <= num:
batches.append(data[i: i+batch_size])
i += batch_size
for batch in batches:
trgs = random.choices(list(self.embed), k=3)
for trg in trgs:
if src == trg:
continue
self.data.append((batch, self.embed[src], self.embed[trg]))
random.shuffle(self.data)
def __len__(self):
return len(self.data)
def __getitem__(self, index):
return self.data[index]
def get_dataloader(training=True, batch_size=16, num_workers=2):
if training:
dataset = VCCDataset('./data/train/', batch_size=batch_size)
else:
dataset = VCCDataset('./data/test/', batch_size=batch_size)
data_loader = DataLoader(
dataset=dataset,
batch_size=1,
shuffle=True,
num_workers=num_workers
)
return data_loader