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,271 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Literal
|
|
3
|
+
|
|
4
|
+
import tensorflow as tf
|
|
5
|
+
|
|
6
|
+
from justdata.vision.augmentations.registry import get_crop_strategy
|
|
7
|
+
from justdata.vision.transforms import nhwc_to_nchw, normalize, resize_image
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class EvalViewConfig:
|
|
12
|
+
image_size: int
|
|
13
|
+
mode: Literal["center_crop", "resize_crop", "multi_crop"] = "center_crop"
|
|
14
|
+
resize_size: int | None = None
|
|
15
|
+
num_crops: int = 1
|
|
16
|
+
include_flip: bool = False
|
|
17
|
+
interpolation: str = "bilinear"
|
|
18
|
+
|
|
19
|
+
def __post_init__(self) -> None:
|
|
20
|
+
if self.image_size <= 0:
|
|
21
|
+
raise ValueError("image_size must be positive")
|
|
22
|
+
if self.mode not in {"center_crop", "resize_crop", "multi_crop"}:
|
|
23
|
+
raise ValueError("mode must be center_crop, resize_crop, or multi_crop")
|
|
24
|
+
if self.resize_size is not None and self.resize_size <= 0:
|
|
25
|
+
raise ValueError("resize_size must be positive when set")
|
|
26
|
+
if self.num_crops <= 0:
|
|
27
|
+
raise ValueError("num_crops must be positive")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _eval_view_config(config: EvalViewConfig | dict) -> EvalViewConfig:
|
|
31
|
+
if isinstance(config, EvalViewConfig):
|
|
32
|
+
return config
|
|
33
|
+
return EvalViewConfig(**dict(config))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _resize_for_eval_view(
|
|
37
|
+
image: tf.Tensor,
|
|
38
|
+
config: EvalViewConfig,
|
|
39
|
+
) -> tuple[tf.Tensor, tf.Tensor]:
|
|
40
|
+
image = tf.cast(image, tf.float32)
|
|
41
|
+
shape = tf.shape(image)
|
|
42
|
+
h = tf.cast(shape[0], tf.float32)
|
|
43
|
+
w = tf.cast(shape[1], tf.float32)
|
|
44
|
+
min_side = tf.minimum(h, w)
|
|
45
|
+
|
|
46
|
+
if config.mode == "resize_crop" or config.resize_size is not None:
|
|
47
|
+
resize_size = config.resize_size
|
|
48
|
+
if resize_size is None:
|
|
49
|
+
resize_size = int(config.image_size / 0.875)
|
|
50
|
+
scale = tf.cast(resize_size, tf.float32) / min_side
|
|
51
|
+
|
|
52
|
+
new_h = tf.cast(tf.round(h * scale), tf.int32)
|
|
53
|
+
new_w = tf.cast(tf.round(w * scale), tf.int32)
|
|
54
|
+
image = tf.image.resize(
|
|
55
|
+
image,
|
|
56
|
+
[new_h, new_w],
|
|
57
|
+
method=config.interpolation,
|
|
58
|
+
)
|
|
59
|
+
return image, scale
|
|
60
|
+
|
|
61
|
+
min_scale = tf.cast(config.image_size, tf.float32) / min_side
|
|
62
|
+
should_resize = min_side < tf.cast(config.image_size, tf.float32)
|
|
63
|
+
scale = tf.cond(
|
|
64
|
+
should_resize,
|
|
65
|
+
lambda: min_scale,
|
|
66
|
+
lambda: tf.constant(1.0, dtype=tf.float32),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
def _resize() -> tf.Tensor:
|
|
70
|
+
new_h = tf.cast(tf.round(h * scale), tf.int32)
|
|
71
|
+
new_w = tf.cast(tf.round(w * scale), tf.int32)
|
|
72
|
+
return tf.image.resize(
|
|
73
|
+
image,
|
|
74
|
+
[new_h, new_w],
|
|
75
|
+
method=config.interpolation,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
return tf.cond(should_resize, _resize, lambda: image), scale
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _vision_crop_boxes(image: tf.Tensor, config: EvalViewConfig) -> tf.Tensor:
|
|
82
|
+
shape = tf.shape(image)
|
|
83
|
+
crop_size = tf.cast(config.image_size, tf.int32)
|
|
84
|
+
max_y = tf.maximum(shape[0] - crop_size, 0)
|
|
85
|
+
max_x = tf.maximum(shape[1] - crop_size, 0)
|
|
86
|
+
center_y = max_y // 2
|
|
87
|
+
center_x = max_x // 2
|
|
88
|
+
|
|
89
|
+
if config.mode != "multi_crop" or config.num_crops == 1:
|
|
90
|
+
return tf.reshape(
|
|
91
|
+
tf.stack([center_y, center_x, crop_size, crop_size]),
|
|
92
|
+
[1, 4],
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
if config.num_crops == 5:
|
|
96
|
+
return tf.stack(
|
|
97
|
+
[
|
|
98
|
+
tf.stack([0, 0, crop_size, crop_size]),
|
|
99
|
+
tf.stack([0, max_x, crop_size, crop_size]),
|
|
100
|
+
tf.stack([max_y, 0, crop_size, crop_size]),
|
|
101
|
+
tf.stack([max_y, max_x, crop_size, crop_size]),
|
|
102
|
+
tf.stack([center_y, center_x, crop_size, crop_size]),
|
|
103
|
+
],
|
|
104
|
+
axis=0,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
ys = tf.cast(
|
|
108
|
+
tf.round(tf.linspace(0.0, tf.cast(max_y, tf.float32), config.num_crops)),
|
|
109
|
+
tf.int32,
|
|
110
|
+
)
|
|
111
|
+
xs = tf.cast(
|
|
112
|
+
tf.round(tf.linspace(0.0, tf.cast(max_x, tf.float32), config.num_crops)),
|
|
113
|
+
tf.int32,
|
|
114
|
+
)
|
|
115
|
+
sizes = tf.fill([config.num_crops], crop_size)
|
|
116
|
+
return tf.stack([ys, xs, sizes, sizes], axis=1)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def apply_eval_views(
|
|
120
|
+
sample: dict,
|
|
121
|
+
*,
|
|
122
|
+
config: EvalViewConfig | dict,
|
|
123
|
+
image_key: str = "image",
|
|
124
|
+
) -> dict:
|
|
125
|
+
config = _eval_view_config(config)
|
|
126
|
+
image, scale = _resize_for_eval_view(sample[image_key], config)
|
|
127
|
+
crop_boxes = _vision_crop_boxes(image, config)
|
|
128
|
+
|
|
129
|
+
def _crop(box: tf.Tensor) -> tf.Tensor:
|
|
130
|
+
return tf.image.crop_to_bounding_box(
|
|
131
|
+
image,
|
|
132
|
+
box[0],
|
|
133
|
+
box[1],
|
|
134
|
+
box[2],
|
|
135
|
+
box[3],
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
crops = tf.map_fn(_crop, crop_boxes, fn_output_signature=image.dtype)
|
|
139
|
+
flip = tf.zeros([tf.shape(crops)[0]], dtype=tf.bool)
|
|
140
|
+
|
|
141
|
+
if config.include_flip:
|
|
142
|
+
flipped = tf.image.flip_left_right(crops)
|
|
143
|
+
crops = tf.concat([crops, flipped], axis=0)
|
|
144
|
+
crop_boxes = tf.concat([crop_boxes, crop_boxes], axis=0)
|
|
145
|
+
flip = tf.concat([flip, tf.ones_like(flip)], axis=0)
|
|
146
|
+
|
|
147
|
+
num_views = tf.shape(crops)[0]
|
|
148
|
+
view_metadata = {
|
|
149
|
+
"crop_box": crop_boxes,
|
|
150
|
+
"scale": tf.fill([num_views], scale),
|
|
151
|
+
"flip": flip,
|
|
152
|
+
"view_index": tf.range(num_views, dtype=tf.int32),
|
|
153
|
+
}
|
|
154
|
+
return sample | {image_key: crops, "view_metadata": view_metadata}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def normalize_image_format(sample: dict, *, image_key: str = "image") -> dict:
|
|
158
|
+
"""Rank fix, CHW->HWC, grayscale->RGB, RGBA->RGB."""
|
|
159
|
+
image = sample[image_key]
|
|
160
|
+
|
|
161
|
+
image = tf.cond(
|
|
162
|
+
tf.equal(tf.rank(image), 2),
|
|
163
|
+
lambda: tf.expand_dims(image, -1),
|
|
164
|
+
lambda: image,
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
shape = tf.shape(image)
|
|
168
|
+
is_chw = tf.logical_and(shape[0] <= 4, shape[-1] > 4)
|
|
169
|
+
image = tf.cond(is_chw, lambda: tf.transpose(image, [1, 2, 0]), lambda: image)
|
|
170
|
+
|
|
171
|
+
shape = tf.shape(image)
|
|
172
|
+
image = tf.cond(
|
|
173
|
+
tf.equal(shape[-1], 1),
|
|
174
|
+
lambda: tf.image.grayscale_to_rgb(image),
|
|
175
|
+
lambda: image,
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
shape = tf.shape(image)
|
|
179
|
+
image = tf.cond(shape[-1] > 3, lambda: image[..., :3], lambda: image)
|
|
180
|
+
|
|
181
|
+
image.set_shape([None, None, 3])
|
|
182
|
+
return sample | {image_key: image}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _per_image_channel_standardize(image: tf.Tensor) -> tf.Tensor:
|
|
186
|
+
image = tf.cast(image, tf.float32) / 255.0
|
|
187
|
+
if image.shape.rank == 4:
|
|
188
|
+
axes = [1, 2]
|
|
189
|
+
else:
|
|
190
|
+
axes = [0, 1]
|
|
191
|
+
|
|
192
|
+
mean = tf.reduce_mean(image, axis=axes, keepdims=True)
|
|
193
|
+
variance = tf.reduce_mean(tf.square(image - mean), axis=axes, keepdims=True)
|
|
194
|
+
std = tf.sqrt(variance)
|
|
195
|
+
centered = image - mean
|
|
196
|
+
has_variance = std > 1e-6
|
|
197
|
+
safe_std = tf.where(has_variance, std, tf.ones_like(std))
|
|
198
|
+
return tf.where(has_variance, centered / safe_std, tf.zeros_like(centered))
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _normalize_image(
|
|
202
|
+
image: tf.Tensor,
|
|
203
|
+
*,
|
|
204
|
+
normalization_mode: Literal["mean_std", "per_image"],
|
|
205
|
+
normalization_params: tuple | None,
|
|
206
|
+
) -> tf.Tensor:
|
|
207
|
+
if normalization_mode == "per_image":
|
|
208
|
+
return _per_image_channel_standardize(image)
|
|
209
|
+
if normalization_mode != "mean_std":
|
|
210
|
+
raise ValueError("normalization_mode must be 'mean_std' or 'per_image'")
|
|
211
|
+
if normalization_params is None:
|
|
212
|
+
raise ValueError(
|
|
213
|
+
"`normalization_params` needs to be provided when "
|
|
214
|
+
"`normalization_mode='mean_std'`"
|
|
215
|
+
)
|
|
216
|
+
return normalize(image, *normalization_params)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def resize_and_normalize(
|
|
220
|
+
sample: dict,
|
|
221
|
+
*,
|
|
222
|
+
image_keys: list[str],
|
|
223
|
+
image_size: int,
|
|
224
|
+
resize_size: int | None = None,
|
|
225
|
+
normalize_image: bool = True,
|
|
226
|
+
normalization_mode: Literal["mean_std", "per_image"] = "mean_std",
|
|
227
|
+
normalization_params: tuple | None = None,
|
|
228
|
+
permute: bool = True,
|
|
229
|
+
) -> dict:
|
|
230
|
+
"""Resize, [0,1]->normalized, optionally HWC->CHW."""
|
|
231
|
+
res = {}
|
|
232
|
+
for key in image_keys:
|
|
233
|
+
if key not in sample:
|
|
234
|
+
continue
|
|
235
|
+
image = sample[key]
|
|
236
|
+
|
|
237
|
+
if "crops" not in key:
|
|
238
|
+
image = resize_image(
|
|
239
|
+
image,
|
|
240
|
+
image_size=image_size,
|
|
241
|
+
resize_size=resize_size,
|
|
242
|
+
)
|
|
243
|
+
if normalize_image:
|
|
244
|
+
image = _normalize_image(
|
|
245
|
+
image,
|
|
246
|
+
normalization_mode=normalization_mode,
|
|
247
|
+
normalization_params=normalization_params,
|
|
248
|
+
)
|
|
249
|
+
if permute:
|
|
250
|
+
image = nhwc_to_nchw(image)
|
|
251
|
+
|
|
252
|
+
res[key] = image
|
|
253
|
+
return sample | res
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def apply_crop_strategy(
|
|
257
|
+
sample: dict, *, image_key: str, crop_type: str, seed, **kwargs
|
|
258
|
+
) -> dict:
|
|
259
|
+
"""Dispatches to registered crop strategy."""
|
|
260
|
+
crop_fn = get_crop_strategy(crop_type)
|
|
261
|
+
cropped = crop_fn(sample[image_key], seed=seed, **kwargs)
|
|
262
|
+
return sample | {image_key: cropped}
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def co_transform(
|
|
266
|
+
sample: dict, *, image_key: str, mask_key: str, transform_fn, seed
|
|
267
|
+
) -> dict:
|
|
268
|
+
"""Applies the same spatial transform to both image and mask."""
|
|
269
|
+
image = transform_fn(sample[image_key], seed)
|
|
270
|
+
mask = transform_fn(sample[mask_key], seed)
|
|
271
|
+
return sample | {image_key: image, mask_key: mask}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
from typing import Literal
|
|
2
|
+
|
|
3
|
+
import tensorflow as tf
|
|
4
|
+
|
|
5
|
+
from justdata.vision.augmentations.composed import (
|
|
6
|
+
create_global_crops,
|
|
7
|
+
create_local_crops,
|
|
8
|
+
)
|
|
9
|
+
from justdata.vision.augmentations.mixing import (
|
|
10
|
+
mixup_cutmix,
|
|
11
|
+
random_erasing,
|
|
12
|
+
)
|
|
13
|
+
from justdata.vision.augmentations.registry import (
|
|
14
|
+
get_augment_strategy,
|
|
15
|
+
get_crop_strategy,
|
|
16
|
+
)
|
|
17
|
+
from justdata.vision.stages import (
|
|
18
|
+
apply_eval_views,
|
|
19
|
+
normalize_image_format,
|
|
20
|
+
resize_and_normalize,
|
|
21
|
+
_normalize_image,
|
|
22
|
+
)
|
|
23
|
+
from justdata.vision.transforms import nhwc_to_nchw
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _is_dense_label_vector(label: tf.Tensor, num_classes: int) -> bool:
|
|
27
|
+
if label.shape.rank != 1:
|
|
28
|
+
return False
|
|
29
|
+
return label.shape[-1] == num_classes if label.shape[-1] is not None else False
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _add_hard_label_metadata(
|
|
33
|
+
metadata: dict,
|
|
34
|
+
label: tf.Tensor,
|
|
35
|
+
class_names: tuple[str, ...] | list[str] | None,
|
|
36
|
+
) -> dict:
|
|
37
|
+
if label.shape.rank not in (0, None):
|
|
38
|
+
return metadata
|
|
39
|
+
|
|
40
|
+
hard_label = tf.cast(tf.reshape(label, []), tf.int64)
|
|
41
|
+
metadata = dict(metadata)
|
|
42
|
+
metadata["hard_label"] = hard_label
|
|
43
|
+
if class_names is not None:
|
|
44
|
+
metadata["class_name"] = tf.gather(
|
|
45
|
+
tf.constant(tuple(class_names), dtype=tf.string),
|
|
46
|
+
tf.cast(hard_label, tf.int32),
|
|
47
|
+
)
|
|
48
|
+
return metadata
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def make_preprocessing(image_key: str = "image"):
|
|
52
|
+
def preprocessing(sample):
|
|
53
|
+
return normalize_image_format(sample, image_key=image_key)
|
|
54
|
+
|
|
55
|
+
return preprocessing
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def make_augmentations(
|
|
59
|
+
image_size: int,
|
|
60
|
+
enable: bool = True,
|
|
61
|
+
local_crops: bool = True,
|
|
62
|
+
n_global_crops: int = 2,
|
|
63
|
+
n_local_crops: int = 8,
|
|
64
|
+
mode: Literal["ssl", "sl"] = "sl",
|
|
65
|
+
ra_kwargs: dict = None,
|
|
66
|
+
ta_kwargs: dict = None,
|
|
67
|
+
cj_kwargs: dict = None,
|
|
68
|
+
gc_kwargs: dict = None,
|
|
69
|
+
lc_kwargs: dict = None,
|
|
70
|
+
crop_type: str = "random_resized",
|
|
71
|
+
interpolation: str = "bilinear",
|
|
72
|
+
padding: int = 4,
|
|
73
|
+
pad_mode: str = "REFLECT",
|
|
74
|
+
augment_type: str = "rand_augment",
|
|
75
|
+
):
|
|
76
|
+
if not enable:
|
|
77
|
+
|
|
78
|
+
def no_op(sample, seed):
|
|
79
|
+
return sample
|
|
80
|
+
|
|
81
|
+
return no_op
|
|
82
|
+
|
|
83
|
+
crop_fn = get_crop_strategy(crop_type)
|
|
84
|
+
aug_fn = get_augment_strategy(augment_type)
|
|
85
|
+
|
|
86
|
+
ra_kwargs = ra_kwargs or {}
|
|
87
|
+
ta_kwargs = ta_kwargs or {}
|
|
88
|
+
cj_kwargs = cj_kwargs or {}
|
|
89
|
+
gc_kwargs = gc_kwargs or {}
|
|
90
|
+
lc_kwargs = lc_kwargs or {}
|
|
91
|
+
|
|
92
|
+
gc_kwargs.setdefault("size", image_size)
|
|
93
|
+
gc_kwargs.setdefault("scale", (0.4, 1.0))
|
|
94
|
+
lc_kwargs.setdefault("size", int(image_size * 0.425))
|
|
95
|
+
lc_kwargs.setdefault("scale", (0.05, 0.4))
|
|
96
|
+
|
|
97
|
+
# Select augmentation kwargs based on strategy
|
|
98
|
+
if augment_type == "rand_augment":
|
|
99
|
+
_aug_kwargs = ra_kwargs
|
|
100
|
+
elif augment_type == "trivial_augment":
|
|
101
|
+
_aug_kwargs = ta_kwargs
|
|
102
|
+
elif augment_type == "color_jitter":
|
|
103
|
+
_aug_kwargs = cj_kwargs
|
|
104
|
+
else:
|
|
105
|
+
_aug_kwargs = {}
|
|
106
|
+
|
|
107
|
+
def augmentations(sample, seed):
|
|
108
|
+
seeds = tf.random.split(seed, 2)
|
|
109
|
+
image = sample["image"]
|
|
110
|
+
|
|
111
|
+
res = {}
|
|
112
|
+
if "label" in sample:
|
|
113
|
+
res["label"] = sample["label"]
|
|
114
|
+
|
|
115
|
+
if mode == "ssl":
|
|
116
|
+
res["global_crops"] = create_global_crops(
|
|
117
|
+
image, crops_number=n_global_crops, seed=seeds[0], **gc_kwargs
|
|
118
|
+
)
|
|
119
|
+
if local_crops:
|
|
120
|
+
res["local_crops"] = create_local_crops(
|
|
121
|
+
image, crops_number=n_local_crops, seed=seeds[1], **lc_kwargs
|
|
122
|
+
)
|
|
123
|
+
elif mode == "sl":
|
|
124
|
+
cropped_image = crop_fn(
|
|
125
|
+
image,
|
|
126
|
+
size=image_size,
|
|
127
|
+
seed=seeds[0],
|
|
128
|
+
padding=padding,
|
|
129
|
+
pad_mode=pad_mode,
|
|
130
|
+
interpolation=interpolation,
|
|
131
|
+
)
|
|
132
|
+
aug_image = aug_fn(cropped_image, seeds[1], **_aug_kwargs)
|
|
133
|
+
res["image"] = aug_image
|
|
134
|
+
else:
|
|
135
|
+
raise ValueError(f"Unknown mode `{mode}`. Expected one of ['sl', 'ssl']")
|
|
136
|
+
|
|
137
|
+
# Pass through any other keys
|
|
138
|
+
for k, v in sample.items():
|
|
139
|
+
if k not in res and k not in ("image", "label"):
|
|
140
|
+
res[k] = v
|
|
141
|
+
|
|
142
|
+
return res
|
|
143
|
+
|
|
144
|
+
return augmentations
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def make_late_augmentations(
|
|
148
|
+
enable: bool = True,
|
|
149
|
+
mixup_alpha: float = 0.8,
|
|
150
|
+
cutmix_alpha: float = 1.0,
|
|
151
|
+
prob: float = 1.0,
|
|
152
|
+
switch_prob: float = 0.5,
|
|
153
|
+
label_smoothing: float = 0.1,
|
|
154
|
+
bce_target: bool = False,
|
|
155
|
+
label_mode: Literal["single_label", "multi_label", "event_frames"] = "single_label",
|
|
156
|
+
permute_image: bool = True,
|
|
157
|
+
mode: Literal["ssl", "sl"] = "sl",
|
|
158
|
+
random_erasing_prob: float = 0.0,
|
|
159
|
+
):
|
|
160
|
+
def late_augmentations(sample, num_classes, seed):
|
|
161
|
+
if (
|
|
162
|
+
not enable
|
|
163
|
+
or mode == "ssl"
|
|
164
|
+
or "image" not in sample
|
|
165
|
+
or "label" not in sample
|
|
166
|
+
):
|
|
167
|
+
return sample
|
|
168
|
+
|
|
169
|
+
images = sample["image"]
|
|
170
|
+
labels = sample["label"]
|
|
171
|
+
|
|
172
|
+
seeds = tf.random.split(seed, 2)
|
|
173
|
+
|
|
174
|
+
input_is_nchw = tf.logical_and(
|
|
175
|
+
tf.shape(images)[1] <= 4,
|
|
176
|
+
tf.shape(images)[-1] > 4,
|
|
177
|
+
)
|
|
178
|
+
images = tf.cond(
|
|
179
|
+
input_is_nchw, lambda: tf.transpose(images, [0, 2, 3, 1]), lambda: images
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# Random erasing is applied per-sample before batch mixing (tensor domain)
|
|
183
|
+
if random_erasing_prob > 0:
|
|
184
|
+
images = random_erasing(images, seed=seeds[0], p=random_erasing_prob)
|
|
185
|
+
|
|
186
|
+
if mixup_alpha > 0 or cutmix_alpha > 0:
|
|
187
|
+
if num_classes is None:
|
|
188
|
+
raise ValueError("`num_classes` must be provided for mixup/cutmix.")
|
|
189
|
+
images, labels = mixup_cutmix(
|
|
190
|
+
images=images,
|
|
191
|
+
labels=labels,
|
|
192
|
+
seed=seeds[1],
|
|
193
|
+
num_classes=num_classes,
|
|
194
|
+
mixup_alpha=mixup_alpha,
|
|
195
|
+
cutmix_alpha=cutmix_alpha,
|
|
196
|
+
prob=prob,
|
|
197
|
+
switch_prob=switch_prob,
|
|
198
|
+
label_smoothing=label_smoothing,
|
|
199
|
+
bce_target=bce_target,
|
|
200
|
+
label_mode=label_mode,
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
should_permute = tf.constant(permute_image)
|
|
204
|
+
should_permute = tf.logical_or(should_permute, input_is_nchw)
|
|
205
|
+
images = tf.cond(should_permute, lambda: nhwc_to_nchw(images), lambda: images)
|
|
206
|
+
|
|
207
|
+
result = {k: v for k, v in sample.items() if k not in ("image", "label")}
|
|
208
|
+
result["image"] = images
|
|
209
|
+
result["label"] = labels
|
|
210
|
+
|
|
211
|
+
return result
|
|
212
|
+
|
|
213
|
+
return late_augmentations
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def make_postprocessing(
|
|
217
|
+
image_size: int,
|
|
218
|
+
train_image_size: int | None = None,
|
|
219
|
+
is_training: bool = False,
|
|
220
|
+
normalize_image: bool = True,
|
|
221
|
+
normalization_mode: Literal["mean_std", "per_image"] = "mean_std",
|
|
222
|
+
normalization_params: tuple | None = (
|
|
223
|
+
(0.485, 0.456, 0.406),
|
|
224
|
+
(0.229, 0.224, 0.225),
|
|
225
|
+
),
|
|
226
|
+
permute_image: bool = True,
|
|
227
|
+
one_hot_labels: bool = False,
|
|
228
|
+
num_classes: int | None = None,
|
|
229
|
+
label_smoothing: float = 0.0,
|
|
230
|
+
label_mode: Literal["single_label", "multi_label"] = "single_label",
|
|
231
|
+
class_names: tuple[str, ...] | list[str] | None = None,
|
|
232
|
+
keep_hard_label_in_metadata: bool = True,
|
|
233
|
+
image_keys: list[str] = None,
|
|
234
|
+
label_keys: list[str] = None,
|
|
235
|
+
val_resize_size: int | None | Literal["auto"] = "auto",
|
|
236
|
+
eval_view_config: dict | None = None,
|
|
237
|
+
):
|
|
238
|
+
if image_keys is None:
|
|
239
|
+
image_keys = ["image", "images", "global_crops", "local_crops"]
|
|
240
|
+
if label_keys is None:
|
|
241
|
+
label_keys = ["label"]
|
|
242
|
+
|
|
243
|
+
if val_resize_size == "auto":
|
|
244
|
+
val_resize_buffer = int(image_size / 0.875)
|
|
245
|
+
else:
|
|
246
|
+
val_resize_buffer = val_resize_size
|
|
247
|
+
|
|
248
|
+
effective_image_size = (
|
|
249
|
+
train_image_size if (is_training and train_image_size) else image_size
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
def postprocessing(sample, num_classes=num_classes):
|
|
253
|
+
if eval_view_config is not None and not is_training and "image" in sample:
|
|
254
|
+
sample = apply_eval_views(
|
|
255
|
+
sample,
|
|
256
|
+
config=eval_view_config,
|
|
257
|
+
image_key="image",
|
|
258
|
+
)
|
|
259
|
+
image = sample["image"]
|
|
260
|
+
if normalize_image:
|
|
261
|
+
image = _normalize_image(
|
|
262
|
+
image,
|
|
263
|
+
normalization_mode=normalization_mode,
|
|
264
|
+
normalization_params=normalization_params,
|
|
265
|
+
)
|
|
266
|
+
if permute_image:
|
|
267
|
+
image = nhwc_to_nchw(image)
|
|
268
|
+
sample = sample | {"image": image}
|
|
269
|
+
else:
|
|
270
|
+
sample = resize_and_normalize(
|
|
271
|
+
sample,
|
|
272
|
+
image_keys=image_keys,
|
|
273
|
+
image_size=effective_image_size,
|
|
274
|
+
resize_size=val_resize_buffer if not is_training else None,
|
|
275
|
+
normalize_image=normalize_image,
|
|
276
|
+
normalization_mode=normalization_mode,
|
|
277
|
+
normalization_params=normalization_params,
|
|
278
|
+
permute=permute_image,
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
res = dict(sample)
|
|
282
|
+
for key in label_keys:
|
|
283
|
+
if key not in sample:
|
|
284
|
+
continue
|
|
285
|
+
label = sample[key]
|
|
286
|
+
original_label = label
|
|
287
|
+
|
|
288
|
+
if keep_hard_label_in_metadata and not is_training:
|
|
289
|
+
res["metadata"] = _add_hard_label_metadata(
|
|
290
|
+
res.get("metadata", {}),
|
|
291
|
+
tf.convert_to_tensor(original_label),
|
|
292
|
+
class_names,
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
if one_hot_labels:
|
|
296
|
+
if num_classes is None:
|
|
297
|
+
raise ValueError(
|
|
298
|
+
"`num_classes` must be provided when `one_hot_labels=True`"
|
|
299
|
+
)
|
|
300
|
+
label = tf.convert_to_tensor(label)
|
|
301
|
+
if label_mode == "multi_label":
|
|
302
|
+
label = tf.cast(label, tf.float32)
|
|
303
|
+
elif _is_dense_label_vector(label, num_classes):
|
|
304
|
+
label = tf.cast(label, tf.float32)
|
|
305
|
+
elif label_smoothing > 0:
|
|
306
|
+
off_value = label_smoothing / float(num_classes)
|
|
307
|
+
on_value = 1.0 - label_smoothing + off_value
|
|
308
|
+
label = tf.one_hot(
|
|
309
|
+
tf.cast(label, tf.int32),
|
|
310
|
+
num_classes,
|
|
311
|
+
on_value=on_value,
|
|
312
|
+
off_value=off_value,
|
|
313
|
+
)
|
|
314
|
+
else:
|
|
315
|
+
label = tf.one_hot(tf.cast(label, tf.int32), num_classes)
|
|
316
|
+
|
|
317
|
+
res[key] = label
|
|
318
|
+
|
|
319
|
+
return res
|
|
320
|
+
|
|
321
|
+
return postprocessing
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import tensorflow as tf
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@tf.function
|
|
5
|
+
def prepare_depth(
|
|
6
|
+
depth: tf.Tensor,
|
|
7
|
+
image_size: int,
|
|
8
|
+
method: str = "nearest",
|
|
9
|
+
normalize: bool = True,
|
|
10
|
+
min_depth: float = 0.1,
|
|
11
|
+
max_depth: float = 10.0,
|
|
12
|
+
):
|
|
13
|
+
depth = tf.cast(depth, tf.float32)
|
|
14
|
+
|
|
15
|
+
# Needed to avoid error with depth maps when resizing
|
|
16
|
+
depth = tf.expand_dims(depth, axis=-1)
|
|
17
|
+
depth = tf.image.resize(depth, [image_size, image_size], method=method)
|
|
18
|
+
depth = tf.clip_by_value(depth, min_depth, max_depth)
|
|
19
|
+
|
|
20
|
+
if normalize:
|
|
21
|
+
depth = (depth - min_depth) / (max_depth - min_depth)
|
|
22
|
+
|
|
23
|
+
depth = tf.squeeze(depth, axis=-1)
|
|
24
|
+
|
|
25
|
+
return depth
|