对于大小为 128x128x3 的图像,如何在输入层构造用于内核初始化的 sobel 滤波器?

How to construct a sobel filter for kernel initialization in input layer for images of size 128x128x3?

这是我的 sobel 过滤器代码:

def init_f(shape, dtype=None):

    sobel_x = tf.constant([[-5, -4, 0, 4, 5], [-8, -10, 0, 10, 8], [-10, -20, 0, 20, 10], [-8, -10, 0, 10, 8], [-5, -4, 0, 4, 5]])

    ker = np.zeros(shape, dtype)
    ker_shape = tf.shape(ker)
    kernel = tf.tile(sobel_x, ker_shape)//*Is this correct?*
    return kernel

model.add(Conv2D(filters=30, kernel_size=(5,5), kernel_initializer=init_f, strides=(1,1), activation='relu'))

到目前为止,我已经设法做到了这一点。 但是,这给了我错误:

Shape must be rank 2 but is rank 4 for 'conv2d_17/Tile' (op: 'Tile') with input shapes: [5,5], [4].

张量流版本:2.1.0

您很接近,但平铺的参数似乎不正确。这就是为什么您会收到错误 "Shape must be rank 2 but is rank 4 for..." You're sobel_x must be a rank 4 tensor,因此您需要再添加两个维度。我在这个例子中使用了 reshape。

from tensorflow import keras
import tensorflow as tf
import numpy

def kernelInitializer(shape, dtype=None):
    print(shape)    
    sobel_x = tf.constant(
        [
            [-5, -4, 0, 4, 5], 
            [-8, -10, 0, 10, 8], 
            [-10, -20, 0, 20, 10], 
            [-8, -10, 0, 10, 8], 
            [-5, -4, 0, 4, 5]
        ], dtype=dtype )
    #create the missing dims.
    sobel_x = tf.reshape(sobel_x, (5, 5, 1, 1))

    print(tf.shape(sobel_x))
    #tile the last 2 axis to get the expected dims.
    sobel_x = tf.tile(sobel_x, (1, 1, shape[-2],shape[-1]))

    print(tf.shape(sobel_x))
    return sobel_x

x1 = keras.layers.Input((128, 128, 3))

cvl = keras.layers.Conv2D(30, kernel_size=(5,5), kernel_initializer=kernelInitializer, strides=(2,2), activation='relu')

model = keras.Sequential();
model.add(x1)
model.add(cvl)

data = numpy.ones((1, 128, 128, 3))
data[:, 0:64, 0:64, :] = 0

pd = model.predict(data)
print(pd.shape)

d = pd[0, :, :, 0]
for row in d:
    for col in row:
        m = '0'
        if col != 0:
            m = 'X'
        print(m, end="")
    print("")

我考虑过使用 expand_dims 而不是重塑,但没有任何优势。 broadcast_to 看起来很理想,但您仍然必须添加尺寸,所以我认为它并不比 tile 好。

为什么同一个滤镜有 30 个滤镜?以后会改吗?