Create wmms-script.py
Browse files- wmms-script.py +120 -0
wmms-script.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
|
| 3 |
+
"""Watkins Marine Mammal Sound Database."""
|
| 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
|
| 15 |
+
|
| 16 |
+
_COMPRESSED_FILENAME = 'watkins.zip'
|
| 17 |
+
|
| 18 |
+
CLASSES = ['Atlantic_Spotted_Dolphin', 'Bearded_Seal', 'Beluga,_White_Whale', 'Bottlenose_Dolphin', 'Bowhead_Whale', 'Clymene_Dolphin', 'Common_Dolphin', 'False_Killer_Whale', 'Fin,_Finback_Whale', 'Frasers_Dolphin', 'Grampus,_Rissos_Dolphin', 'Harp_Seal', 'Humpback_Whale', 'Killer_Whale', 'Leopard_Seal', 'Long-Finned_Pilot_Whale', 'Melon_Headed_Whale', 'Minke_Whale', 'Narwhal', 'Northern_Right_Whale', 'Pantropical_Spotted_Dolphin', 'Ross_Seal', 'Rough-Toothed_Dolphin', 'Short-Finned_Pacific_Pilot_Whale', 'Southern_Right_Whale', 'Sperm_Whale', 'Spinner_Dolphin', 'Striped_Dolphin', 'Walrus', 'Weddell_Seal', 'White-beaked_Dolphin', 'White-sided_Dolphin']
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class WmmsConfig(datasets.BuilderConfig):
|
| 22 |
+
"""BuilderConfig for WMMS."""
|
| 23 |
+
|
| 24 |
+
def __init__(self, features, **kwargs):
|
| 25 |
+
super(WmmsConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
|
| 26 |
+
self.features = features
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class WMMS(datasets.GeneratorBasedBuilder):
|
| 30 |
+
|
| 31 |
+
BUILDER_CONFIGS = [
|
| 32 |
+
WmmsConfig(
|
| 33 |
+
features=datasets.Features(
|
| 34 |
+
{
|
| 35 |
+
"file": datasets.Value("string"),
|
| 36 |
+
"audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
|
| 37 |
+
"species": datasets.Value("string"),
|
| 38 |
+
"label": datasets.ClassLabel(names=CLASSES),
|
| 39 |
+
}
|
| 40 |
+
),
|
| 41 |
+
name="wmms",
|
| 42 |
+
description='',
|
| 43 |
+
),
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
def _info(self):
|
| 47 |
+
return datasets.DatasetInfo(
|
| 48 |
+
description="Database can be downloaded from https://archive.org/details/watkins_202104",
|
| 49 |
+
features=self.config.features,
|
| 50 |
+
supervised_keys=None,
|
| 51 |
+
homepage="",
|
| 52 |
+
citation="",
|
| 53 |
+
task_templates=None,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
def _split_generators(self, dl_manager):
|
| 57 |
+
"""Returns SplitGenerators."""
|
| 58 |
+
archive_path = dl_manager.extract(_COMPRESSED_FILENAME)
|
| 59 |
+
extensions = ['.wav']
|
| 60 |
+
_, _walker = fast_scandir(archive_path, extensions, recursive=True)
|
| 61 |
+
|
| 62 |
+
train_walker, val_test_walker = train_test_split(
|
| 63 |
+
_walker, test_size=0.3, random_state=914, stratify=[default_find_classes(f) for f in _walker]
|
| 64 |
+
)
|
| 65 |
+
val_walker, test_walker = train_test_split(
|
| 66 |
+
val_test_walker, test_size=0.5, random_state=914, stratify=[default_find_classes(f) for f in val_test_walker]
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return [
|
| 70 |
+
datasets.SplitGenerator(
|
| 71 |
+
name=datasets.Split.TRAIN, gen_kwargs={"audio_paths": train_walker, "split": "train"}
|
| 72 |
+
),
|
| 73 |
+
datasets.SplitGenerator(
|
| 74 |
+
name=datasets.Split.VALIDATION, gen_kwargs={"audio_paths": val_walker, "split": "validation"}
|
| 75 |
+
),
|
| 76 |
+
datasets.SplitGenerator(
|
| 77 |
+
name=datasets.Split.TEST, gen_kwargs={"audio_paths": test_walker, "split": "test"}
|
| 78 |
+
),
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
def _generate_examples(self, audio_paths, split=None):
|
| 82 |
+
for guid, audio_path in enumerate(audio_paths):
|
| 83 |
+
yield guid, {
|
| 84 |
+
"id": str(guid),
|
| 85 |
+
"file": audio_path,
|
| 86 |
+
"audio": audio_path,
|
| 87 |
+
"species": default_find_classes(audio_path),
|
| 88 |
+
"label": default_find_classes(audio_path),
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def default_find_classes(audio_path):
|
| 93 |
+
return Path(audio_path).parent.stem
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
|
| 97 |
+
# Scan files recursively faster than glob
|
| 98 |
+
# From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
|
| 99 |
+
subfolders, files = [], []
|
| 100 |
+
|
| 101 |
+
try: # hope to avoid 'permission denied' by this try
|
| 102 |
+
for f in os.scandir(path):
|
| 103 |
+
try: # 'hope to avoid too many levels of symbolic links' error
|
| 104 |
+
if f.is_dir():
|
| 105 |
+
subfolders.append(f.path)
|
| 106 |
+
elif f.is_file():
|
| 107 |
+
if os.path.splitext(f.name)[1].lower() in exts:
|
| 108 |
+
files.append(f.path)
|
| 109 |
+
except Exception:
|
| 110 |
+
pass
|
| 111 |
+
except Exception:
|
| 112 |
+
pass
|
| 113 |
+
|
| 114 |
+
if recursive:
|
| 115 |
+
for path in list(subfolders):
|
| 116 |
+
sf, f = fast_scandir(path, exts, recursive=recursive)
|
| 117 |
+
subfolders.extend(sf)
|
| 118 |
+
files.extend(f) # type: ignore
|
| 119 |
+
|
| 120 |
+
return subfolders, files
|