问题描述
我有使用Tensorflow后端的Keras训练的CNN模型.我想通过以下教程可视化我的CNN过滤器: https ://blog.keras.io/how-convolutional-neural-networks-see-the-world.html
I have CNN models trained using Keras with Tensorflow backend.And I want to visualize my CNN filters with this tutorial: https://blog.keras.io/how-convolutional-neural-networks-see-the-world.html
from keras import backend as K
from keras.models import load_model
import numpy as np
model = load_model('my_cnn_model.h5')
input_img = np.load('my_picture.npy')
# get the symbolic outputs of each "key" layer (we gave them unique names).
layer_dict = dict([(layer.name, layer) for layer in model.layers])
layer_name = 'block5_conv3'
filter_index = 0 # can be any integer from 0 to 511, as there are 512 filters in that layer
# build a loss function that maximizes the activation
# of the nth filter of the layer considered
layer_output = layer_dict[layer_name].output
loss = K.mean(layer_output[:, :, :, filter_index])
# compute the gradient of the input picture wrt this loss
grads = K.gradients(loss, input_img)[0]
# normalization trick: we normalize the gradient
grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
# this function returns the loss and grads given the input picture
iterate = K.function([input_img], [loss, grads])
但是,当代码执行到此行时:grads = K.gradients(loss, input_img)[0]
我发现它只返回None
对象,因此该程序无法继续执行.
However, when the code execute to this line:grads = K.gradients(loss, input_img)[0]
I found it returns nothing but None
object, so the program fail to progress after that.
我正在寻找解决方案.有人说input_img
应该是tensorflow的Tensor类型: https://github.com/keras-team/keras/issues/5455
I search for some solution. Some people say theinput_img
should be tensorflow's Tensor type:https://github.com/keras-team/keras/issues/5455
但是当我尝试将img转换为Tensor时,问题仍然存在.
我在上面的链接中尝试了该解决方案,但仍然失败.
But when I tried to convert the img to Tensor, the problem is still exist.
I tried the solution in the link above, but still fail.
也有人说存在此问题,因为您的CNN模型不可区分. https://github.com/keras-team/keras/issues/8478
There is also someone say that this problem exists because your CNN model is not differentiable.https://github.com/keras-team/keras/issues/8478
但是我的模型仅使用ReLU和Sigmoid(在输出层)的激活功能.这个问题真的是由不可微问题引起的吗?
But my model use only the activate function of ReLU and Sigmoid(at output layer).Is this problem really caused by nondifferentiable problem?
有人可以帮助我吗?非常感谢你!
Can anyone help me? Thank you very much!
推荐答案
如果您有一个Model实例,则要考虑相对于输入的损耗梯度,您应该这样做:
If you have a Model instance, then to take the gradient of the loss with respect to the input, you should do:
grads = K.gradients(loss, model.input)[0]
model.input
包含表示模型输入的符号张量.使用普通的numpy数组毫无意义,因为TensorFlow不知道如何将其连接到计算图,并返回None作为梯度.
model.input
contains the symbolic tensor that represents the input to the model. Using a plain numpy array makes no sense because TensorFlow then has no idea how this connects to the computational graph, and returns None as the gradient.
然后,您还应该将iterate
函数重写为:
Then you should also rewrite the iterate
function as:
iterate = K.function([model.input], [loss, grads])
这篇关于K.gradients(loss,input_img)[0]返回“无". (带有Tensorflow后端的Keras CNN可视化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!