【问题标题】:Blur and Motion Blur augmentation模糊和运动模糊增强
【发布时间】:2020-03-17 17:39:13
【问题描述】:

我正在使用 Deeplabv3+ 存储库,我想知道您如何应用 Motion Blur 和 Blur 作为增强。我已经有了一些增强功能,例如随机比例(示例)。

但是,我找不到任何开箱即用的解决方案来在 tensorflow 上应用 Motion Blur。 有谁知道任何库或如何构建这种转换?

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

【问题讨论】:

    标签: tensorflow deeplab


    【解决方案1】:

    有一个非常好的库,叫做albumentations。

    您可以在这里查看:https://github.com/albumentations-team/albumentations/blob/master/notebooks/example.ipynb。

    我相信它会很有用;它包含各种增强功能,适用于不同的用例(对象检测、图像分割)。

    【讨论】:

    • 我知道那个图书馆。但是cv2 要求将图像转换为numpy 数组。我正在从 tensorflow 中寻找一些东西,比如我粘贴的示例
    • 我不明白。如果你有一个 Numpy 数组,为什么会有问题?即便如此,为什么不将 NumPy 数组转换为 TF 张量?
    • 据我所知,这些库的发明正是出于这个原因:dl 框架没有针对此类扩充的开箱即用解决方案。
    • 从未尝试过,但我会尝试这样做。你有什么以前用过的东西可以方便地做到这一点吗?我会将其标记为已解决,因为它可能会解决我的问题。谢谢!
    • 您可以使用:tf.convert_to_tensor(my_numpy_array, dtype=tf.float32)(至少在 TF 2.0 中)。您的“my_numpy_array”是通过增强转换的任何图像,就像我在上面提供给您的教程中一样。
    【解决方案2】:

    我在 tensorflow 中找不到运动模糊的直接实现。您必须使用tf.nn.depthwise_conv2d 来实现它。从allementation 中查看运动模糊的实现,您需要创建一个随机大小的过滤器,例如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数组上绘制随机线

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多