import tensorflow as tfimport numpy as npimport matplotlib.pyplot as pltfrom scipy.misc import imreadimg = 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 convenienceplt.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. 解决方案 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. 这篇关于为什么我只能通过tf.nn.conv2d获得一个通道输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 11:34
查看更多