Delete data_utils.py
Browse files- data_utils.py +0 -94
data_utils.py
DELETED
|
@@ -1,94 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import numpy as np
|
| 3 |
-
import torch
|
| 4 |
-
from torch import Tensor
|
| 5 |
-
import librosa
|
| 6 |
-
from torch.utils.data import Dataset
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Audioni padding qilish
|
| 10 |
-
def pad(x, max_len=64600):
|
| 11 |
-
x_len = x.shape[0]
|
| 12 |
-
if x_len >= max_len:
|
| 13 |
-
return x[:max_len]
|
| 14 |
-
# Padding kerak
|
| 15 |
-
num_repeats = (max_len // x_len) + 1
|
| 16 |
-
padded_x = np.tile(x, (1, num_repeats))[:, :max_len][0]
|
| 17 |
-
return padded_x
|
| 18 |
-
|
| 19 |
-
def genSpoof_list(dir_meta, is_train=False, is_eval=False):
|
| 20 |
-
d_meta = {}
|
| 21 |
-
file_list = []
|
| 22 |
-
with open(dir_meta, 'r') as f:
|
| 23 |
-
l_meta = f.readlines()
|
| 24 |
-
|
| 25 |
-
if is_train:
|
| 26 |
-
for line in l_meta:
|
| 27 |
-
_, key, _, _, label = line.strip().split(' ')
|
| 28 |
-
file_list.append(key)
|
| 29 |
-
d_meta[key] = 1 if label == 'bonafide' else 0
|
| 30 |
-
return d_meta, file_list
|
| 31 |
-
elif is_eval:
|
| 32 |
-
for line in l_meta:
|
| 33 |
-
key = line.strip()
|
| 34 |
-
file_list.append(key)
|
| 35 |
-
return file_list
|
| 36 |
-
else:
|
| 37 |
-
for line in l_meta:
|
| 38 |
-
_, key, _, _, label = line.strip().split(' ')
|
| 39 |
-
file_list.append(key)
|
| 40 |
-
d_meta[key] = 1 if label == 'bonafide' else 0
|
| 41 |
-
return d_meta, file_list
|
| 42 |
-
|
| 43 |
-
class Dataset_ASVspoof2019_train(Dataset):
|
| 44 |
-
def __init__(self, list_IDs, labels, base_dir, cut=64600):
|
| 45 |
-
"""
|
| 46 |
-
Args:
|
| 47 |
-
list_IDs: Utts kalitlari ro'yxati (string).
|
| 48 |
-
labels: Kalitlar va tegishli yorliqlar lug'ati.
|
| 49 |
-
base_dir: Ma'lumotlar joylashgan katalog (flac katalogsiz).
|
| 50 |
-
cut: Maksimal uzunlik (standart: 64600).
|
| 51 |
-
"""
|
| 52 |
-
self.list_IDs = list_IDs
|
| 53 |
-
self.labels = labels
|
| 54 |
-
self.base_dir = base_dir
|
| 55 |
-
self.cut = cut
|
| 56 |
-
|
| 57 |
-
def __len__(self):
|
| 58 |
-
return len(self.list_IDs)
|
| 59 |
-
|
| 60 |
-
def __getitem__(self, index):
|
| 61 |
-
key = self.list_IDs[index]
|
| 62 |
-
file_path = os.path.join(self.base_dir, f"{key}.flac") # flac ni qayta qo‘shmang
|
| 63 |
-
if not os.path.exists(file_path):
|
| 64 |
-
raise FileNotFoundError(f"File not found: {file_path}")
|
| 65 |
-
|
| 66 |
-
X, fs = librosa.load(file_path, sr=16000)
|
| 67 |
-
X_pad = pad(X, self.cut)
|
| 68 |
-
x_inp = Tensor(X_pad)
|
| 69 |
-
y = self.labels[key]
|
| 70 |
-
return x_inp, y
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# ASVspoof2021 baholash ma'lumotlar to'plami uchun Dataset sinfi
|
| 74 |
-
class Dataset_ASVspoof2021_eval(Dataset):
|
| 75 |
-
def __init__(self, list_IDs, base_dir, cut=64600):
|
| 76 |
-
self.list_IDs = [x.replace(' ', '_') for x in list_IDs] # Bo'sh joylarni almashtirish
|
| 77 |
-
self.base_dir = base_dir
|
| 78 |
-
self.cut = cut
|
| 79 |
-
|
| 80 |
-
def __len__(self):
|
| 81 |
-
return len(self.list_IDs)
|
| 82 |
-
|
| 83 |
-
def __getitem__(self, index):
|
| 84 |
-
key = self.list_IDs[index]
|
| 85 |
-
file_path = os.path.join(self.base_dir, f"{key}.flac")
|
| 86 |
-
if not os.path.exists(file_path):
|
| 87 |
-
print(f"Checking file: {file_path}, Exists: {os.path.exists(file_path)}") # Fayl mavjudligini tekshirish
|
| 88 |
-
raise FileNotFoundError(f"File not found: {file_path}")
|
| 89 |
-
|
| 90 |
-
X, fs = librosa.load(file_path, sr=16000)
|
| 91 |
-
X_pad = pad(X, self.cut)
|
| 92 |
-
x_inp = Tensor(X_pad)
|
| 93 |
-
return x_inp, key
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|