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,813 @@
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.augment.configs import (
10
+ AudioBatchMixStyleConfig,
11
+ AudioCutMixSpecConfig,
12
+ AudioMixupConfig,
13
+ AudioWavMixConfig,
14
+ )
15
+ from justdata.acoustic.augment.spectrogram import (
16
+ BatchSpectrogramLayout,
17
+ _as_btfc,
18
+ _restore_from_btfc,
19
+ )
20
+ from justdata.acoustic.registry import (
21
+ get_audio_batch_augment,
22
+ register_audio_batch_augment,
23
+ )
24
+ from justdata.acoustic.schema import FEATURES, LABEL, WAVEFORM
25
+ from justdata.core.label_mixing import (
26
+ LabelMixMode,
27
+ mix_labels,
28
+ prepare_labels_for_mixing,
29
+ )
30
+
31
+
32
+ _EPS = tf.constant(1e-6, dtype=tf.float32)
33
+
34
+ _ALIASES = {
35
+ "cutmix": "cutmix_spec",
36
+ "mixstyle": "batch_mixstyle",
37
+ }
38
+ _LABEL_MIXING_AUGMENTS = {"mixup", "cutmix_spec", "wavmix"}
39
+
40
+
41
+ def _seed_tensor(seed: tf.Tensor | int | None) -> tf.Tensor:
42
+ if seed is None:
43
+ return tf.constant([0, 0], dtype=tf.int32)
44
+
45
+ seed = tf.cast(tf.convert_to_tensor(seed), tf.int32)
46
+ if seed.shape.rank == 0:
47
+ return tf.stack([seed, tf.constant(0, dtype=tf.int32)])
48
+ if seed.shape.rank == 1 and seed.shape[0] == 1:
49
+ return tf.stack([seed[0], tf.constant(0, dtype=tf.int32)])
50
+ return seed[:2]
51
+
52
+
53
+ def _enabled(is_training: bool, augment_eval: bool) -> bool:
54
+ return bool(is_training or augment_eval)
55
+
56
+
57
+ def _merge_config(
58
+ config: Any,
59
+ config_cls: type,
60
+ defaults: Mapping[str, Any],
61
+ overrides: Mapping[str, Any],
62
+ ):
63
+ data = dict(defaults)
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 _sample_beta(
78
+ shape: tf.Tensor | Sequence[int], alpha: float, seed: tf.Tensor
79
+ ) -> tf.Tensor:
80
+ first_seed, second_seed = tf.unstack(tf.random.split(seed, 2))
81
+ first = tf.random.stateless_gamma(
82
+ shape, seed=first_seed, alpha=alpha, dtype=tf.float32
83
+ )
84
+ second = tf.random.stateless_gamma(
85
+ shape, seed=second_seed, alpha=alpha, dtype=tf.float32
86
+ )
87
+ return first / (first + second + _EPS)
88
+
89
+
90
+ def _sample_lambda(alpha: float, seed: tf.Tensor, keep_max_lambda: bool) -> tf.Tensor:
91
+ lam = _sample_beta([], alpha, seed)
92
+ if keep_max_lambda:
93
+ lam = tf.maximum(lam, 1.0 - lam)
94
+ return lam
95
+
96
+
97
+ def _stateless_shuffled_range(size: tf.Tensor, seed: tf.Tensor) -> tf.Tensor:
98
+ size = tf.cast(size, tf.int32)
99
+ order = tf.argsort(
100
+ tf.random.stateless_uniform(tf.reshape(size, [1]), seed=seed),
101
+ stable=True,
102
+ )
103
+ identity = tf.range(size)
104
+ is_identity = tf.reduce_all(tf.equal(order, identity))
105
+ return tf.cond(
106
+ tf.logical_and(size > 1, is_identity),
107
+ lambda: tf.roll(order, shift=1, axis=0),
108
+ lambda: order,
109
+ )
110
+
111
+
112
+ def _uniform_int(seed: tf.Tensor, minval: tf.Tensor, maxval: tf.Tensor) -> tf.Tensor:
113
+ minval = tf.cast(minval, tf.int32)
114
+ maxval = tf.cast(maxval, tf.int32)
115
+ return tf.cond(
116
+ tf.equal(minval, maxval),
117
+ lambda: minval,
118
+ lambda: tf.random.stateless_uniform(
119
+ [],
120
+ seed=seed,
121
+ minval=minval,
122
+ maxval=maxval + 1,
123
+ dtype=tf.int32,
124
+ ),
125
+ )
126
+
127
+
128
+ def _mix_tensor(x: tf.Tensor, partner: tf.Tensor, lam: tf.Tensor) -> tf.Tensor:
129
+ dtype = x.dtype
130
+ mixed = tf.cast(lam, tf.float32) * tf.cast(x, tf.float32)
131
+ mixed += (1.0 - tf.cast(lam, tf.float32)) * tf.cast(partner, tf.float32)
132
+ return tf.cast(mixed, dtype)
133
+
134
+
135
+ def _maybe_apply_pair(
136
+ x: tf.Tensor,
137
+ labels: tf.Tensor,
138
+ prob: float,
139
+ seed: tf.Tensor,
140
+ apply_fn,
141
+ skip_fn,
142
+ ) -> tuple[tf.Tensor, tf.Tensor]:
143
+ if prob <= 0.0:
144
+ return x, labels
145
+ if prob >= 1.0:
146
+ return apply_fn()
147
+ should_apply = tf.random.stateless_uniform([], seed=seed) < tf.cast(
148
+ prob, tf.float32
149
+ )
150
+ return tf.cond(should_apply, apply_fn, skip_fn)
151
+
152
+
153
+ def _maybe_apply_tensor(
154
+ x: tf.Tensor,
155
+ prob: float,
156
+ seed: tf.Tensor,
157
+ apply_fn,
158
+ ) -> tf.Tensor:
159
+ if prob <= 0.0:
160
+ return x
161
+ if prob >= 1.0:
162
+ return apply_fn()
163
+ should_apply = tf.random.stateless_uniform([], seed=seed) < tf.cast(
164
+ prob, tf.float32
165
+ )
166
+ return tf.cond(should_apply, apply_fn, lambda: x)
167
+
168
+
169
+ @register_audio_batch_augment("mixup", requires_labels=True)
170
+ def audio_mixup(
171
+ inputs: tf.Tensor,
172
+ labels: tf.Tensor,
173
+ *,
174
+ seed: tf.Tensor | int | None = None,
175
+ config: AudioMixupConfig | Mapping[str, Any] | None = None,
176
+ alpha: float | None = None,
177
+ prob: float | None = None,
178
+ label_mode: LabelMixMode | None = None,
179
+ num_classes: int | None = None,
180
+ keep_max_lambda: bool = True,
181
+ is_training: bool = True,
182
+ augment_eval: bool = False,
183
+ ) -> tuple[tf.Tensor, tf.Tensor]:
184
+ if not _enabled(is_training, augment_eval):
185
+ return tf.convert_to_tensor(inputs), tf.convert_to_tensor(labels)
186
+
187
+ cfg = _merge_config(
188
+ config,
189
+ AudioMixupConfig,
190
+ {"alpha": 0.8, "prob": 1.0, "label_mode": label_mode or "single_label"},
191
+ {"alpha": alpha, "prob": prob, "label_mode": label_mode},
192
+ )
193
+ x = tf.convert_to_tensor(inputs)
194
+ y = tf.convert_to_tensor(labels)
195
+ if cfg.prob <= 0.0:
196
+ return x, y
197
+
198
+ seed = _seed_tensor(seed)
199
+ gate_seed, shuffle_seed, beta_seed = tf.unstack(tf.random.split(seed, 3))
200
+ prepared_labels = prepare_labels_for_mixing(
201
+ y,
202
+ label_mode=cfg.label_mode,
203
+ num_classes=num_classes,
204
+ )
205
+
206
+ def apply() -> tuple[tf.Tensor, tf.Tensor]:
207
+ batch = tf.shape(x)[0]
208
+ partner = _stateless_shuffled_range(batch, shuffle_seed)
209
+ lam = _sample_lambda(cfg.alpha, beta_seed, keep_max_lambda)
210
+ mixed_x = _mix_tensor(x, tf.gather(x, partner, axis=0), lam)
211
+ mixed_y = mix_labels(
212
+ y,
213
+ tf.gather(y, partner, axis=0),
214
+ lam,
215
+ label_mode=cfg.label_mode,
216
+ num_classes=num_classes,
217
+ )
218
+ return mixed_x, mixed_y
219
+
220
+ return _maybe_apply_pair(
221
+ x, prepared_labels, cfg.prob, gate_seed, apply, lambda: (x, prepared_labels)
222
+ )
223
+
224
+
225
+ @register_audio_batch_augment("wavmix", requires_labels=True)
226
+ def audio_wavmix(
227
+ waveform: tf.Tensor,
228
+ labels: tf.Tensor | None = None,
229
+ *,
230
+ seed: tf.Tensor | int | None = None,
231
+ config: AudioWavMixConfig | Mapping[str, Any] | None = None,
232
+ alpha: float | None = None,
233
+ prob: float | None = None,
234
+ label_mode: LabelMixMode | None = None,
235
+ num_classes: int | None = None,
236
+ input_kind: Literal["waveform", "spectrogram"] = "waveform",
237
+ keep_max_lambda: bool = True,
238
+ is_training: bool = True,
239
+ augment_eval: bool = False,
240
+ ):
241
+ if input_kind != "waveform":
242
+ raise ValueError(
243
+ "WavMix only supports waveform inputs; got input_kind != 'waveform'."
244
+ )
245
+
246
+ x = tf.convert_to_tensor(waveform)
247
+ if x.shape.rank not in (2, 3):
248
+ raise ValueError("WavMix input must have shape [B, T] or [B, T, C].")
249
+
250
+ if not _enabled(is_training, augment_eval):
251
+ return (x, tf.convert_to_tensor(labels)) if labels is not None else x
252
+
253
+ cfg = _merge_config(
254
+ config,
255
+ AudioWavMixConfig,
256
+ {"alpha": 0.8, "prob": 1.0},
257
+ {"alpha": alpha, "prob": prob},
258
+ )
259
+ if cfg.prob <= 0.0:
260
+ return (x, tf.convert_to_tensor(labels)) if labels is not None else x
261
+
262
+ seed = _seed_tensor(seed)
263
+ gate_seed, shuffle_seed, beta_seed = tf.unstack(tf.random.split(seed, 3))
264
+
265
+ def mix_waveform() -> tf.Tensor:
266
+ partner = _stateless_shuffled_range(tf.shape(x)[0], shuffle_seed)
267
+ lam = _sample_lambda(cfg.alpha, beta_seed, keep_max_lambda)
268
+ return _mix_tensor(x, tf.gather(x, partner, axis=0), lam)
269
+
270
+ if labels is None:
271
+ return _maybe_apply_tensor(x, cfg.prob, gate_seed, mix_waveform)
272
+
273
+ y = tf.convert_to_tensor(labels)
274
+ resolved_label_mode = label_mode or "single_label"
275
+ prepared_labels = prepare_labels_for_mixing(
276
+ y,
277
+ label_mode=resolved_label_mode,
278
+ num_classes=num_classes,
279
+ )
280
+
281
+ def apply() -> tuple[tf.Tensor, tf.Tensor]:
282
+ partner = _stateless_shuffled_range(tf.shape(x)[0], shuffle_seed)
283
+ lam = _sample_lambda(cfg.alpha, beta_seed, keep_max_lambda)
284
+ return (
285
+ _mix_tensor(x, tf.gather(x, partner, axis=0), lam),
286
+ mix_labels(
287
+ y,
288
+ tf.gather(y, partner, axis=0),
289
+ lam,
290
+ label_mode=resolved_label_mode,
291
+ num_classes=num_classes,
292
+ ),
293
+ )
294
+
295
+ return _maybe_apply_pair(
296
+ x, prepared_labels, cfg.prob, gate_seed, apply, lambda: (x, prepared_labels)
297
+ )
298
+
299
+
300
+ def _selected_width(size: tf.Tensor, fraction: tf.Tensor) -> tf.Tensor:
301
+ size = tf.cast(size, tf.int32)
302
+ width = tf.cast(tf.round(tf.cast(size, tf.float32) * fraction), tf.int32)
303
+ width = tf.clip_by_value(width, 0, size)
304
+ return tf.where(size > 0, tf.maximum(width, 1), width)
305
+
306
+
307
+ def _cutmix_widths(
308
+ time: tf.Tensor,
309
+ frequency: tf.Tensor,
310
+ lam: tf.Tensor,
311
+ axes: Literal["time", "frequency", "time_frequency"],
312
+ ) -> tuple[tf.Tensor, tf.Tensor]:
313
+ cut_fraction = 1.0 - tf.cast(lam, tf.float32)
314
+ if axes == "time":
315
+ return _selected_width(time, cut_fraction), tf.cast(frequency, tf.int32)
316
+ if axes == "frequency":
317
+ return tf.cast(time, tf.int32), _selected_width(frequency, cut_fraction)
318
+ if axes == "time_frequency":
319
+ side_fraction = tf.sqrt(cut_fraction)
320
+ return _selected_width(time, side_fraction), _selected_width(
321
+ frequency, side_fraction
322
+ )
323
+ raise ValueError("axes must be one of 'time', 'frequency', or 'time_frequency'")
324
+
325
+
326
+ def _spliced_event_labels(
327
+ labels: tf.Tensor,
328
+ partner_labels: tf.Tensor,
329
+ time_mask: tf.Tensor,
330
+ spectrogram_time: tf.Tensor,
331
+ ) -> tf.Tensor:
332
+ labels = tf.cast(labels, tf.float32)
333
+ partner_labels = tf.cast(partner_labels, tf.float32)
334
+ if labels.shape.rank != 3:
335
+ raise ValueError("event_frames labels must have shape [B, T_frames, C].")
336
+ if (
337
+ labels.shape[1] is not None
338
+ and time_mask.shape[0] is not None
339
+ and labels.shape[1] != time_mask.shape[0]
340
+ ):
341
+ raise ValueError(
342
+ "CutMixSpec event-frame splicing requires label frames to match "
343
+ "spectrogram time bins."
344
+ )
345
+ with tf.control_dependencies(
346
+ [
347
+ tf.debugging.assert_equal(
348
+ tf.shape(labels)[1],
349
+ spectrogram_time,
350
+ message=(
351
+ "CutMixSpec event-frame splicing requires label frames to match "
352
+ "spectrogram time bins."
353
+ ),
354
+ )
355
+ ]
356
+ ):
357
+ return tf.where(time_mask[tf.newaxis, :, tf.newaxis], partner_labels, labels)
358
+
359
+
360
+ @register_audio_batch_augment("cutmix_spec", requires_labels=True)
361
+ def audio_cutmix_spec(
362
+ spectrogram: tf.Tensor,
363
+ labels: tf.Tensor,
364
+ *,
365
+ seed: tf.Tensor | int | None = None,
366
+ config: AudioCutMixSpecConfig | Mapping[str, Any] | None = None,
367
+ alpha: float | None = None,
368
+ prob: float | None = None,
369
+ axes: Literal["time", "frequency", "time_frequency"] | None = None,
370
+ label_mode: LabelMixMode | None = None,
371
+ num_classes: int | None = None,
372
+ layout: BatchSpectrogramLayout | None = None,
373
+ input_kind: Literal["waveform", "spectrogram"] = "spectrogram",
374
+ keep_max_lambda: bool = True,
375
+ is_training: bool = True,
376
+ augment_eval: bool = False,
377
+ ) -> tuple[tf.Tensor, tf.Tensor]:
378
+ if input_kind != "spectrogram":
379
+ raise ValueError("CutMixSpec only supports spectrogram inputs.")
380
+ if not _enabled(is_training, augment_eval):
381
+ return tf.convert_to_tensor(spectrogram), tf.convert_to_tensor(labels)
382
+
383
+ cfg = _merge_config(
384
+ config,
385
+ AudioCutMixSpecConfig,
386
+ {"alpha": 1.0, "prob": 1.0, "axes": axes or "time_frequency"},
387
+ {"alpha": alpha, "prob": prob, "axes": axes},
388
+ )
389
+
390
+ original = tf.convert_to_tensor(spectrogram)
391
+ y = tf.convert_to_tensor(labels)
392
+ if cfg.prob <= 0.0:
393
+ return original, y
394
+
395
+ x, resolved_layout = _as_btfc(original, layout)
396
+ seed = _seed_tensor(seed)
397
+ gate_seed, shuffle_seed, beta_seed, time_seed, freq_seed = tf.unstack(
398
+ tf.random.split(seed, 5)
399
+ )
400
+ resolved_label_mode = label_mode or "single_label"
401
+ if (
402
+ resolved_label_mode == "event_frames"
403
+ and y.shape.rank == 3
404
+ and x.shape[1] is not None
405
+ and y.shape[1] is not None
406
+ and x.shape[1] != y.shape[1]
407
+ ):
408
+ raise ValueError(
409
+ "CutMixSpec event-frame labels must match spectrogram time bins unless "
410
+ "an explicit alignment config is provided."
411
+ )
412
+ prepared_labels = prepare_labels_for_mixing(
413
+ y,
414
+ label_mode=resolved_label_mode,
415
+ num_classes=num_classes,
416
+ )
417
+
418
+ def apply() -> tuple[tf.Tensor, tf.Tensor]:
419
+ batch = tf.shape(x)[0]
420
+ time = tf.shape(x)[1]
421
+ freq = tf.shape(x)[2]
422
+ partner = _stateless_shuffled_range(batch, shuffle_seed)
423
+ lam = _sample_lambda(cfg.alpha, beta_seed, keep_max_lambda)
424
+ time_width, freq_width = _cutmix_widths(time, freq, lam, cfg.axes)
425
+ time_start = _uniform_int(
426
+ time_seed, tf.constant(0, tf.int32), tf.maximum(time - time_width, 0)
427
+ )
428
+ freq_start = _uniform_int(
429
+ freq_seed, tf.constant(0, tf.int32), tf.maximum(freq - freq_width, 0)
430
+ )
431
+
432
+ time_positions = tf.range(time)
433
+ freq_positions = tf.range(freq)
434
+ time_mask = tf.logical_and(
435
+ time_positions >= time_start,
436
+ time_positions < time_start + time_width,
437
+ )
438
+ freq_mask = tf.logical_and(
439
+ freq_positions >= freq_start,
440
+ freq_positions < freq_start + freq_width,
441
+ )
442
+ mask = tf.logical_and(time_mask[:, tf.newaxis], freq_mask[tf.newaxis, :])
443
+ mixed = tf.where(
444
+ mask[tf.newaxis, :, :, tf.newaxis],
445
+ tf.gather(x, partner, axis=0),
446
+ x,
447
+ )
448
+
449
+ replaced_area = tf.cast(time_width * freq_width, tf.float32)
450
+ total_area = tf.cast(time * freq, tf.float32)
451
+ lam_effective = 1.0 - tf.math.divide_no_nan(replaced_area, total_area)
452
+ partner_y = tf.gather(y, partner, axis=0)
453
+ if resolved_label_mode == "event_frames" and cfg.axes == "time":
454
+ mixed_labels = _spliced_event_labels(y, partner_y, time_mask, time)
455
+ else:
456
+ mixed_labels = mix_labels(
457
+ y,
458
+ partner_y,
459
+ lam_effective,
460
+ label_mode=resolved_label_mode,
461
+ num_classes=num_classes,
462
+ )
463
+ return _restore_from_btfc(mixed, resolved_layout), mixed_labels
464
+
465
+ return _maybe_apply_pair(
466
+ original,
467
+ prepared_labels,
468
+ cfg.prob,
469
+ gate_seed,
470
+ apply,
471
+ lambda: (original, prepared_labels),
472
+ )
473
+
474
+
475
+ @register_audio_batch_augment("batch_mixstyle")
476
+ def audio_batch_mixstyle(
477
+ spectrogram: tf.Tensor,
478
+ labels: tf.Tensor | None = None,
479
+ *,
480
+ seed: tf.Tensor | int | None = None,
481
+ config: AudioBatchMixStyleConfig | Mapping[str, Any] | None = None,
482
+ prob: float | None = None,
483
+ mix: Literal["global", "frequency", "channel"] | None = None,
484
+ alpha: float = 0.1,
485
+ layout: BatchSpectrogramLayout | None = None,
486
+ input_kind: Literal["waveform", "spectrogram"] = "spectrogram",
487
+ is_training: bool = True,
488
+ augment_eval: bool = False,
489
+ ):
490
+ if input_kind != "spectrogram":
491
+ raise ValueError("Batch MixStyle only supports spectrogram inputs.")
492
+
493
+ original = tf.convert_to_tensor(spectrogram)
494
+ if not _enabled(is_training, augment_eval):
495
+ return (original, labels) if labels is not None else original
496
+
497
+ cfg = _merge_config(
498
+ config,
499
+ AudioBatchMixStyleConfig,
500
+ {"prob": 1.0, "mix": mix or "global"},
501
+ {"prob": prob, "mix": mix},
502
+ )
503
+ x, resolved_layout = _as_btfc(original, layout)
504
+ seed = _seed_tensor(seed)
505
+ gate_seed, shuffle_seed, beta_seed = tf.unstack(tf.random.split(seed, 3))
506
+ stat_axes = {
507
+ "global": (1, 2, 3),
508
+ "frequency": (1, 3),
509
+ "channel": (1, 2),
510
+ }[cfg.mix]
511
+
512
+ def apply() -> tf.Tensor:
513
+ batch = tf.shape(x)[0]
514
+
515
+ def mix_batch() -> tf.Tensor:
516
+ x_float = tf.cast(x, tf.float32)
517
+ mean = tf.reduce_mean(x_float, axis=stat_axes, keepdims=True)
518
+ var = tf.reduce_mean(
519
+ tf.square(x_float - mean), axis=stat_axes, keepdims=True
520
+ )
521
+ std = tf.sqrt(var + _EPS)
522
+ partner = _stateless_shuffled_range(batch, shuffle_seed)
523
+ partner_mean = tf.gather(mean, partner, axis=0)
524
+ partner_std = tf.gather(std, partner, axis=0)
525
+ lam_shape = tf.concat(
526
+ [tf.reshape(batch, [1]), tf.ones([tf.rank(mean) - 1], tf.int32)],
527
+ axis=0,
528
+ )
529
+ lam = _sample_beta(lam_shape, alpha, beta_seed)
530
+ mixed_mean = lam * mean + (1.0 - lam) * partner_mean
531
+ mixed_std = lam * std + (1.0 - lam) * partner_std
532
+ mixed = (x_float - mean) / std * mixed_std + mixed_mean
533
+ return tf.cast(mixed, x.dtype)
534
+
535
+ return tf.cond(batch > 1, mix_batch, lambda: x)
536
+
537
+ mixed = _maybe_apply_tensor(x, cfg.prob, gate_seed, apply)
538
+ restored = _restore_from_btfc(mixed, resolved_layout)
539
+ return (restored, labels) if labels is not None else restored
540
+
541
+
542
+ def _canonical_name(name: str) -> str:
543
+ return _ALIASES.get(name, name)
544
+
545
+
546
+ def normalize_batch_augment_specs(
547
+ augmentations: Mapping[str, Any] | Sequence[Any] | str | None,
548
+ ) -> list[dict[str, Any]]:
549
+ if augmentations is None:
550
+ return []
551
+ if isinstance(augmentations, str):
552
+ return [{"name": _canonical_name(augmentations)}]
553
+ if isinstance(augmentations, Mapping):
554
+ if "name" in augmentations:
555
+ spec = dict(augmentations)
556
+ spec["name"] = _canonical_name(str(spec["name"]))
557
+ return [spec]
558
+
559
+ specs = []
560
+ for name, value in augmentations.items():
561
+ if value is None or value is False:
562
+ continue
563
+ canonical = _canonical_name(str(name))
564
+ if value is True:
565
+ specs.append({"name": canonical})
566
+ elif isinstance(value, Mapping):
567
+ specs.append({"name": canonical, **dict(value)})
568
+ else:
569
+ specs.append({"name": canonical, "config": value})
570
+ return specs
571
+
572
+ specs = []
573
+ for value in augmentations:
574
+ specs.extend(normalize_batch_augment_specs(value))
575
+ return specs
576
+
577
+
578
+ def _label_transform_data(label_transform: Any) -> Mapping[str, Any] | None:
579
+ if label_transform is None:
580
+ return None
581
+ if dataclasses.is_dataclass(label_transform):
582
+ return dataclasses.asdict(label_transform)
583
+ if isinstance(label_transform, Mapping):
584
+ return label_transform
585
+ return None
586
+
587
+
588
+ def _label_mode_from_transform(label_transform: Any) -> str | None:
589
+ data = _label_transform_data(label_transform)
590
+ if data is None:
591
+ return None
592
+ mode = data.get("mode")
593
+ if mode in {"index", "one_hot"}:
594
+ return "single_label"
595
+ if mode == "multi_hot":
596
+ return "multi_label"
597
+ if mode == "event_frames":
598
+ return "event_frames"
599
+ if mode == "text":
600
+ return "text"
601
+ return None
602
+
603
+
604
+ def _num_classes_from_transform(label_transform: Any) -> int | None:
605
+ data = _label_transform_data(label_transform)
606
+ if data is None:
607
+ return None
608
+ return data.get("num_classes")
609
+
610
+
611
+ def _config_has_key(config: Any, key: str) -> bool:
612
+ if config is None:
613
+ return False
614
+ if dataclasses.is_dataclass(config):
615
+ return hasattr(config, key)
616
+ if isinstance(config, Mapping):
617
+ return key in config
618
+ return False
619
+
620
+
621
+ def _is_text_labels(labels: Any, label_mode: str | None) -> bool:
622
+ if label_mode == "text":
623
+ return True
624
+ if labels is None:
625
+ return False
626
+ try:
627
+ return tf.convert_to_tensor(labels).dtype == tf.string
628
+ except (TypeError, ValueError):
629
+ return False
630
+
631
+
632
+ def _resolve_input_key(
633
+ sample: Mapping[str, Any],
634
+ name: str,
635
+ input_key: str | None,
636
+ input_kind: str | None,
637
+ ) -> str | None:
638
+ if input_key is not None:
639
+ return input_key
640
+ if name == "wavmix":
641
+ return WAVEFORM if WAVEFORM in sample else None
642
+ if name in {"cutmix_spec", "batch_mixstyle"}:
643
+ return FEATURES if FEATURES in sample else None
644
+ if input_kind == "waveform" and WAVEFORM in sample:
645
+ return WAVEFORM
646
+ if input_kind == "spectrogram" and FEATURES in sample:
647
+ return FEATURES
648
+ if FEATURES in sample:
649
+ return FEATURES
650
+ if WAVEFORM in sample:
651
+ return WAVEFORM
652
+ return None
653
+
654
+
655
+ def _input_kind_from_key(input_key: str, input_kind: str | None) -> str:
656
+ if input_kind is not None:
657
+ return input_kind
658
+ return "waveform" if input_key == WAVEFORM else "spectrogram"
659
+
660
+
661
+ def apply_batch_augmentations(
662
+ sample: Mapping[str, Any],
663
+ augmentations: Mapping[str, Any] | Sequence[Any] | str | None,
664
+ *,
665
+ seed: tf.Tensor | int | None = None,
666
+ is_training: bool = True,
667
+ augment_eval: bool = False,
668
+ input_key: str | None = None,
669
+ input_kind: Literal["waveform", "spectrogram"] | None = None,
670
+ label_mode: LabelMixMode | Literal["text"] | None = None,
671
+ num_classes: int | None = None,
672
+ label_transform: Mapping[str, Any] | None = None,
673
+ spectrogram_layout: BatchSpectrogramLayout | None = None,
674
+ ) -> dict:
675
+ specs = normalize_batch_augment_specs(augmentations)
676
+ if not specs or not _enabled(is_training, augment_eval):
677
+ return dict(sample)
678
+
679
+ resolved_label_mode = (
680
+ label_mode or _label_mode_from_transform(label_transform) or "single_label"
681
+ )
682
+ resolved_num_classes = num_classes
683
+ if resolved_num_classes is None:
684
+ resolved_num_classes = _num_classes_from_transform(label_transform)
685
+ seed = _seed_tensor(seed)
686
+ seeds = tf.random.split(seed, len(specs))
687
+ result = dict(sample)
688
+
689
+ for spec, spec_seed in zip(specs, tf.unstack(seeds)):
690
+ kwargs = dict(spec)
691
+ name = kwargs.pop("name")
692
+ key = _resolve_input_key(
693
+ result, name, kwargs.pop("input_key", input_key), input_kind
694
+ )
695
+ if key is None or key not in result:
696
+ continue
697
+
698
+ labels = result.get(LABEL)
699
+ if name in _LABEL_MIXING_AUGMENTS:
700
+ if LABEL not in result or _is_text_labels(labels, resolved_label_mode):
701
+ continue
702
+ if "label_mode" not in kwargs and not _config_has_key(
703
+ kwargs.get("config"),
704
+ "label_mode",
705
+ ):
706
+ kwargs["label_mode"] = resolved_label_mode
707
+ kwargs.setdefault("num_classes", resolved_num_classes)
708
+
709
+ kind = _input_kind_from_key(key, kwargs.pop("input_kind", input_kind))
710
+ fn = get_audio_batch_augment(name)
711
+ if name == "batch_mixstyle":
712
+ output = fn(
713
+ result[key],
714
+ labels,
715
+ seed=spec_seed,
716
+ layout=kwargs.pop("layout", spectrogram_layout),
717
+ input_kind=kind,
718
+ is_training=is_training,
719
+ augment_eval=augment_eval,
720
+ **kwargs,
721
+ )
722
+ if isinstance(output, tuple):
723
+ result[key], result[LABEL] = output
724
+ else:
725
+ result[key] = output
726
+ elif name == "cutmix_spec":
727
+ result[key], result[LABEL] = fn(
728
+ result[key],
729
+ labels,
730
+ seed=spec_seed,
731
+ layout=kwargs.pop("layout", spectrogram_layout),
732
+ input_kind=kind,
733
+ is_training=is_training,
734
+ augment_eval=augment_eval,
735
+ **kwargs,
736
+ )
737
+ elif name == "wavmix":
738
+ result[key], result[LABEL] = fn(
739
+ result[key],
740
+ labels,
741
+ seed=spec_seed,
742
+ input_kind=kind,
743
+ is_training=is_training,
744
+ augment_eval=augment_eval,
745
+ **kwargs,
746
+ )
747
+ else:
748
+ result[key], result[LABEL] = fn(
749
+ result[key],
750
+ labels,
751
+ seed=spec_seed,
752
+ is_training=is_training,
753
+ augment_eval=augment_eval,
754
+ **kwargs,
755
+ )
756
+
757
+ return result
758
+
759
+
760
+ def make_batch_augmentation_stage(
761
+ augmentations: Mapping[str, Any] | Sequence[Any] | str | None,
762
+ *,
763
+ is_training: bool = True,
764
+ augment_eval: bool = False,
765
+ input_key: str | None = None,
766
+ input_kind: Literal["waveform", "spectrogram"] | None = None,
767
+ label_mode: LabelMixMode | Literal["text"] | None = None,
768
+ label_transform: Mapping[str, Any] | None = None,
769
+ spectrogram_layout: BatchSpectrogramLayout | None = None,
770
+ ):
771
+ specs = normalize_batch_augment_specs(augmentations)
772
+
773
+ def stage(sample: Mapping[str, Any], num_classes=None, seed=None) -> dict:
774
+ return apply_batch_augmentations(
775
+ sample,
776
+ specs,
777
+ seed=seed,
778
+ is_training=is_training,
779
+ augment_eval=augment_eval,
780
+ input_key=input_key,
781
+ input_kind=input_kind,
782
+ label_mode=label_mode,
783
+ num_classes=num_classes,
784
+ label_transform=label_transform,
785
+ spectrogram_layout=spectrogram_layout,
786
+ )
787
+
788
+ return stage
789
+
790
+
791
+ cutmix_spec = audio_cutmix_spec
792
+ mixup = audio_mixup
793
+ wavmix = audio_wavmix
794
+ batch_mixstyle = audio_batch_mixstyle
795
+
796
+
797
+ __all__ = [
798
+ "AudioBatchMixStyleConfig",
799
+ "AudioCutMixSpecConfig",
800
+ "AudioMixupConfig",
801
+ "AudioWavMixConfig",
802
+ "apply_batch_augmentations",
803
+ "audio_batch_mixstyle",
804
+ "audio_cutmix_spec",
805
+ "audio_mixup",
806
+ "audio_wavmix",
807
+ "batch_mixstyle",
808
+ "cutmix_spec",
809
+ "make_batch_augmentation_stage",
810
+ "mixup",
811
+ "normalize_batch_augment_specs",
812
+ "wavmix",
813
+ ]