为什么我只能通过 tf.nn.conv2d 获得一个通道输出?

Why am I getting only one channeled-output through the tf.nn.conv2d?

import tensorflow as tf 
import numpy as np 
import matplotlib.pyplot as plt 
from scipy.misc import imread

img = imread('dog2.jpg')
#img is a shape of (360, 480, 3)

w =  img.shape[0]
h =  img.shape[1]
c =  img.shape[2]
k = 3 # for my convenience 

plt.subplot(1,2,1)
plt.imshow(img)
img = tf.cast(img, tf.float32)
img4d = tf.reshape(img,[1,w,h,c])
diag = np.array([[1,1,1],[0,0,0],[1,1,1]]*k, np.float32)
# diag = np.diag(diag)
diag4d = tf.reshape(diag,[k,k,c,1])
convolved = tf.nn.conv2d(img4d, diag4d, strides=[1,1,1,1], padding='SAME')

with tf.Session() as sess:
    result = sess.run(convolved)
    print result.shape
    plt.subplot(1,2,2)
    plt.imshow(np.squeeze(result))
    plt.show()

我只是尝试使用卷积并最初应用一些模糊效果。是的,我知道我的内核值不正确。但我的问题是,我给出了一个具有 3 个通道的输入图像。我怎样才能得到 3 个通道的输出图像。出色地。我试过。但我得到的只是一些单独的渠道价值。

您正在将形状为 [3, 3, 3, 1] 的内核传递给 tf.nn.conv2d()。如果您想从卷积中获得 3 通道图像输出,则内核的第四维(在 official documentation 中称为 out_channels)应该是 3 而不是 1 ; [3, 3, 3, 3] 例如。

您还可以查看 conv2d documentation, and 以更好地理解 Tensorflow 的 conv2d 方法。