模糊和运动模糊增强
Blur and Motion Blur augmentation
我正在使用 Deeplabv3+ 存储库,我想知道你们如何应用运动模糊和模糊作为增强。我已经有一些增强,比如随机比例(示例)。
但是,我找不到任何开箱即用的解决方案来在 tensorflow 上应用运动模糊。
有谁知道任何库或如何构建此转换?
def randomly_scale_image_and_label(image, label=None, scale=1.0):
"""Randomly scales image and label.
Args:
image: Image with shape [height, width, 3].
label: Label with shape [height, width, 1].
scale: The value to scale image and label.
Returns:
Scaled image and label.
"""
# No random scaling if scale == 1.
if scale == 1.0:
return image, label
image_shape = tf.shape(image)
new_dim = tf.cast(
tf.cast([image_shape[0], image_shape[1]], tf.float32) * scale,
tf.int32)
# Need squeeze and expand_dims because image interpolation takes
# 4D tensors as input.
image = tf.squeeze(tf.image.resize_bilinear(
tf.expand_dims(image, 0),
new_dim,
align_corners=True), [0])
if label is not None:
label = tf.squeeze(tf.image.resize_nearest_neighbor(
tf.expand_dims(label, 0),
new_dim,
align_corners=True), [0])
return image, label
有一个很好的库叫做 albumentations。
你可以看这里:https://github.com/albumentations-team/albumentations/blob/master/notebooks/example.ipynb.
我相信它会有很大的用处;它包含各种增强功能,适用于不同的用例(对象检测、图像分割)。
我在 tensorflow 中找不到运动模糊的直接实现。您必须使用 tf.nn.depthwise_conv2d
来实现它。从 albumentation 看运动模糊的实现,你需要创建一个随机大小的过滤器,比如 nxn
,然后在过滤器上绘制一条随机线。然后使用该过滤器在图像上应用深度卷积。
例如,尺寸 5 在 180/0 度的运动模糊过滤器看起来像
>>> kernel = np.zeros([5,5])
>>> kernel[2] = 0.2
>>> kernel
array([[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0.2, 0.2, 0.2, 0.2, 0.2],
[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ]])
现在将过滤器应用于 image
([高度,宽度,3])
>>> kernel_motion_blur = tf.convert_to_tensor(kernel, tf.float32)
>>> kernel_motion_blur = tf.tile(kernel_motion_blur[..., None, None], [1,1,3,1])
>>> blurred_image = tf.nn.depthwise_conv2d(image[None], kernel_motion_blur, strides=(1,1,1,1), padding='VALID')
注意:要生成运动模糊内核,可以使用cv2.line
在numpy数组上绘制随机线
我正在使用 Deeplabv3+ 存储库,我想知道你们如何应用运动模糊和模糊作为增强。我已经有一些增强,比如随机比例(示例)。
但是,我找不到任何开箱即用的解决方案来在 tensorflow 上应用运动模糊。 有谁知道任何库或如何构建此转换?
def randomly_scale_image_and_label(image, label=None, scale=1.0):
"""Randomly scales image and label.
Args:
image: Image with shape [height, width, 3].
label: Label with shape [height, width, 1].
scale: The value to scale image and label.
Returns:
Scaled image and label.
"""
# No random scaling if scale == 1.
if scale == 1.0:
return image, label
image_shape = tf.shape(image)
new_dim = tf.cast(
tf.cast([image_shape[0], image_shape[1]], tf.float32) * scale,
tf.int32)
# Need squeeze and expand_dims because image interpolation takes
# 4D tensors as input.
image = tf.squeeze(tf.image.resize_bilinear(
tf.expand_dims(image, 0),
new_dim,
align_corners=True), [0])
if label is not None:
label = tf.squeeze(tf.image.resize_nearest_neighbor(
tf.expand_dims(label, 0),
new_dim,
align_corners=True), [0])
return image, label
有一个很好的库叫做 albumentations。
你可以看这里:https://github.com/albumentations-team/albumentations/blob/master/notebooks/example.ipynb.
我相信它会有很大的用处;它包含各种增强功能,适用于不同的用例(对象检测、图像分割)。
我在 tensorflow 中找不到运动模糊的直接实现。您必须使用 tf.nn.depthwise_conv2d
来实现它。从 albumentation 看运动模糊的实现,你需要创建一个随机大小的过滤器,比如 nxn
,然后在过滤器上绘制一条随机线。然后使用该过滤器在图像上应用深度卷积。
例如,尺寸 5 在 180/0 度的运动模糊过滤器看起来像
>>> kernel = np.zeros([5,5])
>>> kernel[2] = 0.2
>>> kernel
array([[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0.2, 0.2, 0.2, 0.2, 0.2],
[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ]])
现在将过滤器应用于 image
([高度,宽度,3])
>>> kernel_motion_blur = tf.convert_to_tensor(kernel, tf.float32)
>>> kernel_motion_blur = tf.tile(kernel_motion_blur[..., None, None], [1,1,3,1])
>>> blurred_image = tf.nn.depthwise_conv2d(image[None], kernel_motion_blur, strides=(1,1,1,1), padding='VALID')
注意:要生成运动模糊内核,可以使用cv2.line
在numpy数组上绘制随机线