justdata 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- justdata/__init__.py +3 -0
- justdata/acoustic/__init__.py +381 -0
- justdata/acoustic/adapters.py +329 -0
- justdata/acoustic/augment/__init__.py +119 -0
- justdata/acoustic/augment/batch.py +813 -0
- justdata/acoustic/augment/configs.py +204 -0
- justdata/acoustic/augment/patchout.py +331 -0
- justdata/acoustic/augment/spectrogram.py +806 -0
- justdata/acoustic/augment/waveform.py +879 -0
- justdata/acoustic/batching.py +27 -0
- justdata/acoustic/channel.py +53 -0
- justdata/acoustic/compat/__init__.py +95 -0
- justdata/acoustic/compat/ast.py +626 -0
- justdata/acoustic/compat/ced.py +152 -0
- justdata/acoustic/compat/efficientat.py +243 -0
- justdata/acoustic/compat/panns.py +115 -0
- justdata/acoustic/compat/passt.py +191 -0
- justdata/acoustic/configs.py +519 -0
- justdata/acoustic/corruptions/__init__.py +33 -0
- justdata/acoustic/corruptions/codec.py +92 -0
- justdata/acoustic/corruptions/datasets.py +155 -0
- justdata/acoustic/corruptions/device.py +39 -0
- justdata/acoustic/corruptions/dynamics.py +55 -0
- justdata/acoustic/corruptions/filters.py +105 -0
- justdata/acoustic/corruptions/noise.py +173 -0
- justdata/acoustic/corruptions/registry.py +230 -0
- justdata/acoustic/corruptions/reverb.py +49 -0
- justdata/acoustic/corruptions/timefreq.py +127 -0
- justdata/acoustic/datasets.py +52 -0
- justdata/acoustic/dcase2025.py +814 -0
- justdata/acoustic/decoding.py +73 -0
- justdata/acoustic/eval_views.py +54 -0
- justdata/acoustic/evals.py +71 -0
- justdata/acoustic/filters.py +18 -0
- justdata/acoustic/frontends/__init__.py +37 -0
- justdata/acoustic/frontends/ast.py +116 -0
- justdata/acoustic/frontends/kaldi.py +34 -0
- justdata/acoustic/frontends/log.py +94 -0
- justdata/acoustic/frontends/mel.py +173 -0
- justdata/acoustic/frontends/mfcc.py +26 -0
- justdata/acoustic/frontends/pcen.py +26 -0
- justdata/acoustic/frontends/stft.py +174 -0
- justdata/acoustic/jax.py +23 -0
- justdata/acoustic/labels.py +169 -0
- justdata/acoustic/layouts.py +86 -0
- justdata/acoustic/metadata.py +152 -0
- justdata/acoustic/normalization.py +196 -0
- justdata/acoustic/pipelines.py +112 -0
- justdata/acoustic/postprocessing.py +328 -0
- justdata/acoustic/preprocessing.py +175 -0
- justdata/acoustic/presets.py +257 -0
- justdata/acoustic/registry.py +381 -0
- justdata/acoustic/resampling.py +218 -0
- justdata/acoustic/schema.py +51 -0
- justdata/acoustic/segment.py +369 -0
- justdata/acoustic/sources.py +503 -0
- justdata/acoustic/stages.py +59 -0
- justdata/acoustic/stats.py +50 -0
- justdata/acoustic/tasks.py +375 -0
- justdata/audio/__init__.py +20 -0
- justdata/core/__init__.py +74 -0
- justdata/core/adapters.py +46 -0
- justdata/core/augmentations.py +34 -0
- justdata/core/filters.py +94 -0
- justdata/core/finalization.py +173 -0
- justdata/core/label_mixing.py +129 -0
- justdata/core/loader.py +384 -0
- justdata/core/metadata.py +250 -0
- justdata/core/presets.py +155 -0
- justdata/core/registry.py +229 -0
- justdata/core/sources.py +87 -0
- justdata/core/stats.py +227 -0
- justdata/corruptions.py +52 -0
- justdata/py.typed +0 -0
- justdata/vision/__init__.py +17 -0
- justdata/vision/augmentations/__init__.py +31 -0
- justdata/vision/augmentations/auto.py +480 -0
- justdata/vision/augmentations/color.py +163 -0
- justdata/vision/augmentations/composed.py +156 -0
- justdata/vision/augmentations/geometric.py +302 -0
- justdata/vision/augmentations/mixing.py +232 -0
- justdata/vision/augmentations/registry.py +142 -0
- justdata/vision/corruptions/__init__.py +16 -0
- justdata/vision/corruptions/blur.py +65 -0
- justdata/vision/corruptions/digital.py +26 -0
- justdata/vision/corruptions/noise.py +20 -0
- justdata/vision/corruptions/registry.py +113 -0
- justdata/vision/corruptions/weather.py +100 -0
- justdata/vision/datasets.py +34 -0
- justdata/vision/encodings/__init__.py +1 -0
- justdata/vision/encodings/heatmaps.py +196 -0
- justdata/vision/minic.py +69 -0
- justdata/vision/pipelines.py +62 -0
- justdata/vision/presets.py +509 -0
- justdata/vision/sources.py +1080 -0
- justdata/vision/stages.py +271 -0
- justdata/vision/tasks/__init__.py +1 -0
- justdata/vision/tasks/classification.py +321 -0
- justdata/vision/tasks/depth.py +25 -0
- justdata/vision/tasks/segmentation.py +194 -0
- justdata/vision/transforms.py +113 -0
- justdata/vision/utils.py +741 -0
- justdata-1.0.0.dist-info/METADATA +860 -0
- justdata-1.0.0.dist-info/RECORD +106 -0
- justdata-1.0.0.dist-info/WHEEL +4 -0
- justdata-1.0.0.dist-info/licenses/LICENSE +23 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Literal
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _ensure_probability(name: str, value: float) -> None:
|
|
8
|
+
if not 0.0 <= value <= 1.0:
|
|
9
|
+
raise ValueError(f"{name} must be in [0, 1]; got {value!r}")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _ensure_order(
|
|
13
|
+
name_min: str, min_value: float, name_max: str, max_value: float
|
|
14
|
+
) -> None:
|
|
15
|
+
if min_value > max_value:
|
|
16
|
+
raise ValueError(f"{name_min} must be <= {name_max}")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class RandomGainConfig:
|
|
21
|
+
prob: float = 0.5
|
|
22
|
+
min_db: float = -12.0
|
|
23
|
+
max_db: float = 12.0
|
|
24
|
+
|
|
25
|
+
def __post_init__(self) -> None:
|
|
26
|
+
_ensure_probability("prob", self.prob)
|
|
27
|
+
_ensure_order("min_db", self.min_db, "max_db", self.max_db)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class RandomTimeShiftConfig:
|
|
32
|
+
prob: float = 0.5
|
|
33
|
+
max_shift_seconds: float = 0.5
|
|
34
|
+
mode: Literal["roll", "zero"] = "roll"
|
|
35
|
+
|
|
36
|
+
def __post_init__(self) -> None:
|
|
37
|
+
_ensure_probability("prob", self.prob)
|
|
38
|
+
if self.max_shift_seconds < 0:
|
|
39
|
+
raise ValueError("max_shift_seconds must be non-negative")
|
|
40
|
+
if self.mode not in {"roll", "zero"}:
|
|
41
|
+
raise ValueError("mode must be one of 'roll' or 'zero'")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class AdditiveNoiseConfig:
|
|
46
|
+
prob: float = 0.5
|
|
47
|
+
snr_db_min: float = 5.0
|
|
48
|
+
snr_db_max: float = 30.0
|
|
49
|
+
noise_kind: Literal["white", "pink", "brown"] = "white"
|
|
50
|
+
|
|
51
|
+
def __post_init__(self) -> None:
|
|
52
|
+
_ensure_probability("prob", self.prob)
|
|
53
|
+
_ensure_order("snr_db_min", self.snr_db_min, "snr_db_max", self.snr_db_max)
|
|
54
|
+
if self.noise_kind not in {"white", "pink", "brown"}:
|
|
55
|
+
raise ValueError("noise_kind must be one of 'white', 'pink', or 'brown'")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True)
|
|
59
|
+
class RandomClippingConfig:
|
|
60
|
+
prob: float = 0.2
|
|
61
|
+
min_threshold: float = 0.2
|
|
62
|
+
max_threshold: float = 1.0
|
|
63
|
+
|
|
64
|
+
def __post_init__(self) -> None:
|
|
65
|
+
_ensure_probability("prob", self.prob)
|
|
66
|
+
if self.min_threshold <= 0 or self.max_threshold <= 0:
|
|
67
|
+
raise ValueError("clipping thresholds must be positive")
|
|
68
|
+
_ensure_order(
|
|
69
|
+
"min_threshold",
|
|
70
|
+
self.min_threshold,
|
|
71
|
+
"max_threshold",
|
|
72
|
+
self.max_threshold,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(frozen=True)
|
|
77
|
+
class DynamicRangeCompressionConfig:
|
|
78
|
+
prob: float = 0.3
|
|
79
|
+
threshold_db_min: float = -30.0
|
|
80
|
+
threshold_db_max: float = -6.0
|
|
81
|
+
ratio_min: float = 2.0
|
|
82
|
+
ratio_max: float = 8.0
|
|
83
|
+
|
|
84
|
+
def __post_init__(self) -> None:
|
|
85
|
+
_ensure_probability("prob", self.prob)
|
|
86
|
+
_ensure_order(
|
|
87
|
+
"threshold_db_min",
|
|
88
|
+
self.threshold_db_min,
|
|
89
|
+
"threshold_db_max",
|
|
90
|
+
self.threshold_db_max,
|
|
91
|
+
)
|
|
92
|
+
if self.ratio_min < 1.0 or self.ratio_max < 1.0:
|
|
93
|
+
raise ValueError("compression ratios must be >= 1")
|
|
94
|
+
_ensure_order("ratio_min", self.ratio_min, "ratio_max", self.ratio_max)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@dataclass(frozen=True)
|
|
98
|
+
class RIRConvolutionConfig:
|
|
99
|
+
prob: float = 0.3
|
|
100
|
+
rir_dataset: str | None = None
|
|
101
|
+
normalize_ir: bool = True
|
|
102
|
+
compensate_delay: bool = True
|
|
103
|
+
|
|
104
|
+
def __post_init__(self) -> None:
|
|
105
|
+
_ensure_probability("prob", self.prob)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@dataclass(frozen=True)
|
|
109
|
+
class SpeedPerturbConfig:
|
|
110
|
+
prob: float = 0.3
|
|
111
|
+
rates: tuple[float, ...] = (0.9, 1.0, 1.1)
|
|
112
|
+
|
|
113
|
+
def __post_init__(self) -> None:
|
|
114
|
+
_ensure_probability("prob", self.prob)
|
|
115
|
+
object.__setattr__(self, "rates", tuple(self.rates))
|
|
116
|
+
if not self.rates:
|
|
117
|
+
raise ValueError("rates must be non-empty")
|
|
118
|
+
if any(rate <= 0 for rate in self.rates):
|
|
119
|
+
raise ValueError("all speed perturb rates must be positive")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@dataclass(frozen=True)
|
|
123
|
+
class CodecSimulationConfig:
|
|
124
|
+
prob: float = 0.2
|
|
125
|
+
codec: Literal["mulaw", "alaw", "mp3_proxy"] = "mulaw"
|
|
126
|
+
bitrate: int | None = None
|
|
127
|
+
|
|
128
|
+
def __post_init__(self) -> None:
|
|
129
|
+
_ensure_probability("prob", self.prob)
|
|
130
|
+
if self.codec not in {"mulaw", "alaw", "mp3_proxy"}:
|
|
131
|
+
raise ValueError("codec must be one of 'mulaw', 'alaw', or 'mp3_proxy'")
|
|
132
|
+
if self.bitrate is not None and self.bitrate <= 0:
|
|
133
|
+
raise ValueError("bitrate must be positive when set")
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@dataclass(frozen=True)
|
|
137
|
+
class AudioMixupConfig:
|
|
138
|
+
alpha: float
|
|
139
|
+
prob: float
|
|
140
|
+
label_mode: Literal["single_label", "multi_label", "event_frames"]
|
|
141
|
+
|
|
142
|
+
def __post_init__(self) -> None:
|
|
143
|
+
if self.alpha <= 0:
|
|
144
|
+
raise ValueError("alpha must be positive")
|
|
145
|
+
_ensure_probability("prob", self.prob)
|
|
146
|
+
if self.label_mode not in {"single_label", "multi_label", "event_frames"}:
|
|
147
|
+
raise ValueError(
|
|
148
|
+
"label_mode must be one of 'single_label', 'multi_label', or "
|
|
149
|
+
"'event_frames'"
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@dataclass(frozen=True)
|
|
154
|
+
class AudioCutMixSpecConfig:
|
|
155
|
+
alpha: float
|
|
156
|
+
prob: float
|
|
157
|
+
axes: Literal["time", "frequency", "time_frequency"]
|
|
158
|
+
|
|
159
|
+
def __post_init__(self) -> None:
|
|
160
|
+
if self.alpha <= 0:
|
|
161
|
+
raise ValueError("alpha must be positive")
|
|
162
|
+
_ensure_probability("prob", self.prob)
|
|
163
|
+
if self.axes not in {"time", "frequency", "time_frequency"}:
|
|
164
|
+
raise ValueError(
|
|
165
|
+
"axes must be one of 'time', 'frequency', or 'time_frequency'"
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@dataclass(frozen=True)
|
|
170
|
+
class AudioWavMixConfig:
|
|
171
|
+
alpha: float
|
|
172
|
+
prob: float
|
|
173
|
+
|
|
174
|
+
def __post_init__(self) -> None:
|
|
175
|
+
if self.alpha <= 0:
|
|
176
|
+
raise ValueError("alpha must be positive")
|
|
177
|
+
_ensure_probability("prob", self.prob)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@dataclass(frozen=True)
|
|
181
|
+
class AudioBatchMixStyleConfig:
|
|
182
|
+
prob: float
|
|
183
|
+
mix: Literal["global", "frequency", "channel"]
|
|
184
|
+
|
|
185
|
+
def __post_init__(self) -> None:
|
|
186
|
+
_ensure_probability("prob", self.prob)
|
|
187
|
+
if self.mix not in {"global", "frequency", "channel"}:
|
|
188
|
+
raise ValueError("mix must be one of 'global', 'frequency', or 'channel'")
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
__all__ = [
|
|
192
|
+
"AdditiveNoiseConfig",
|
|
193
|
+
"AudioBatchMixStyleConfig",
|
|
194
|
+
"AudioCutMixSpecConfig",
|
|
195
|
+
"AudioMixupConfig",
|
|
196
|
+
"AudioWavMixConfig",
|
|
197
|
+
"CodecSimulationConfig",
|
|
198
|
+
"DynamicRangeCompressionConfig",
|
|
199
|
+
"RIRConvolutionConfig",
|
|
200
|
+
"RandomClippingConfig",
|
|
201
|
+
"RandomGainConfig",
|
|
202
|
+
"RandomTimeShiftConfig",
|
|
203
|
+
"SpeedPerturbConfig",
|
|
204
|
+
]
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
import tensorflow as tf
|
|
7
|
+
|
|
8
|
+
from justdata.acoustic.augment.spectrogram import (
|
|
9
|
+
SpectrogramLayout,
|
|
10
|
+
_as_tfc,
|
|
11
|
+
_merge_config,
|
|
12
|
+
_restore_from_tfc,
|
|
13
|
+
_seed_tensor,
|
|
14
|
+
_stateless_shuffled_range,
|
|
15
|
+
)
|
|
16
|
+
from justdata.acoustic.registry import register_audio_spectrogram_augment
|
|
17
|
+
from justdata.acoustic.schema import FEATURES, METADATA
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _non_negative_count(name: str, value: int | tf.Tensor | None) -> int | tf.Tensor:
|
|
21
|
+
if value is None:
|
|
22
|
+
return 0
|
|
23
|
+
if isinstance(value, int) and value < 0:
|
|
24
|
+
raise ValueError(f"{name} must be non-negative")
|
|
25
|
+
return value
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _is_zero_count(value: int | tf.Tensor) -> bool:
|
|
29
|
+
if isinstance(value, int):
|
|
30
|
+
return value == 0
|
|
31
|
+
static_value = tf.get_static_value(value)
|
|
32
|
+
return static_value is not None and int(static_value) == 0
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _drop_axis(
|
|
36
|
+
x: tf.Tensor,
|
|
37
|
+
*,
|
|
38
|
+
axis: int,
|
|
39
|
+
count: int | tf.Tensor,
|
|
40
|
+
seed: tf.Tensor,
|
|
41
|
+
) -> tuple[tf.Tensor, tf.Tensor]:
|
|
42
|
+
count = tf.cast(count, tf.int32)
|
|
43
|
+
size = tf.shape(x)[axis]
|
|
44
|
+
count = tf.minimum(tf.maximum(count, 0), size)
|
|
45
|
+
dropped = tf.sort(_stateless_shuffled_range(size, seed)[:count])
|
|
46
|
+
positions = tf.range(size)
|
|
47
|
+
keep_mask = ~tf.reduce_any(
|
|
48
|
+
tf.equal(positions[:, tf.newaxis], dropped[tf.newaxis, :]),
|
|
49
|
+
axis=1,
|
|
50
|
+
)
|
|
51
|
+
keep = tf.reshape(tf.where(keep_mask), [-1])
|
|
52
|
+
return tf.gather(x, keep, axis=axis), dropped
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _structured_patchout_frequency_with_indices(
|
|
56
|
+
x: tf.Tensor,
|
|
57
|
+
n_freq_patches: int | tf.Tensor,
|
|
58
|
+
seed: tf.Tensor | int | None,
|
|
59
|
+
*,
|
|
60
|
+
layout: SpectrogramLayout | None = None,
|
|
61
|
+
) -> tuple[tf.Tensor, tf.Tensor]:
|
|
62
|
+
seed = _seed_tensor(seed)
|
|
63
|
+
grid, resolved_layout = _as_tfc(x, layout)
|
|
64
|
+
dropped_grid, dropped = _drop_axis(
|
|
65
|
+
grid,
|
|
66
|
+
axis=1,
|
|
67
|
+
count=n_freq_patches,
|
|
68
|
+
seed=seed,
|
|
69
|
+
)
|
|
70
|
+
return _restore_from_tfc(dropped_grid, resolved_layout), dropped
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _structured_patchout_time_with_indices(
|
|
74
|
+
x: tf.Tensor,
|
|
75
|
+
n_time_patches: int | tf.Tensor,
|
|
76
|
+
seed: tf.Tensor | int | None,
|
|
77
|
+
*,
|
|
78
|
+
layout: SpectrogramLayout | None = None,
|
|
79
|
+
) -> tuple[tf.Tensor, tf.Tensor]:
|
|
80
|
+
seed = _seed_tensor(seed)
|
|
81
|
+
grid, resolved_layout = _as_tfc(x, layout)
|
|
82
|
+
dropped_grid, dropped = _drop_axis(
|
|
83
|
+
grid,
|
|
84
|
+
axis=0,
|
|
85
|
+
count=n_time_patches,
|
|
86
|
+
seed=seed,
|
|
87
|
+
)
|
|
88
|
+
return _restore_from_tfc(dropped_grid, resolved_layout), dropped
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _unstructured_patchout_with_indices(
|
|
92
|
+
x: tf.Tensor,
|
|
93
|
+
n_patches: int | tf.Tensor,
|
|
94
|
+
seed: tf.Tensor | int | None,
|
|
95
|
+
*,
|
|
96
|
+
layout: SpectrogramLayout | None = None,
|
|
97
|
+
) -> tuple[tf.Tensor, tf.Tensor]:
|
|
98
|
+
seed = _seed_tensor(seed)
|
|
99
|
+
grid, _ = _as_tfc(x, layout)
|
|
100
|
+
time = tf.shape(grid)[0]
|
|
101
|
+
freq = tf.shape(grid)[1]
|
|
102
|
+
channels = tf.shape(grid)[2]
|
|
103
|
+
flat = tf.reshape(grid, [time * freq, channels])
|
|
104
|
+
dropped_flat, dropped = _drop_axis(flat, axis=0, count=n_patches, seed=seed)
|
|
105
|
+
|
|
106
|
+
if tf.convert_to_tensor(x).shape.rank == 2:
|
|
107
|
+
return tf.squeeze(dropped_flat, axis=-1), dropped
|
|
108
|
+
return dropped_flat, dropped
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def structured_patchout_frequency(
|
|
112
|
+
x: tf.Tensor,
|
|
113
|
+
n_freq_patches: int | tf.Tensor,
|
|
114
|
+
seed: tf.Tensor | int | None = None,
|
|
115
|
+
*,
|
|
116
|
+
layout: SpectrogramLayout | None = None,
|
|
117
|
+
is_training: bool = True,
|
|
118
|
+
augment_eval: bool = False,
|
|
119
|
+
) -> tf.Tensor:
|
|
120
|
+
if not (is_training or augment_eval):
|
|
121
|
+
return tf.convert_to_tensor(x)
|
|
122
|
+
n_freq_patches = _non_negative_count("n_freq_patches", n_freq_patches)
|
|
123
|
+
result, _ = _structured_patchout_frequency_with_indices(
|
|
124
|
+
x,
|
|
125
|
+
n_freq_patches,
|
|
126
|
+
seed,
|
|
127
|
+
layout=layout,
|
|
128
|
+
)
|
|
129
|
+
return result
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def structured_patchout_time(
|
|
133
|
+
x: tf.Tensor,
|
|
134
|
+
n_time_patches: int | tf.Tensor,
|
|
135
|
+
seed: tf.Tensor | int | None = None,
|
|
136
|
+
*,
|
|
137
|
+
layout: SpectrogramLayout | None = None,
|
|
138
|
+
is_training: bool = True,
|
|
139
|
+
augment_eval: bool = False,
|
|
140
|
+
) -> tf.Tensor:
|
|
141
|
+
if not (is_training or augment_eval):
|
|
142
|
+
return tf.convert_to_tensor(x)
|
|
143
|
+
n_time_patches = _non_negative_count("n_time_patches", n_time_patches)
|
|
144
|
+
result, _ = _structured_patchout_time_with_indices(
|
|
145
|
+
x,
|
|
146
|
+
n_time_patches,
|
|
147
|
+
seed,
|
|
148
|
+
layout=layout,
|
|
149
|
+
)
|
|
150
|
+
return result
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def unstructured_patchout(
|
|
154
|
+
x: tf.Tensor,
|
|
155
|
+
n_patches: int | tf.Tensor,
|
|
156
|
+
seed: tf.Tensor | int | None = None,
|
|
157
|
+
*,
|
|
158
|
+
layout: SpectrogramLayout | None = None,
|
|
159
|
+
is_training: bool = True,
|
|
160
|
+
augment_eval: bool = False,
|
|
161
|
+
) -> tf.Tensor:
|
|
162
|
+
if not (is_training or augment_eval):
|
|
163
|
+
return tf.convert_to_tensor(x)
|
|
164
|
+
n_patches = _non_negative_count("n_patches", n_patches)
|
|
165
|
+
if _is_zero_count(n_patches):
|
|
166
|
+
return tf.convert_to_tensor(x)
|
|
167
|
+
result, _ = _unstructured_patchout_with_indices(x, n_patches, seed, layout=layout)
|
|
168
|
+
return result
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _resolve_patchout_counts(
|
|
172
|
+
config: Mapping[str, Any] | None,
|
|
173
|
+
overrides: Mapping[str, Any],
|
|
174
|
+
) -> dict[str, Any]:
|
|
175
|
+
cfg = _merge_config(config, overrides)
|
|
176
|
+
return {
|
|
177
|
+
"n_freq_patches": _non_negative_count(
|
|
178
|
+
"n_freq_patches",
|
|
179
|
+
cfg.get("n_freq_patches", cfg.get("structured_frequency", 0)),
|
|
180
|
+
),
|
|
181
|
+
"n_time_patches": _non_negative_count(
|
|
182
|
+
"n_time_patches",
|
|
183
|
+
cfg.get("n_time_patches", cfg.get("structured_time", 0)),
|
|
184
|
+
),
|
|
185
|
+
"n_patches": _non_negative_count(
|
|
186
|
+
"n_patches",
|
|
187
|
+
cfg.get("n_patches", cfg.get("unstructured", 0)),
|
|
188
|
+
),
|
|
189
|
+
"layout": cfg.get("layout"),
|
|
190
|
+
"debug": bool(cfg.get("debug", False)),
|
|
191
|
+
"feature_key": cfg.get("feature_key", FEATURES),
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _patchout_tensor(
|
|
196
|
+
x: tf.Tensor,
|
|
197
|
+
*,
|
|
198
|
+
seed: tf.Tensor | int | None,
|
|
199
|
+
n_freq_patches: int | tf.Tensor,
|
|
200
|
+
n_time_patches: int | tf.Tensor,
|
|
201
|
+
n_patches: int | tf.Tensor,
|
|
202
|
+
layout: SpectrogramLayout | None,
|
|
203
|
+
) -> tuple[tf.Tensor, dict[str, tf.Tensor]]:
|
|
204
|
+
seed = _seed_tensor(seed)
|
|
205
|
+
freq_seed, time_seed, unstructured_seed = tf.unstack(tf.random.split(seed, 3))
|
|
206
|
+
|
|
207
|
+
result, dropped_freq = _structured_patchout_frequency_with_indices(
|
|
208
|
+
x,
|
|
209
|
+
n_freq_patches,
|
|
210
|
+
freq_seed,
|
|
211
|
+
layout=layout,
|
|
212
|
+
)
|
|
213
|
+
result, dropped_time = _structured_patchout_time_with_indices(
|
|
214
|
+
result,
|
|
215
|
+
n_time_patches,
|
|
216
|
+
time_seed,
|
|
217
|
+
layout=layout,
|
|
218
|
+
)
|
|
219
|
+
dropped_unstructured = tf.zeros([0], dtype=tf.int32)
|
|
220
|
+
if not _is_zero_count(n_patches):
|
|
221
|
+
result, dropped_unstructured = _unstructured_patchout_with_indices(
|
|
222
|
+
result,
|
|
223
|
+
n_patches,
|
|
224
|
+
unstructured_seed,
|
|
225
|
+
layout=layout,
|
|
226
|
+
)
|
|
227
|
+
return result, {
|
|
228
|
+
"structured_frequency": dropped_freq,
|
|
229
|
+
"structured_time": dropped_time,
|
|
230
|
+
"unstructured": dropped_unstructured,
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _patchout_sample(
|
|
235
|
+
sample: Mapping[str, Any],
|
|
236
|
+
*,
|
|
237
|
+
seed: tf.Tensor | int | None,
|
|
238
|
+
n_freq_patches: int | tf.Tensor,
|
|
239
|
+
n_time_patches: int | tf.Tensor,
|
|
240
|
+
n_patches: int | tf.Tensor,
|
|
241
|
+
layout: SpectrogramLayout | None,
|
|
242
|
+
feature_key: str,
|
|
243
|
+
debug: bool,
|
|
244
|
+
) -> dict:
|
|
245
|
+
if feature_key not in sample:
|
|
246
|
+
return dict(sample)
|
|
247
|
+
|
|
248
|
+
result = dict(sample)
|
|
249
|
+
patched, dropped = _patchout_tensor(
|
|
250
|
+
result[feature_key],
|
|
251
|
+
seed=seed,
|
|
252
|
+
n_freq_patches=n_freq_patches,
|
|
253
|
+
n_time_patches=n_time_patches,
|
|
254
|
+
n_patches=n_patches,
|
|
255
|
+
layout=layout,
|
|
256
|
+
)
|
|
257
|
+
result[feature_key] = patched
|
|
258
|
+
|
|
259
|
+
if debug:
|
|
260
|
+
metadata = dict(result.get(METADATA, {}))
|
|
261
|
+
metadata["patchout"] = dropped
|
|
262
|
+
result[METADATA] = metadata
|
|
263
|
+
|
|
264
|
+
return result
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
@register_audio_spectrogram_augment("passt_patchout")
|
|
268
|
+
def passt_patchout(
|
|
269
|
+
x: tf.Tensor | Mapping[str, Any],
|
|
270
|
+
*,
|
|
271
|
+
seed: tf.Tensor | int | None = None,
|
|
272
|
+
config: Mapping[str, Any] | None = None,
|
|
273
|
+
n_freq_patches: int | tf.Tensor | None = None,
|
|
274
|
+
n_time_patches: int | tf.Tensor | None = None,
|
|
275
|
+
n_patches: int | tf.Tensor | None = None,
|
|
276
|
+
structured_frequency: int | tf.Tensor | None = None,
|
|
277
|
+
structured_time: int | tf.Tensor | None = None,
|
|
278
|
+
unstructured: int | tf.Tensor | None = None,
|
|
279
|
+
layout: SpectrogramLayout | None = None,
|
|
280
|
+
feature_key: str = FEATURES,
|
|
281
|
+
debug: bool | None = None,
|
|
282
|
+
is_training: bool = True,
|
|
283
|
+
augment_eval: bool = False,
|
|
284
|
+
) -> tf.Tensor | dict:
|
|
285
|
+
if not (is_training or augment_eval):
|
|
286
|
+
return dict(x) if isinstance(x, Mapping) else tf.convert_to_tensor(x)
|
|
287
|
+
|
|
288
|
+
cfg = _resolve_patchout_counts(
|
|
289
|
+
config,
|
|
290
|
+
{
|
|
291
|
+
"n_freq_patches": n_freq_patches,
|
|
292
|
+
"n_time_patches": n_time_patches,
|
|
293
|
+
"n_patches": n_patches,
|
|
294
|
+
"structured_frequency": structured_frequency,
|
|
295
|
+
"structured_time": structured_time,
|
|
296
|
+
"unstructured": unstructured,
|
|
297
|
+
"layout": layout,
|
|
298
|
+
"feature_key": feature_key,
|
|
299
|
+
"debug": debug,
|
|
300
|
+
},
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
if isinstance(x, Mapping):
|
|
304
|
+
return _patchout_sample(
|
|
305
|
+
x,
|
|
306
|
+
seed=seed,
|
|
307
|
+
n_freq_patches=cfg["n_freq_patches"],
|
|
308
|
+
n_time_patches=cfg["n_time_patches"],
|
|
309
|
+
n_patches=cfg["n_patches"],
|
|
310
|
+
layout=cfg["layout"],
|
|
311
|
+
feature_key=cfg["feature_key"],
|
|
312
|
+
debug=cfg["debug"],
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
patched, _ = _patchout_tensor(
|
|
316
|
+
x,
|
|
317
|
+
seed=seed,
|
|
318
|
+
n_freq_patches=cfg["n_freq_patches"],
|
|
319
|
+
n_time_patches=cfg["n_time_patches"],
|
|
320
|
+
n_patches=cfg["n_patches"],
|
|
321
|
+
layout=cfg["layout"],
|
|
322
|
+
)
|
|
323
|
+
return patched
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
__all__ = [
|
|
327
|
+
"passt_patchout",
|
|
328
|
+
"structured_patchout_frequency",
|
|
329
|
+
"structured_patchout_time",
|
|
330
|
+
"unstructured_patchout",
|
|
331
|
+
]
|