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,806 @@
1
+ from __future__ import annotations
2
+
3
+ import dataclasses
4
+ from collections.abc import Mapping, Sequence
5
+ from typing import Any, Literal
6
+
7
+ import tensorflow as tf
8
+
9
+ from justdata.acoustic.registry import (
10
+ get_audio_spectrogram_augment,
11
+ register_audio_spectrogram_augment,
12
+ )
13
+ from justdata.acoustic.schema import FEATURES
14
+
15
+
16
+ SpectrogramLayout = Literal["tf", "tfc", "cft"]
17
+ BatchSpectrogramLayout = Literal["tf", "tfc", "cft", "btf", "btfc", "bcft"]
18
+
19
+ _EPS = tf.constant(1e-6, dtype=tf.float32)
20
+
21
+
22
+ def _seed_tensor(seed: tf.Tensor | int | None) -> tf.Tensor:
23
+ if seed is None:
24
+ return tf.constant([0, 0], dtype=tf.int32)
25
+
26
+ seed = tf.cast(tf.convert_to_tensor(seed), tf.int32)
27
+ if seed.shape.rank == 0:
28
+ return tf.stack([seed, tf.constant(0, dtype=tf.int32)])
29
+ if seed.shape.rank == 1 and seed.shape[0] == 1:
30
+ return tf.stack([seed[0], tf.constant(0, dtype=tf.int32)])
31
+ return seed[:2]
32
+
33
+
34
+ def _enabled(is_training: bool, augment_eval: bool) -> bool:
35
+ return bool(is_training or augment_eval)
36
+
37
+
38
+ def _uniform_int(seed: tf.Tensor, minval: tf.Tensor, maxval: tf.Tensor) -> tf.Tensor:
39
+ minval = tf.cast(minval, tf.int32)
40
+ maxval = tf.cast(maxval, tf.int32)
41
+ return tf.cond(
42
+ tf.equal(minval, maxval),
43
+ lambda: minval,
44
+ lambda: tf.random.stateless_uniform(
45
+ [],
46
+ seed=seed,
47
+ minval=minval,
48
+ maxval=maxval + 1,
49
+ dtype=tf.int32,
50
+ ),
51
+ )
52
+
53
+
54
+ def _uniform_float(seed: tf.Tensor, minval: float, maxval: float) -> tf.Tensor:
55
+ if minval == maxval:
56
+ return tf.constant(minval, dtype=tf.float32)
57
+ return tf.random.stateless_uniform(
58
+ [],
59
+ seed=seed,
60
+ minval=tf.cast(minval, tf.float32),
61
+ maxval=tf.cast(maxval, tf.float32),
62
+ dtype=tf.float32,
63
+ )
64
+
65
+
66
+ def _stateless_shuffled_range(size: tf.Tensor, seed: tf.Tensor) -> tf.Tensor:
67
+ random_values = tf.random.stateless_uniform(tf.reshape(size, [1]), seed=seed)
68
+ return tf.argsort(random_values, stable=True)
69
+
70
+
71
+ def _maybe_apply(x: tf.Tensor, prob: float, seed: tf.Tensor, apply_fn) -> tf.Tensor:
72
+ if prob <= 0.0:
73
+ return x
74
+ if prob >= 1.0:
75
+ return apply_fn()
76
+ should_apply = tf.random.stateless_uniform([], seed=seed) < tf.cast(
77
+ prob, tf.float32
78
+ )
79
+ return tf.cond(should_apply, apply_fn, lambda: x)
80
+
81
+
82
+ def _merge_config(config: Any, overrides: Mapping[str, Any]) -> dict[str, Any]:
83
+ data: dict[str, Any] = {}
84
+ if config is not None:
85
+ if dataclasses.is_dataclass(config):
86
+ data.update(dataclasses.asdict(config))
87
+ elif isinstance(config, Mapping):
88
+ data.update(config)
89
+ else:
90
+ raise TypeError("config must be a dataclass, mapping, or None")
91
+ data.update({key: value for key, value in overrides.items() if value is not None})
92
+ return data
93
+
94
+
95
+ def _resolve_layout(
96
+ x: tf.Tensor,
97
+ layout: SpectrogramLayout | None,
98
+ ) -> SpectrogramLayout:
99
+ if layout is not None:
100
+ return layout
101
+ if x.shape.rank == 2:
102
+ return "tf"
103
+ if x.shape.rank == 3:
104
+ return "tfc"
105
+ raise ValueError("Spectrogram augment input must have rank 2 or 3")
106
+
107
+
108
+ def _as_tfc(
109
+ spectrogram: tf.Tensor,
110
+ layout: SpectrogramLayout | None = None,
111
+ ) -> tuple[tf.Tensor, SpectrogramLayout]:
112
+ x = tf.convert_to_tensor(spectrogram)
113
+ resolved = _resolve_layout(x, layout)
114
+
115
+ if resolved == "tf":
116
+ if x.shape.rank != 2:
117
+ raise ValueError("layout='tf' expects shape [T, F]")
118
+ return x[:, :, tf.newaxis], resolved
119
+ if resolved == "tfc":
120
+ if x.shape.rank != 3:
121
+ raise ValueError("layout='tfc' expects shape [T, F, C]")
122
+ return x, resolved
123
+ if resolved == "cft":
124
+ if x.shape.rank != 3:
125
+ raise ValueError("layout='cft' expects shape [C, F, T]")
126
+ return tf.transpose(x, [2, 1, 0]), resolved
127
+ raise ValueError("layout must be one of 'tf', 'tfc', or 'cft'")
128
+
129
+
130
+ def _restore_from_tfc(x: tf.Tensor, layout: SpectrogramLayout) -> tf.Tensor:
131
+ if layout == "tf":
132
+ return tf.squeeze(x, axis=-1)
133
+ if layout == "tfc":
134
+ return x
135
+ if layout == "cft":
136
+ return tf.transpose(x, [2, 1, 0])
137
+ raise ValueError("layout must be one of 'tf', 'tfc', or 'cft'")
138
+
139
+
140
+ def _resolve_batch_layout(
141
+ x: tf.Tensor,
142
+ layout: BatchSpectrogramLayout | None,
143
+ ) -> BatchSpectrogramLayout:
144
+ if layout is not None:
145
+ return layout
146
+ if x.shape.rank == 2:
147
+ return "tf"
148
+ if x.shape.rank == 3:
149
+ return "btf"
150
+ if x.shape.rank == 4:
151
+ return "btfc"
152
+ raise ValueError("MixStyle input must have rank 2, 3, or 4")
153
+
154
+
155
+ def _as_btfc(
156
+ spectrogram: tf.Tensor,
157
+ layout: BatchSpectrogramLayout | None = None,
158
+ ) -> tuple[tf.Tensor, BatchSpectrogramLayout]:
159
+ x = tf.convert_to_tensor(spectrogram)
160
+ resolved = _resolve_batch_layout(x, layout)
161
+
162
+ if resolved in {"tf", "tfc", "cft"}:
163
+ sample, sample_layout = _as_tfc(x, resolved)
164
+ del sample_layout
165
+ return sample[tf.newaxis, ...], resolved
166
+ if resolved == "btf":
167
+ if x.shape.rank != 3:
168
+ raise ValueError("layout='btf' expects shape [B, T, F]")
169
+ return x[:, :, :, tf.newaxis], resolved
170
+ if resolved == "btfc":
171
+ if x.shape.rank != 4:
172
+ raise ValueError("layout='btfc' expects shape [B, T, F, C]")
173
+ return x, resolved
174
+ if resolved == "bcft":
175
+ if x.shape.rank != 4:
176
+ raise ValueError("layout='bcft' expects shape [B, C, F, T]")
177
+ return tf.transpose(x, [0, 3, 2, 1]), resolved
178
+ raise ValueError("unsupported MixStyle layout")
179
+
180
+
181
+ def _restore_from_btfc(x: tf.Tensor, layout: BatchSpectrogramLayout) -> tf.Tensor:
182
+ if layout == "tf":
183
+ return _restore_from_tfc(x[0], "tf")
184
+ if layout == "tfc":
185
+ return _restore_from_tfc(x[0], "tfc")
186
+ if layout == "cft":
187
+ return _restore_from_tfc(x[0], "cft")
188
+ if layout == "btf":
189
+ return tf.squeeze(x, axis=-1)
190
+ if layout == "btfc":
191
+ return x
192
+ if layout == "bcft":
193
+ return tf.transpose(x, [0, 3, 2, 1])
194
+ raise ValueError("unsupported MixStyle layout")
195
+
196
+
197
+ def _max_width(width: int | tf.Tensor | None, limit: tf.Tensor) -> tf.Tensor:
198
+ if width is None:
199
+ return tf.cast(limit, tf.int32)
200
+ if isinstance(width, int) and width < 0:
201
+ raise ValueError("mask width must be non-negative")
202
+ return tf.minimum(tf.cast(width, tf.int32), tf.cast(limit, tf.int32))
203
+
204
+
205
+ def _fill_scalar(x: tf.Tensor, fill_value: Literal["zero", "mean", "min"]) -> tf.Tensor:
206
+ if fill_value == "zero":
207
+ return tf.zeros([], dtype=x.dtype)
208
+ if fill_value == "mean":
209
+ return tf.cast(tf.reduce_mean(tf.cast(x, tf.float32)), x.dtype)
210
+ if fill_value == "min":
211
+ return tf.reduce_min(x)
212
+ raise ValueError("fill_value must be one of 'zero', 'mean', or 'min'")
213
+
214
+
215
+ def _sample_span(
216
+ size: tf.Tensor,
217
+ max_width: int | tf.Tensor | None,
218
+ width_seed: tf.Tensor,
219
+ start_seed: tf.Tensor,
220
+ ) -> tuple[tf.Tensor, tf.Tensor]:
221
+ width_max = _max_width(max_width, size)
222
+ width = _uniform_int(width_seed, tf.constant(0, tf.int32), width_max)
223
+ max_start = tf.maximum(tf.cast(size, tf.int32) - width, 0)
224
+ start = _uniform_int(start_seed, tf.constant(0, tf.int32), max_start)
225
+ return start, width
226
+
227
+
228
+ @register_audio_spectrogram_augment("frequency_mask")
229
+ def frequency_mask(
230
+ spectrogram: tf.Tensor,
231
+ *,
232
+ seed: tf.Tensor | int | None = None,
233
+ config: Mapping[str, Any] | None = None,
234
+ max_width: int | tf.Tensor | None = None,
235
+ fill_value: Literal["zero", "mean", "min"] | None = None,
236
+ prob: float | None = None,
237
+ layout: SpectrogramLayout | None = None,
238
+ is_training: bool = True,
239
+ augment_eval: bool = False,
240
+ ) -> tf.Tensor:
241
+ if not _enabled(is_training, augment_eval):
242
+ return tf.convert_to_tensor(spectrogram)
243
+
244
+ cfg = _merge_config(
245
+ config,
246
+ {
247
+ "max_width": max_width,
248
+ "fill_value": fill_value,
249
+ "prob": prob,
250
+ "layout": layout,
251
+ },
252
+ )
253
+ max_width = cfg.get("max_width", 0)
254
+ fill_value = cfg.get("fill_value", "zero")
255
+ prob = cfg.get("prob", 1.0)
256
+ layout = cfg.get("layout")
257
+
258
+ seed = _seed_tensor(seed)
259
+ gate_seed, width_seed, start_seed = tf.unstack(tf.random.split(seed, 3))
260
+ x, resolved_layout = _as_tfc(spectrogram, layout)
261
+ fill = _fill_scalar(x, fill_value)
262
+
263
+ def apply() -> tf.Tensor:
264
+ freq = tf.shape(x)[1]
265
+ start, width = _sample_span(freq, max_width, width_seed, start_seed)
266
+ positions = tf.range(freq)
267
+ mask = tf.logical_and(positions >= start, positions < start + width)
268
+ masked = tf.where(mask[tf.newaxis, :, tf.newaxis], fill, x)
269
+ return _restore_from_tfc(masked, resolved_layout)
270
+
271
+ return _maybe_apply(tf.convert_to_tensor(spectrogram), prob, gate_seed, apply)
272
+
273
+
274
+ @register_audio_spectrogram_augment("time_mask")
275
+ def time_mask(
276
+ spectrogram: tf.Tensor,
277
+ *,
278
+ seed: tf.Tensor | int | None = None,
279
+ config: Mapping[str, Any] | None = None,
280
+ max_width: int | tf.Tensor | None = None,
281
+ fill_value: Literal["zero", "mean", "min"] | None = None,
282
+ prob: float | None = None,
283
+ layout: SpectrogramLayout | None = None,
284
+ is_training: bool = True,
285
+ augment_eval: bool = False,
286
+ ) -> tf.Tensor:
287
+ if not _enabled(is_training, augment_eval):
288
+ return tf.convert_to_tensor(spectrogram)
289
+
290
+ cfg = _merge_config(
291
+ config,
292
+ {
293
+ "max_width": max_width,
294
+ "fill_value": fill_value,
295
+ "prob": prob,
296
+ "layout": layout,
297
+ },
298
+ )
299
+ max_width = cfg.get("max_width", 0)
300
+ fill_value = cfg.get("fill_value", "zero")
301
+ prob = cfg.get("prob", 1.0)
302
+ layout = cfg.get("layout")
303
+
304
+ seed = _seed_tensor(seed)
305
+ gate_seed, width_seed, start_seed = tf.unstack(tf.random.split(seed, 3))
306
+ x, resolved_layout = _as_tfc(spectrogram, layout)
307
+ fill = _fill_scalar(x, fill_value)
308
+
309
+ def apply() -> tf.Tensor:
310
+ time = tf.shape(x)[0]
311
+ start, width = _sample_span(time, max_width, width_seed, start_seed)
312
+ positions = tf.range(time)
313
+ mask = tf.logical_and(positions >= start, positions < start + width)
314
+ masked = tf.where(mask[:, tf.newaxis, tf.newaxis], fill, x)
315
+ return _restore_from_tfc(masked, resolved_layout)
316
+
317
+ return _maybe_apply(tf.convert_to_tensor(spectrogram), prob, gate_seed, apply)
318
+
319
+
320
+ @register_audio_spectrogram_augment("spectrogram_time_roll")
321
+ def spectrogram_time_roll(
322
+ spectrogram: tf.Tensor,
323
+ *,
324
+ seed: tf.Tensor | int | None = None,
325
+ config: Mapping[str, Any] | None = None,
326
+ max_shift: int | tf.Tensor | None = None,
327
+ shift: int | tf.Tensor | None = None,
328
+ prob: float | None = None,
329
+ layout: SpectrogramLayout | None = None,
330
+ is_training: bool = True,
331
+ augment_eval: bool = False,
332
+ ) -> tf.Tensor:
333
+ if not _enabled(is_training, augment_eval):
334
+ return tf.convert_to_tensor(spectrogram)
335
+
336
+ cfg = _merge_config(
337
+ config,
338
+ {"max_shift": max_shift, "shift": shift, "prob": prob, "layout": layout},
339
+ )
340
+ max_shift = cfg.get("max_shift", 0)
341
+ shift = cfg.get("shift")
342
+ prob = cfg.get("prob", 1.0)
343
+ layout = cfg.get("layout")
344
+
345
+ seed = _seed_tensor(seed)
346
+ gate_seed, shift_seed = tf.unstack(tf.random.split(seed, 2))
347
+ x, resolved_layout = _as_tfc(spectrogram, layout)
348
+
349
+ def apply() -> tf.Tensor:
350
+ if shift is None:
351
+ time = tf.shape(x)[0]
352
+ limit = _max_width(max_shift, time)
353
+ sampled_shift = _uniform_int(shift_seed, -limit, limit)
354
+ else:
355
+ sampled_shift = tf.cast(shift, tf.int32)
356
+ return _restore_from_tfc(
357
+ tf.roll(x, shift=sampled_shift, axis=0), resolved_layout
358
+ )
359
+
360
+ return _maybe_apply(tf.convert_to_tensor(spectrogram), prob, gate_seed, apply)
361
+
362
+
363
+ def _zero_shifted_frequency_edges(x: tf.Tensor, shift: tf.Tensor) -> tf.Tensor:
364
+ freq = tf.shape(x)[1]
365
+ shift = tf.clip_by_value(tf.cast(shift, tf.int32), -freq, freq)
366
+ positions = tf.range(freq)
367
+
368
+ def positive() -> tf.Tensor:
369
+ mask = positions < shift
370
+ return tf.where(mask[tf.newaxis, :, tf.newaxis], tf.zeros([], dtype=x.dtype), x)
371
+
372
+ def negative() -> tf.Tensor:
373
+ mask = positions >= freq + shift
374
+ return tf.where(mask[tf.newaxis, :, tf.newaxis], tf.zeros([], dtype=x.dtype), x)
375
+
376
+ return tf.cond(
377
+ shift > 0,
378
+ positive,
379
+ lambda: tf.cond(shift < 0, negative, lambda: x),
380
+ )
381
+
382
+
383
+ @register_audio_spectrogram_augment("mel_bin_shift")
384
+ def mel_bin_shift(
385
+ spectrogram: tf.Tensor,
386
+ *,
387
+ seed: tf.Tensor | int | None = None,
388
+ config: Mapping[str, Any] | None = None,
389
+ max_bins: int | tf.Tensor | None = None,
390
+ shift: int | tf.Tensor | None = None,
391
+ zero_pad: bool | None = None,
392
+ prob: float | None = None,
393
+ layout: SpectrogramLayout | None = None,
394
+ is_training: bool = True,
395
+ augment_eval: bool = False,
396
+ ) -> tf.Tensor:
397
+ if not _enabled(is_training, augment_eval):
398
+ return tf.convert_to_tensor(spectrogram)
399
+
400
+ cfg = _merge_config(
401
+ config,
402
+ {
403
+ "max_bins": max_bins,
404
+ "shift": shift,
405
+ "zero_pad": zero_pad,
406
+ "prob": prob,
407
+ "layout": layout,
408
+ },
409
+ )
410
+ max_bins = cfg.get("max_bins", 0)
411
+ shift = cfg.get("shift")
412
+ zero_pad = cfg.get("zero_pad", True)
413
+ prob = cfg.get("prob", 1.0)
414
+ layout = cfg.get("layout")
415
+
416
+ seed = _seed_tensor(seed)
417
+ gate_seed, shift_seed = tf.unstack(tf.random.split(seed, 2))
418
+ x, resolved_layout = _as_tfc(spectrogram, layout)
419
+
420
+ def apply() -> tf.Tensor:
421
+ if shift is None:
422
+ freq = tf.shape(x)[1]
423
+ limit = _max_width(max_bins, freq)
424
+ sampled_shift = _uniform_int(shift_seed, -limit, limit)
425
+ else:
426
+ sampled_shift = tf.cast(shift, tf.int32)
427
+ shifted = tf.roll(x, shift=sampled_shift, axis=1)
428
+ if zero_pad:
429
+ shifted = _zero_shifted_frequency_edges(shifted, sampled_shift)
430
+ return _restore_from_tfc(shifted, resolved_layout)
431
+
432
+ return _maybe_apply(tf.convert_to_tensor(spectrogram), prob, gate_seed, apply)
433
+
434
+
435
+ @register_audio_spectrogram_augment("time_frequency_erasing")
436
+ def time_frequency_erasing(
437
+ spectrogram: tf.Tensor,
438
+ *,
439
+ seed: tf.Tensor | int | None = None,
440
+ config: Mapping[str, Any] | None = None,
441
+ max_time_width: int | tf.Tensor | None = None,
442
+ max_freq_width: int | tf.Tensor | None = None,
443
+ fill_value: Literal["zero", "mean", "min"] | None = None,
444
+ prob: float | None = None,
445
+ layout: SpectrogramLayout | None = None,
446
+ is_training: bool = True,
447
+ augment_eval: bool = False,
448
+ ) -> tf.Tensor:
449
+ if not _enabled(is_training, augment_eval):
450
+ return tf.convert_to_tensor(spectrogram)
451
+
452
+ cfg = _merge_config(
453
+ config,
454
+ {
455
+ "max_time_width": max_time_width,
456
+ "max_freq_width": max_freq_width,
457
+ "fill_value": fill_value,
458
+ "prob": prob,
459
+ "layout": layout,
460
+ },
461
+ )
462
+ max_time_width = cfg.get("max_time_width")
463
+ max_freq_width = cfg.get("max_freq_width")
464
+ fill_value = cfg.get("fill_value", "zero")
465
+ prob = cfg.get("prob", 1.0)
466
+ layout = cfg.get("layout")
467
+
468
+ seed = _seed_tensor(seed)
469
+ gate_seed, t_width_seed, t_start_seed, f_width_seed, f_start_seed = tf.unstack(
470
+ tf.random.split(seed, 5)
471
+ )
472
+ x, resolved_layout = _as_tfc(spectrogram, layout)
473
+ fill = _fill_scalar(x, fill_value)
474
+
475
+ def apply() -> tf.Tensor:
476
+ time = tf.shape(x)[0]
477
+ freq = tf.shape(x)[1]
478
+ t_start, t_width = _sample_span(
479
+ time, max_time_width, t_width_seed, t_start_seed
480
+ )
481
+ f_start, f_width = _sample_span(
482
+ freq, max_freq_width, f_width_seed, f_start_seed
483
+ )
484
+ time_positions = tf.range(time)
485
+ freq_positions = tf.range(freq)
486
+ time_mask = tf.logical_and(
487
+ time_positions >= t_start,
488
+ time_positions < t_start + t_width,
489
+ )
490
+ freq_mask = tf.logical_and(
491
+ freq_positions >= f_start,
492
+ freq_positions < f_start + f_width,
493
+ )
494
+ mask = tf.logical_and(time_mask[:, tf.newaxis], freq_mask[tf.newaxis, :])
495
+ erased = tf.where(mask[:, :, tf.newaxis], fill, x)
496
+ return _restore_from_tfc(erased, resolved_layout)
497
+
498
+ return _maybe_apply(tf.convert_to_tensor(spectrogram), prob, gate_seed, apply)
499
+
500
+
501
+ def _sample_beta(
502
+ shape: tf.Tensor | Sequence[int], alpha: float, seed: tf.Tensor
503
+ ) -> tf.Tensor:
504
+ if alpha <= 0:
505
+ raise ValueError("alpha must be positive")
506
+ first_seed, second_seed = tf.unstack(tf.random.split(seed, 2))
507
+ first = tf.random.stateless_gamma(
508
+ shape, seed=first_seed, alpha=alpha, dtype=tf.float32
509
+ )
510
+ second = tf.random.stateless_gamma(
511
+ shape, seed=second_seed, alpha=alpha, dtype=tf.float32
512
+ )
513
+ return first / (first + second + _EPS)
514
+
515
+
516
+ def _mixstyle_impl(
517
+ spectrogram: tf.Tensor,
518
+ *,
519
+ seed: tf.Tensor | int | None,
520
+ alpha: float,
521
+ prob: float,
522
+ layout: BatchSpectrogramLayout | None,
523
+ stat_axes: Sequence[int],
524
+ ) -> tf.Tensor:
525
+ seed = _seed_tensor(seed)
526
+ gate_seed, shuffle_seed, beta_seed = tf.unstack(tf.random.split(seed, 3))
527
+ x, resolved_layout = _as_btfc(spectrogram, layout)
528
+ input_dtype = x.dtype
529
+ x_float = tf.cast(x, tf.float32)
530
+
531
+ def apply() -> tf.Tensor:
532
+ batch = tf.shape(x_float)[0]
533
+
534
+ def mix() -> tf.Tensor:
535
+ axes = list(stat_axes)
536
+ mean = tf.reduce_mean(x_float, axis=axes, keepdims=True)
537
+ var = tf.reduce_mean(tf.square(x_float - mean), axis=axes, keepdims=True)
538
+ std = tf.sqrt(var + _EPS)
539
+ partner = _stateless_shuffled_range(batch, shuffle_seed)
540
+ partner_mean = tf.gather(mean, partner, axis=0)
541
+ partner_std = tf.gather(std, partner, axis=0)
542
+ lam_shape = tf.concat(
543
+ [tf.reshape(batch, [1]), tf.ones([tf.rank(mean) - 1], tf.int32)],
544
+ axis=0,
545
+ )
546
+ lam = _sample_beta(lam_shape, alpha, beta_seed)
547
+ mixed_mean = lam * mean + (1.0 - lam) * partner_mean
548
+ mixed_std = lam * std + (1.0 - lam) * partner_std
549
+ mixed = (x_float - mean) / std * mixed_std + mixed_mean
550
+ return tf.cast(mixed, input_dtype)
551
+
552
+ return tf.cond(batch > 1, mix, lambda: x)
553
+
554
+ mixed = _maybe_apply(x, prob, gate_seed, apply)
555
+ return _restore_from_btfc(mixed, resolved_layout)
556
+
557
+
558
+ @register_audio_spectrogram_augment("mixstyle")
559
+ def mixstyle(
560
+ spectrogram: tf.Tensor,
561
+ *,
562
+ seed: tf.Tensor | int | None = None,
563
+ config: Mapping[str, Any] | None = None,
564
+ alpha: float | None = None,
565
+ prob: float | None = None,
566
+ layout: BatchSpectrogramLayout | None = None,
567
+ is_training: bool = True,
568
+ augment_eval: bool = False,
569
+ ) -> tf.Tensor:
570
+ if not _enabled(is_training, augment_eval):
571
+ return tf.convert_to_tensor(spectrogram)
572
+
573
+ cfg = _merge_config(config, {"alpha": alpha, "prob": prob, "layout": layout})
574
+ return _mixstyle_impl(
575
+ spectrogram,
576
+ seed=seed,
577
+ alpha=cfg.get("alpha", 0.1),
578
+ prob=cfg.get("prob", 1.0),
579
+ layout=cfg.get("layout"),
580
+ stat_axes=(1, 2, 3),
581
+ )
582
+
583
+
584
+ @register_audio_spectrogram_augment("frequency_mixstyle")
585
+ def frequency_mixstyle(
586
+ spectrogram: tf.Tensor,
587
+ *,
588
+ seed: tf.Tensor | int | None = None,
589
+ config: Mapping[str, Any] | None = None,
590
+ alpha: float | None = None,
591
+ prob: float | None = None,
592
+ layout: BatchSpectrogramLayout | None = None,
593
+ is_training: bool = True,
594
+ augment_eval: bool = False,
595
+ ) -> tf.Tensor:
596
+ if not _enabled(is_training, augment_eval):
597
+ return tf.convert_to_tensor(spectrogram)
598
+
599
+ cfg = _merge_config(config, {"alpha": alpha, "prob": prob, "layout": layout})
600
+ return _mixstyle_impl(
601
+ spectrogram,
602
+ seed=seed,
603
+ alpha=cfg.get("alpha", 0.1),
604
+ prob=cfg.get("prob", 1.0),
605
+ layout=cfg.get("layout"),
606
+ stat_axes=(1, 3),
607
+ )
608
+
609
+
610
+ @register_audio_spectrogram_augment("random_eq")
611
+ def random_eq(
612
+ spectrogram: tf.Tensor,
613
+ *,
614
+ seed: tf.Tensor | int | None = None,
615
+ config: Mapping[str, Any] | None = None,
616
+ num_bands: int | None = None,
617
+ min_db: float | None = None,
618
+ max_db: float | None = None,
619
+ min_band_width: int | None = None,
620
+ max_band_width: int | None = None,
621
+ prob: float | None = None,
622
+ layout: SpectrogramLayout | None = None,
623
+ is_training: bool = True,
624
+ augment_eval: bool = False,
625
+ ) -> tf.Tensor:
626
+ if not _enabled(is_training, augment_eval):
627
+ return tf.convert_to_tensor(spectrogram)
628
+
629
+ cfg = _merge_config(
630
+ config,
631
+ {
632
+ "num_bands": num_bands,
633
+ "min_db": min_db,
634
+ "max_db": max_db,
635
+ "min_band_width": min_band_width,
636
+ "max_band_width": max_band_width,
637
+ "prob": prob,
638
+ "layout": layout,
639
+ },
640
+ )
641
+ num_bands = int(cfg.get("num_bands", 1))
642
+ min_db = cfg.get("min_db", -6.0)
643
+ max_db = cfg.get("max_db", 6.0)
644
+ min_band_width = int(cfg.get("min_band_width", 1))
645
+ max_band_width = cfg.get("max_band_width")
646
+ prob = cfg.get("prob", 1.0)
647
+ layout = cfg.get("layout")
648
+
649
+ if num_bands < 0:
650
+ raise ValueError("num_bands must be non-negative")
651
+ if min_band_width <= 0:
652
+ raise ValueError("min_band_width must be positive")
653
+ if min_db > max_db:
654
+ raise ValueError("min_db must be <= max_db")
655
+
656
+ seed = _seed_tensor(seed)
657
+ seeds = tf.unstack(tf.random.split(seed, 1 + max(num_bands, 1) * 3))
658
+ gate_seed = seeds[0]
659
+ band_seeds = seeds[1:]
660
+ x, resolved_layout = _as_tfc(spectrogram, layout)
661
+ input_dtype = x.dtype
662
+
663
+ def apply() -> tf.Tensor:
664
+ freq = tf.shape(x)[1]
665
+ positions = tf.range(freq)
666
+ gains = tf.ones([freq], dtype=tf.float32)
667
+ width_max = _max_width(max_band_width, freq)
668
+ width_min = tf.minimum(tf.cast(min_band_width, tf.int32), width_max)
669
+
670
+ for band_index in range(num_bands):
671
+ width_seed = band_seeds[band_index * 3]
672
+ start_seed = band_seeds[band_index * 3 + 1]
673
+ gain_seed = band_seeds[band_index * 3 + 2]
674
+ width = _uniform_int(width_seed, width_min, width_max)
675
+ max_start = tf.maximum(freq - width, 0)
676
+ start = _uniform_int(start_seed, tf.constant(0, tf.int32), max_start)
677
+ gain_db = _uniform_float(gain_seed, min_db, max_db)
678
+ gain = tf.pow(tf.constant(10.0, dtype=tf.float32), gain_db / 20.0)
679
+ mask = tf.logical_and(positions >= start, positions < start + width)
680
+ gains = tf.where(mask, gains * gain, gains)
681
+
682
+ equalized = tf.cast(x, tf.float32) * gains[tf.newaxis, :, tf.newaxis]
683
+ return _restore_from_tfc(tf.cast(equalized, input_dtype), resolved_layout)
684
+
685
+ return _maybe_apply(tf.convert_to_tensor(spectrogram), prob, gate_seed, apply)
686
+
687
+
688
+ def normalize_spectrogram_augment_specs(
689
+ augmentations: Mapping[str, Any] | Sequence[Any] | str | None,
690
+ ) -> list[dict[str, Any]]:
691
+ if augmentations is None:
692
+ return []
693
+ if isinstance(augmentations, str):
694
+ return [{"name": augmentations}]
695
+ if isinstance(augmentations, Mapping):
696
+ if "name" in augmentations:
697
+ spec = dict(augmentations)
698
+ spec["name"] = str(spec["name"])
699
+ return [spec]
700
+
701
+ specs = []
702
+ for name, value in augmentations.items():
703
+ if value is None or value is False:
704
+ continue
705
+ registered_name = "passt_patchout" if name == "patchout" else str(name)
706
+ if value is True:
707
+ specs.append({"name": registered_name})
708
+ elif isinstance(value, Mapping):
709
+ specs.append({"name": registered_name, **dict(value)})
710
+ else:
711
+ specs.append({"name": registered_name, "config": value})
712
+ return specs
713
+
714
+ specs = []
715
+ for value in augmentations:
716
+ specs.extend(normalize_spectrogram_augment_specs(value))
717
+ return specs
718
+
719
+
720
+ def apply_spectrogram_augmentations(
721
+ sample: dict,
722
+ augmentations: Mapping[str, Any] | Sequence[Any] | str | None,
723
+ *,
724
+ seed: tf.Tensor | int | None = None,
725
+ is_training: bool = True,
726
+ augment_eval: bool = False,
727
+ feature_key: str = FEATURES,
728
+ layout: SpectrogramLayout | None = None,
729
+ ) -> dict:
730
+ specs = normalize_spectrogram_augment_specs(augmentations)
731
+ if (
732
+ not specs
733
+ or not _enabled(is_training, augment_eval)
734
+ or feature_key not in sample
735
+ ):
736
+ return sample
737
+
738
+ seed = _seed_tensor(seed)
739
+ seeds = tf.random.split(seed, len(specs))
740
+ result = dict(sample)
741
+
742
+ for spec, spec_seed in zip(specs, tf.unstack(seeds)):
743
+ kwargs = dict(spec)
744
+ name = kwargs.pop("name")
745
+ spec_layout = kwargs.pop("layout", layout)
746
+ fn = get_audio_spectrogram_augment(name)
747
+ if name == "passt_patchout":
748
+ result = fn(
749
+ result,
750
+ seed=spec_seed,
751
+ feature_key=feature_key,
752
+ layout=spec_layout,
753
+ is_training=is_training,
754
+ augment_eval=augment_eval,
755
+ **kwargs,
756
+ )
757
+ else:
758
+ result[feature_key] = fn(
759
+ result[feature_key],
760
+ seed=spec_seed,
761
+ layout=spec_layout,
762
+ is_training=is_training,
763
+ augment_eval=augment_eval,
764
+ **kwargs,
765
+ )
766
+
767
+ return result
768
+
769
+
770
+ def make_spectrogram_augmentation_stage(
771
+ augmentations: Mapping[str, Any] | Sequence[Any] | str | None,
772
+ *,
773
+ is_training: bool = True,
774
+ augment_eval: bool = False,
775
+ feature_key: str = FEATURES,
776
+ layout: SpectrogramLayout | None = None,
777
+ ):
778
+ specs = normalize_spectrogram_augment_specs(augmentations)
779
+
780
+ def stage(sample: dict, seed: tf.Tensor | int | None = None) -> dict:
781
+ return apply_spectrogram_augmentations(
782
+ sample,
783
+ specs,
784
+ seed=seed,
785
+ is_training=is_training,
786
+ augment_eval=augment_eval,
787
+ feature_key=feature_key,
788
+ layout=layout,
789
+ )
790
+
791
+ return stage
792
+
793
+
794
+ __all__ = [
795
+ "apply_spectrogram_augmentations",
796
+ "frequency_mask",
797
+ "frequency_mixstyle",
798
+ "make_spectrogram_augmentation_stage",
799
+ "mel_bin_shift",
800
+ "mixstyle",
801
+ "normalize_spectrogram_augment_specs",
802
+ "random_eq",
803
+ "spectrogram_time_roll",
804
+ "time_frequency_erasing",
805
+ "time_mask",
806
+ ]