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
justdata/vision/utils.py
ADDED
|
@@ -0,0 +1,741 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from typing import List, Optional, Tuple, Union
|
|
3
|
+
|
|
4
|
+
import tensorflow as tf
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _pad(
|
|
8
|
+
image: tf.Tensor,
|
|
9
|
+
filter_shape: Union[List[int], Tuple[int, ...]],
|
|
10
|
+
pad_mode: str = "CONSTANT",
|
|
11
|
+
constant_values: Union[int, tf.Tensor] = 0,
|
|
12
|
+
) -> tf.Tensor:
|
|
13
|
+
"""Explicitly pads a 4-D image.
|
|
14
|
+
|
|
15
|
+
Equivalent to the implicit padding method offered in `tf.nn.conv2d` and
|
|
16
|
+
`tf.nn.depthwise_conv2d`, but supports non-zero, reflect and symmetric
|
|
17
|
+
padding mode. For the even-sized filter, it pads one more value to the
|
|
18
|
+
right or the bottom side.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
image: A 4-D `Tensor` of shape `[batch_size, height, width, channels]`.
|
|
22
|
+
filter_shape: A `tuple`/`list` of 2 integers, specifying the height and
|
|
23
|
+
width of the 2-D filter.
|
|
24
|
+
pad_mode: A `string`, one of "REFLECT", "CONSTANT", or "SYMMETRIC". The type of
|
|
25
|
+
padding algorithm to use, which is compatible with `pad_mode` argument in
|
|
26
|
+
`tf.pad`. For more details, please refer to
|
|
27
|
+
https://www.tensorflow.org/api_docs/python/tf/pad.
|
|
28
|
+
constant_values: A `scalar`, the pad value to use in "CONSTANT" padding
|
|
29
|
+
mode.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
A padded image.
|
|
33
|
+
"""
|
|
34
|
+
if pad_mode.upper() not in {"REFLECT", "CONSTANT", "SYMMETRIC"}:
|
|
35
|
+
raise ValueError(
|
|
36
|
+
'padding should be one of "REFLECT", "CONSTANT", or "SYMMETRIC".'
|
|
37
|
+
)
|
|
38
|
+
constant_values = tf.convert_to_tensor(constant_values, image.dtype)
|
|
39
|
+
filter_height, filter_width = filter_shape
|
|
40
|
+
pad_top = (filter_height - 1) // 2
|
|
41
|
+
pad_bottom = filter_height - 1 - pad_top
|
|
42
|
+
pad_left = (filter_width - 1) // 2
|
|
43
|
+
pad_right = filter_width - 1 - pad_left
|
|
44
|
+
paddings = [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]
|
|
45
|
+
return tf.pad(image, paddings, mode=pad_mode, constant_values=constant_values)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _get_gaussian_kernel(sigma: tf.Tensor, filter_size: tf.Tensor) -> tf.Tensor:
|
|
49
|
+
"""Computes 1D Gaussian kernel."""
|
|
50
|
+
x = tf.range(
|
|
51
|
+
tf.cast(-filter_size // 2 + 1, tf.float32),
|
|
52
|
+
tf.cast(filter_size // 2 + 1, tf.float32),
|
|
53
|
+
)
|
|
54
|
+
x = tf.cast(x**2, sigma.dtype)
|
|
55
|
+
x = tf.nn.softmax(-x / (2.0 * tf.cast(sigma**2, x.dtype)))
|
|
56
|
+
return x
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _get_gaussian_kernel_2d(
|
|
60
|
+
gaussian_filter_x: tf.Tensor, gaussian_filter_y: tf.Tensor
|
|
61
|
+
) -> tf.Tensor:
|
|
62
|
+
"""Computes 2D Gaussian kernel given 1D kernels."""
|
|
63
|
+
return tf.matmul(gaussian_filter_y[:, tf.newaxis], gaussian_filter_x[tf.newaxis, :])
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _convert_translation_to_transform(translations: tf.Tensor) -> tf.Tensor:
|
|
67
|
+
translations = tf.convert_to_tensor(translations, dtype=tf.float32)
|
|
68
|
+
if translations.shape.rank == 1:
|
|
69
|
+
translations = translations[None]
|
|
70
|
+
|
|
71
|
+
num_translations = tf.shape(translations)[0]
|
|
72
|
+
zeros = tf.zeros((num_translations,), dtype=tf.float32)
|
|
73
|
+
ones = tf.ones((num_translations,), dtype=tf.float32)
|
|
74
|
+
|
|
75
|
+
# The Matrix is [1, 0, -tx, 0, 1, -ty, 0, 0]
|
|
76
|
+
return tf.stack(
|
|
77
|
+
[
|
|
78
|
+
ones,
|
|
79
|
+
zeros,
|
|
80
|
+
-translations[:, 0],
|
|
81
|
+
zeros,
|
|
82
|
+
ones,
|
|
83
|
+
-translations[:, 1],
|
|
84
|
+
zeros,
|
|
85
|
+
zeros,
|
|
86
|
+
],
|
|
87
|
+
axis=1,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _convert_angles_to_transform(
|
|
92
|
+
angles: tf.Tensor, image_width: tf.Tensor, image_height: tf.Tensor
|
|
93
|
+
) -> tf.Tensor:
|
|
94
|
+
angles = tf.convert_to_tensor(angles, dtype=tf.float32)
|
|
95
|
+
x_offset = (
|
|
96
|
+
(image_width - 1)
|
|
97
|
+
- (
|
|
98
|
+
tf.math.cos(angles) * (image_width - 1)
|
|
99
|
+
- tf.math.sin(angles) * (image_height - 1)
|
|
100
|
+
)
|
|
101
|
+
) / 2.0
|
|
102
|
+
y_offset = (
|
|
103
|
+
(image_height - 1)
|
|
104
|
+
- (
|
|
105
|
+
tf.math.sin(angles) * (image_width - 1)
|
|
106
|
+
+ tf.math.cos(angles) * (image_height - 1)
|
|
107
|
+
)
|
|
108
|
+
) / 2.0
|
|
109
|
+
num_angles = tf.shape(angles)[0]
|
|
110
|
+
return tf.concat(
|
|
111
|
+
values=[
|
|
112
|
+
tf.math.cos(angles)[:, None],
|
|
113
|
+
-tf.math.sin(angles)[:, None],
|
|
114
|
+
x_offset[:, None],
|
|
115
|
+
tf.math.sin(angles)[:, None],
|
|
116
|
+
tf.math.cos(angles)[:, None],
|
|
117
|
+
y_offset[:, None],
|
|
118
|
+
tf.zeros((num_angles, 2), tf.float32),
|
|
119
|
+
],
|
|
120
|
+
axis=1,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _apply_transform(
|
|
125
|
+
image: tf.Tensor,
|
|
126
|
+
transforms: tf.Tensor,
|
|
127
|
+
fill_mode: str = "REFLECT",
|
|
128
|
+
fill_value: float = 0.0,
|
|
129
|
+
interpolation: str = "NEAREST",
|
|
130
|
+
) -> tf.Tensor:
|
|
131
|
+
output_shape = tf.shape(image)[1:3]
|
|
132
|
+
return tf.raw_ops.ImageProjectiveTransformV3(
|
|
133
|
+
images=image,
|
|
134
|
+
output_shape=output_shape,
|
|
135
|
+
fill_value=fill_value,
|
|
136
|
+
transforms=transforms,
|
|
137
|
+
fill_mode=fill_mode.upper(),
|
|
138
|
+
interpolation=interpolation.upper(),
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _transform(
|
|
143
|
+
image: tf.Tensor,
|
|
144
|
+
transforms: tf.Tensor,
|
|
145
|
+
interpolation: str = "NEAREST",
|
|
146
|
+
fill_mode: str = "REFLECT",
|
|
147
|
+
fill_value: float = 0.0,
|
|
148
|
+
) -> tf.Tensor:
|
|
149
|
+
original_ndims = tf.rank(image)
|
|
150
|
+
transforms = tf.convert_to_tensor(transforms, dtype=tf.float32)
|
|
151
|
+
if transforms.shape.rank == 1:
|
|
152
|
+
transforms = transforms[None]
|
|
153
|
+
# Reuse existing to_4d
|
|
154
|
+
image = to_4d(image)
|
|
155
|
+
image = _apply_transform(
|
|
156
|
+
image,
|
|
157
|
+
transforms,
|
|
158
|
+
fill_mode=fill_mode,
|
|
159
|
+
fill_value=fill_value,
|
|
160
|
+
interpolation=interpolation,
|
|
161
|
+
)
|
|
162
|
+
# Reuse existing from_4d
|
|
163
|
+
return from_4d(image, original_ndims)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _translate(
|
|
167
|
+
image: tf.Tensor,
|
|
168
|
+
translations: tf.Tensor,
|
|
169
|
+
replace: int = 0,
|
|
170
|
+
interpolation: str = "NEAREST",
|
|
171
|
+
) -> tf.Tensor:
|
|
172
|
+
transforms = _convert_translation_to_transform(translations)
|
|
173
|
+
return _transform(
|
|
174
|
+
image,
|
|
175
|
+
transforms=transforms,
|
|
176
|
+
interpolation=interpolation,
|
|
177
|
+
fill_value=float(replace),
|
|
178
|
+
fill_mode="CONSTANT",
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _rotate(
|
|
183
|
+
image: tf.Tensor,
|
|
184
|
+
degrees: float,
|
|
185
|
+
replace: int = 0,
|
|
186
|
+
interpolation: str = "NEAREST",
|
|
187
|
+
) -> tf.Tensor:
|
|
188
|
+
degrees_to_radians = math.pi / 180.0
|
|
189
|
+
radians = tf.convert_to_tensor(degrees * degrees_to_radians, dtype=tf.float32)
|
|
190
|
+
if radians.shape.rank == 0:
|
|
191
|
+
radians = radians[None]
|
|
192
|
+
|
|
193
|
+
original_ndims = tf.rank(image)
|
|
194
|
+
image = to_4d(image)
|
|
195
|
+
image_height = tf.cast(tf.shape(image)[1], tf.float32)
|
|
196
|
+
image_width = tf.cast(tf.shape(image)[2], tf.float32)
|
|
197
|
+
|
|
198
|
+
transforms = _convert_angles_to_transform(radians, image_width, image_height)
|
|
199
|
+
image = _transform(
|
|
200
|
+
image,
|
|
201
|
+
transforms=transforms,
|
|
202
|
+
interpolation=interpolation,
|
|
203
|
+
fill_mode="CONSTANT",
|
|
204
|
+
fill_value=float(replace),
|
|
205
|
+
)
|
|
206
|
+
return from_4d(image, original_ndims)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _blend(image1: tf.Tensor, image2: tf.Tensor, factor) -> tf.Tensor:
|
|
210
|
+
image1 = tf.cast(image1, tf.float32)
|
|
211
|
+
image2 = tf.cast(image2, tf.float32)
|
|
212
|
+
factor = tf.cast(factor, tf.float32)
|
|
213
|
+
|
|
214
|
+
blended = image1 + factor * (image2 - image1)
|
|
215
|
+
blended = tf.clip_by_value(blended, 0.0, 255.0)
|
|
216
|
+
return tf.cast(blended, tf.uint8)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _fill_rectangle(
|
|
220
|
+
image, center_width, center_height, half_width, half_height, replace=None, seed=None
|
|
221
|
+
):
|
|
222
|
+
image_height = tf.shape(image)[0]
|
|
223
|
+
image_width = tf.shape(image)[1]
|
|
224
|
+
|
|
225
|
+
lower_pad = tf.maximum(0, center_height - half_height)
|
|
226
|
+
upper_pad = tf.maximum(0, image_height - center_height - half_height)
|
|
227
|
+
left_pad = tf.maximum(0, center_width - half_width)
|
|
228
|
+
right_pad = tf.maximum(0, image_width - center_width - half_width)
|
|
229
|
+
|
|
230
|
+
cutout_shape = [
|
|
231
|
+
image_height - (lower_pad + upper_pad),
|
|
232
|
+
image_width - (left_pad + right_pad),
|
|
233
|
+
]
|
|
234
|
+
padding_dims = [[lower_pad, upper_pad], [left_pad, right_pad]]
|
|
235
|
+
mask = tf.pad(
|
|
236
|
+
tf.zeros(cutout_shape, dtype=image.dtype), padding_dims, constant_values=1
|
|
237
|
+
)
|
|
238
|
+
mask = tf.expand_dims(mask, -1)
|
|
239
|
+
num_channels = tf.shape(image)[-1]
|
|
240
|
+
mask = tf.tile(mask, [1, 1, num_channels])
|
|
241
|
+
|
|
242
|
+
if replace is None:
|
|
243
|
+
fill_seed = seed if seed is not None else tf.constant([0, 0], dtype=tf.int32)
|
|
244
|
+
fill = tf.random.stateless_normal(
|
|
245
|
+
tf.shape(image), seed=fill_seed, dtype=image.dtype
|
|
246
|
+
)
|
|
247
|
+
elif isinstance(replace, tf.Tensor):
|
|
248
|
+
fill = replace
|
|
249
|
+
else:
|
|
250
|
+
fill = tf.ones_like(image, dtype=image.dtype) * replace
|
|
251
|
+
return tf.where(tf.equal(mask, 0), fill, image)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _cutout(
|
|
255
|
+
image: tf.Tensor, pad_size: int, replace: int, seed: tf.Tensor
|
|
256
|
+
) -> tf.Tensor:
|
|
257
|
+
image_height = tf.shape(image)[0]
|
|
258
|
+
image_width = tf.shape(image)[1]
|
|
259
|
+
|
|
260
|
+
seeds = tf.random.split(seed, 3)
|
|
261
|
+
cutout_center_height = tf.random.stateless_uniform(
|
|
262
|
+
[], minval=0, maxval=image_height, dtype=tf.int32, seed=seeds[0]
|
|
263
|
+
)
|
|
264
|
+
cutout_center_width = tf.random.stateless_uniform(
|
|
265
|
+
[], minval=0, maxval=image_width, dtype=tf.int32, seed=seeds[1]
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
return _fill_rectangle(
|
|
269
|
+
image,
|
|
270
|
+
cutout_center_width,
|
|
271
|
+
cutout_center_height,
|
|
272
|
+
pad_size,
|
|
273
|
+
pad_size,
|
|
274
|
+
replace=replace,
|
|
275
|
+
seed=seeds[2],
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def _solarize_val(image: tf.Tensor, threshold: int = 128) -> tf.Tensor:
|
|
280
|
+
threshold = tf.cast(threshold, image.dtype)
|
|
281
|
+
max_val = tf.cast(255, image.dtype)
|
|
282
|
+
return tf.where(image < threshold, image, max_val - image)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def _solarize_add(
|
|
286
|
+
image: tf.Tensor, addition: int = 0, threshold: int = 128
|
|
287
|
+
) -> tf.Tensor:
|
|
288
|
+
threshold = tf.cast(threshold, image.dtype)
|
|
289
|
+
|
|
290
|
+
added_image = tf.cast(image, tf.int64) + tf.cast(addition, tf.int64)
|
|
291
|
+
added_image = tf.cast(tf.clip_by_value(added_image, 0, 255), image.dtype)
|
|
292
|
+
|
|
293
|
+
return tf.where(image < threshold, added_image, image)
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _posterize(image: tf.Tensor, bits: int) -> tf.Tensor:
|
|
297
|
+
shift = tf.cast(8 - bits, image.dtype)
|
|
298
|
+
return tf.bitwise.left_shift(tf.bitwise.right_shift(image, shift), shift)
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def _autocontrast(image: tf.Tensor) -> tf.Tensor:
|
|
302
|
+
def scale_channel(img):
|
|
303
|
+
lo = tf.cast(tf.reduce_min(img), tf.float32)
|
|
304
|
+
hi = tf.cast(tf.reduce_max(img), tf.float32)
|
|
305
|
+
|
|
306
|
+
def scale_values(im):
|
|
307
|
+
scale = 255.0 / (hi - lo)
|
|
308
|
+
offset = -lo * scale
|
|
309
|
+
im = tf.cast(im, tf.float32) * scale + offset
|
|
310
|
+
im = tf.clip_by_value(im, 0.0, 255.0)
|
|
311
|
+
return tf.cast(im, tf.uint8)
|
|
312
|
+
|
|
313
|
+
return tf.cond(hi > lo, lambda: scale_values(img), lambda: img)
|
|
314
|
+
|
|
315
|
+
s1 = scale_channel(image[..., 0])
|
|
316
|
+
s2 = scale_channel(image[..., 1])
|
|
317
|
+
s3 = scale_channel(image[..., 2])
|
|
318
|
+
return tf.stack([s1, s2, s3], -1)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def _sharpness(image: tf.Tensor, factor: float) -> tf.Tensor:
|
|
322
|
+
orig_image = image
|
|
323
|
+
image = tf.cast(image, tf.float32)
|
|
324
|
+
image = tf.expand_dims(image, 0)
|
|
325
|
+
kernel = (
|
|
326
|
+
tf.constant(
|
|
327
|
+
[[1, 1, 1], [1, 5, 1], [1, 1, 1]], dtype=tf.float32, shape=[3, 3, 1, 1]
|
|
328
|
+
)
|
|
329
|
+
/ 13.0
|
|
330
|
+
)
|
|
331
|
+
kernel = tf.tile(kernel, [1, 1, 3, 1])
|
|
332
|
+
degenerate = tf.nn.depthwise_conv2d(
|
|
333
|
+
image, kernel, strides=[1, 1, 1, 1], padding="VALID"
|
|
334
|
+
)
|
|
335
|
+
degenerate = tf.clip_by_value(degenerate, 0.0, 255.0)
|
|
336
|
+
degenerate = tf.squeeze(tf.cast(degenerate, tf.uint8), [0])
|
|
337
|
+
|
|
338
|
+
mask = tf.ones_like(degenerate)
|
|
339
|
+
paddings = [[1, 1], [1, 1], [0, 0]]
|
|
340
|
+
padded_mask = tf.pad(mask, paddings)
|
|
341
|
+
padded_degenerate = tf.pad(degenerate, paddings)
|
|
342
|
+
result = tf.where(tf.equal(padded_mask, 1), padded_degenerate, orig_image)
|
|
343
|
+
return _blend(result, orig_image, factor)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def _equalize(image: tf.Tensor) -> tf.Tensor:
|
|
347
|
+
def scale_channel(im, c):
|
|
348
|
+
im = tf.cast(im[..., c], tf.int32)
|
|
349
|
+
histo = tf.histogram_fixed_width(im, [0, 255], nbins=256)
|
|
350
|
+
nonzero = tf.where(tf.not_equal(histo, 0))
|
|
351
|
+
nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1])
|
|
352
|
+
step = (tf.reduce_sum(nonzero_histo) - nonzero_histo[-1]) // 255
|
|
353
|
+
|
|
354
|
+
def build_lut(histo, step):
|
|
355
|
+
lut = (tf.cumsum(histo) + (step // 2)) // step
|
|
356
|
+
lut = tf.concat([[0], lut[:-1]], 0)
|
|
357
|
+
return tf.clip_by_value(lut, 0, 255)
|
|
358
|
+
|
|
359
|
+
result = tf.cond(
|
|
360
|
+
tf.equal(step, 0),
|
|
361
|
+
lambda: im,
|
|
362
|
+
lambda: tf.gather(build_lut(histo, step), im),
|
|
363
|
+
)
|
|
364
|
+
return tf.cast(result, tf.uint8)
|
|
365
|
+
|
|
366
|
+
s1 = scale_channel(image, 0)
|
|
367
|
+
s2 = scale_channel(image, 1)
|
|
368
|
+
s3 = scale_channel(image, 2)
|
|
369
|
+
return tf.stack([s1, s2, s3], -1)
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def _invert(image: tf.Tensor) -> tf.Tensor:
|
|
373
|
+
return tf.cast(255, image.dtype) - image
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def _wrap(image: tf.Tensor) -> tf.Tensor:
|
|
377
|
+
shape = tf.shape(image)
|
|
378
|
+
extended_channel = tf.expand_dims(tf.ones(shape[:-1], image.dtype), -1)
|
|
379
|
+
return tf.concat([image, extended_channel], axis=-1)
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def _unwrap(image: tf.Tensor, replace: int) -> tf.Tensor:
|
|
383
|
+
image_shape = tf.shape(image)
|
|
384
|
+
flattened_image = tf.reshape(image, [-1, image_shape[-1]])
|
|
385
|
+
alpha_channel = tf.expand_dims(flattened_image[..., 3], axis=-1)
|
|
386
|
+
|
|
387
|
+
if isinstance(replace, int):
|
|
388
|
+
replace_t = tf.fill([3], tf.cast(replace, image.dtype))
|
|
389
|
+
else:
|
|
390
|
+
replace_t = tf.cast(replace, image.dtype)
|
|
391
|
+
|
|
392
|
+
replace_t = tf.concat([replace_t, tf.ones([1], image.dtype)], 0)
|
|
393
|
+
|
|
394
|
+
flattened_image = tf.where(
|
|
395
|
+
tf.equal(alpha_channel, 0),
|
|
396
|
+
tf.ones_like(flattened_image, dtype=image.dtype) * replace_t,
|
|
397
|
+
flattened_image,
|
|
398
|
+
)
|
|
399
|
+
image = tf.reshape(flattened_image, image_shape)
|
|
400
|
+
return image[..., :3]
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def _wrapped_rotate(image: tf.Tensor, degrees: float, replace: int) -> tf.Tensor:
|
|
404
|
+
image = _rotate(_wrap(image), degrees=degrees)
|
|
405
|
+
return _unwrap(image, replace)
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
def _translate_x(image: tf.Tensor, pixels: int, replace: int) -> tf.Tensor:
|
|
409
|
+
image = _translate(_wrap(image), [-pixels, 0])
|
|
410
|
+
return _unwrap(image, replace)
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def _translate_y(image: tf.Tensor, pixels: int, replace: int) -> tf.Tensor:
|
|
414
|
+
image = _translate(_wrap(image), [0, -pixels])
|
|
415
|
+
return _unwrap(image, replace)
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def _shear_x(image: tf.Tensor, level: float, replace: int) -> tf.Tensor:
|
|
419
|
+
image = _transform(
|
|
420
|
+
image=_wrap(image), transforms=[1.0, level, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]
|
|
421
|
+
)
|
|
422
|
+
return _unwrap(image, replace)
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def _shear_y(image: tf.Tensor, level: float, replace: int) -> tf.Tensor:
|
|
426
|
+
image = _transform(
|
|
427
|
+
image=_wrap(image), transforms=[1.0, 0.0, 0.0, level, 1.0, 0.0, 0.0, 0.0]
|
|
428
|
+
)
|
|
429
|
+
return _unwrap(image, replace)
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def _grayscale(image: tf.Tensor) -> tf.Tensor:
|
|
433
|
+
return tf.image.grayscale_to_rgb(tf.image.rgb_to_grayscale(image))
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def _color(image: tf.Tensor, factor: float) -> tf.Tensor:
|
|
437
|
+
degenerate = _grayscale(image)
|
|
438
|
+
return _blend(degenerate, image, factor)
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def _contrast(image: tf.Tensor, factor: float) -> tf.Tensor:
|
|
442
|
+
degenerate = tf.image.rgb_to_grayscale(image)
|
|
443
|
+
mean = tf.reduce_mean(tf.cast(degenerate, tf.float32))
|
|
444
|
+
degenerate = tf.ones_like(degenerate, dtype=tf.float32) * mean
|
|
445
|
+
degenerate = tf.clip_by_value(degenerate, 0.0, 255.0)
|
|
446
|
+
degenerate = tf.image.grayscale_to_rgb(tf.cast(degenerate, tf.uint8))
|
|
447
|
+
return _blend(degenerate, image, factor)
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def _brightness(image: tf.Tensor, factor: float) -> tf.Tensor:
|
|
451
|
+
degenerate = tf.zeros_like(image)
|
|
452
|
+
return _blend(degenerate, image, factor)
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
def _clip_bbox(min_y, min_x, max_y, max_x):
|
|
456
|
+
min_y = tf.clip_by_value(min_y, 0.0, 1.0)
|
|
457
|
+
min_x = tf.clip_by_value(min_x, 0.0, 1.0)
|
|
458
|
+
max_y = tf.clip_by_value(max_y, 0.0, 1.0)
|
|
459
|
+
max_x = tf.clip_by_value(max_x, 0.0, 1.0)
|
|
460
|
+
return min_y, min_x, max_y, max_x
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def _check_bbox_area(min_y, min_x, max_y, max_x, delta=0.05):
|
|
464
|
+
height = max_y - min_y
|
|
465
|
+
width = max_x - min_x
|
|
466
|
+
|
|
467
|
+
def _adjust(min_coord, max_coord):
|
|
468
|
+
new_min = tf.maximum(min_coord - delta / 2, 0.0)
|
|
469
|
+
new_max = tf.minimum(max_coord + delta / 2, 1.0)
|
|
470
|
+
return new_min, new_max
|
|
471
|
+
|
|
472
|
+
min_y, max_y = tf.cond(
|
|
473
|
+
tf.equal(height, 0.0),
|
|
474
|
+
lambda: _adjust(min_y, max_y),
|
|
475
|
+
lambda: (min_y, max_y),
|
|
476
|
+
)
|
|
477
|
+
min_x, max_x = tf.cond(
|
|
478
|
+
tf.equal(width, 0.0),
|
|
479
|
+
lambda: _adjust(min_x, max_x),
|
|
480
|
+
lambda: (min_x, max_x),
|
|
481
|
+
)
|
|
482
|
+
return min_y, min_x, max_y, max_x
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
def _rotate_bbox(bbox, image_height, image_width, degrees):
|
|
486
|
+
image_height, image_width = (
|
|
487
|
+
tf.cast(image_height, tf.float32),
|
|
488
|
+
tf.cast(image_width, tf.float32),
|
|
489
|
+
)
|
|
490
|
+
degrees_to_radians = math.pi / 180.0
|
|
491
|
+
radians = degrees * degrees_to_radians
|
|
492
|
+
|
|
493
|
+
min_y = -tf.cast(image_height * (bbox[0] - 0.5), tf.int32)
|
|
494
|
+
min_x = tf.cast(image_width * (bbox[1] - 0.5), tf.int32)
|
|
495
|
+
max_y = -tf.cast(image_height * (bbox[2] - 0.5), tf.int32)
|
|
496
|
+
max_x = tf.cast(image_width * (bbox[3] - 0.5), tf.int32)
|
|
497
|
+
coordinates = tf.stack(
|
|
498
|
+
[[min_y, min_x], [min_y, max_x], [max_y, min_x], [max_y, max_x]]
|
|
499
|
+
)
|
|
500
|
+
coordinates = tf.cast(coordinates, tf.float32)
|
|
501
|
+
|
|
502
|
+
rotation_matrix = tf.stack(
|
|
503
|
+
[
|
|
504
|
+
[tf.cos(radians), tf.sin(radians)],
|
|
505
|
+
[-tf.sin(radians), tf.cos(radians)],
|
|
506
|
+
]
|
|
507
|
+
)
|
|
508
|
+
new_coords = tf.cast(
|
|
509
|
+
tf.matmul(rotation_matrix, tf.transpose(coordinates)), tf.int32
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
min_y = -(tf.cast(tf.reduce_max(new_coords[0, :]), tf.float32) / image_height - 0.5)
|
|
513
|
+
min_x = tf.cast(tf.reduce_min(new_coords[1, :]), tf.float32) / image_width + 0.5
|
|
514
|
+
max_y = -(tf.cast(tf.reduce_min(new_coords[0, :]), tf.float32) / image_height - 0.5)
|
|
515
|
+
max_x = tf.cast(tf.reduce_max(new_coords[1, :]), tf.float32) / image_width + 0.5
|
|
516
|
+
|
|
517
|
+
min_y, min_x, max_y, max_x = _clip_bbox(min_y, min_x, max_y, max_x)
|
|
518
|
+
min_y, min_x, max_y, max_x = _check_bbox_area(min_y, min_x, max_y, max_x)
|
|
519
|
+
return tf.stack([min_y, min_x, max_y, max_x])
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def _rotate_with_bboxes(image, bboxes, degrees, replace):
|
|
523
|
+
image = _wrapped_rotate(image, degrees, replace)
|
|
524
|
+
image_height = tf.shape(image)[0]
|
|
525
|
+
image_width = tf.shape(image)[1]
|
|
526
|
+
|
|
527
|
+
def wrapped_rotate_bbox(bbox):
|
|
528
|
+
return _rotate_bbox(bbox, image_height, image_width, degrees)
|
|
529
|
+
|
|
530
|
+
bboxes = tf.map_fn(wrapped_rotate_bbox, bboxes)
|
|
531
|
+
return image, bboxes
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
def _shear_bbox(bbox, image_height, image_width, level, shear_horizontal):
|
|
535
|
+
image_height, image_width = (
|
|
536
|
+
tf.cast(image_height, tf.float32),
|
|
537
|
+
tf.cast(image_width, tf.float32),
|
|
538
|
+
)
|
|
539
|
+
min_y = tf.cast(image_height * bbox[0], tf.int32)
|
|
540
|
+
min_x = tf.cast(image_width * bbox[1], tf.int32)
|
|
541
|
+
max_y = tf.cast(image_height * bbox[2], tf.int32)
|
|
542
|
+
max_x = tf.cast(image_width * bbox[3], tf.int32)
|
|
543
|
+
coordinates = tf.stack(
|
|
544
|
+
[[min_y, min_x], [min_y, max_x], [max_y, min_x], [max_y, max_x]]
|
|
545
|
+
)
|
|
546
|
+
coordinates = tf.cast(coordinates, tf.float32)
|
|
547
|
+
|
|
548
|
+
if shear_horizontal:
|
|
549
|
+
translation_matrix = tf.stack([[1, 0], [-level, 1]])
|
|
550
|
+
else:
|
|
551
|
+
translation_matrix = tf.stack([[1, -level], [0, 1]])
|
|
552
|
+
translation_matrix = tf.cast(translation_matrix, tf.float32)
|
|
553
|
+
new_coords = tf.cast(
|
|
554
|
+
tf.matmul(translation_matrix, tf.transpose(coordinates)), tf.int32
|
|
555
|
+
)
|
|
556
|
+
|
|
557
|
+
min_y = tf.cast(tf.reduce_min(new_coords[0, :]), tf.float32) / image_height
|
|
558
|
+
min_x = tf.cast(tf.reduce_min(new_coords[1, :]), tf.float32) / image_width
|
|
559
|
+
max_y = tf.cast(tf.reduce_max(new_coords[0, :]), tf.float32) / image_height
|
|
560
|
+
max_x = tf.cast(tf.reduce_max(new_coords[1, :]), tf.float32) / image_width
|
|
561
|
+
|
|
562
|
+
min_y, min_x, max_y, max_x = _clip_bbox(min_y, min_x, max_y, max_x)
|
|
563
|
+
min_y, min_x, max_y, max_x = _check_bbox_area(min_y, min_x, max_y, max_x)
|
|
564
|
+
return tf.stack([min_y, min_x, max_y, max_x])
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def _shear_with_bboxes(image, bboxes, level, replace, shear_horizontal):
|
|
568
|
+
if shear_horizontal:
|
|
569
|
+
image = _shear_x(image, level, replace)
|
|
570
|
+
else:
|
|
571
|
+
image = _shear_y(image, level, replace)
|
|
572
|
+
image_height = tf.shape(image)[0]
|
|
573
|
+
image_width = tf.shape(image)[1]
|
|
574
|
+
|
|
575
|
+
def wrapped_shear_bbox(bbox):
|
|
576
|
+
return _shear_bbox(bbox, image_height, image_width, level, shear_horizontal)
|
|
577
|
+
|
|
578
|
+
bboxes = tf.map_fn(wrapped_shear_bbox, bboxes)
|
|
579
|
+
return image, bboxes
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
def _shift_bbox(bbox, image_height, image_width, pixels, shift_horizontal):
|
|
583
|
+
pixels = tf.cast(pixels, tf.int32)
|
|
584
|
+
min_y = tf.cast(tf.cast(image_height, tf.float32) * bbox[0], tf.int32)
|
|
585
|
+
min_x = tf.cast(tf.cast(image_width, tf.float32) * bbox[1], tf.int32)
|
|
586
|
+
max_y = tf.cast(tf.cast(image_height, tf.float32) * bbox[2], tf.int32)
|
|
587
|
+
max_x = tf.cast(tf.cast(image_width, tf.float32) * bbox[3], tf.int32)
|
|
588
|
+
|
|
589
|
+
if shift_horizontal:
|
|
590
|
+
min_x = tf.maximum(0, min_x - pixels)
|
|
591
|
+
max_x = tf.minimum(image_width, max_x - pixels)
|
|
592
|
+
else:
|
|
593
|
+
min_y = tf.maximum(0, min_y - pixels)
|
|
594
|
+
max_y = tf.minimum(image_height, max_y - pixels)
|
|
595
|
+
|
|
596
|
+
min_y = tf.cast(min_y, tf.float32) / tf.cast(image_height, tf.float32)
|
|
597
|
+
min_x = tf.cast(min_x, tf.float32) / tf.cast(image_width, tf.float32)
|
|
598
|
+
max_y = tf.cast(max_y, tf.float32) / tf.cast(image_height, tf.float32)
|
|
599
|
+
max_x = tf.cast(max_x, tf.float32) / tf.cast(image_width, tf.float32)
|
|
600
|
+
|
|
601
|
+
min_y, min_x, max_y, max_x = _clip_bbox(min_y, min_x, max_y, max_x)
|
|
602
|
+
min_y, min_x, max_y, max_x = _check_bbox_area(min_y, min_x, max_y, max_x)
|
|
603
|
+
return tf.stack([min_y, min_x, max_y, max_x])
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
def _translate_bbox(image, bboxes, pixels, replace, shift_horizontal):
|
|
607
|
+
if shift_horizontal:
|
|
608
|
+
image = _translate_x(image, pixels, replace)
|
|
609
|
+
else:
|
|
610
|
+
image = _translate_y(image, pixels, replace)
|
|
611
|
+
|
|
612
|
+
image_height = tf.shape(image)[0]
|
|
613
|
+
image_width = tf.shape(image)[1]
|
|
614
|
+
|
|
615
|
+
def wrapped_shift_bbox(bbox):
|
|
616
|
+
return _shift_bbox(bbox, image_height, image_width, pixels, shift_horizontal)
|
|
617
|
+
|
|
618
|
+
bboxes = tf.map_fn(wrapped_shift_bbox, bboxes)
|
|
619
|
+
return image, bboxes
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
def _randomly_negate_tensor(tensor, seed):
|
|
623
|
+
should_flip = tf.random.stateless_uniform([], seed=seed) > 0.5
|
|
624
|
+
return tf.cond(should_flip, lambda: -tensor, lambda: tensor)
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
def to_4d(image: tf.Tensor) -> tf.Tensor:
|
|
628
|
+
"""Converts an input Tensor to 4 dimensions.
|
|
629
|
+
|
|
630
|
+
4D image => [N, H, W, C] or [N, C, H, W]
|
|
631
|
+
3D image => [1, H, W, C] or [1, C, H, W]
|
|
632
|
+
2D image => [1, H, W, 1]
|
|
633
|
+
|
|
634
|
+
Args:
|
|
635
|
+
image: The 2/3/4D input tensor.
|
|
636
|
+
|
|
637
|
+
Returns:
|
|
638
|
+
A 4D image tensor.
|
|
639
|
+
|
|
640
|
+
Raises:
|
|
641
|
+
`TypeError` if `image` is not a 2/3/4D tensor.
|
|
642
|
+
|
|
643
|
+
"""
|
|
644
|
+
shape = tf.shape(image)
|
|
645
|
+
original_rank = tf.rank(image)
|
|
646
|
+
left_pad = tf.cast(tf.less_equal(original_rank, 3), dtype=tf.int32)
|
|
647
|
+
right_pad = tf.cast(tf.equal(original_rank, 2), dtype=tf.int32)
|
|
648
|
+
new_shape = tf.concat(
|
|
649
|
+
[
|
|
650
|
+
tf.ones(shape=left_pad, dtype=tf.int32),
|
|
651
|
+
shape,
|
|
652
|
+
tf.ones(shape=right_pad, dtype=tf.int32),
|
|
653
|
+
],
|
|
654
|
+
axis=0,
|
|
655
|
+
)
|
|
656
|
+
return tf.reshape(image, new_shape)
|
|
657
|
+
|
|
658
|
+
|
|
659
|
+
def from_4d(image: tf.Tensor, ndims: tf.Tensor) -> tf.Tensor:
|
|
660
|
+
"""Converts a 4D image back to `ndims` rank."""
|
|
661
|
+
shape = tf.shape(image)
|
|
662
|
+
begin = tf.cast(tf.less_equal(ndims, 3), dtype=tf.int32)
|
|
663
|
+
end = 4 - tf.cast(tf.equal(ndims, 2), dtype=tf.int32)
|
|
664
|
+
new_shape = shape[begin:end]
|
|
665
|
+
return tf.reshape(image, new_shape)
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
@tf.function
|
|
669
|
+
def gaussian_filter2d(
|
|
670
|
+
image: tf.Tensor,
|
|
671
|
+
filter_shape: int = 3,
|
|
672
|
+
sigma: float = 1.0,
|
|
673
|
+
padding: str = "REFLECT",
|
|
674
|
+
constant_values: Union[int, float] = 0,
|
|
675
|
+
name: Optional[str] = None,
|
|
676
|
+
) -> tf.Tensor:
|
|
677
|
+
"""Performs Gaussian blur on image(s)."""
|
|
678
|
+
with tf.name_scope(name or "gaussian_filter2d"):
|
|
679
|
+
# Convert inputs to tensors
|
|
680
|
+
image = tf.convert_to_tensor(image)
|
|
681
|
+
|
|
682
|
+
# Handle filter_shape
|
|
683
|
+
filter_h = filter_w = filter_shape
|
|
684
|
+
|
|
685
|
+
# Handle sigma
|
|
686
|
+
sigma_h = sigma_w = sigma
|
|
687
|
+
|
|
688
|
+
# Input validation
|
|
689
|
+
tf.debugging.assert_greater_equal(sigma_h, 0.0, "sigma_h must be >= 0")
|
|
690
|
+
tf.debugging.assert_greater_equal(sigma_w, 0.0, "sigma_w must be >= 0")
|
|
691
|
+
|
|
692
|
+
# Convert image to 4D
|
|
693
|
+
original_ndims = tf.rank(image)
|
|
694
|
+
image = to_4d(image)
|
|
695
|
+
|
|
696
|
+
# Handle dtype
|
|
697
|
+
orig_dtype = image.dtype
|
|
698
|
+
if not image.dtype.is_floating:
|
|
699
|
+
image = tf.cast(image, tf.float32)
|
|
700
|
+
|
|
701
|
+
# Get number of channels
|
|
702
|
+
channels = tf.shape(image)[3]
|
|
703
|
+
|
|
704
|
+
# Create gaussian kernels
|
|
705
|
+
sigma_t = tf.convert_to_tensor([sigma_h, sigma_w], dtype=image.dtype)
|
|
706
|
+
|
|
707
|
+
gaussian_kernel_x = _get_gaussian_kernel(
|
|
708
|
+
sigma_t[1], tf.cast(filter_w, tf.int32)
|
|
709
|
+
)
|
|
710
|
+
gaussian_kernel_y = _get_gaussian_kernel(
|
|
711
|
+
sigma_t[0], tf.cast(filter_h, tf.int32)
|
|
712
|
+
)
|
|
713
|
+
|
|
714
|
+
# Create 2D kernel
|
|
715
|
+
gaussian_kernel_2d = _get_gaussian_kernel_2d(
|
|
716
|
+
gaussian_kernel_x, gaussian_kernel_y
|
|
717
|
+
)
|
|
718
|
+
|
|
719
|
+
# Reshape kernel for depthwise conv
|
|
720
|
+
gaussian_kernel_2d = tf.reshape(gaussian_kernel_2d, [filter_h, filter_w, 1, 1])
|
|
721
|
+
gaussian_kernel_2d = tf.tile(gaussian_kernel_2d, [1, 1, channels, 1])
|
|
722
|
+
|
|
723
|
+
# Pad image
|
|
724
|
+
image = _pad(
|
|
725
|
+
image,
|
|
726
|
+
[filter_h, filter_w],
|
|
727
|
+
pad_mode=padding,
|
|
728
|
+
constant_values=constant_values,
|
|
729
|
+
)
|
|
730
|
+
|
|
731
|
+
# Apply convolution
|
|
732
|
+
output = tf.nn.depthwise_conv2d(
|
|
733
|
+
input=image,
|
|
734
|
+
filter=gaussian_kernel_2d,
|
|
735
|
+
strides=[1, 1, 1, 1],
|
|
736
|
+
padding="VALID",
|
|
737
|
+
)
|
|
738
|
+
|
|
739
|
+
# Convert back to original dimensions and dtype
|
|
740
|
+
output = from_4d(output, original_ndims)
|
|
741
|
+
return tf.cast(output, orig_dtype)
|