Why am I getting only one channeled-output through

2019-08-03 08:32发布

问题:

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()

I am just trying to use convolution and apply some blur effect initially. Yeah I know that my kernel values aren't right. But my question is, I am giving an input image that has 3 channels. How could I get an output image of 3 channels. Well. I tried. But all I get is some one channeled values alone.

回答1:

You are passing a kernel of shape [3, 3, 3, 1] to tf.nn.conv2d(). If you want to get a 3-channel image output from your convolution, the fourth dimension of your kernel (called out_channels in the official documentation) should be 3 instead of 1; [3, 3, 3, 3] for example.

You could also take a look at the conv2d documentation, this question and this question to better understand Tensorflow's conv2d method.