我想使用梯度上升方法(https://blog.keras.io/how-convolutional-neural-networks-see-the-world.html)可视化我的1D-CNN的1D过滤器。下面给出的是我的模型摘要和我尝试过的代码。
Model.summary() #after removing the fully connected layers.
Layer (type) Output Shape Param #
=================================================================
conv1d_5_input (InputLayer) (None, 289, 2493) 0
_________________________________________________________________
conv1d_5 (Conv1D) (None, 131, 22) 1535710
_________________________________________________________________
batch_normalization_12 (Batc (None, 131, 22) 88
_________________________________________________________________
activation_12 (Activation) (None, 131, 22) 0
_________________________________________________________________
max_pooling1d_5 (MaxPooling1 (None, 65, 22) 0
_________________________________________________________________
dropout_14 (Dropout) (None, 65, 22) 0
_________________________________________________________________
conv1d_6 (Conv1D) (None, 30, 30) 23790
_________________________________________________________________
batch_normalization_13 (Batc (None, 30, 30) 120
_________________________________________________________________
activation_13 (Activation) (None, 30, 30) 0
_________________________________________________________________
max_pooling1d_6 (MaxPooling1 (None, 15, 30) 0
=================================================================
Total params: 1,559,708
Trainable params: 1,559,604
Non-trainable params: 104
layer_dict = dict([(layer.name, layer) for layer in model2.layers])
input_img = model2.input
layer_name = 'conv1d_5'
filter_index = 18
layer_output = layer_dict[layer_name].output
loss_fxn = K.mean(layer_output[ :, :, filter_index])
# Find gradient of loss_fxn w.r.t. the input_img
gradient = K.gradients(loss_fxn, input_img)[0]
# Normalize the gradient
gradient = gradient / (K.sqrt(K.mean(K.square(gradient))) + 1e-05)
iterate = K.function([input_img], [loss_fxn, gradient])
w = 256
h = 256
input_img_data = np.random.random((1, w, h, 3))*20 + 128 ##NOISY IMAGE
step_size = 1
steps = 100
for i in range(steps):
loss, grads = iterate([input_img_data])
input_img_data = input_img_data + grads*step_size
print("Step: {}/{}, Loss: {}".format(i+1, steps, loss))
这是我得到的错误:
InvalidArgumentError:转置期望大小为5的向量。但是 input(1)是大小为4 [[{{node conv1d_5_1 / convolution / Conv2D-0-TransposeNHWCToNCHW-LayoutOptimizer}}]]] [[{{node Mean_10}}]]
如何可视化一维滤镜?