input (4,4,3)
filter (3,3,3,3)
output (4,4,3)
我正在尝试使用过滤器对输入图像进行卷积。过滤器是按层应用的(即,仅应使用当前通道中的值,而不是在整个深度上应用转化)。
我设计过滤器的方式是,只有过滤器的相应层具有该值,而其他通道为零:
enter array([[[[1., 0., 1.],
[0., 1., 0.],
[1., 0., 1.]],
[[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.]],
[[1., 0., 1.],
[0., 1., 0.],
[1., 0., 1.]],
[[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.]],
[[1., 0., 1.],
[0., 1., 0.],
[1., 0., 1.]]]], dtype=float32)code here
为了进行测试,我制作了一个3通道(4,4)数组,第一个通道全为1,其余均为零。
预期输出是第一个通道具有卷积输出,其余通道应为零。但这得到了(打印的单个图层):
enter array([[2., 2., 2., 1.],
[2., 3., 3., 2.],
[2., 3., 3., 2.],
[1., 2., 2., 2.]], dtype=float32)
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]], dtype=float32)
array([[2., 2., 2., 1.],
[2., 3., 3., 2.],
[2., 3., 3., 2.],
[1., 2., 2., 2.]], dtype=float32)
这是我的代码:
weightwithout5 =tf.constant(
[[1., 0, 1],
[0, 1, 0],
[1., 0, 1],
[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],
[1., 0., 1],
[0, 1., 0],
[1., 0., 1.],
[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],
[1, 0, 1.],
[0, 1, 0],
[1, 0, 1.]],shape=(3,3,3,3),dtype=tf.float32)
x = tf.placeholder(tf.float32, (None, 4, 4, 3))
convwithout5 = tf.nn.convolution(x,weightwithout5,strides=[1,1],padding="SAME")
in0_ = np.ones(shape=(4,4))## input with ones on channel 0 and zeros for other channels
in1_ = np.zeros(shape=(4,4))
in2_ = np.zeros(shape=(4,4))
input_ = cv2.merge([in0_, in1_, in2_])
input_1 = np.expand_dims(input_,axis=0)
print(input_1.shape)
with tf.Session() as sess:
convolved_output = sess.run([convwithout5],feed_dict={x:input_1})
print("the conv results")
print(convolved_output[0][0][:,:,0])## not sure on why i have to use extra [0] to get 4,4 matrix
print(convolved_output[0][0][:,:,1])
print(convolved_output[0][0][:,:,2])