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,480 @@
|
|
|
1
|
+
from typing import List, Optional, Tuple, Union
|
|
2
|
+
|
|
3
|
+
import tensorflow as tf
|
|
4
|
+
|
|
5
|
+
from justdata.vision.augmentations.registry import register_augment_strategy
|
|
6
|
+
from justdata.vision.utils import (
|
|
7
|
+
_autocontrast,
|
|
8
|
+
_brightness,
|
|
9
|
+
_color,
|
|
10
|
+
_contrast,
|
|
11
|
+
_cutout,
|
|
12
|
+
_equalize,
|
|
13
|
+
_grayscale,
|
|
14
|
+
_invert,
|
|
15
|
+
_posterize,
|
|
16
|
+
_randomly_negate_tensor,
|
|
17
|
+
_rotate,
|
|
18
|
+
_rotate_with_bboxes,
|
|
19
|
+
_sharpness,
|
|
20
|
+
_shear_with_bboxes,
|
|
21
|
+
_shear_x,
|
|
22
|
+
_shear_y,
|
|
23
|
+
_solarize_add,
|
|
24
|
+
_solarize_val,
|
|
25
|
+
_translate_bbox,
|
|
26
|
+
_transform,
|
|
27
|
+
_translate,
|
|
28
|
+
_translate_x,
|
|
29
|
+
_translate_y,
|
|
30
|
+
_wrapped_rotate,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# 31-bin scale: magnitude m ∈ {0, …, 30}, B = max index = 30
|
|
34
|
+
_B = 30.0
|
|
35
|
+
|
|
36
|
+
# Ops outside the strict 14-op RA space; not in the default pool for any algorithm
|
|
37
|
+
_NON_RA_OPS = ["Invert", "Cutout", "SolarizeAdd", "Grayscale"]
|
|
38
|
+
|
|
39
|
+
RAND_AUGMENT_OPS = (
|
|
40
|
+
"Identity",
|
|
41
|
+
"AutoContrast",
|
|
42
|
+
"Equalize",
|
|
43
|
+
"Rotate",
|
|
44
|
+
"Posterize",
|
|
45
|
+
"Solarize",
|
|
46
|
+
"Color",
|
|
47
|
+
"Contrast",
|
|
48
|
+
"Brightness",
|
|
49
|
+
"Sharpness",
|
|
50
|
+
"ShearX",
|
|
51
|
+
"ShearY",
|
|
52
|
+
"TranslateX",
|
|
53
|
+
"TranslateY",
|
|
54
|
+
)
|
|
55
|
+
RAND_AUGMENT_SPATIAL_OPS = (
|
|
56
|
+
"Rotate",
|
|
57
|
+
"ShearX",
|
|
58
|
+
"ShearY",
|
|
59
|
+
"TranslateX",
|
|
60
|
+
"TranslateY",
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _identity(image):
|
|
65
|
+
return image
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _apply_segmentation_geometric_op(
|
|
69
|
+
image,
|
|
70
|
+
mask,
|
|
71
|
+
name,
|
|
72
|
+
args,
|
|
73
|
+
image_replace,
|
|
74
|
+
mask_replace,
|
|
75
|
+
):
|
|
76
|
+
def apply(value, interpolation, replace):
|
|
77
|
+
if name == "Rotate":
|
|
78
|
+
return _rotate(
|
|
79
|
+
value,
|
|
80
|
+
args[0],
|
|
81
|
+
replace=replace,
|
|
82
|
+
interpolation=interpolation,
|
|
83
|
+
)
|
|
84
|
+
if name in ("TranslateX", "TranslateY"):
|
|
85
|
+
translations = [-args[0], 0] if name == "TranslateX" else [0, -args[0]]
|
|
86
|
+
return _translate(
|
|
87
|
+
value,
|
|
88
|
+
translations,
|
|
89
|
+
replace=replace,
|
|
90
|
+
interpolation=interpolation,
|
|
91
|
+
)
|
|
92
|
+
if name in ("ShearX", "ShearY"):
|
|
93
|
+
transforms = (
|
|
94
|
+
[1.0, args[0], 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]
|
|
95
|
+
if name == "ShearX"
|
|
96
|
+
else [1.0, 0.0, 0.0, args[0], 1.0, 0.0, 0.0, 0.0]
|
|
97
|
+
)
|
|
98
|
+
return _transform(
|
|
99
|
+
value,
|
|
100
|
+
transforms=transforms,
|
|
101
|
+
interpolation=interpolation,
|
|
102
|
+
fill_mode="CONSTANT",
|
|
103
|
+
fill_value=float(replace),
|
|
104
|
+
)
|
|
105
|
+
raise ValueError(f"Unsupported segmentation geometric operation: {name}")
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
apply(image, interpolation="BILINEAR", replace=image_replace),
|
|
109
|
+
apply(mask, interpolation="NEAREST", replace=mask_replace),
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@tf.function
|
|
114
|
+
def rand_augment(
|
|
115
|
+
image: tf.Tensor,
|
|
116
|
+
seed: tf.Tensor,
|
|
117
|
+
bboxes: Optional[tf.Tensor] = None,
|
|
118
|
+
num_layers: int = 2,
|
|
119
|
+
magnitude: float = 9.0,
|
|
120
|
+
cutout_const: float = 40.0,
|
|
121
|
+
translate_const: float = 100.0,
|
|
122
|
+
magnitude_std: float = 0.0,
|
|
123
|
+
prob_to_apply: Optional[float] = None,
|
|
124
|
+
exclude_ops: Optional[List[str]] = None,
|
|
125
|
+
rotate_max: float = 30.0,
|
|
126
|
+
shear_max: float = 0.3,
|
|
127
|
+
enhance_max: float = 0.9,
|
|
128
|
+
posterize_max_bits: int = 4,
|
|
129
|
+
segmentation_mask: Optional[tf.Tensor] = None,
|
|
130
|
+
segmentation_fill_value: int = 255,
|
|
131
|
+
) -> Union[tf.Tensor, Tuple[tf.Tensor, tf.Tensor]]:
|
|
132
|
+
"""RandAugment on the 31-bin (m ∈ {0…30}, B=30) scale.
|
|
133
|
+
|
|
134
|
+
Physical mappings per the spec:
|
|
135
|
+
- Rotate: (m/B) * rotate_max * s default ±30°
|
|
136
|
+
- Translate: (m/B) * translate_const * s
|
|
137
|
+
- Shear: (m/B) * shear_max * s default ±0.3
|
|
138
|
+
- Enhance: 1 + (m/B) * enhance_max * s default MaxDelta=0.9
|
|
139
|
+
- Posterize: 8 - round(m * posterize_max_bits / B) default min 4 bits
|
|
140
|
+
- Solarize: 255 * (1 - m/B)
|
|
141
|
+
|
|
142
|
+
When ``segmentation_mask`` is provided, geometric operations reuse the
|
|
143
|
+
image's sampled parameters with bilinear image interpolation and nearest
|
|
144
|
+
mask interpolation. Vacated mask pixels use ``segmentation_fill_value``.
|
|
145
|
+
"""
|
|
146
|
+
if bboxes is not None and segmentation_mask is not None:
|
|
147
|
+
raise ValueError("RandAugment cannot transform bboxes and a mask together")
|
|
148
|
+
|
|
149
|
+
input_image_type = image.dtype
|
|
150
|
+
if input_image_type != tf.uint8:
|
|
151
|
+
image = tf.clip_by_value(image, 0.0, 255.0)
|
|
152
|
+
image = tf.cast(image, dtype=tf.uint8)
|
|
153
|
+
|
|
154
|
+
replace_value = 128
|
|
155
|
+
|
|
156
|
+
def _rotate_level_to_arg(level, seed):
|
|
157
|
+
level = (level / _B) * rotate_max
|
|
158
|
+
level = _randomly_negate_tensor(level, seed)
|
|
159
|
+
return (level,)
|
|
160
|
+
|
|
161
|
+
def _enhance_level_to_arg(level, seed):
|
|
162
|
+
# v' = 1.0 + (m/B) * MaxDelta * s, s ∈ {-1, +1}
|
|
163
|
+
delta = (level / _B) * enhance_max
|
|
164
|
+
delta = _randomly_negate_tensor(delta, seed)
|
|
165
|
+
return (1.0 + delta,)
|
|
166
|
+
|
|
167
|
+
def _shear_level_to_arg(level, seed):
|
|
168
|
+
level = (level / _B) * shear_max
|
|
169
|
+
level = _randomly_negate_tensor(level, seed)
|
|
170
|
+
return (level,)
|
|
171
|
+
|
|
172
|
+
def _translate_level_to_arg(level, t_const, seed):
|
|
173
|
+
level = (level / _B) * t_const
|
|
174
|
+
level = _randomly_negate_tensor(level, seed)
|
|
175
|
+
return (level,)
|
|
176
|
+
|
|
177
|
+
def _mult_to_arg(level, multiplier=1.0):
|
|
178
|
+
return (tf.cast((level / _B) * multiplier, tf.int32),)
|
|
179
|
+
|
|
180
|
+
def level_to_arg(name, level, seed):
|
|
181
|
+
if name in ["Identity", "AutoContrast", "Equalize", "Invert", "Grayscale"]:
|
|
182
|
+
return ()
|
|
183
|
+
if name == "Posterize":
|
|
184
|
+
# 8 - round(m / (B / MaxBits)) = 8 - round(m * MaxBits / B)
|
|
185
|
+
num_bits = 8 - tf.cast(
|
|
186
|
+
tf.round((level / _B) * float(posterize_max_bits)), tf.int32
|
|
187
|
+
)
|
|
188
|
+
return (num_bits,)
|
|
189
|
+
if name == "Solarize":
|
|
190
|
+
# Inverts pixels >= threshold; threshold decreases with magnitude
|
|
191
|
+
threshold = tf.cast(255.0 * (1.0 - level / _B), tf.int32)
|
|
192
|
+
return (threshold,)
|
|
193
|
+
if name == "SolarizeAdd":
|
|
194
|
+
return _mult_to_arg(level, 110)
|
|
195
|
+
if name == "Cutout":
|
|
196
|
+
return _mult_to_arg(level, cutout_const)
|
|
197
|
+
if name in ["Color", "Contrast", "Brightness", "Sharpness"]:
|
|
198
|
+
return _enhance_level_to_arg(level, seed)
|
|
199
|
+
if name in ["ShearX", "ShearY", "ShearX_BBox", "ShearY_BBox"]:
|
|
200
|
+
return _shear_level_to_arg(level, seed)
|
|
201
|
+
if name in [
|
|
202
|
+
"TranslateX",
|
|
203
|
+
"TranslateY",
|
|
204
|
+
"TranslateX_BBox",
|
|
205
|
+
"TranslateY_BBox",
|
|
206
|
+
]:
|
|
207
|
+
return _translate_level_to_arg(level, translate_const, seed)
|
|
208
|
+
if name in ["Rotate", "Rotate_BBox"]:
|
|
209
|
+
return _rotate_level_to_arg(level, seed)
|
|
210
|
+
return ()
|
|
211
|
+
|
|
212
|
+
def wrap_func(func, name):
|
|
213
|
+
needs_bbox = "BBox" in name
|
|
214
|
+
needs_seed = name == "Cutout"
|
|
215
|
+
is_replace = name in [
|
|
216
|
+
"Rotate",
|
|
217
|
+
"TranslateX",
|
|
218
|
+
"ShearX",
|
|
219
|
+
"ShearY",
|
|
220
|
+
"TranslateY",
|
|
221
|
+
"Cutout",
|
|
222
|
+
"Rotate_BBox",
|
|
223
|
+
"ShearX_BBox",
|
|
224
|
+
"ShearY_BBox",
|
|
225
|
+
"TranslateX_BBox",
|
|
226
|
+
"TranslateY_BBox",
|
|
227
|
+
]
|
|
228
|
+
|
|
229
|
+
def wrapped(img, boxes, args, seed):
|
|
230
|
+
if segmentation_mask is not None and name in RAND_AUGMENT_SPATIAL_OPS:
|
|
231
|
+
return _apply_segmentation_geometric_op(
|
|
232
|
+
img,
|
|
233
|
+
boxes,
|
|
234
|
+
name,
|
|
235
|
+
args,
|
|
236
|
+
replace_value,
|
|
237
|
+
segmentation_fill_value,
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
call_args = [img]
|
|
241
|
+
if needs_bbox:
|
|
242
|
+
call_args.append(boxes)
|
|
243
|
+
call_args.extend(list(args))
|
|
244
|
+
if is_replace:
|
|
245
|
+
call_args.append(replace_value)
|
|
246
|
+
if needs_seed:
|
|
247
|
+
call_args.append(seed)
|
|
248
|
+
|
|
249
|
+
res = func(*call_args)
|
|
250
|
+
if needs_bbox:
|
|
251
|
+
return res
|
|
252
|
+
return res, boxes
|
|
253
|
+
|
|
254
|
+
return wrapped
|
|
255
|
+
|
|
256
|
+
NAME_TO_FUNC = {
|
|
257
|
+
"Identity": _identity,
|
|
258
|
+
"AutoContrast": _autocontrast,
|
|
259
|
+
"Equalize": _equalize,
|
|
260
|
+
"Invert": _invert,
|
|
261
|
+
"Rotate": _wrapped_rotate,
|
|
262
|
+
"Posterize": _posterize,
|
|
263
|
+
"Solarize": _solarize_val,
|
|
264
|
+
"SolarizeAdd": _solarize_add,
|
|
265
|
+
"Color": _color,
|
|
266
|
+
"Contrast": _contrast,
|
|
267
|
+
"Brightness": _brightness,
|
|
268
|
+
"Sharpness": _sharpness,
|
|
269
|
+
"ShearX": _shear_x,
|
|
270
|
+
"ShearY": _shear_y,
|
|
271
|
+
"TranslateX": _translate_x,
|
|
272
|
+
"TranslateY": _translate_y,
|
|
273
|
+
"Cutout": _cutout,
|
|
274
|
+
"Grayscale": _grayscale,
|
|
275
|
+
"Rotate_BBox": _rotate_with_bboxes,
|
|
276
|
+
"ShearX_BBox": lambda i, b, lvl, r: _shear_with_bboxes(
|
|
277
|
+
i, b, lvl, r, shear_horizontal=True
|
|
278
|
+
),
|
|
279
|
+
"ShearY_BBox": lambda i, b, lvl, r: _shear_with_bboxes(
|
|
280
|
+
i, b, lvl, r, shear_horizontal=False
|
|
281
|
+
),
|
|
282
|
+
"TranslateX_BBox": lambda i, b, p, r: _translate_bbox(
|
|
283
|
+
i, b, p, r, shift_horizontal=True
|
|
284
|
+
),
|
|
285
|
+
"TranslateY_BBox": lambda i, b, p, r: _translate_bbox(
|
|
286
|
+
i, b, p, r, shift_horizontal=False
|
|
287
|
+
),
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
# Strict 14-op RA space: 3 magnitude-independent + 11 magnitude-dependent
|
|
291
|
+
available_ops = list(RAND_AUGMENT_OPS)
|
|
292
|
+
|
|
293
|
+
if bboxes is not None:
|
|
294
|
+
box_aware_ops = {
|
|
295
|
+
"Rotate": "Rotate_BBox",
|
|
296
|
+
"ShearX": "ShearX_BBox",
|
|
297
|
+
"ShearY": "ShearY_BBox",
|
|
298
|
+
"TranslateX": "TranslateX_BBox",
|
|
299
|
+
"TranslateY": "TranslateY_BBox",
|
|
300
|
+
}
|
|
301
|
+
available_ops = [box_aware_ops.get(op, op) for op in available_ops]
|
|
302
|
+
|
|
303
|
+
if exclude_ops:
|
|
304
|
+
available_ops = [op for op in available_ops if op not in exclude_ops]
|
|
305
|
+
if not available_ops:
|
|
306
|
+
raise ValueError("RandAugment requires at least one non-excluded operation")
|
|
307
|
+
|
|
308
|
+
aug_image = image
|
|
309
|
+
aug_bboxes = segmentation_mask if segmentation_mask is not None else bboxes
|
|
310
|
+
seed_layers = tf.random.split(seed, num_layers)
|
|
311
|
+
|
|
312
|
+
for i in range(num_layers):
|
|
313
|
+
layer_seed = seed_layers[i]
|
|
314
|
+
seeds = tf.random.split(layer_seed, 4)
|
|
315
|
+
|
|
316
|
+
op_idx = tf.random.stateless_uniform(
|
|
317
|
+
[], maxval=len(available_ops), dtype=tf.int32, seed=seeds[0]
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
should_apply = tf.constant(True)
|
|
321
|
+
if prob_to_apply is not None:
|
|
322
|
+
should_apply = (
|
|
323
|
+
tf.random.stateless_uniform([], seed=seeds[1]) < prob_to_apply
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
level = magnitude
|
|
327
|
+
if magnitude_std > 0:
|
|
328
|
+
level += tf.random.stateless_normal([], seed=seeds[2]) * magnitude_std
|
|
329
|
+
level = tf.clip_by_value(level, 0.0, _B)
|
|
330
|
+
|
|
331
|
+
branch_fns = []
|
|
332
|
+
op_seeds = tf.random.split(seeds[3], len(available_ops))
|
|
333
|
+
for j, op_name in enumerate(available_ops):
|
|
334
|
+
func = NAME_TO_FUNC[op_name]
|
|
335
|
+
wrapped_fn = wrap_func(func, op_name)
|
|
336
|
+
|
|
337
|
+
def create_branch_fn(f, seed_val, name, im=aug_image, bx=aug_bboxes):
|
|
338
|
+
def branch_fn():
|
|
339
|
+
ar = level_to_arg(name, level, seed_val)
|
|
340
|
+
return f(im, bx, ar, seed_val)
|
|
341
|
+
|
|
342
|
+
return branch_fn
|
|
343
|
+
|
|
344
|
+
branch_fns.append((j, create_branch_fn(wrapped_fn, op_seeds[j], op_name)))
|
|
345
|
+
|
|
346
|
+
def apply_op():
|
|
347
|
+
return tf.switch_case(
|
|
348
|
+
branch_index=op_idx,
|
|
349
|
+
branch_fns=branch_fns,
|
|
350
|
+
default=lambda: (aug_image, aug_bboxes),
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
aug_image, aug_bboxes = tf.cond(
|
|
354
|
+
should_apply, apply_op, lambda: (aug_image, aug_bboxes)
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
aug_image = tf.cast(aug_image, dtype=input_image_type)
|
|
358
|
+
|
|
359
|
+
if bboxes is None and segmentation_mask is None:
|
|
360
|
+
return aug_image
|
|
361
|
+
return aug_image, aug_bboxes
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
@tf.function
|
|
365
|
+
def trivial_augment(
|
|
366
|
+
image: tf.Tensor,
|
|
367
|
+
seed: tf.Tensor,
|
|
368
|
+
bboxes: Optional[tf.Tensor] = None,
|
|
369
|
+
translate_const: Optional[float] = None,
|
|
370
|
+
exclude_ops: Optional[List[str]] = None,
|
|
371
|
+
segmentation_mask: Optional[tf.Tensor] = None,
|
|
372
|
+
segmentation_fill_value: int = 255,
|
|
373
|
+
) -> Union[tf.Tensor, Tuple[tf.Tensor, tf.Tensor]]:
|
|
374
|
+
"""TrivialAugment: one op from the strict 14-op RA space, m ~ U{0,…,30}.
|
|
375
|
+
|
|
376
|
+
Standard bounds. translate_const defaults to (150/331)*image_width if not
|
|
377
|
+
provided, matching the proportional RA-space definition. Segmentation
|
|
378
|
+
masks follow the paired geometric behavior documented by ``rand_augment``.
|
|
379
|
+
"""
|
|
380
|
+
seeds = tf.random.split(seed, 2)
|
|
381
|
+
# Discrete uniform magnitude: m ~ U{0, 1, …, 30}
|
|
382
|
+
magnitude = tf.cast(
|
|
383
|
+
tf.random.stateless_uniform(
|
|
384
|
+
[], minval=0, maxval=31, dtype=tf.int32, seed=seeds[0]
|
|
385
|
+
),
|
|
386
|
+
tf.float32,
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
# Standard translate bound: proportional to image width
|
|
390
|
+
if translate_const is None:
|
|
391
|
+
translate_const = tf.cast(tf.shape(image)[1], tf.float32) * (150.0 / 331.0)
|
|
392
|
+
|
|
393
|
+
# Strict 14-op RA space: exclude ops not in the pool
|
|
394
|
+
ta_exclude = list(_NON_RA_OPS)
|
|
395
|
+
if exclude_ops:
|
|
396
|
+
ta_exclude = ta_exclude + [op for op in exclude_ops if op not in ta_exclude]
|
|
397
|
+
|
|
398
|
+
return rand_augment(
|
|
399
|
+
image=image,
|
|
400
|
+
seed=seeds[1],
|
|
401
|
+
bboxes=bboxes,
|
|
402
|
+
num_layers=1,
|
|
403
|
+
magnitude=magnitude,
|
|
404
|
+
translate_const=translate_const,
|
|
405
|
+
exclude_ops=ta_exclude,
|
|
406
|
+
segmentation_mask=segmentation_mask,
|
|
407
|
+
segmentation_fill_value=segmentation_fill_value,
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
@tf.function
|
|
412
|
+
def trivial_augment_wide(
|
|
413
|
+
image: tf.Tensor,
|
|
414
|
+
seed: tf.Tensor,
|
|
415
|
+
bboxes: Optional[tf.Tensor] = None,
|
|
416
|
+
exclude_ops: Optional[List[str]] = None,
|
|
417
|
+
segmentation_mask: Optional[tf.Tensor] = None,
|
|
418
|
+
segmentation_fill_value: int = 255,
|
|
419
|
+
) -> Union[tf.Tensor, Tuple[tf.Tensor, tf.Tensor]]:
|
|
420
|
+
"""TrivialAugmentWide: one op from the strict 14-op RA space, m ~ U{0,…,30}.
|
|
421
|
+
|
|
422
|
+
Wide bounds per the spec:
|
|
423
|
+
- Rotate: ±135° (vs standard ±30°)
|
|
424
|
+
- Translate: ±32 px fixed (vs proportional; note: standard may exceed wide
|
|
425
|
+
for images wider than ~71 px)
|
|
426
|
+
- Shear: ±0.99 (vs standard ±0.3)
|
|
427
|
+
- Enhance: MaxDelta=0.99 (vs standard 0.9)
|
|
428
|
+
- Posterize: min 2 bits (vs standard min 4 bits)
|
|
429
|
+
|
|
430
|
+
Segmentation masks follow the paired geometric behavior documented by
|
|
431
|
+
``rand_augment``.
|
|
432
|
+
"""
|
|
433
|
+
seeds = tf.random.split(seed, 2)
|
|
434
|
+
# Discrete uniform magnitude: m ~ U{0, 1, …, 30}
|
|
435
|
+
magnitude = tf.cast(
|
|
436
|
+
tf.random.stateless_uniform(
|
|
437
|
+
[], minval=0, maxval=31, dtype=tf.int32, seed=seeds[0]
|
|
438
|
+
),
|
|
439
|
+
tf.float32,
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
ta_exclude = list(_NON_RA_OPS)
|
|
443
|
+
if exclude_ops:
|
|
444
|
+
ta_exclude = ta_exclude + [op for op in exclude_ops if op not in ta_exclude]
|
|
445
|
+
|
|
446
|
+
return rand_augment(
|
|
447
|
+
image=image,
|
|
448
|
+
seed=seeds[1],
|
|
449
|
+
bboxes=bboxes,
|
|
450
|
+
num_layers=1,
|
|
451
|
+
magnitude=magnitude,
|
|
452
|
+
translate_const=32.0, # Wide: fixed 32 px (not image-proportional)
|
|
453
|
+
rotate_max=135.0, # Wide: ±135°
|
|
454
|
+
shear_max=0.99, # Wide: ±0.99
|
|
455
|
+
enhance_max=0.99, # Wide: MaxDelta=0.99
|
|
456
|
+
posterize_max_bits=6, # Wide: min 2 bits kept (8 − 6 = 2)
|
|
457
|
+
exclude_ops=ta_exclude,
|
|
458
|
+
segmentation_mask=segmentation_mask,
|
|
459
|
+
segmentation_fill_value=segmentation_fill_value,
|
|
460
|
+
)
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
@register_augment_strategy("rand_augment")
|
|
464
|
+
def _aug_rand_augment(image, seed, **kwargs):
|
|
465
|
+
return rand_augment(image, seed, **kwargs)
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
@register_augment_strategy("trivial_augment")
|
|
469
|
+
def _aug_trivial_augment(image, seed, **kwargs):
|
|
470
|
+
return trivial_augment(image, seed, **kwargs)
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
@register_augment_strategy("trivial_augment_wide")
|
|
474
|
+
def _aug_trivial_augment_wide(image, seed, **kwargs):
|
|
475
|
+
return trivial_augment_wide(image, seed, **kwargs)
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
@register_augment_strategy("none")
|
|
479
|
+
def _aug_none(image, seed, **kwargs):
|
|
480
|
+
return image
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
import tensorflow as tf
|
|
4
|
+
|
|
5
|
+
from justdata.vision.augmentations.registry import register_augment_strategy
|
|
6
|
+
from justdata.vision.utils import _brightness, _color, _contrast, gaussian_filter2d
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@tf.function
|
|
10
|
+
def color_jitter(
|
|
11
|
+
image: tf.Tensor,
|
|
12
|
+
seed,
|
|
13
|
+
brightness: Optional[float] = 0.0,
|
|
14
|
+
contrast: Optional[float] = 0.0,
|
|
15
|
+
saturation: Optional[float] = 0.0,
|
|
16
|
+
hue: Optional[float] = 0.0,
|
|
17
|
+
p: Optional[float] = 1.0,
|
|
18
|
+
p_grayscale: Optional[float] = 0.0,
|
|
19
|
+
) -> tf.Tensor:
|
|
20
|
+
"""Applies color jitter and random grayscale to an image.
|
|
21
|
+
|
|
22
|
+
ColorJitter is applied with probability ``p``, then RandomGrayscale is
|
|
23
|
+
applied independently with probability ``p_grayscale``. The two
|
|
24
|
+
operations are *not* mutually exclusive.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
image (tf.Tensor): Of shape [height, width, 3] in the image domain
|
|
28
|
+
([0, 255]), either uint8 or float from resize operations.
|
|
29
|
+
seed: Random seed tensor.
|
|
30
|
+
brightness (float, optional): Factor magnitude for brightness jitter.
|
|
31
|
+
Defaults to 0.
|
|
32
|
+
contrast (float, optional): Factor magnitude for contrast jitter. Defaults
|
|
33
|
+
to 0.
|
|
34
|
+
saturation (float, optional): Factor magnitude for saturation jitter.
|
|
35
|
+
Defaults to 0.
|
|
36
|
+
hue (float, optional): Hue delta magnitude. Defaults to 0.
|
|
37
|
+
p (float, optional): Probability of applying color jitter. Defaults to 1.0.
|
|
38
|
+
p_grayscale (float, optional): Probability to convert the image to
|
|
39
|
+
grayscale. Defaults to 0.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
tf.Tensor: The augmented ``image`` with the input dtype preserved.
|
|
43
|
+
"""
|
|
44
|
+
seeds = tf.random.split(seed, 6)
|
|
45
|
+
input_image_type = image.dtype
|
|
46
|
+
|
|
47
|
+
def magnitude(value):
|
|
48
|
+
if value is None:
|
|
49
|
+
return tf.constant(0.0, dtype=tf.float32)
|
|
50
|
+
return tf.cast(value, dtype=tf.float32)
|
|
51
|
+
|
|
52
|
+
def image_to_uint8(img):
|
|
53
|
+
if img.dtype == tf.uint8:
|
|
54
|
+
return img
|
|
55
|
+
img = tf.clip_by_value(img, 0.0, 255.0)
|
|
56
|
+
return tf.cast(img, dtype=tf.uint8)
|
|
57
|
+
|
|
58
|
+
def restore_dtype(img):
|
|
59
|
+
return tf.cast(img, dtype=input_image_type)
|
|
60
|
+
|
|
61
|
+
def sample_factor(amount, factor_seed):
|
|
62
|
+
amount = magnitude(amount)
|
|
63
|
+
|
|
64
|
+
def jitter_factor():
|
|
65
|
+
lower = tf.maximum(tf.constant(0.0, dtype=tf.float32), 1.0 - amount)
|
|
66
|
+
upper = 1.0 + amount
|
|
67
|
+
return tf.random.stateless_uniform(
|
|
68
|
+
[], minval=lower, maxval=upper, seed=factor_seed
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
return tf.cond(amount > 0.0, jitter_factor, lambda: tf.constant(1.0))
|
|
72
|
+
|
|
73
|
+
def apply_hue(img, amount, hue_seed):
|
|
74
|
+
amount = magnitude(amount)
|
|
75
|
+
|
|
76
|
+
def jitter_hue():
|
|
77
|
+
delta = tf.random.stateless_uniform(
|
|
78
|
+
[], minval=-amount, maxval=amount, seed=hue_seed
|
|
79
|
+
)
|
|
80
|
+
img_f = tf.cast(img, tf.float32) / 255.0
|
|
81
|
+
hsv = tf.image.rgb_to_hsv(img_f)
|
|
82
|
+
hue_channel = tf.math.floormod(hsv[..., 0] + delta, 1.0)
|
|
83
|
+
hsv = tf.concat([hue_channel[..., tf.newaxis], hsv[..., 1:]], axis=-1)
|
|
84
|
+
rgb = tf.image.hsv_to_rgb(hsv)
|
|
85
|
+
rgb = tf.clip_by_value(rgb * 255.0, 0.0, 255.0)
|
|
86
|
+
return tf.cast(rgb, dtype=tf.uint8)
|
|
87
|
+
|
|
88
|
+
return tf.cond(amount > 0.0, jitter_hue, lambda: img)
|
|
89
|
+
|
|
90
|
+
def apply_color_jitter(img):
|
|
91
|
+
should_transform = tf.logical_or(
|
|
92
|
+
tf.logical_or(magnitude(brightness) > 0.0, magnitude(contrast) > 0.0),
|
|
93
|
+
tf.logical_or(magnitude(saturation) > 0.0, magnitude(hue) > 0.0),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
def transform():
|
|
97
|
+
jittered = image_to_uint8(img)
|
|
98
|
+
jittered = _brightness(jittered, sample_factor(brightness, seeds[0]))
|
|
99
|
+
jittered = _contrast(jittered, sample_factor(contrast, seeds[1]))
|
|
100
|
+
jittered = _color(jittered, sample_factor(saturation, seeds[2]))
|
|
101
|
+
jittered = apply_hue(jittered, hue, seeds[3])
|
|
102
|
+
return restore_dtype(jittered)
|
|
103
|
+
|
|
104
|
+
return tf.cond(should_transform, transform, lambda: img)
|
|
105
|
+
|
|
106
|
+
def apply_grayscale(img):
|
|
107
|
+
gray = tf.image.rgb_to_grayscale(img)
|
|
108
|
+
return tf.image.grayscale_to_rgb(gray)
|
|
109
|
+
|
|
110
|
+
# Apply color jitter with probability p
|
|
111
|
+
should_jitter = tf.random.stateless_uniform([], seed=seeds[4]) < p
|
|
112
|
+
result = tf.cond(should_jitter, lambda: apply_color_jitter(image), lambda: image)
|
|
113
|
+
|
|
114
|
+
# Apply grayscale independently with probability p_grayscale
|
|
115
|
+
should_grayscale = tf.random.stateless_uniform([], seed=seeds[5]) < p_grayscale
|
|
116
|
+
result = tf.cond(should_grayscale, lambda: apply_grayscale(result), lambda: result)
|
|
117
|
+
|
|
118
|
+
return result
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@tf.function
|
|
122
|
+
def gaussian_blur(
|
|
123
|
+
image: tf.Tensor,
|
|
124
|
+
seed,
|
|
125
|
+
p: float = 0.5,
|
|
126
|
+
) -> tf.Tensor:
|
|
127
|
+
seeds = tf.random.split(seed, 2)
|
|
128
|
+
|
|
129
|
+
random_value = tf.random.stateless_uniform([], seed=seeds[0])
|
|
130
|
+
sigma = tf.random.stateless_uniform([], minval=0.1, maxval=2.0, seed=seeds[1])
|
|
131
|
+
|
|
132
|
+
should_blur = tf.less(random_value, p)
|
|
133
|
+
|
|
134
|
+
def do_blur():
|
|
135
|
+
return gaussian_filter2d(image, filter_shape=9, sigma=sigma)
|
|
136
|
+
|
|
137
|
+
return tf.cond(should_blur, do_blur, lambda: image)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@tf.function
|
|
141
|
+
def solarize(
|
|
142
|
+
image: tf.Tensor,
|
|
143
|
+
seed,
|
|
144
|
+
p: float = 0.2,
|
|
145
|
+
normalized_image: bool = False, # i.e., pixels \in [0,1]
|
|
146
|
+
) -> tf.Tensor:
|
|
147
|
+
random_value = tf.random.stateless_uniform([], seed=seed)
|
|
148
|
+
|
|
149
|
+
should_solarize = tf.less(random_value, p)
|
|
150
|
+
|
|
151
|
+
threshold = 0.5 if normalized_image else 127.5
|
|
152
|
+
max_value = 1.0 if normalized_image else 255.0
|
|
153
|
+
|
|
154
|
+
max_value_t = tf.cast(max_value, image.dtype)
|
|
155
|
+
threshold_t = tf.cast(threshold, image.dtype)
|
|
156
|
+
solarized = tf.where(image < threshold_t, image, max_value_t - image)
|
|
157
|
+
|
|
158
|
+
return tf.cond(should_solarize, lambda: solarized, lambda: image)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
@register_augment_strategy("color_jitter")
|
|
162
|
+
def _aug_color_jitter(image, seed, **kwargs):
|
|
163
|
+
return color_jitter(image, seed, **kwargs)
|