【问题标题】:Define a binary mask in Keras在 Keras 中定义二进制掩码
【发布时间】:2019-06-03 05:29:03
【问题描述】:

我有一个形状为 [X,Y,3] 的输入图像,我有 2 个坐标 (x,y)。现在我想用这些坐标创建一个蒙版,然后将它与输入图像相乘。掩码应该是与图像大小相同的二进制矩阵,坐标为[x:x+p_size,y:y+p_size],其他位置为零。

我的问题是如何在 Keras(tensorflow 后端)中定义掩码?

请注意,此操作发生在模型中(因此仅使用 numpy 无济于事)。

img = Input(shape=(32,32,3))
xy = Input(shape=(2)) # x and y coordinates for the mask
mask = ?
output = keras.layers.Multiply()([img, mask])

【问题讨论】:

    标签: python tensorflow keras conv-neural-network mask


    【解决方案1】:

    您可以使用实现自定义功能的Lambda 层来完成所有工作:

    from keras.models import Model
    from keras.layers import Input, Lambda
    from keras import backend as K
    import numpy as np
    
    # Masking function factory
    def mask_img(x_size, y_size=None):
        if y_size is None:
            y_size = x_size
        # Masking function
        def mask_func(tensors):
            img, xy = tensors
            img_shape = K.shape(img)
            # Make indexing arrays
            xx = K.arange(img_shape[1])
            yy = K.arange(img_shape[2])
            # Get coordinates
            xy = K.cast(xy, img_shape.dtype)
            x = xy[:, 0:1]
            y = xy[:, 1:2]
            # Make X and Y masks
            mask_x = (xx >= x) & (xx < x + x_size)
            mask_y = (yy >= y) & (yy < y + y_size)
            # Make full mask
            mask = K.expand_dims(mask_x, 2) & K.expand_dims(mask_y, 1)
            # Add channels dimension
            mask = K.expand_dims(mask, -1)
            # Multiply image and mask
            mask = K.cast(mask, img.dtype)
            return img * mask
        return mask_func
    
    # Model
    img = Input(shape=(10, 10, 3))  # Small size for test
    xy = Input(shape=(2,))
    output = Lambda(mask_img(3))([img, xy])
    model = Model(inputs=[img, xy], outputs=output)
    
    # Test
    img_test = np.arange(100).reshape((1, 10, 10, 1)).repeat(3, axis=-1)
    xy_test = np.array([[2, 4]])
    output_test = model.predict(x=[img_test, xy_test])
    print(output_test[0, :, :, 0])
    

    输出:

    [[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0. 24. 25. 26.  0.  0.  0.]
     [ 0.  0.  0.  0. 34. 35. 36.  0.  0.  0.]
     [ 0.  0.  0.  0. 44. 45. 46.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
    

    【讨论】:

    • 谢谢。但我无法弄清楚我可以使用它的方式。最初的想法是给神经网络提供两张相同大小的图像,一张是图片,另一张是蒙版。在您的示例中, img_test 和 xy_test 具有不同的大小,对吗?我可以为图片和蒙版使用相同尺寸的图像吗? img = Input(shape=(10, 10, 3)) xy = Input(shape=(10, 10, 3)) 如果我们知道它总是相同的大小,那么lambda可以简化,不是吗?跨度>
    • @SteveBrown 如果你只有一个图像和一个形状相同的蒙版,你应该能够简单地将两个张量相乘,例如使用Multiply 层(如果您愿意,也可以使用Lambda 层,只需将两个输入相乘)。
    【解决方案2】:

    看起来https://keras.io/api/layers/merging_layers/multiply/ 是答案,我将尝试发布结果。像这样:

    >>> tf.keras.layers.Multiply()([np.arange(5).reshape(5, 1),
    ...                             np.arange(5, 10).reshape(5, 1)])
    <tf.Tensor: shape=(5, 1), dtype=int64, numpy=
    array([[ 0],
         [ 6],
         [14],
         [24],
         [36]])>
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 2017-03-19
      • 1970-01-01
      • 2020-04-13
      • 2018-07-21
      • 1970-01-01
      • 2015-04-15
      • 2013-08-09
      • 1970-01-01
      相关资源
      最近更新 更多