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.
Files changed (106) hide show
  1. justdata/__init__.py +3 -0
  2. justdata/acoustic/__init__.py +381 -0
  3. justdata/acoustic/adapters.py +329 -0
  4. justdata/acoustic/augment/__init__.py +119 -0
  5. justdata/acoustic/augment/batch.py +813 -0
  6. justdata/acoustic/augment/configs.py +204 -0
  7. justdata/acoustic/augment/patchout.py +331 -0
  8. justdata/acoustic/augment/spectrogram.py +806 -0
  9. justdata/acoustic/augment/waveform.py +879 -0
  10. justdata/acoustic/batching.py +27 -0
  11. justdata/acoustic/channel.py +53 -0
  12. justdata/acoustic/compat/__init__.py +95 -0
  13. justdata/acoustic/compat/ast.py +626 -0
  14. justdata/acoustic/compat/ced.py +152 -0
  15. justdata/acoustic/compat/efficientat.py +243 -0
  16. justdata/acoustic/compat/panns.py +115 -0
  17. justdata/acoustic/compat/passt.py +191 -0
  18. justdata/acoustic/configs.py +519 -0
  19. justdata/acoustic/corruptions/__init__.py +33 -0
  20. justdata/acoustic/corruptions/codec.py +92 -0
  21. justdata/acoustic/corruptions/datasets.py +155 -0
  22. justdata/acoustic/corruptions/device.py +39 -0
  23. justdata/acoustic/corruptions/dynamics.py +55 -0
  24. justdata/acoustic/corruptions/filters.py +105 -0
  25. justdata/acoustic/corruptions/noise.py +173 -0
  26. justdata/acoustic/corruptions/registry.py +230 -0
  27. justdata/acoustic/corruptions/reverb.py +49 -0
  28. justdata/acoustic/corruptions/timefreq.py +127 -0
  29. justdata/acoustic/datasets.py +52 -0
  30. justdata/acoustic/dcase2025.py +814 -0
  31. justdata/acoustic/decoding.py +73 -0
  32. justdata/acoustic/eval_views.py +54 -0
  33. justdata/acoustic/evals.py +71 -0
  34. justdata/acoustic/filters.py +18 -0
  35. justdata/acoustic/frontends/__init__.py +37 -0
  36. justdata/acoustic/frontends/ast.py +116 -0
  37. justdata/acoustic/frontends/kaldi.py +34 -0
  38. justdata/acoustic/frontends/log.py +94 -0
  39. justdata/acoustic/frontends/mel.py +173 -0
  40. justdata/acoustic/frontends/mfcc.py +26 -0
  41. justdata/acoustic/frontends/pcen.py +26 -0
  42. justdata/acoustic/frontends/stft.py +174 -0
  43. justdata/acoustic/jax.py +23 -0
  44. justdata/acoustic/labels.py +169 -0
  45. justdata/acoustic/layouts.py +86 -0
  46. justdata/acoustic/metadata.py +152 -0
  47. justdata/acoustic/normalization.py +196 -0
  48. justdata/acoustic/pipelines.py +112 -0
  49. justdata/acoustic/postprocessing.py +328 -0
  50. justdata/acoustic/preprocessing.py +175 -0
  51. justdata/acoustic/presets.py +257 -0
  52. justdata/acoustic/registry.py +381 -0
  53. justdata/acoustic/resampling.py +218 -0
  54. justdata/acoustic/schema.py +51 -0
  55. justdata/acoustic/segment.py +369 -0
  56. justdata/acoustic/sources.py +503 -0
  57. justdata/acoustic/stages.py +59 -0
  58. justdata/acoustic/stats.py +50 -0
  59. justdata/acoustic/tasks.py +375 -0
  60. justdata/audio/__init__.py +20 -0
  61. justdata/core/__init__.py +74 -0
  62. justdata/core/adapters.py +46 -0
  63. justdata/core/augmentations.py +34 -0
  64. justdata/core/filters.py +94 -0
  65. justdata/core/finalization.py +173 -0
  66. justdata/core/label_mixing.py +129 -0
  67. justdata/core/loader.py +384 -0
  68. justdata/core/metadata.py +250 -0
  69. justdata/core/presets.py +155 -0
  70. justdata/core/registry.py +229 -0
  71. justdata/core/sources.py +87 -0
  72. justdata/core/stats.py +227 -0
  73. justdata/corruptions.py +52 -0
  74. justdata/py.typed +0 -0
  75. justdata/vision/__init__.py +17 -0
  76. justdata/vision/augmentations/__init__.py +31 -0
  77. justdata/vision/augmentations/auto.py +480 -0
  78. justdata/vision/augmentations/color.py +163 -0
  79. justdata/vision/augmentations/composed.py +156 -0
  80. justdata/vision/augmentations/geometric.py +302 -0
  81. justdata/vision/augmentations/mixing.py +232 -0
  82. justdata/vision/augmentations/registry.py +142 -0
  83. justdata/vision/corruptions/__init__.py +16 -0
  84. justdata/vision/corruptions/blur.py +65 -0
  85. justdata/vision/corruptions/digital.py +26 -0
  86. justdata/vision/corruptions/noise.py +20 -0
  87. justdata/vision/corruptions/registry.py +113 -0
  88. justdata/vision/corruptions/weather.py +100 -0
  89. justdata/vision/datasets.py +34 -0
  90. justdata/vision/encodings/__init__.py +1 -0
  91. justdata/vision/encodings/heatmaps.py +196 -0
  92. justdata/vision/minic.py +69 -0
  93. justdata/vision/pipelines.py +62 -0
  94. justdata/vision/presets.py +509 -0
  95. justdata/vision/sources.py +1080 -0
  96. justdata/vision/stages.py +271 -0
  97. justdata/vision/tasks/__init__.py +1 -0
  98. justdata/vision/tasks/classification.py +321 -0
  99. justdata/vision/tasks/depth.py +25 -0
  100. justdata/vision/tasks/segmentation.py +194 -0
  101. justdata/vision/transforms.py +113 -0
  102. justdata/vision/utils.py +741 -0
  103. justdata-1.0.0.dist-info/METADATA +860 -0
  104. justdata-1.0.0.dist-info/RECORD +106 -0
  105. justdata-1.0.0.dist-info/WHEEL +4 -0
  106. justdata-1.0.0.dist-info/licenses/LICENSE +23 -0
