forked from eigensharks/mfcc-speaker-recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
36 lines (29 loc) · 1.27 KB
/
data.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
import os
import glob
from torch.utils.data import Dataset, DataLoader, Subset
import feature_extraction
import numpy as np
class TripletDataset(Dataset):
def __init__(self, folder_path):
self.folder_path = folder_path
self.files = sorted(os.listdir(folder_path))
self.speakers = sorted(set(record.split('-')[0] for record in self.files))
self.data = self.load_data()
def load_data(self):
data = []
for recording in self.files:
path = os.path.join(self.folder_path, recording)
label = recording.split('-')[0]
data.append((feature_extraction.get_features(path), label))
return data
def __len__(self):
return len(self.data)
def __getitem__(self, index):
anchor = self.data[index]
# Sample positive from the same class
positive_class_samples = [i for i, (features, label) in enumerate(self.data) if label == anchor[1]]
positive_index = np.random.choice(positive_class_samples)
# Sample negative from a different class
negative_class_samples = [i for i, (features, label) in enumerate(self.data) if label != anchor[1]]
negative_index = np.random.choice(negative_class_samples)
return index, positive_index, negative_index