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,369 @@
1
+ from __future__ import annotations
2
+
3
+ import tensorflow as tf
4
+
5
+ from justdata.acoustic.configs import SegmentStrategyConfig
6
+ from justdata.acoustic.registry import register_audio_segment_strategy
7
+ from justdata.acoustic.schema import AUDIO, METADATA
8
+
9
+
10
+ def _target_samples(
11
+ sample_rate: tf.Tensor | int, config: SegmentStrategyConfig
12
+ ) -> tf.Tensor:
13
+ target = tf.round(
14
+ tf.cast(config.clip_duration, tf.float32) * tf.cast(sample_rate, tf.float32)
15
+ )
16
+ return tf.maximum(tf.cast(target, tf.int32), 1)
17
+
18
+
19
+ def _seed_tensor(seed: tf.Tensor | int | None) -> tf.Tensor:
20
+ if seed is None:
21
+ return tf.constant([0, 0], dtype=tf.int32)
22
+
23
+ seed = tf.cast(tf.convert_to_tensor(seed), tf.int32)
24
+ if seed.shape.rank == 0:
25
+ return tf.stack([seed, tf.constant(0, dtype=tf.int32)])
26
+ if seed.shape.rank == 1 and seed.shape[0] == 1:
27
+ return tf.stack([seed[0], tf.constant(0, dtype=tf.int32)])
28
+ return seed[:2]
29
+
30
+
31
+ def _padding_counts(
32
+ audio: tf.Tensor,
33
+ target_samples: tf.Tensor,
34
+ pad_position: str,
35
+ seed: tf.Tensor | int | None,
36
+ ) -> tuple[tf.Tensor, tf.Tensor]:
37
+ deficit = tf.maximum(target_samples - tf.shape(audio)[0], 0)
38
+ if pad_position == "right":
39
+ before = tf.constant(0, dtype=tf.int32)
40
+ elif pad_position == "center":
41
+ before = deficit // 2
42
+ elif pad_position == "random":
43
+ before = tf.random.stateless_uniform(
44
+ shape=[],
45
+ seed=_seed_tensor(seed),
46
+ minval=0,
47
+ maxval=deficit + 1,
48
+ dtype=tf.int32,
49
+ )
50
+ else:
51
+ raise ValueError(f"Unknown audio pad_position: {pad_position!r}")
52
+ return before, deficit - before
53
+
54
+
55
+ def _pad_zero(
56
+ audio: tf.Tensor,
57
+ target_samples: tf.Tensor,
58
+ before: tf.Tensor,
59
+ after: tf.Tensor,
60
+ ) -> tf.Tensor:
61
+ padded = tf.pad(audio, [[before, after], [0, 0]])
62
+ return padded[:target_samples]
63
+
64
+
65
+ def _pad_repeat(
66
+ audio: tf.Tensor,
67
+ target_samples: tf.Tensor,
68
+ before: tf.Tensor,
69
+ after: tf.Tensor,
70
+ ) -> tf.Tensor:
71
+ t = tf.shape(audio)[0]
72
+ channels = tf.shape(audio)[-1]
73
+
74
+ def _empty() -> tf.Tensor:
75
+ return tf.zeros([target_samples, channels], dtype=audio.dtype)
76
+
77
+ def _repeat() -> tf.Tensor:
78
+ indices = tf.range(-before, t + after)
79
+ return tf.gather(audio, tf.math.floormod(indices, t))[:target_samples]
80
+
81
+ return tf.cond(t > 0, _repeat, _empty)
82
+
83
+
84
+ def _pad_reflect(
85
+ audio: tf.Tensor,
86
+ target_samples: tf.Tensor,
87
+ before: tf.Tensor,
88
+ after: tf.Tensor,
89
+ ) -> tf.Tensor:
90
+ t = tf.shape(audio)[0]
91
+
92
+ def _zero_pad() -> tf.Tensor:
93
+ return _pad_zero(audio, target_samples, before, after)
94
+
95
+ def _reflect() -> tf.Tensor:
96
+ period = 2 * t - 2
97
+ indices = tf.math.floormod(tf.range(-before, t + after), period)
98
+ indices = tf.where(indices < t, indices, period - indices)
99
+ return tf.gather(audio, indices)[:target_samples]
100
+
101
+ return tf.cond(t > 1, _reflect, _zero_pad)
102
+
103
+
104
+ def pad_waveform(
105
+ audio: tf.Tensor,
106
+ target_samples: tf.Tensor,
107
+ pad_mode: str,
108
+ *,
109
+ pad_position: str = "right",
110
+ seed: tf.Tensor | int | None = None,
111
+ ) -> tf.Tensor:
112
+ audio = tf.convert_to_tensor(audio)
113
+ before, after = _padding_counts(audio, target_samples, pad_position, seed)
114
+ if pad_mode == "zero":
115
+ return _pad_zero(audio, target_samples, before, after)
116
+ if pad_mode == "repeat":
117
+ return _pad_repeat(audio, target_samples, before, after)
118
+ if pad_mode == "reflect":
119
+ return _pad_reflect(audio, target_samples, before, after)
120
+ raise ValueError(f"Unknown audio pad_mode: {pad_mode!r}")
121
+
122
+
123
+ def _crop_or_pad(
124
+ audio: tf.Tensor,
125
+ start: tf.Tensor,
126
+ target_samples: tf.Tensor,
127
+ pad_mode: str,
128
+ pad_position: str,
129
+ seed: tf.Tensor | int | None,
130
+ ) -> tf.Tensor:
131
+ t = tf.shape(audio)[0]
132
+ start = tf.clip_by_value(start, 0, tf.maximum(t - 1, 0))
133
+ available = tf.minimum(target_samples, tf.maximum(t - start, 0))
134
+ cropped = audio[start : start + available]
135
+
136
+ return tf.cond(
137
+ tf.shape(cropped)[0] < target_samples,
138
+ lambda: pad_waveform(
139
+ cropped,
140
+ target_samples,
141
+ pad_mode,
142
+ pad_position=pad_position,
143
+ seed=seed,
144
+ ),
145
+ lambda: cropped[:target_samples],
146
+ )
147
+
148
+
149
+ def _center_start(t: tf.Tensor, target_samples: tf.Tensor) -> tf.Tensor:
150
+ return tf.maximum((t - target_samples) // 2, 0)
151
+
152
+
153
+ def _random_start(
154
+ t: tf.Tensor,
155
+ target_samples: tf.Tensor,
156
+ seed: tf.Tensor | int | None,
157
+ ) -> tf.Tensor:
158
+ max_start = tf.maximum(t - target_samples, 0)
159
+
160
+ def _sample() -> tf.Tensor:
161
+ return tf.random.stateless_uniform(
162
+ shape=[],
163
+ seed=_seed_tensor(seed),
164
+ minval=0,
165
+ maxval=max_start + 1,
166
+ dtype=tf.int32,
167
+ )
168
+
169
+ return tf.cond(max_start > 0, _sample, lambda: tf.constant(0, dtype=tf.int32))
170
+
171
+
172
+ def _multi_crop_starts(
173
+ t: tf.Tensor,
174
+ target_samples: tf.Tensor,
175
+ num_views: int,
176
+ ) -> tf.Tensor:
177
+ if num_views == 1:
178
+ return tf.reshape(_center_start(t, target_samples), [1])
179
+
180
+ max_start = tf.maximum(t - target_samples, 0)
181
+ starts = tf.round(
182
+ tf.linspace(
183
+ tf.constant(0.0, dtype=tf.float32),
184
+ tf.cast(max_start, tf.float32),
185
+ num_views,
186
+ )
187
+ )
188
+ return tf.cast(starts, tf.int32)
189
+
190
+
191
+ def _sliding_starts(
192
+ t: tf.Tensor,
193
+ target_samples: tf.Tensor,
194
+ sample_rate: tf.Tensor | int,
195
+ config: SegmentStrategyConfig,
196
+ ) -> tf.Tensor:
197
+ max_start = tf.maximum(t - target_samples, 0)
198
+ hop_duration = (
199
+ config.sliding_hop_duration
200
+ if config.sliding_hop_duration is not None
201
+ else config.clip_duration
202
+ )
203
+ hop = tf.maximum(
204
+ tf.cast(
205
+ tf.round(
206
+ tf.cast(hop_duration, tf.float32) * tf.cast(sample_rate, tf.float32)
207
+ ),
208
+ tf.int32,
209
+ ),
210
+ 1,
211
+ )
212
+ starts = tf.range(0, max_start + 1, hop, dtype=tf.int32)
213
+ return tf.cond(
214
+ tf.equal(starts[-1], max_start),
215
+ lambda: starts,
216
+ lambda: tf.concat([starts, tf.reshape(max_start, [1])], axis=0),
217
+ )
218
+
219
+
220
+ def _metadata(
221
+ starts: tf.Tensor,
222
+ lengths: tf.Tensor,
223
+ sample_rate: tf.Tensor | int,
224
+ ) -> dict:
225
+ starts = tf.cast(starts, tf.int32)
226
+ lengths = tf.cast(lengths, tf.int32)
227
+ sample_rate = tf.cast(sample_rate, tf.float32)
228
+ view_index = tf.range(tf.shape(starts)[0], dtype=tf.int32)
229
+ return {
230
+ "view_start_time": tf.cast(starts, tf.float32) / sample_rate,
231
+ "view_end_time": tf.cast(starts + lengths, tf.float32) / sample_rate,
232
+ "view_index": view_index,
233
+ }
234
+
235
+
236
+ def _full_result(audio: tf.Tensor, sample_rate: tf.Tensor | int) -> dict:
237
+ starts = tf.constant([0], dtype=tf.int32)
238
+ lengths = tf.reshape(tf.shape(audio)[0], [1])
239
+ return {AUDIO: audio, METADATA: _metadata(starts, lengths, sample_rate)}
240
+
241
+
242
+ def _views_result(
243
+ audio: tf.Tensor,
244
+ sample_rate: tf.Tensor | int,
245
+ starts: tf.Tensor,
246
+ target_samples: tf.Tensor,
247
+ pad_mode: str,
248
+ pad_position: str,
249
+ seed: tf.Tensor | int | None,
250
+ ) -> dict:
251
+ starts = tf.cast(starts, tf.int32)
252
+
253
+ def _view(start: tf.Tensor) -> tf.Tensor:
254
+ return _crop_or_pad(audio, start, target_samples, pad_mode, pad_position, seed)
255
+
256
+ views = tf.map_fn(_view, starts, fn_output_signature=audio.dtype)
257
+ lengths = tf.fill(tf.shape(starts), target_samples)
258
+ return {AUDIO: views, METADATA: _metadata(starts, lengths, sample_rate)}
259
+
260
+
261
+ def _single_result(
262
+ audio: tf.Tensor,
263
+ sample_rate: tf.Tensor | int,
264
+ start: tf.Tensor,
265
+ target_samples: tf.Tensor,
266
+ pad_mode: str,
267
+ pad_position: str,
268
+ seed: tf.Tensor | int | None,
269
+ ) -> dict:
270
+ view = _crop_or_pad(audio, start, target_samples, pad_mode, pad_position, seed)
271
+ starts = tf.reshape(tf.cast(start, tf.int32), [1])
272
+ lengths = tf.reshape(target_samples, [1])
273
+ return {AUDIO: view, METADATA: _metadata(starts, lengths, sample_rate)}
274
+
275
+
276
+ @register_audio_segment_strategy("random_crop")
277
+ @register_audio_segment_strategy("center_crop")
278
+ @register_audio_segment_strategy("full")
279
+ @register_audio_segment_strategy("sliding")
280
+ @register_audio_segment_strategy("pad_or_crop")
281
+ def segment_waveform(
282
+ audio: tf.Tensor,
283
+ sample_rate: tf.Tensor | int,
284
+ config: SegmentStrategyConfig,
285
+ *,
286
+ is_training: bool,
287
+ seed: tf.Tensor | int | None,
288
+ ) -> dict:
289
+ config = SegmentStrategyConfig.from_dict(config)
290
+ audio = tf.convert_to_tensor(audio)
291
+ sample_rate = tf.cast(sample_rate, tf.int32)
292
+ target_samples = _target_samples(sample_rate, config)
293
+ t = tf.shape(audio)[0]
294
+
295
+ mode = config.train_mode if is_training else config.eval_mode
296
+ pad_mode = config.pad_mode
297
+
298
+ if config.duration_policy == "keep_1s":
299
+ return _full_result(audio, sample_rate)
300
+ if config.duration_policy == "pad_to_model_duration":
301
+ mode = "pad_or_crop"
302
+ pad_mode = "zero"
303
+ elif config.duration_policy in {
304
+ "tile_to_model_duration",
305
+ "repeat_pad_to_model_duration",
306
+ }:
307
+ mode = "pad_or_crop"
308
+ pad_mode = "repeat"
309
+ elif config.duration_policy == "sliding_windows":
310
+ mode = "sliding"
311
+
312
+ if is_training and mode == "sliding" and not config.allow_train_sliding:
313
+ raise ValueError(
314
+ "sliding segmentation is not allowed during training unless "
315
+ "allow_train_sliding=True"
316
+ )
317
+
318
+ if mode == "full":
319
+ return _full_result(audio, sample_rate)
320
+ if mode == "random_crop":
321
+ return _single_result(
322
+ audio,
323
+ sample_rate,
324
+ _random_start(t, target_samples, seed),
325
+ target_samples,
326
+ pad_mode,
327
+ config.pad_position,
328
+ seed,
329
+ )
330
+ if mode in {"center_crop", "pad_or_crop"}:
331
+ return _single_result(
332
+ audio,
333
+ sample_rate,
334
+ _center_start(t, target_samples),
335
+ target_samples,
336
+ pad_mode,
337
+ config.pad_position,
338
+ seed,
339
+ )
340
+ if mode == "multi_crop":
341
+ starts = _multi_crop_starts(t, target_samples, config.num_views)
342
+ return _views_result(
343
+ audio,
344
+ sample_rate,
345
+ starts,
346
+ target_samples,
347
+ pad_mode,
348
+ config.pad_position,
349
+ seed,
350
+ )
351
+ if mode == "sliding":
352
+ starts = _sliding_starts(t, target_samples, sample_rate, config)
353
+ return _views_result(
354
+ audio,
355
+ sample_rate,
356
+ starts,
357
+ target_samples,
358
+ pad_mode,
359
+ config.pad_position,
360
+ seed,
361
+ )
362
+
363
+ raise ValueError(f"Unknown segment mode: {mode!r}")
364
+
365
+
366
+ __all__ = [
367
+ "pad_waveform",
368
+ "segment_waveform",
369
+ ]