如何从形状张量(?,1152,8)可视化图像?

时间:2019-02-20 14:23:59

标签: python-3.x tensorflow

我正在尝试可视化胶囊网络层。以下是图层:

conv_layer1=tflearn.layers.conv.conv_2d(input_layer, nb_filter=256, filter_size=9, strides=[1,1,1,1],
                                    padding='same', activation='relu', regularizer="L2", name='conv_layer_1')
conv_layer2=tflearn.layers.conv.conv_2d(conv_layer1, nb_filter=256, filter_size=9, strides=[1,2,2,1],
                                    padding='same', activation='relu', regularizer="L2", name='conv_layer_2')
conv_layer3=tf.reshape(conv_layer2,[-1,1152,8], name='conv_layer3')

每层的形状如下:

layer_1: (?, 50, 50, 256)
layer_2: (?, 25, 25, 256)
layer_3: (?, 1152, 8)

在这里,我可以使用随机训练图像来可视化前两层。可视化代码如下:

image = X_train[1]
test = tf.Session()
init = tf.global_variables_initializer()

test.run(init) #(tf.global_variables_initializer())
filteredImage = test.run(conv_layer3, feed_dict{x:image.reshape(1,50,50,3)})
for i in range(64):
    plt.imshow(filteredImage[:,:,:,i].reshape(-1,25))
    plt.title('filter{}'.format(i))
    plt.show()

在这里,为了可视化第三层,出现以下错误:

InvalidArgumentError: Input to reshape is a tensor with 160000 values, but the requested shape requires a multiple of 9216
 [[node conv_layer3_9 (defined at <ipython-input-36-fd98b9e18bda>:20)  = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](conv_layer_2_11/Relu, conv_layer3_9/shape)]]

如何克服这一点并可视化第3层?

1 个答案:

答案 0 :(得分:1)

问题出在定义第三层conv_layer3=tf.reshape(conv_layer2,[-1,1152,8], name='conv_layer3')的行上,该层conv_layer2的输入的形状为?,25x25x256,在输入160000上给出了{错误,您想将其重塑为?, 1152x8的形状,从而得到9216。为了使重塑工作正常,第一个应该是第二个的倍数。