dataset.py
4.49 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
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
141
142
143
144
145
146
147
148
149
import torch
from torch.utils.data import Dataset
import numpy as np
import librosa
import random
from pathlib import Path
from typing import Dict, List, Tuple
import json
import os
class ACRDataset(Dataset):
def __init__(
self,
data_dir: str,
split: str = "train",
sr: int = 16000,
n_mels: int = 80,
n_fft: int = 512,
hop_length: int = 160,
segment_dur: float = 5.0,
augment: bool = True,
n_crops_per_song: int = 4,
):
self.sr = sr
self.n_mels = n_mels
self.n_fft = n_fft
self.hop_length = hop_length
self.segment_len = int(segment_dur * sr)
self.augment = augment
self.n_crops = n_crops_per_song
self.data_dir = Path(data_dir)
meta_path = Path(data_dir) / f"{split}.json"
with open(meta_path) as f:
self.metadata = json.load(f)
self.samples = []
for item in self.metadata:
song_path = Path(data_dir) / item["audio_path"]
if song_path.exists():
self.samples.append(item)
self.song_ids = sorted(set(s["song_id"] for s in self.samples))
self.song_to_idx = {sid: i for i, sid in enumerate(self.song_ids)}
def __len__(self):
return len(self.samples) * self.n_crops
def _load_segment(self, path: str, offset: float, duration: float) -> np.ndarray:
y, _ = librosa.load(
path, sr=self.sr, mono=True,
offset=offset, duration=duration
)
if len(y) < self.segment_len:
y = np.pad(y, (0, self.segment_len - len(y)))
else:
y = y[:self.segment_len]
return y
def _to_mel(self, y: np.ndarray) -> np.ndarray:
mel = librosa.feature.melspectrogram(
y=y, sr=self.sr, n_mels=self.n_mels,
n_fft=self.n_fft, hop_length=self.hop_length
)
return librosa.power_to_db(mel, ref=np.max)
def __getitem__(self, idx):
sample = self.samples[idx // self.n_crops]
duration = sample["duration"]
max_offset = max(0, duration - 5.0)
offset = random.uniform(0, max_offset) if max_offset > 0 else 0
audio_path = self.data_dir / sample["audio_path"]
y = self._load_segment(str(audio_path), offset, 5.0)
if self.augment:
from src.utils.augment import AugmentPipeline
aug = AugmentPipeline(self.sr)
y = aug(y)
mel = self._to_mel(y)
mel_tensor = torch.FloatTensor(mel)
song_id = sample["song_id"]
class_id = self.song_to_idx[song_id]
return {
"mel": mel_tensor,
"song_id": torch.tensor(class_id, dtype=torch.long),
"song_name": song_id,
}
class ACRTestDataset(Dataset):
def __init__(
self,
data_dir: str,
split: str = "test",
sr: int = 16000,
n_mels: int = 80,
n_fft: int = 512,
hop_length: int = 160,
):
self.sr = sr
self.n_mels = n_mels
self.n_fft = n_fft
self.hop_length = hop_length
self.data_dir = Path(data_dir)
meta_path = Path(data_dir) / f"{split}.json"
with open(meta_path) as f:
self.metadata = json.load(f)
self.samples = []
for item in self.metadata:
p = Path(data_dir) / item["audio_path"]
if p.exists():
self.samples.append(item)
self.song_ids = sorted(set(s["song_id"] for s in self.samples))
self.song_to_idx = {sid: i for i, sid in enumerate(self.song_ids)}
def __len__(self):
return len(self.samples)
def __getitem__(self, idx):
sample = self.samples[idx]
audio_path = self.data_dir / sample["audio_path"]
y, _ = librosa.load(
str(audio_path), sr=self.sr, mono=True,
offset=0, duration=min(sample["duration"], 5.0)
)
seg_len = 5 * self.sr
if len(y) < seg_len:
y = np.pad(y, (0, seg_len - len(y)))
else:
y = y[:seg_len]
mel = librosa.power_to_db(
librosa.feature.melspectrogram(y=y, sr=self.sr, n_mels=self.n_mels,
n_fft=self.n_fft, hop_length=self.hop_length),
ref=np.max
)
class_id = self.song_to_idx[sample["song_id"]]
return {
"mel": torch.FloatTensor(mel),
"song_id": torch.tensor(class_id, dtype=torch.long),
"song_name": sample["song_id"],
}