【问题标题】:Why am I getting only one channeled-output through the tf.nn.conv2d?为什么我只能通过 tf.nn.conv2d 获得一个通道输出?
【发布时间】:2017-02-28 11:37:46
【问题描述】:
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 个通道的输出图像。好。我试过。但我得到的只是一些单独的引导值。

【问题讨论】:

    标签: machine-learning tensorflow convolution


    【解决方案1】:

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

    您还可以查看conv2d documentationthis questionthis question 以更好地了解Tensorflow 的conv2d 方法。

    【讨论】:

    • 如果我的回答对你有用,请考虑接受。 Here's 怎么做。
    猜你喜欢
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 2017-04-30
    相关资源
    最近更新 更多