张量流中的反卷积(conv2d_transpose)

时间:2017-04-16 04:34:24

标签: machine-learning tensorflow computer-vision deep-learning conv-neural-network

我有一个训练有素的网络(AlexNet)用于图像分类;目前,我正在尝试使用Zeiler提出的反卷积技术来可视化不同卷积层的特征激活。我在结果中得到以下结果:

Input shape: (1,227,227,3)
Filter shape: (11,11,3,96)
stride = (4,4)

为了获得卷积输出,我使用了以下代码:

wts = model.layers[1].get_weights()
inp = tf.constant(x_test)
ftr = tf.constant(wts[0])
b = tf.constant(wts[1])
c = tf.nn.conv2d(inp, ftr, strides = [1,4,4,1], padding='VALID')
c = c + b

sess = tf.Session()
res = sess.run(c)
print(res.shape)
sess.close()

fig=plt.figure(figsize=(12,12))
for i in range(96):
    plt.subplot(10, 10, i+1)
    img = res[0,:,:,i]
    plt.imshow(img, cmap='gray')
    plt.xticks(np.array([]))
    plt.yticks(np.array([]))
    plt.tight_layout()
plt.show()

以下是看似正确的结果 enter image description here 但是,当我尝试逆操作时,我期望得到原始图像,但我得到了别的东西。请帮助我理解为什么我没有接近原始图像。

inp = tf.constant(res)
ftr = tf.constant(wts[0])
b   = tf.constant(wts[1])
c = inp - b
c = tf.nn.conv2d_transpose(c, ftr, output_shape = [1,227,227,3], strides = [1,4,4,1], padding='VALID')

sess = tf.Session()
res = sess.run(c)
print(res.shape)
sess.close()

plt.imshow(res[0,:,:,:])
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:0)

不确定您是否已解决此问题。尝试使用:

c = tf.layers.conv2d_transpose(c, filters = 3, kernel_size = (11,11))

看看它在哪里而不是初始 tf.nn.conv2d_transpose 。 即使你已经解决了这个问题,我也很乐意反馈你的意见。

相关问题