Update wmms.py
Browse files
wmms.py
CHANGED
|
@@ -4,11 +4,13 @@
|
|
| 4 |
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 7 |
import textwrap
|
| 8 |
import datasets
|
| 9 |
import itertools
|
| 10 |
import typing as tp
|
| 11 |
from pathlib import Path
|
|
|
|
| 12 |
from sklearn.model_selection import train_test_split
|
| 13 |
|
| 14 |
SAMPLE_RATE = 16_000
|
|
@@ -57,18 +59,40 @@ class WMMS(datasets.GeneratorBasedBuilder):
|
|
| 57 |
"""Returns SplitGenerators."""
|
| 58 |
archive_path = dl_manager.extract(_COMPRESSED_FILENAME)
|
| 59 |
extensions = ['.wav']
|
| 60 |
-
_,
|
|
|
|
| 61 |
|
| 62 |
-
train_walker, test_walker = train_test_split(
|
| 63 |
-
|
| 64 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
return [
|
| 67 |
datasets.SplitGenerator(
|
| 68 |
-
name=datasets.Split.TRAIN, gen_kwargs={"audio_paths":
|
| 69 |
),
|
| 70 |
datasets.SplitGenerator(
|
| 71 |
-
name=datasets.Split.TEST, gen_kwargs={"audio_paths":
|
| 72 |
),
|
| 73 |
]
|
| 74 |
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
import os
|
| 7 |
+
import random
|
| 8 |
import textwrap
|
| 9 |
import datasets
|
| 10 |
import itertools
|
| 11 |
import typing as tp
|
| 12 |
from pathlib import Path
|
| 13 |
+
from collections import defaultdict
|
| 14 |
from sklearn.model_selection import train_test_split
|
| 15 |
|
| 16 |
SAMPLE_RATE = 16_000
|
|
|
|
| 59 |
"""Returns SplitGenerators."""
|
| 60 |
archive_path = dl_manager.extract(_COMPRESSED_FILENAME)
|
| 61 |
extensions = ['.wav']
|
| 62 |
+
_, filepaths = fast_scandir(archive_path, extensions, recursive=True)
|
| 63 |
+
labels = [default_find_classes(f) for f in filepaths]
|
| 64 |
|
| 65 |
+
# train_walker, test_walker = train_test_split(
|
| 66 |
+
# _walker, test_size=0.2, random_state=914, stratify=[default_find_classes(f) for f in _walker]
|
| 67 |
+
# )
|
| 68 |
+
|
| 69 |
+
# Step 1: Organize samples by class
|
| 70 |
+
class_to_files = defaultdict(list)
|
| 71 |
+
for filepath, label in zip(filepaths, labels):
|
| 72 |
+
class_to_files[label].append(filepath)
|
| 73 |
+
|
| 74 |
+
# Step 2: Select exactly 2 samples per class for the test set
|
| 75 |
+
test_files, test_labels = [], []
|
| 76 |
+
train_files, train_labels = [], []
|
| 77 |
+
|
| 78 |
+
for label, files in class_to_files.items():
|
| 79 |
+
if len(files) < 2:
|
| 80 |
+
raise ValueError(f"Not enough samples for class {label}") # Ensure each class has at least 2 samples
|
| 81 |
+
|
| 82 |
+
random.Random(914).shuffle(files) # Shuffle to ensure randomness
|
| 83 |
+
|
| 84 |
+
test_files.extend(files[:2]) # Pick first 2 for test
|
| 85 |
+
test_labels.extend([label] * 2)
|
| 86 |
+
|
| 87 |
+
train_files.extend(files[2:]) # Remaining go to train
|
| 88 |
+
train_labels.extend([label] * (len(files) - 2))
|
| 89 |
|
| 90 |
return [
|
| 91 |
datasets.SplitGenerator(
|
| 92 |
+
name=datasets.Split.TRAIN, gen_kwargs={"audio_paths": train_files, "split": "train"}
|
| 93 |
),
|
| 94 |
datasets.SplitGenerator(
|
| 95 |
+
name=datasets.Split.TEST, gen_kwargs={"audio_paths": test_files, "split": "test"}
|
| 96 |
),
|
| 97 |
]
|
| 98 |
|