【问题标题】:How to get boolean mask for padding如何获得用于填充的布尔掩码
【发布时间】:2019-07-04 05:07:25
【问题描述】:

使用 tf.extract_image_patches 和填充 'SAME' 会导致一些补丁包含填充(这很好)。

有没有一种简单的方法来获得一个 TensorFlow 布尔掩码来屏蔽所有包含填充的补丁?还是我需要重新实现填充过程?

【问题讨论】:

  • 您需要查找填充补丁的索引吗?或者您需要在补丁中填充(零)值的像素掩码?
  • @DmytroPrylipko 我只需要填充补丁的索引

标签: tensorflow padding mask


【解决方案1】:

我当前的解决方案是添加一个表示位标志的附加通道。 提取图像补丁后,位标志为 0 用于填充通道,1 用于非填充通道。

完整解决方案:

input_tensor = tf.random.normal([10, 28, 28, 1])
window_shape, strides, padding = (4, 4), (2, 2), 'SAME'

# ----------------------------

bits = tf.ones([tf.shape(input_tensor)[0], input_tensor.shape[1], input_tensor.shape[2], 1])
input_for_patching = tf.concat([input_tensor, bits], axis=-1)

patches = tf.extract_image_patches(input_for_patching, ksizes=(1, *window_shape, 1), strides=(1, *strides, 1), rates=(1, 1, 1, 1), padding=padding)

patches_shape = patches.shape

patches = tf.reshape(patches, [-1, *window_shape, input_tensor.shape[3] + 1])

padding_mask = tf.to_float(tf.reduce_all(tf.equal(patches[:, :, :, -1:], 1.0), [1, 2, 3]))

patches = tf.reshape(patches[:, :, :, :-1], [-1, patches_shape[1], patches_shape[2], window_shape[0] * window_shape[1] * input_tensor.shape[3]])

上面代码中的padding_mask是我需要的。

如果有人有更短、更优雅和/或更集成的版本,请随时分享。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 2020-11-19
    • 2018-09-13
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多