justdata 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- justdata/__init__.py +3 -0
- justdata/acoustic/__init__.py +381 -0
- justdata/acoustic/adapters.py +329 -0
- justdata/acoustic/augment/__init__.py +119 -0
- justdata/acoustic/augment/batch.py +813 -0
- justdata/acoustic/augment/configs.py +204 -0
- justdata/acoustic/augment/patchout.py +331 -0
- justdata/acoustic/augment/spectrogram.py +806 -0
- justdata/acoustic/augment/waveform.py +879 -0
- justdata/acoustic/batching.py +27 -0
- justdata/acoustic/channel.py +53 -0
- justdata/acoustic/compat/__init__.py +95 -0
- justdata/acoustic/compat/ast.py +626 -0
- justdata/acoustic/compat/ced.py +152 -0
- justdata/acoustic/compat/efficientat.py +243 -0
- justdata/acoustic/compat/panns.py +115 -0
- justdata/acoustic/compat/passt.py +191 -0
- justdata/acoustic/configs.py +519 -0
- justdata/acoustic/corruptions/__init__.py +33 -0
- justdata/acoustic/corruptions/codec.py +92 -0
- justdata/acoustic/corruptions/datasets.py +155 -0
- justdata/acoustic/corruptions/device.py +39 -0
- justdata/acoustic/corruptions/dynamics.py +55 -0
- justdata/acoustic/corruptions/filters.py +105 -0
- justdata/acoustic/corruptions/noise.py +173 -0
- justdata/acoustic/corruptions/registry.py +230 -0
- justdata/acoustic/corruptions/reverb.py +49 -0
- justdata/acoustic/corruptions/timefreq.py +127 -0
- justdata/acoustic/datasets.py +52 -0
- justdata/acoustic/dcase2025.py +814 -0
- justdata/acoustic/decoding.py +73 -0
- justdata/acoustic/eval_views.py +54 -0
- justdata/acoustic/evals.py +71 -0
- justdata/acoustic/filters.py +18 -0
- justdata/acoustic/frontends/__init__.py +37 -0
- justdata/acoustic/frontends/ast.py +116 -0
- justdata/acoustic/frontends/kaldi.py +34 -0
- justdata/acoustic/frontends/log.py +94 -0
- justdata/acoustic/frontends/mel.py +173 -0
- justdata/acoustic/frontends/mfcc.py +26 -0
- justdata/acoustic/frontends/pcen.py +26 -0
- justdata/acoustic/frontends/stft.py +174 -0
- justdata/acoustic/jax.py +23 -0
- justdata/acoustic/labels.py +169 -0
- justdata/acoustic/layouts.py +86 -0
- justdata/acoustic/metadata.py +152 -0
- justdata/acoustic/normalization.py +196 -0
- justdata/acoustic/pipelines.py +112 -0
- justdata/acoustic/postprocessing.py +328 -0
- justdata/acoustic/preprocessing.py +175 -0
- justdata/acoustic/presets.py +257 -0
- justdata/acoustic/registry.py +381 -0
- justdata/acoustic/resampling.py +218 -0
- justdata/acoustic/schema.py +51 -0
- justdata/acoustic/segment.py +369 -0
- justdata/acoustic/sources.py +503 -0
- justdata/acoustic/stages.py +59 -0
- justdata/acoustic/stats.py +50 -0
- justdata/acoustic/tasks.py +375 -0
- justdata/audio/__init__.py +20 -0
- justdata/core/__init__.py +74 -0
- justdata/core/adapters.py +46 -0
- justdata/core/augmentations.py +34 -0
- justdata/core/filters.py +94 -0
- justdata/core/finalization.py +173 -0
- justdata/core/label_mixing.py +129 -0
- justdata/core/loader.py +384 -0
- justdata/core/metadata.py +250 -0
- justdata/core/presets.py +155 -0
- justdata/core/registry.py +229 -0
- justdata/core/sources.py +87 -0
- justdata/core/stats.py +227 -0
- justdata/corruptions.py +52 -0
- justdata/py.typed +0 -0
- justdata/vision/__init__.py +17 -0
- justdata/vision/augmentations/__init__.py +31 -0
- justdata/vision/augmentations/auto.py +480 -0
- justdata/vision/augmentations/color.py +163 -0
- justdata/vision/augmentations/composed.py +156 -0
- justdata/vision/augmentations/geometric.py +302 -0
- justdata/vision/augmentations/mixing.py +232 -0
- justdata/vision/augmentations/registry.py +142 -0
- justdata/vision/corruptions/__init__.py +16 -0
- justdata/vision/corruptions/blur.py +65 -0
- justdata/vision/corruptions/digital.py +26 -0
- justdata/vision/corruptions/noise.py +20 -0
- justdata/vision/corruptions/registry.py +113 -0
- justdata/vision/corruptions/weather.py +100 -0
- justdata/vision/datasets.py +34 -0
- justdata/vision/encodings/__init__.py +1 -0
- justdata/vision/encodings/heatmaps.py +196 -0
- justdata/vision/minic.py +69 -0
- justdata/vision/pipelines.py +62 -0
- justdata/vision/presets.py +509 -0
- justdata/vision/sources.py +1080 -0
- justdata/vision/stages.py +271 -0
- justdata/vision/tasks/__init__.py +1 -0
- justdata/vision/tasks/classification.py +321 -0
- justdata/vision/tasks/depth.py +25 -0
- justdata/vision/tasks/segmentation.py +194 -0
- justdata/vision/transforms.py +113 -0
- justdata/vision/utils.py +741 -0
- justdata-1.0.0.dist-info/METADATA +860 -0
- justdata-1.0.0.dist-info/RECORD +106 -0
- justdata-1.0.0.dist-info/WHEEL +4 -0
- justdata-1.0.0.dist-info/licenses/LICENSE +23 -0
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import threading
|
|
2
|
+
from collections.abc import Callable
|
|
3
|
+
from typing import TypeVar
|
|
4
|
+
|
|
5
|
+
from justdata.core.augmentations import (
|
|
6
|
+
AugmentationDomain,
|
|
7
|
+
AugmentationMetadata,
|
|
8
|
+
attach_augmentation_metadata,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
RegistryFn = TypeVar("RegistryFn", bound=Callable)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
_REGISTRY_LOCK = threading.Lock()
|
|
16
|
+
|
|
17
|
+
_AUDIO_DECODERS: dict[str, Callable] = {}
|
|
18
|
+
_AUDIO_RESAMPLERS: dict[str, Callable] = {}
|
|
19
|
+
_AUDIO_CHANNEL_STRATEGIES: dict[str, Callable] = {}
|
|
20
|
+
_AUDIO_SEGMENT_STRATEGIES: dict[str, Callable] = {}
|
|
21
|
+
_AUDIO_FRONTENDS: dict[str, Callable] = {}
|
|
22
|
+
_AUDIO_WAVEFORM_AUGMENTS: dict[str, Callable] = {}
|
|
23
|
+
_AUDIO_SPECTROGRAM_AUGMENTS: dict[str, Callable] = {}
|
|
24
|
+
_AUDIO_BATCH_AUGMENTS: dict[str, Callable] = {}
|
|
25
|
+
_AUDIO_WAVEFORM_AUGMENT_METADATA: dict[str, AugmentationMetadata] = {}
|
|
26
|
+
_AUDIO_SPECTROGRAM_AUGMENT_METADATA: dict[str, AugmentationMetadata] = {}
|
|
27
|
+
_AUDIO_BATCH_AUGMENT_METADATA: dict[str, AugmentationMetadata] = {}
|
|
28
|
+
_AUDIO_NORMALIZATIONS: dict[str, Callable] = {}
|
|
29
|
+
_AUDIO_EVAL_VIEW_STRATEGIES: dict[str, Callable] = {}
|
|
30
|
+
_AUDIO_POSTPROCESSORS: dict[str, Callable] = {}
|
|
31
|
+
_AUDIO_CORRUPTIONS: dict[str, Callable] = {}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _register(registry: dict[str, Callable], label: str, name: str):
|
|
35
|
+
def decorator(fn: RegistryFn) -> RegistryFn:
|
|
36
|
+
with _REGISTRY_LOCK:
|
|
37
|
+
if name in registry:
|
|
38
|
+
existing = registry[name]
|
|
39
|
+
raise ValueError(
|
|
40
|
+
f"{label} '{name}' already registered by "
|
|
41
|
+
f"{existing.__module__}.{existing.__qualname__}"
|
|
42
|
+
)
|
|
43
|
+
registry[name] = fn
|
|
44
|
+
return fn
|
|
45
|
+
|
|
46
|
+
return decorator
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _register_augment(
|
|
50
|
+
registry: dict[str, Callable],
|
|
51
|
+
metadata_registry: dict[str, AugmentationMetadata],
|
|
52
|
+
label: str,
|
|
53
|
+
name: str,
|
|
54
|
+
*,
|
|
55
|
+
domain: AugmentationDomain,
|
|
56
|
+
is_training_only: bool = True,
|
|
57
|
+
requires_labels: bool = False,
|
|
58
|
+
):
|
|
59
|
+
metadata = AugmentationMetadata(
|
|
60
|
+
name=name,
|
|
61
|
+
domain=domain,
|
|
62
|
+
is_training_only=is_training_only,
|
|
63
|
+
requires_labels=requires_labels,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
def decorator(fn: RegistryFn) -> RegistryFn:
|
|
67
|
+
with _REGISTRY_LOCK:
|
|
68
|
+
if name in registry:
|
|
69
|
+
existing = registry[name]
|
|
70
|
+
raise ValueError(
|
|
71
|
+
f"{label} '{name}' already registered by "
|
|
72
|
+
f"{existing.__module__}.{existing.__qualname__}"
|
|
73
|
+
)
|
|
74
|
+
registry[name] = fn
|
|
75
|
+
metadata_registry[name] = metadata
|
|
76
|
+
attach_augmentation_metadata(fn, metadata)
|
|
77
|
+
return fn
|
|
78
|
+
|
|
79
|
+
return decorator
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _get(registry: dict[str, Callable], label: str, name: str) -> Callable:
|
|
83
|
+
if name not in registry:
|
|
84
|
+
available = ", ".join(sorted(registry)) or "<none>"
|
|
85
|
+
raise ValueError(f"{label} '{name}' not found. Available: {available}")
|
|
86
|
+
return registry[name]
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _list(registry: dict[str, Callable]) -> tuple[str, ...]:
|
|
90
|
+
return tuple(sorted(registry))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _has(registry: dict[str, Callable], name: str) -> bool:
|
|
94
|
+
return name in registry
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _get_metadata(
|
|
98
|
+
registry: dict[str, AugmentationMetadata],
|
|
99
|
+
label: str,
|
|
100
|
+
name: str,
|
|
101
|
+
) -> AugmentationMetadata:
|
|
102
|
+
if name not in registry:
|
|
103
|
+
available = ", ".join(sorted(registry)) or "<none>"
|
|
104
|
+
raise ValueError(f"{label} metadata '{name}' not found. Available: {available}")
|
|
105
|
+
return registry[name]
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _list_metadata(
|
|
109
|
+
registry: dict[str, AugmentationMetadata],
|
|
110
|
+
) -> tuple[AugmentationMetadata, ...]:
|
|
111
|
+
return tuple(registry[name] for name in sorted(registry))
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def register_audio_decoder(name: str):
|
|
115
|
+
return _register(_AUDIO_DECODERS, "Audio decoder", name)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def get_audio_decoder(name: str) -> Callable:
|
|
119
|
+
return _get(_AUDIO_DECODERS, "Audio decoder", name)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def list_audio_decoders() -> tuple[str, ...]:
|
|
123
|
+
return _list(_AUDIO_DECODERS)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def has_audio_decoder(name: str) -> bool:
|
|
127
|
+
return _has(_AUDIO_DECODERS, name)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def register_audio_resampler(name: str):
|
|
131
|
+
return _register(_AUDIO_RESAMPLERS, "Audio resampler", name)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def get_audio_resampler(name: str) -> Callable:
|
|
135
|
+
return _get(_AUDIO_RESAMPLERS, "Audio resampler", name)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def list_audio_resamplers() -> tuple[str, ...]:
|
|
139
|
+
return _list(_AUDIO_RESAMPLERS)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def has_audio_resampler(name: str) -> bool:
|
|
143
|
+
return _has(_AUDIO_RESAMPLERS, name)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def register_audio_channel_strategy(name: str):
|
|
147
|
+
return _register(_AUDIO_CHANNEL_STRATEGIES, "Audio channel strategy", name)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def get_audio_channel_strategy(name: str) -> Callable:
|
|
151
|
+
return _get(_AUDIO_CHANNEL_STRATEGIES, "Audio channel strategy", name)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def list_audio_channel_strategies() -> tuple[str, ...]:
|
|
155
|
+
return _list(_AUDIO_CHANNEL_STRATEGIES)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def has_audio_channel_strategy(name: str) -> bool:
|
|
159
|
+
return _has(_AUDIO_CHANNEL_STRATEGIES, name)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def register_audio_segment_strategy(name: str):
|
|
163
|
+
return _register(_AUDIO_SEGMENT_STRATEGIES, "Audio segment strategy", name)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def get_audio_segment_strategy(name: str) -> Callable:
|
|
167
|
+
return _get(_AUDIO_SEGMENT_STRATEGIES, "Audio segment strategy", name)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def list_audio_segment_strategies() -> tuple[str, ...]:
|
|
171
|
+
return _list(_AUDIO_SEGMENT_STRATEGIES)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def has_audio_segment_strategy(name: str) -> bool:
|
|
175
|
+
return _has(_AUDIO_SEGMENT_STRATEGIES, name)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def register_audio_frontend(name: str):
|
|
179
|
+
return _register(_AUDIO_FRONTENDS, "Audio frontend", name)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def get_audio_frontend(name: str) -> Callable:
|
|
183
|
+
return _get(_AUDIO_FRONTENDS, "Audio frontend", name)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def list_audio_frontends() -> tuple[str, ...]:
|
|
187
|
+
return _list(_AUDIO_FRONTENDS)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def has_audio_frontend(name: str) -> bool:
|
|
191
|
+
return _has(_AUDIO_FRONTENDS, name)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def register_audio_waveform_augment(
|
|
195
|
+
name: str,
|
|
196
|
+
*,
|
|
197
|
+
is_training_only: bool = True,
|
|
198
|
+
requires_labels: bool = False,
|
|
199
|
+
):
|
|
200
|
+
return _register_augment(
|
|
201
|
+
_AUDIO_WAVEFORM_AUGMENTS,
|
|
202
|
+
_AUDIO_WAVEFORM_AUGMENT_METADATA,
|
|
203
|
+
"Audio waveform augment",
|
|
204
|
+
name,
|
|
205
|
+
domain="waveform",
|
|
206
|
+
is_training_only=is_training_only,
|
|
207
|
+
requires_labels=requires_labels,
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def get_audio_waveform_augment(name: str) -> Callable:
|
|
212
|
+
fn = _get(_AUDIO_WAVEFORM_AUGMENTS, "Audio waveform augment", name)
|
|
213
|
+
return attach_augmentation_metadata(fn, _AUDIO_WAVEFORM_AUGMENT_METADATA[name])
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def list_audio_waveform_augments() -> tuple[str, ...]:
|
|
217
|
+
return _list(_AUDIO_WAVEFORM_AUGMENTS)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def get_audio_waveform_augment_metadata(name: str) -> AugmentationMetadata:
|
|
221
|
+
return _get_metadata(
|
|
222
|
+
_AUDIO_WAVEFORM_AUGMENT_METADATA,
|
|
223
|
+
"Audio waveform augment",
|
|
224
|
+
name,
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def list_audio_waveform_augment_metadata() -> tuple[AugmentationMetadata, ...]:
|
|
229
|
+
return _list_metadata(_AUDIO_WAVEFORM_AUGMENT_METADATA)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def has_audio_waveform_augment(name: str) -> bool:
|
|
233
|
+
return _has(_AUDIO_WAVEFORM_AUGMENTS, name)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def register_audio_spectrogram_augment(
|
|
237
|
+
name: str,
|
|
238
|
+
*,
|
|
239
|
+
is_training_only: bool = True,
|
|
240
|
+
requires_labels: bool = False,
|
|
241
|
+
):
|
|
242
|
+
return _register_augment(
|
|
243
|
+
_AUDIO_SPECTROGRAM_AUGMENTS,
|
|
244
|
+
_AUDIO_SPECTROGRAM_AUGMENT_METADATA,
|
|
245
|
+
"Audio spectrogram augment",
|
|
246
|
+
name,
|
|
247
|
+
domain="spectrogram",
|
|
248
|
+
is_training_only=is_training_only,
|
|
249
|
+
requires_labels=requires_labels,
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def get_audio_spectrogram_augment(name: str) -> Callable:
|
|
254
|
+
fn = _get(_AUDIO_SPECTROGRAM_AUGMENTS, "Audio spectrogram augment", name)
|
|
255
|
+
return attach_augmentation_metadata(fn, _AUDIO_SPECTROGRAM_AUGMENT_METADATA[name])
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def list_audio_spectrogram_augments() -> tuple[str, ...]:
|
|
259
|
+
return _list(_AUDIO_SPECTROGRAM_AUGMENTS)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def get_audio_spectrogram_augment_metadata(name: str) -> AugmentationMetadata:
|
|
263
|
+
return _get_metadata(
|
|
264
|
+
_AUDIO_SPECTROGRAM_AUGMENT_METADATA,
|
|
265
|
+
"Audio spectrogram augment",
|
|
266
|
+
name,
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def list_audio_spectrogram_augment_metadata() -> tuple[AugmentationMetadata, ...]:
|
|
271
|
+
return _list_metadata(_AUDIO_SPECTROGRAM_AUGMENT_METADATA)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def has_audio_spectrogram_augment(name: str) -> bool:
|
|
275
|
+
return _has(_AUDIO_SPECTROGRAM_AUGMENTS, name)
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def register_audio_batch_augment(
|
|
279
|
+
name: str,
|
|
280
|
+
*,
|
|
281
|
+
is_training_only: bool = True,
|
|
282
|
+
requires_labels: bool = False,
|
|
283
|
+
):
|
|
284
|
+
return _register_augment(
|
|
285
|
+
_AUDIO_BATCH_AUGMENTS,
|
|
286
|
+
_AUDIO_BATCH_AUGMENT_METADATA,
|
|
287
|
+
"Audio batch augment",
|
|
288
|
+
name,
|
|
289
|
+
domain="batch",
|
|
290
|
+
is_training_only=is_training_only,
|
|
291
|
+
requires_labels=requires_labels,
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def get_audio_batch_augment(name: str) -> Callable:
|
|
296
|
+
fn = _get(_AUDIO_BATCH_AUGMENTS, "Audio batch augment", name)
|
|
297
|
+
return attach_augmentation_metadata(fn, _AUDIO_BATCH_AUGMENT_METADATA[name])
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def list_audio_batch_augments() -> tuple[str, ...]:
|
|
301
|
+
return _list(_AUDIO_BATCH_AUGMENTS)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def get_audio_batch_augment_metadata(name: str) -> AugmentationMetadata:
|
|
305
|
+
return _get_metadata(
|
|
306
|
+
_AUDIO_BATCH_AUGMENT_METADATA,
|
|
307
|
+
"Audio batch augment",
|
|
308
|
+
name,
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
def list_audio_batch_augment_metadata() -> tuple[AugmentationMetadata, ...]:
|
|
313
|
+
return _list_metadata(_AUDIO_BATCH_AUGMENT_METADATA)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def has_audio_batch_augment(name: str) -> bool:
|
|
317
|
+
return _has(_AUDIO_BATCH_AUGMENTS, name)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def register_audio_normalization(name: str):
|
|
321
|
+
return _register(_AUDIO_NORMALIZATIONS, "Audio normalization", name)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def get_audio_normalization(name: str) -> Callable:
|
|
325
|
+
return _get(_AUDIO_NORMALIZATIONS, "Audio normalization", name)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def list_audio_normalizations() -> tuple[str, ...]:
|
|
329
|
+
return _list(_AUDIO_NORMALIZATIONS)
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def has_audio_normalization(name: str) -> bool:
|
|
333
|
+
return _has(_AUDIO_NORMALIZATIONS, name)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def register_audio_eval_view_strategy(name: str):
|
|
337
|
+
return _register(_AUDIO_EVAL_VIEW_STRATEGIES, "Audio eval view strategy", name)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def get_audio_eval_view_strategy(name: str) -> Callable:
|
|
341
|
+
return _get(_AUDIO_EVAL_VIEW_STRATEGIES, "Audio eval view strategy", name)
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def list_audio_eval_view_strategies() -> tuple[str, ...]:
|
|
345
|
+
return _list(_AUDIO_EVAL_VIEW_STRATEGIES)
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def has_audio_eval_view_strategy(name: str) -> bool:
|
|
349
|
+
return _has(_AUDIO_EVAL_VIEW_STRATEGIES, name)
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def register_audio_postprocessor(name: str):
|
|
353
|
+
return _register(_AUDIO_POSTPROCESSORS, "Audio postprocessor", name)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def get_audio_postprocessor(name: str) -> Callable:
|
|
357
|
+
return _get(_AUDIO_POSTPROCESSORS, "Audio postprocessor", name)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def list_audio_postprocessors() -> tuple[str, ...]:
|
|
361
|
+
return _list(_AUDIO_POSTPROCESSORS)
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def has_audio_postprocessor(name: str) -> bool:
|
|
365
|
+
return _has(_AUDIO_POSTPROCESSORS, name)
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def register_audio_corruption(name: str):
|
|
369
|
+
return _register(_AUDIO_CORRUPTIONS, "Audio corruption", name)
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def get_audio_corruption(name: str) -> Callable:
|
|
373
|
+
return _get(_AUDIO_CORRUPTIONS, "Audio corruption", name)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def list_audio_corruptions() -> tuple[str, ...]:
|
|
377
|
+
return _list(_AUDIO_CORRUPTIONS)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def has_audio_corruption(name: str) -> bool:
|
|
381
|
+
return _has(_AUDIO_CORRUPTIONS, name)
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import tensorflow as tf
|
|
5
|
+
|
|
6
|
+
from justdata.acoustic.registry import register_audio_resampler
|
|
7
|
+
from justdata.acoustic.schema import SAMPLE_RATE, WAVEFORM
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _resample_tensorflow(
|
|
11
|
+
waveform: tf.Tensor,
|
|
12
|
+
original_sample_rate: tf.Tensor | int,
|
|
13
|
+
target_sample_rate: tf.Tensor | int,
|
|
14
|
+
) -> tf.Tensor:
|
|
15
|
+
"""Resample [T, C] waveform with TensorFlow linear interpolation."""
|
|
16
|
+
original_sample_rate = tf.cast(original_sample_rate, tf.float32)
|
|
17
|
+
target_sample_rate = tf.cast(target_sample_rate, tf.float32)
|
|
18
|
+
|
|
19
|
+
num_samples = tf.shape(waveform)[0]
|
|
20
|
+
num_channels = tf.shape(waveform)[1]
|
|
21
|
+
target_samples = tf.cast(
|
|
22
|
+
tf.round(
|
|
23
|
+
tf.cast(num_samples, tf.float32) * target_sample_rate / original_sample_rate
|
|
24
|
+
),
|
|
25
|
+
tf.int32,
|
|
26
|
+
)
|
|
27
|
+
target_samples = tf.maximum(target_samples, 1)
|
|
28
|
+
|
|
29
|
+
channels_first = tf.transpose(waveform, [1, 0])
|
|
30
|
+
image = channels_first[:, :, tf.newaxis]
|
|
31
|
+
resized = tf.image.resize(
|
|
32
|
+
image,
|
|
33
|
+
[num_channels, target_samples],
|
|
34
|
+
method="bilinear",
|
|
35
|
+
antialias=True,
|
|
36
|
+
)
|
|
37
|
+
result = tf.transpose(tf.squeeze(resized, axis=-1), [1, 0])
|
|
38
|
+
result.set_shape([None, waveform.shape[-1]])
|
|
39
|
+
return result
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _soxr_resample_numpy(
|
|
43
|
+
waveform: np.ndarray,
|
|
44
|
+
original_sample_rate: np.ndarray,
|
|
45
|
+
target_sample_rate: np.ndarray,
|
|
46
|
+
) -> np.ndarray:
|
|
47
|
+
try:
|
|
48
|
+
import soxr
|
|
49
|
+
except ImportError as exc: # pragma: no cover - depends on optional extra.
|
|
50
|
+
raise ImportError("Install the acoustic extra to use soxr resampling.") from exc
|
|
51
|
+
|
|
52
|
+
result = soxr.resample(
|
|
53
|
+
waveform.astype(np.float32, copy=False),
|
|
54
|
+
int(original_sample_rate),
|
|
55
|
+
int(target_sample_rate),
|
|
56
|
+
axis=0,
|
|
57
|
+
)
|
|
58
|
+
return result.astype(np.float32, copy=False)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _kaiser_best_resample_numpy(
|
|
62
|
+
waveform: np.ndarray,
|
|
63
|
+
original_sample_rate: np.ndarray,
|
|
64
|
+
target_sample_rate: np.ndarray,
|
|
65
|
+
) -> np.ndarray:
|
|
66
|
+
try:
|
|
67
|
+
import librosa
|
|
68
|
+
except ImportError:
|
|
69
|
+
return _soxr_resample_numpy(waveform, original_sample_rate, target_sample_rate)
|
|
70
|
+
|
|
71
|
+
result = librosa.resample(
|
|
72
|
+
waveform.astype(np.float32, copy=False),
|
|
73
|
+
orig_sr=int(original_sample_rate),
|
|
74
|
+
target_sr=int(target_sample_rate),
|
|
75
|
+
res_type="kaiser_best",
|
|
76
|
+
axis=0,
|
|
77
|
+
)
|
|
78
|
+
return result.astype(np.float32, copy=False)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _numpy_resample(
|
|
82
|
+
waveform: tf.Tensor,
|
|
83
|
+
original_sample_rate: tf.Tensor | int,
|
|
84
|
+
target_sample_rate: tf.Tensor | int,
|
|
85
|
+
*,
|
|
86
|
+
method: str,
|
|
87
|
+
) -> tf.Tensor:
|
|
88
|
+
"""Resample through optional NumPy backends.
|
|
89
|
+
|
|
90
|
+
This path is deterministic for fixed inputs but is not graph-pure because
|
|
91
|
+
it uses ``tf.numpy_function``.
|
|
92
|
+
"""
|
|
93
|
+
fn = _soxr_resample_numpy if method == "soxr" else _kaiser_best_resample_numpy
|
|
94
|
+
result = tf.numpy_function(
|
|
95
|
+
fn,
|
|
96
|
+
[
|
|
97
|
+
waveform,
|
|
98
|
+
tf.cast(original_sample_rate, tf.int32),
|
|
99
|
+
tf.cast(target_sample_rate, tf.int32),
|
|
100
|
+
],
|
|
101
|
+
Tout=tf.float32,
|
|
102
|
+
)
|
|
103
|
+
result.set_shape([None, waveform.shape[-1]])
|
|
104
|
+
return result
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _assert_same_sample_rate(
|
|
108
|
+
waveform: tf.Tensor,
|
|
109
|
+
original_sample_rate: tf.Tensor | int,
|
|
110
|
+
target_sample_rate: tf.Tensor | int,
|
|
111
|
+
*,
|
|
112
|
+
method: str,
|
|
113
|
+
) -> tf.Tensor:
|
|
114
|
+
with tf.control_dependencies(
|
|
115
|
+
[
|
|
116
|
+
tf.debugging.assert_equal(
|
|
117
|
+
tf.cast(original_sample_rate, tf.int32),
|
|
118
|
+
tf.cast(target_sample_rate, tf.int32),
|
|
119
|
+
message=f"{method} resampling requires audio decoded at target_sample_rate",
|
|
120
|
+
)
|
|
121
|
+
]
|
|
122
|
+
):
|
|
123
|
+
return tf.identity(waveform)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def resample_waveform(
|
|
127
|
+
waveform: tf.Tensor,
|
|
128
|
+
original_sample_rate: tf.Tensor | int,
|
|
129
|
+
target_sample_rate: tf.Tensor | int,
|
|
130
|
+
method: str = "tensorflow",
|
|
131
|
+
) -> tf.Tensor:
|
|
132
|
+
"""Resample a ``[T, C]`` waveform to ``target_sample_rate``."""
|
|
133
|
+
waveform = tf.convert_to_tensor(waveform, dtype=tf.float32)
|
|
134
|
+
same_rate = tf.equal(
|
|
135
|
+
tf.cast(original_sample_rate, tf.int32),
|
|
136
|
+
tf.cast(target_sample_rate, tf.int32),
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
if method in {"identity", "hf_audio"}:
|
|
140
|
+
return _assert_same_sample_rate(
|
|
141
|
+
waveform,
|
|
142
|
+
original_sample_rate,
|
|
143
|
+
target_sample_rate,
|
|
144
|
+
method=method,
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
if method == "tensorflow":
|
|
148
|
+
return tf.cond(
|
|
149
|
+
same_rate,
|
|
150
|
+
lambda: waveform,
|
|
151
|
+
lambda: _resample_tensorflow(
|
|
152
|
+
waveform, original_sample_rate, target_sample_rate
|
|
153
|
+
),
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
if method in {"soxr", "kaiser_best"}:
|
|
157
|
+
return tf.cond(
|
|
158
|
+
same_rate,
|
|
159
|
+
lambda: waveform,
|
|
160
|
+
lambda: _numpy_resample(
|
|
161
|
+
waveform,
|
|
162
|
+
original_sample_rate,
|
|
163
|
+
target_sample_rate,
|
|
164
|
+
method=method,
|
|
165
|
+
),
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
raise ValueError(f"Unknown resampling method: {method!r}")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
@register_audio_resampler("tensorflow")
|
|
172
|
+
def resample_sample(
|
|
173
|
+
sample: dict,
|
|
174
|
+
*,
|
|
175
|
+
target_sample_rate: int,
|
|
176
|
+
method: str = "tensorflow",
|
|
177
|
+
) -> dict:
|
|
178
|
+
result = dict(sample)
|
|
179
|
+
result[WAVEFORM] = resample_waveform(
|
|
180
|
+
result[WAVEFORM],
|
|
181
|
+
result[SAMPLE_RATE],
|
|
182
|
+
target_sample_rate,
|
|
183
|
+
method=method,
|
|
184
|
+
)
|
|
185
|
+
result[SAMPLE_RATE] = tf.cast(target_sample_rate, tf.int32)
|
|
186
|
+
return result
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
@register_audio_resampler("soxr")
|
|
190
|
+
def resample_sample_soxr(sample: dict, *, target_sample_rate: int) -> dict:
|
|
191
|
+
return resample_sample(sample, target_sample_rate=target_sample_rate, method="soxr")
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
@register_audio_resampler("kaiser_best")
|
|
195
|
+
def resample_sample_kaiser_best(sample: dict, *, target_sample_rate: int) -> dict:
|
|
196
|
+
return resample_sample(
|
|
197
|
+
sample,
|
|
198
|
+
target_sample_rate=target_sample_rate,
|
|
199
|
+
method="kaiser_best",
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@register_audio_resampler("hf_audio")
|
|
204
|
+
def resample_sample_hf_audio(sample: dict, *, target_sample_rate: int) -> dict:
|
|
205
|
+
return resample_sample(
|
|
206
|
+
sample,
|
|
207
|
+
target_sample_rate=target_sample_rate,
|
|
208
|
+
method="hf_audio",
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
__all__ = [
|
|
213
|
+
"resample_sample",
|
|
214
|
+
"resample_sample_hf_audio",
|
|
215
|
+
"resample_sample_kaiser_best",
|
|
216
|
+
"resample_sample_soxr",
|
|
217
|
+
"resample_waveform",
|
|
218
|
+
]
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from typing import Any, NotRequired, TypedDict
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
AUDIO = "audio"
|
|
5
|
+
WAVEFORM = "waveform"
|
|
6
|
+
SAMPLE_RATE = "sample_rate"
|
|
7
|
+
ORIGINAL_SAMPLE_RATE = "original_sample_rate"
|
|
8
|
+
LABEL = "label"
|
|
9
|
+
FEATURES = "features"
|
|
10
|
+
DURATION = "duration"
|
|
11
|
+
METADATA = "metadata"
|
|
12
|
+
PATH = "path"
|
|
13
|
+
FILENAME = "filename"
|
|
14
|
+
DATASET = "dataset"
|
|
15
|
+
SPLIT = "split"
|
|
16
|
+
EXAMPLE_ID = "example_id"
|
|
17
|
+
CLIP_ID = "clip_id"
|
|
18
|
+
SOURCE_ID = "source_id"
|
|
19
|
+
START_TIME = "start_time"
|
|
20
|
+
END_TIME = "end_time"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class AudioSample(TypedDict):
|
|
24
|
+
waveform: Any
|
|
25
|
+
sample_rate: int
|
|
26
|
+
label: NotRequired[Any]
|
|
27
|
+
features: NotRequired[Any]
|
|
28
|
+
duration: NotRequired[float]
|
|
29
|
+
metadata: NotRequired[dict[str, Any]]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"AUDIO",
|
|
34
|
+
"CLIP_ID",
|
|
35
|
+
"DATASET",
|
|
36
|
+
"DURATION",
|
|
37
|
+
"END_TIME",
|
|
38
|
+
"EXAMPLE_ID",
|
|
39
|
+
"FEATURES",
|
|
40
|
+
"FILENAME",
|
|
41
|
+
"LABEL",
|
|
42
|
+
"METADATA",
|
|
43
|
+
"ORIGINAL_SAMPLE_RATE",
|
|
44
|
+
"PATH",
|
|
45
|
+
"SAMPLE_RATE",
|
|
46
|
+
"SOURCE_ID",
|
|
47
|
+
"SPLIT",
|
|
48
|
+
"START_TIME",
|
|
49
|
+
"WAVEFORM",
|
|
50
|
+
"AudioSample",
|
|
51
|
+
]
|