@@ -0,0 +1,879 @@
1
+ from __future__ import annotations
2
+
3
+ import dataclasses
4
+ from collections.abc import Mapping, Sequence
5
+ from typing import Any
6
+
7
+ import tensorflow as tf
8
+
9
+ from justdata.acoustic.adapters import infer_duration
10
+ from justdata.acoustic.augment.configs import (
11
+ AdditiveNoiseConfig,
12
+ CodecSimulationConfig,
13
+ DynamicRangeCompressionConfig,
14
+ RIRConvolutionConfig,
15
+ RandomClippingConfig,
16
+ RandomGainConfig,
17
+ RandomTimeShiftConfig,
18
+ SpeedPerturbConfig,
19
+ )
20
+ from justdata.acoustic.registry import (
21
+ get_audio_waveform_augment,
22
+ register_audio_waveform_augment,
23
+ )
24
+ from justdata.acoustic.schema import DURATION, SAMPLE_RATE, WAVEFORM
25
+
26
+
27
+ _EPS = tf.constant(1e-8, dtype=tf.float32)
28
+
29
+
30
+ def _seed_tensor(seed: tf.Tensor | int | None) -> tf.Tensor:
31
+ if seed is None:
32
+ return tf.constant([0, 0], dtype=tf.int32)
33
+
34
+ seed = tf.cast(tf.convert_to_tensor(seed), tf.int32)
35
+ if seed.shape.rank == 0:
36
+ return tf.stack([seed, tf.constant(0, dtype=tf.int32)])
37
+ if seed.shape.rank == 1 and seed.shape[0] == 1:
38
+ return tf.stack([seed[0], tf.constant(0, dtype=tf.int32)])
39
+ return seed[:2]
40
+
41
+
42
+ def _as_waveform_tc(waveform: tf.Tensor) -> tuple[tf.Tensor, int | None]:
43
+ waveform = tf.convert_to_tensor(waveform)
44
+ rank = waveform.shape.rank
45
+ if rank == 1:
46
+ return tf.expand_dims(waveform, -1), rank
47
+ if rank != 2:
48
+ raise ValueError("Waveform augment input must have shape [T] or [T, C]")
49
+ return waveform, rank
50
+
51
+
52
+ def _restore_rank(waveform: tf.Tensor, rank: int | None) -> tf.Tensor:
53
+ if rank == 1:
54
+ return tf.squeeze(waveform, axis=-1)
55
+ return waveform
56
+
57
+
58
+ def _config_from(
59
+ config: Any,
60
+ config_cls: type,
61
+ overrides: Mapping[str, Any],
62
+ ) -> Any:
63
+ data = {}
64
+ if config is not None:
65
+ if isinstance(config, config_cls):
66
+ data.update(dataclasses.asdict(config))
67
+ elif dataclasses.is_dataclass(config):
68
+ data.update(dataclasses.asdict(config))
69
+ elif isinstance(config, Mapping):
70
+ data.update(config)
71
+ else:
72
+ raise TypeError(f"config must be a {config_cls.__name__}, mapping, or None")
73
+ data.update({key: value for key, value in overrides.items() if value is not None})
74
+ return config_cls(**data)
75
+
76
+
77
+ def _enabled(is_training: bool, augment_eval: bool) -> bool:
78
+ return bool(is_training or augment_eval)
79
+
80
+
81
+ def _uniform_float(seed: tf.Tensor, minval: float, maxval: float) -> tf.Tensor:
82
+ if minval == maxval:
83
+ return tf.constant(minval, dtype=tf.float32)
84
+ return tf.random.stateless_uniform(
85
+ [],
86
+ seed=seed,
87
+ minval=tf.cast(minval, tf.float32),
88
+ maxval=tf.cast(maxval, tf.float32),
89
+ dtype=tf.float32,
90
+ )
91
+
92
+
93
+ def _uniform_int(seed: tf.Tensor, minval: tf.Tensor, maxval: tf.Tensor) -> tf.Tensor:
94
+ return tf.cond(
95
+ tf.equal(minval, maxval),
96
+ lambda: tf.cast(minval, tf.int32),
97
+ lambda: tf.random.stateless_uniform(
98
+ [],
99
+ seed=seed,
100
+ minval=tf.cast(minval, tf.int32),
101
+ maxval=tf.cast(maxval + 1, tf.int32),
102
+ dtype=tf.int32,
103
+ ),
104
+ )
105
+
106
+
107
+ def _maybe_apply(
108
+ waveform: tf.Tensor,
109
+ prob: float,
110
+ seed: tf.Tensor,
111
+ apply_fn,
112
+ ) -> tf.Tensor:
113
+ if prob <= 0.0:
114
+ return waveform
115
+ if prob >= 1.0:
116
+ return apply_fn()
117
+ should_apply = tf.random.stateless_uniform([], seed=seed) < tf.cast(
118
+ prob, tf.float32
119
+ )
120
+ return tf.cond(should_apply, apply_fn, lambda: waveform)
121
+
122
+
123
+ def _fit_length(audio: tf.Tensor, target_length: tf.Tensor) -> tf.Tensor:
124
+ target_length = tf.cast(tf.maximum(target_length, 1), tf.int32)
125
+ audio = audio[:target_length]
126
+ pad = tf.maximum(target_length - tf.shape(audio)[0], 0)
127
+ return tf.pad(audio, [[0, pad], [0, 0]])
128
+
129
+
130
+ def _sample_target_length(
131
+ sample_rate: tf.Tensor | int | None,
132
+ target_length: int | tf.Tensor | None,
133
+ crop_duration: float | None,
134
+ ) -> tf.Tensor:
135
+ if target_length is not None:
136
+ return tf.cast(target_length, tf.int32)
137
+ if crop_duration is None:
138
+ raise ValueError("random_crop requires target_length or crop_duration")
139
+ if sample_rate is None:
140
+ raise ValueError("random_crop with crop_duration requires sample_rate")
141
+ target = tf.round(
142
+ tf.cast(crop_duration, tf.float32) * tf.cast(sample_rate, tf.float32)
143
+ )
144
+ return tf.cast(tf.maximum(target, 1.0), tf.int32)
145
+
146
+
147
+ def make_colored_noise(
148
+ shape: tf.Tensor | Sequence[int],
149
+ seed: tf.Tensor | int | None,
150
+ kind: str = "white",
151
+ ) -> tf.Tensor:
152
+ """Create unit-RMS white, pink, or brown noise with stateless randomness."""
153
+ seed = _seed_tensor(seed)
154
+ shape = tf.cast(tf.convert_to_tensor(shape), tf.int32)
155
+ noise = tf.random.stateless_normal(shape, seed=seed, dtype=tf.float32)
156
+ if kind == "white":
157
+ return _normalize_noise(noise)
158
+
159
+ audio, rank = _as_waveform_tc(noise)
160
+ if kind == "brown":
161
+ brown = tf.cumsum(audio, axis=0)
162
+ return _restore_rank(_normalize_noise(brown), rank)
163
+ if kind != "pink":
164
+ raise ValueError("noise kind must be one of 'white', 'pink', or 'brown'")
165
+
166
+ time = tf.shape(audio)[0]
167
+ channels_first = tf.transpose(audio, [1, 0])
168
+ spectrum = tf.signal.rfft(channels_first, fft_length=[time])
169
+ num_bins = tf.shape(spectrum)[-1]
170
+ freqs = tf.cast(tf.range(num_bins), tf.float32)
171
+ weights = tf.where(freqs > 0.0, tf.math.rsqrt(freqs), tf.zeros_like(freqs))
172
+ spectrum = spectrum * tf.cast(weights[tf.newaxis, :], spectrum.dtype)
173
+ pink = tf.signal.irfft(spectrum, fft_length=[time])
174
+ pink = tf.transpose(pink, [1, 0])
175
+ return _restore_rank(_normalize_noise(pink), rank)
176
+
177
+
178
+ def _normalize_noise(noise: tf.Tensor) -> tf.Tensor:
179
+ noise = tf.cast(noise, tf.float32)
180
+ axes = tf.range(tf.rank(noise))
181
+ noise = noise - tf.reduce_mean(noise, axis=axes, keepdims=True)
182
+ rms = tf.sqrt(tf.reduce_mean(tf.square(noise), axis=axes, keepdims=True))
183
+ return tf.math.divide_no_nan(noise, tf.maximum(rms, _EPS))
184
+
185
+
186
+ @register_audio_waveform_augment("random_gain")
187
+ def random_gain(
188
+ waveform: tf.Tensor,
189
+ sample_rate: tf.Tensor | int | None = None,
190
+ *,
191
+ seed: tf.Tensor | int | None = None,
192
+ config: RandomGainConfig | Mapping[str, Any] | None = None,
193
+ prob: float | None = None,
194
+ min_db: float | None = None,
195
+ max_db: float | None = None,
196
+ is_training: bool = True,
197
+ augment_eval: bool = False,
198
+ ) -> tf.Tensor:
199
+ del sample_rate
200
+ if not _enabled(is_training, augment_eval):
201
+ return tf.convert_to_tensor(waveform)
202
+
203
+ cfg = _config_from(
204
+ config,
205
+ RandomGainConfig,
206
+ {"prob": prob, "min_db": min_db, "max_db": max_db},
207
+ )
208
+ seed = _seed_tensor(seed)
209
+ gate_seed, gain_seed = tf.unstack(tf.random.split(seed, 2))
210
+ input_dtype = tf.convert_to_tensor(waveform).dtype
211
+ audio = tf.cast(waveform, tf.float32)
212
+
213
+ def apply() -> tf.Tensor:
214
+ gain_db = _uniform_float(gain_seed, cfg.min_db, cfg.max_db)
215
+ scale = tf.pow(tf.constant(10.0, dtype=tf.float32), gain_db / 20.0)
216
+ return tf.cast(audio * scale, input_dtype)
217
+
218
+ return _maybe_apply(tf.convert_to_tensor(waveform), cfg.prob, gate_seed, apply)
219
+
220
+
221
+ @register_audio_waveform_augment("time_shift")
222
+ def time_shift(
223
+ waveform: tf.Tensor,
224
+ sample_rate: tf.Tensor | int | None = None,
225
+ *,
226
+ seed: tf.Tensor | int | None = None,
227
+ config: RandomTimeShiftConfig | Mapping[str, Any] | None = None,
228
+ prob: float | None = None,
229
+ max_shift_seconds: float | None = None,
230
+ mode: str | None = None,
231
+ shift_samples: int | tf.Tensor | None = None,
232
+ is_training: bool = True,
233
+ augment_eval: bool = False,
234
+ ) -> tf.Tensor:
235
+ if not _enabled(is_training, augment_eval):
236
+ return tf.convert_to_tensor(waveform)
237
+ if sample_rate is None and shift_samples is None:
238
+ raise ValueError("time_shift requires sample_rate unless shift_samples is set")
239
+
240
+ cfg = _config_from(
241
+ config,
242
+ RandomTimeShiftConfig,
243
+ {"prob": prob, "max_shift_seconds": max_shift_seconds, "mode": mode},
244
+ )
245
+ seed = _seed_tensor(seed)
246
+ gate_seed, shift_seed = tf.unstack(tf.random.split(seed, 2))
247
+ audio, rank = _as_waveform_tc(waveform)
248
+
249
+ def apply() -> tf.Tensor:
250
+ if shift_samples is not None:
251
+ shift = tf.cast(shift_samples, tf.int32)
252
+ else:
253
+ max_shift = tf.cast(
254
+ tf.round(
255
+ tf.cast(cfg.max_shift_seconds, tf.float32)
256
+ * tf.cast(sample_rate, tf.float32)
257
+ ),
258
+ tf.int32,
259
+ )
260
+ shift = _uniform_int(shift_seed, -max_shift, max_shift)
261
+ if cfg.mode == "roll":
262
+ shifted = tf.roll(audio, shift=shift, axis=0)
263
+ elif cfg.mode == "zero":
264
+ shifted = _zero_shift(audio, shift)
265
+ else:
266
+ raise ValueError(f"Unknown time shift mode: {cfg.mode!r}")
267
+ return _restore_rank(shifted, rank)
268
+
269
+ return _maybe_apply(tf.convert_to_tensor(waveform), cfg.prob, gate_seed, apply)
270
+
271
+
272
+ def _zero_shift(audio: tf.Tensor, shift: tf.Tensor) -> tf.Tensor:
273
+ time = tf.shape(audio)[0]
274
+ channels = tf.shape(audio)[1]
275
+ shift = tf.clip_by_value(shift, -time, time)
276
+ amount = tf.abs(shift)
277
+ zeros = tf.zeros([amount, channels], dtype=audio.dtype)
278
+
279
+ def positive() -> tf.Tensor:
280
+ return tf.concat([zeros, audio[: time - amount]], axis=0)
281
+
282
+ def negative() -> tf.Tensor:
283
+ return tf.concat([audio[amount:], zeros], axis=0)
284
+
285
+ return tf.cond(
286
+ shift > 0,
287
+ positive,
288
+ lambda: tf.cond(shift < 0, negative, lambda: audio),
289
+ )
290
+
291
+
292
+ @register_audio_waveform_augment("random_crop")
293
+ def random_crop(
294
+ waveform: tf.Tensor,
295
+ sample_rate: tf.Tensor | int | None = None,
296
+ *,
297
+ seed: tf.Tensor | int | None = None,
298
+ target_length: int | tf.Tensor | None = None,
299
+ crop_duration: float | None = None,
300
+ prob: float = 1.0,
301
+ is_training: bool = True,
302
+ augment_eval: bool = False,
303
+ ) -> tf.Tensor:
304
+ if not _enabled(is_training, augment_eval):
305
+ return tf.convert_to_tensor(waveform)
306
+
307
+ seed = _seed_tensor(seed)
308
+ gate_seed, crop_seed = tf.unstack(tf.random.split(seed, 2))
309
+ audio, rank = _as_waveform_tc(waveform)
310
+ target = _sample_target_length(sample_rate, target_length, crop_duration)
311
+
312
+ def apply() -> tf.Tensor:
313
+ time = tf.shape(audio)[0]
314
+ max_start = tf.maximum(time - target, 0)
315
+ start = tf.cond(
316
+ max_start > 0,
317
+ lambda: tf.random.stateless_uniform(
318
+ [],
319
+ seed=crop_seed,
320
+ minval=0,
321
+ maxval=max_start + 1,
322
+ dtype=tf.int32,
323
+ ),
324
+ lambda: tf.constant(0, dtype=tf.int32),
325
+ )
326
+ cropped = _fit_length(audio[start:], target)
327
+ return _restore_rank(cropped, rank)
328
+
329
+ return _maybe_apply(tf.convert_to_tensor(waveform), prob, gate_seed, apply)
330
+
331
+
332
+ @register_audio_waveform_augment("additive_noise")
333
+ def additive_noise(
334
+ waveform: tf.Tensor,
335
+ sample_rate: tf.Tensor | int | None = None,
336
+ *,
337
+ seed: tf.Tensor | int | None = None,
338
+ config: AdditiveNoiseConfig | Mapping[str, Any] | None = None,
339
+ prob: float | None = None,
340
+ snr_db_min: float | None = None,
341
+ snr_db_max: float | None = None,
342
+ noise_kind: str | None = None,
343
+ is_training: bool = True,
344
+ augment_eval: bool = False,
345
+ ) -> tf.Tensor:
346
+ del sample_rate
347
+ if not _enabled(is_training, augment_eval):
348
+ return tf.convert_to_tensor(waveform)
349
+
350
+ cfg = _config_from(
351
+ config,
352
+ AdditiveNoiseConfig,
353
+ {
354
+ "prob": prob,
355
+ "snr_db_min": snr_db_min,
356
+ "snr_db_max": snr_db_max,
357
+ "noise_kind": noise_kind,
358
+ },
359
+ )
360
+ seed = _seed_tensor(seed)
361
+ gate_seed, snr_seed, noise_seed = tf.unstack(tf.random.split(seed, 3))
362
+ input_dtype = tf.convert_to_tensor(waveform).dtype
363
+ audio = tf.cast(waveform, tf.float32)
364
+
365
+ def apply() -> tf.Tensor:
366
+ noise = make_colored_noise(tf.shape(audio), noise_seed, cfg.noise_kind)
367
+ signal_power = tf.reduce_mean(tf.square(audio))
368
+ noise_power = tf.reduce_mean(tf.square(noise))
369
+ snr_db = _uniform_float(snr_seed, cfg.snr_db_min, cfg.snr_db_max)
370
+ target = tf.pow(tf.constant(10.0, dtype=tf.float32), snr_db / 10.0)
371
+ scale = tf.sqrt(tf.math.divide_no_nan(signal_power, noise_power * target))
372
+ return tf.cast(audio + noise * scale, input_dtype)
373
+
374
+ return _maybe_apply(tf.convert_to_tensor(waveform), cfg.prob, gate_seed, apply)
375
+
376
+
377
+ @register_audio_waveform_augment("colored_noise")
378
+ def colored_noise(
379
+ waveform: tf.Tensor,
380
+ sample_rate: tf.Tensor | int | None = None,
381
+ *,
382
+ seed: tf.Tensor | int | None = None,
383
+ config: AdditiveNoiseConfig | Mapping[str, Any] | None = None,
384
+ prob: float | None = None,
385
+ snr_db_min: float | None = None,
386
+ snr_db_max: float | None = None,
387
+ noise_kind: str = "pink",
388
+ is_training: bool = True,
389
+ augment_eval: bool = False,
390
+ ) -> tf.Tensor:
391
+ return additive_noise(
392
+ waveform,
393
+ sample_rate,
394
+ seed=seed,
395
+ config=config,
396
+ prob=prob,
397
+ snr_db_min=snr_db_min,
398
+ snr_db_max=snr_db_max,
399
+ noise_kind=noise_kind,
400
+ is_training=is_training,
401
+ augment_eval=augment_eval,
402
+ )
403
+
404
+
405
+ @register_audio_waveform_augment("polarity_inversion")
406
+ def polarity_inversion(
407
+ waveform: tf.Tensor,
408
+ sample_rate: tf.Tensor | int | None = None,
409
+ *,
410
+ seed: tf.Tensor | int | None = None,
411
+ prob: float = 0.5,
412
+ is_training: bool = True,
413
+ augment_eval: bool = False,
414
+ ) -> tf.Tensor:
415
+ del sample_rate
416
+ if not _enabled(is_training, augment_eval):
417
+ return tf.convert_to_tensor(waveform)
418
+
419
+ seed = _seed_tensor(seed)
420
+ audio = tf.convert_to_tensor(waveform)
421
+ return _maybe_apply(audio, prob, seed, lambda: -audio)
422
+
423
+
424
+ @register_audio_waveform_augment("random_clipping")
425
+ def random_clipping(
426
+ waveform: tf.Tensor,
427
+ sample_rate: tf.Tensor | int | None = None,
428
+ *,
429
+ seed: tf.Tensor | int | None = None,
430
+ config: RandomClippingConfig | Mapping[str, Any] | None = None,
431
+ prob: float | None = None,
432
+ min_threshold: float | None = None,
433
+ max_threshold: float | None = None,
434
+ is_training: bool = True,
435
+ augment_eval: bool = False,
436
+ ) -> tf.Tensor:
437
+ del sample_rate
438
+ if not _enabled(is_training, augment_eval):
439
+ return tf.convert_to_tensor(waveform)
440
+
441
+ cfg = _config_from(
442
+ config,
443
+ RandomClippingConfig,
444
+ {
445
+ "prob": prob,
446
+ "min_threshold": min_threshold,
447
+ "max_threshold": max_threshold,
448
+ },
449
+ )
450
+ seed = _seed_tensor(seed)
451
+ gate_seed, threshold_seed = tf.unstack(tf.random.split(seed, 2))
452
+ audio = tf.convert_to_tensor(waveform)
453
+
454
+ def apply() -> tf.Tensor:
455
+ threshold = tf.cast(
456
+ _uniform_float(threshold_seed, cfg.min_threshold, cfg.max_threshold),
457
+ audio.dtype,
458
+ )
459
+ return tf.clip_by_value(audio, -threshold, threshold)
460
+
461
+ return _maybe_apply(audio, cfg.prob, gate_seed, apply)
462
+
463
+
464
+ @register_audio_waveform_augment("dynamic_range_compression")
465
+ def dynamic_range_compression(
466
+ waveform: tf.Tensor,
467
+ sample_rate: tf.Tensor | int | None = None,
468
+ *,
469
+ seed: tf.Tensor | int | None = None,
470
+ config: DynamicRangeCompressionConfig | Mapping[str, Any] | None = None,
471
+ prob: float | None = None,
472
+ threshold_db_min: float | None = None,
473
+ threshold_db_max: float | None = None,
474
+ ratio_min: float | None = None,
475
+ ratio_max: float | None = None,
476
+ eps: float = 1e-8,
477
+ is_training: bool = True,
478
+ augment_eval: bool = False,
479
+ ) -> tf.Tensor:
480
+ del sample_rate
481
+ if not _enabled(is_training, augment_eval):
482
+ return tf.convert_to_tensor(waveform)
483
+
484
+ cfg = _config_from(
485
+ config,
486
+ DynamicRangeCompressionConfig,
487
+ {
488
+ "prob": prob,
489
+ "threshold_db_min": threshold_db_min,
490
+ "threshold_db_max": threshold_db_max,
491
+ "ratio_min": ratio_min,
492
+ "ratio_max": ratio_max,
493
+ },
494
+ )
495
+ seed = _seed_tensor(seed)
496
+ gate_seed, threshold_seed, ratio_seed = tf.unstack(tf.random.split(seed, 3))
497
+ input_dtype = tf.convert_to_tensor(waveform).dtype
498
+ audio = tf.cast(waveform, tf.float32)
499
+
500
+ def apply() -> tf.Tensor:
501
+ threshold_db = _uniform_float(
502
+ threshold_seed,
503
+ cfg.threshold_db_min,
504
+ cfg.threshold_db_max,
505
+ )
506
+ ratio = _uniform_float(ratio_seed, cfg.ratio_min, cfg.ratio_max)
507
+ magnitude = tf.abs(audio)
508
+ level_db = (
509
+ 20.0 * tf.math.log(magnitude + tf.cast(eps, tf.float32)) / tf.math.log(10.0)
510
+ )
511
+ over = level_db - threshold_db
512
+ compressed_over = over / ratio
513
+ gain_db = tf.where(over > 0.0, compressed_over - over, tf.zeros_like(over))
514
+ gain = tf.pow(tf.constant(10.0, dtype=tf.float32), gain_db / 20.0)
515
+ return tf.cast(tf.sign(audio) * magnitude * gain, input_dtype)
516
+
517
+ return _maybe_apply(tf.convert_to_tensor(waveform), cfg.prob, gate_seed, apply)
518
+
519
+
520
+ @register_audio_waveform_augment("rir_convolution")
521
+ def rir_convolution(
522
+ waveform: tf.Tensor,
523
+ sample_rate: tf.Tensor | int | None = None,
524
+ *,
525
+ seed: tf.Tensor | int | None = None,
526
+ config: RIRConvolutionConfig | Mapping[str, Any] | None = None,
527
+ prob: float | None = None,
528
+ rir: tf.Tensor | Sequence[float] | None = None,
529
+ rir_dataset: str | None = None,
530
+ normalize_ir: bool | None = None,
531
+ compensate_delay: bool | None = None,
532
+ is_training: bool = True,
533
+ augment_eval: bool = False,
534
+ ) -> tf.Tensor:
535
+ del sample_rate
536
+ if not _enabled(is_training, augment_eval):
537
+ return tf.convert_to_tensor(waveform)
538
+
539
+ cfg = _config_from(
540
+ config,
541
+ RIRConvolutionConfig,
542
+ {
543
+ "prob": prob,
544
+ "rir_dataset": rir_dataset,
545
+ "normalize_ir": normalize_ir,
546
+ "compensate_delay": compensate_delay,
547
+ },
548
+ )
549
+ if cfg.rir_dataset is not None and rir is None:
550
+ raise ValueError(
551
+ "rir_dataset loading is not implemented; pass an explicit rir tensor"
552
+ )
553
+
554
+ seed = _seed_tensor(seed)
555
+ gate_seed, rir_seed = tf.unstack(tf.random.split(seed, 2))
556
+ input_dtype = tf.convert_to_tensor(waveform).dtype
557
+ audio, rank = _as_waveform_tc(tf.cast(waveform, tf.float32))
558
+ ir = _prepare_rir(rir, rir_seed, cfg.normalize_ir)
559
+
560
+ def apply() -> tf.Tensor:
561
+ convolved = _convolve_channels(audio, ir, cfg.compensate_delay)
562
+ return tf.cast(_restore_rank(convolved, rank), input_dtype)
563
+
564
+ return _maybe_apply(tf.convert_to_tensor(waveform), cfg.prob, gate_seed, apply)
565
+
566
+
567
+ def _prepare_rir(
568
+ rir: tf.Tensor | Sequence[float] | None,
569
+ seed: tf.Tensor,
570
+ normalize: bool,
571
+ ) -> tf.Tensor:
572
+ if rir is None:
573
+ tail = tf.random.stateless_normal(
574
+ [63], seed=seed, stddev=0.05, dtype=tf.float32
575
+ )
576
+ decay = tf.exp(-tf.linspace(0.0, 4.0, 63))
577
+ rir = tf.concat([tf.ones([1], dtype=tf.float32), tail * decay], axis=0)
578
+ ir = tf.reshape(tf.cast(tf.convert_to_tensor(rir), tf.float32), [-1])
579
+ ir = tf.cond(
580
+ tf.shape(ir)[0] > 0, lambda: ir, lambda: tf.ones([1], dtype=tf.float32)
581
+ )
582
+ if normalize:
583
+ energy = tf.sqrt(tf.reduce_sum(tf.square(ir)))
584
+ ir = tf.math.divide_no_nan(ir, tf.maximum(energy, _EPS))
585
+ return ir
586
+
587
+
588
+ def _convolve_channels(
589
+ audio: tf.Tensor, ir: tf.Tensor, compensate_delay: bool
590
+ ) -> tf.Tensor:
591
+ time = tf.shape(audio)[0]
592
+ kernel = tf.reverse(ir, axis=[0])[:, tf.newaxis, tf.newaxis]
593
+ kernel_length = tf.shape(kernel)[0]
594
+ start = (
595
+ tf.argmax(tf.abs(ir), output_type=tf.int32)
596
+ if compensate_delay
597
+ else (kernel_length - 1) // 2
598
+ )
599
+
600
+ def convolve_channel(channel: tf.Tensor) -> tf.Tensor:
601
+ signal = channel[tf.newaxis, :, tf.newaxis]
602
+ padded = tf.pad(
603
+ signal, [[0, 0], [kernel_length - 1, kernel_length - 1], [0, 0]]
604
+ )
605
+ full = tf.nn.conv1d(padded, kernel, stride=1, padding="VALID")[0, :, 0]
606
+ return _fit_length(full[start:, tf.newaxis], time)[:, 0]
607
+
608
+ channels_first = tf.transpose(audio, [1, 0])
609
+ convolved = tf.map_fn(
610
+ convolve_channel,
611
+ channels_first,
612
+ fn_output_signature=tf.float32,
613
+ )
614
+ return tf.transpose(convolved, [1, 0])
615
+
616
+
617
+ @register_audio_waveform_augment("speed_perturb")
618
+ def speed_perturb(
619
+ waveform: tf.Tensor,
620
+ sample_rate: tf.Tensor | int | None = None,
621
+ *,
622
+ seed: tf.Tensor | int | None = None,
623
+ config: SpeedPerturbConfig | Mapping[str, Any] | None = None,
624
+ prob: float | None = None,
625
+ rates: Sequence[float] | None = None,
626
+ is_training: bool = True,
627
+ augment_eval: bool = False,
628
+ ) -> tf.Tensor:
629
+ del sample_rate
630
+ if not _enabled(is_training, augment_eval):
631
+ return tf.convert_to_tensor(waveform)
632
+
633
+ cfg = _config_from(
634
+ config,
635
+ SpeedPerturbConfig,
636
+ {"prob": prob, "rates": tuple(rates) if rates is not None else None},
637
+ )
638
+ seed = _seed_tensor(seed)
639
+ gate_seed, rate_seed = tf.unstack(tf.random.split(seed, 2))
640
+ input_dtype = tf.convert_to_tensor(waveform).dtype
641
+ audio, rank = _as_waveform_tc(tf.cast(waveform, tf.float32))
642
+
643
+ def apply() -> tf.Tensor:
644
+ rates_tensor = tf.constant(cfg.rates, dtype=tf.float32)
645
+ if len(cfg.rates) == 1:
646
+ rate = rates_tensor[0]
647
+ else:
648
+ index = tf.random.stateless_uniform(
649
+ [],
650
+ seed=rate_seed,
651
+ minval=0,
652
+ maxval=len(cfg.rates),
653
+ dtype=tf.int32,
654
+ )
655
+ rate = rates_tensor[index]
656
+ time = tf.shape(audio)[0]
657
+ new_time = tf.cast(
658
+ tf.maximum(tf.round(tf.cast(time, tf.float32) / rate), 1.0),
659
+ tf.int32,
660
+ )
661
+ resized = _resize_time(audio, new_time)
662
+ return tf.cast(_restore_rank(_fit_length(resized, time), rank), input_dtype)
663
+
664
+ return _maybe_apply(tf.convert_to_tensor(waveform), cfg.prob, gate_seed, apply)
665
+
666
+
667
+ def _resize_time(audio: tf.Tensor, new_time: tf.Tensor) -> tf.Tensor:
668
+ resized = tf.image.resize(
669
+ audio[tf.newaxis, :, tf.newaxis, :],
670
+ [new_time, 1],
671
+ method="bilinear",
672
+ antialias=True,
673
+ )
674
+ return resized[0, :, 0, :]
675
+
676
+
677
+ @register_audio_waveform_augment("codec_simulation")
678
+ def codec_simulation(
679
+ waveform: tf.Tensor,
680
+ sample_rate: tf.Tensor | int | None = None,
681
+ *,
682
+ seed: tf.Tensor | int | None = None,
683
+ config: CodecSimulationConfig | Mapping[str, Any] | None = None,
684
+ prob: float | None = None,
685
+ codec: str | None = None,
686
+ bitrate: int | None = None,
687
+ is_training: bool = True,
688
+ augment_eval: bool = False,
689
+ ) -> tf.Tensor:
690
+ del sample_rate
691
+ if not _enabled(is_training, augment_eval):
692
+ return tf.convert_to_tensor(waveform)
693
+
694
+ cfg = _config_from(
695
+ config,
696
+ CodecSimulationConfig,
697
+ {"prob": prob, "codec": codec, "bitrate": bitrate},
698
+ )
699
+ gate_seed = _seed_tensor(seed)
700
+ input_dtype = tf.convert_to_tensor(waveform).dtype
701
+ audio, rank = _as_waveform_tc(tf.cast(waveform, tf.float32))
702
+
703
+ def apply() -> tf.Tensor:
704
+ clipped = tf.clip_by_value(audio, -1.0, 1.0)
705
+ if cfg.codec == "mulaw":
706
+ coded = _mulaw_roundtrip(clipped)
707
+ elif cfg.codec == "alaw":
708
+ coded = _alaw_roundtrip(clipped)
709
+ elif cfg.codec == "mp3_proxy":
710
+ coded = _mp3_proxy(clipped, cfg.bitrate)
711
+ else:
712
+ raise ValueError(f"Unknown codec simulation: {cfg.codec!r}")
713
+ return tf.cast(
714
+ _restore_rank(tf.clip_by_value(coded, -1.0, 1.0), rank), input_dtype
715
+ )
716
+
717
+ return _maybe_apply(tf.convert_to_tensor(waveform), cfg.prob, gate_seed, apply)
718
+
719
+
720
+ def _quantize_unit(x: tf.Tensor, levels: int) -> tf.Tensor:
721
+ levels_f = tf.cast(levels - 1, tf.float32)
722
+ x = tf.clip_by_value(x, -1.0, 1.0)
723
+ return tf.round((x + 1.0) * 0.5 * levels_f) / levels_f * 2.0 - 1.0
724
+
725
+
726
+ def _mulaw_roundtrip(audio: tf.Tensor) -> tf.Tensor:
727
+ mu = tf.constant(255.0, dtype=tf.float32)
728
+ log_mu = tf.math.log1p(mu)
729
+ encoded = tf.sign(audio) * tf.math.log1p(mu * tf.abs(audio)) / log_mu
730
+ encoded = _quantize_unit(encoded, 256)
731
+ return tf.sign(encoded) * tf.math.expm1(tf.abs(encoded) * log_mu) / mu
732
+
733
+
734
+ def _alaw_roundtrip(audio: tf.Tensor) -> tf.Tensor:
735
+ a = tf.constant(87.6, dtype=tf.float32)
736
+ denom = 1.0 + tf.math.log(a)
737
+ abs_audio = tf.abs(audio)
738
+ encoded_mag = tf.where(
739
+ abs_audio < (1.0 / a),
740
+ a * abs_audio / denom,
741
+ (1.0 + tf.math.log(a * tf.maximum(abs_audio, _EPS))) / denom,
742
+ )
743
+ encoded = _quantize_unit(tf.sign(audio) * encoded_mag, 256)
744
+ abs_encoded = tf.abs(encoded)
745
+ decoded_mag = tf.where(
746
+ abs_encoded < (1.0 / denom),
747
+ abs_encoded * denom / a,
748
+ tf.exp(abs_encoded * denom - 1.0) / a,
749
+ )
750
+ return tf.sign(encoded) * decoded_mag
751
+
752
+
753
+ def _mp3_proxy(audio: tf.Tensor, bitrate: int | None) -> tf.Tensor:
754
+ del bitrate
755
+ kernel = tf.constant([0.2, 0.6, 0.2], dtype=tf.float32)[:, tf.newaxis, tf.newaxis]
756
+
757
+ def smooth_channel(channel: tf.Tensor) -> tf.Tensor:
758
+ signal = channel[tf.newaxis, :, tf.newaxis]
759
+ return tf.nn.conv1d(signal, kernel, stride=1, padding="SAME")[0, :, 0]
760
+
761
+ smoothed = tf.map_fn(
762
+ smooth_channel,
763
+ tf.transpose(audio, [1, 0]),
764
+ fn_output_signature=tf.float32,
765
+ )
766
+ smoothed = tf.transpose(smoothed, [1, 0])
767
+ return _quantize_unit(smoothed, 128)
768
+
769
+
770
+ def normalize_waveform_augment_specs(
771
+ augmentations: Mapping[str, Any] | Sequence[Any] | str | None,
772
+ ) -> list[dict[str, Any]]:
773
+ if augmentations is None:
774
+ return []
775
+ if isinstance(augmentations, str):
776
+ return [{"name": augmentations}]
777
+ if isinstance(augmentations, Mapping):
778
+ if "name" in augmentations:
779
+ spec = dict(augmentations)
780
+ spec["name"] = str(spec["name"])
781
+ return [spec]
782
+
783
+ specs = []
784
+ for name, value in augmentations.items():
785
+ if value is None or value is False:
786
+ continue
787
+ if value is True:
788
+ specs.append({"name": str(name)})
789
+ elif isinstance(value, Mapping):
790
+ specs.append({"name": str(name), **dict(value)})
791
+ else:
792
+ specs.append({"name": str(name), "config": value})
793
+ return specs
794
+
795
+ specs = []
796
+ for value in augmentations:
797
+ specs.extend(normalize_waveform_augment_specs(value))
798
+ return specs
799
+
800
+
801
+ def apply_waveform_augmentations(
802
+ sample: dict,
803
+ augmentations: Mapping[str, Any] | Sequence[Any] | str | None,
804
+ *,
805
+ seed: tf.Tensor | int | None = None,
806
+ is_training: bool = True,
807
+ augment_eval: bool = False,
808
+ audio_key: str = WAVEFORM,
809
+ ) -> dict:
810
+ specs = normalize_waveform_augment_specs(augmentations)
811
+ if not specs or not _enabled(is_training, augment_eval):
812
+ return sample
813
+
814
+ seed = _seed_tensor(seed)
815
+ seeds = tf.random.split(seed, len(specs))
816
+ waveform = sample[audio_key]
817
+ sample_rate = sample.get(SAMPLE_RATE)
818
+
819
+ for spec, spec_seed in zip(specs, tf.unstack(seeds)):
820
+ kwargs = dict(spec)
821
+ name = kwargs.pop("name")
822
+ fn = get_audio_waveform_augment(name)
823
+ waveform = fn(
824
+ waveform,
825
+ sample_rate,
826
+ seed=spec_seed,
827
+ is_training=is_training,
828
+ augment_eval=augment_eval,
829
+ **kwargs,
830
+ )
831
+
832
+ result = dict(sample)
833
+ result[audio_key] = waveform
834
+ if DURATION in result and sample_rate is not None:
835
+ result[DURATION] = infer_duration(
836
+ tf.shape(tf.convert_to_tensor(waveform))[0], sample_rate
837
+ )
838
+ return result
839
+
840
+
841
+ def make_waveform_augmentation_stage(
842
+ augmentations: Mapping[str, Any] | Sequence[Any] | str | None,
843
+ *,
844
+ is_training: bool = True,
845
+ augment_eval: bool = False,
846
+ audio_key: str = WAVEFORM,
847
+ ):
848
+ specs = normalize_waveform_augment_specs(augmentations)
849
+
850
+ def stage(sample: dict, seed: tf.Tensor | int | None = None) -> dict:
851
+ return apply_waveform_augmentations(
852
+ sample,
853
+ specs,
854
+ seed=seed,
855
+ is_training=is_training,
856
+ augment_eval=augment_eval,
857
+ audio_key=audio_key,
858
+ )
859
+
860
+ return stage
861
+
862
+
863
+ __all__ = [
864
+ "additive_noise",
865
+ "apply_waveform_augmentations",
866
+ "codec_simulation",
867
+ "colored_noise",
868
+ "dynamic_range_compression",
869
+ "make_colored_noise",
870
+ "make_waveform_augmentation_stage",
871
+ "normalize_waveform_augment_specs",
872
+ "polarity_inversion",
873
+ "random_clipping",
874
+ "random_crop",
875
+ "random_gain",
876
+ "rir_convolution",
877
+ "speed_perturb",
878
+ "time_shift",
879
+ ]