https://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/5.4-visualizing-what-con

1 篇文章 0 订阅

对于https://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/5.4-visualizing-what-convnets-learn.ipynb中的代码的解析

grads = K.gradients(loss, model.input)[0]

K.gradients作用是Returns the gradients of loss w.r.t. variables,即返回loss关于model.input的梯度。K.gradients返回的tensor的shape和第二个参数model.input相同。
梯度的方向是函数在给定点上升最快的方向,那么梯度的反方向就是函数在给定点下降最快的方向

# We start from a gray image with some noise
input_img_data = np.random.random((1, 150, 150, 3)) * 20 + 128.

# Run gradient ascent for 40 steps
step = 1.  # this is the magnitude of each gradient update
for i in range(40):
    # Compute the loss value and gradient value
    loss_value, grads_value = iterate([input_img_data])
    # Here we adjust the input image in the direction that maximizes the loss
    input_img_data += grads_value * step # 这里用的是+,所以是梯度上升;防止用减法就是梯度下降,参考https://www.cnblogs.com/HongjianChen/p/8718988.html

Visualizing convnet filters模块中

,作者做的事情就是利用梯度上升算法来变化一个初始为np.zeros((1, 150, 150, 3))的输入,使经过ImageNet预训练的VGG16的"block3_conv1"层的过滤器0的平均值最小

这里面讲了一个挺有意思的结论:

convnet中的每一层都只是学习一组过滤器,使得它们的输入可以表示为过滤器的组合。 这类似于傅立叶变换如何将信号分解为余弦函数组。 随着我们在模型中走得更高,这些过滤器组中的过滤器变得越来越复杂和精致:

  • 模型中第一层的过滤器(block1_conv1)编码简单的方向边和颜色(或某些情况下的彩色边)。
  • block2_conv1中的过滤器编码由边和颜色组合构成的简单纹理。
  • 较高层中的过滤器开始类似于自然图像中的纹理:羽毛,眼睛,树叶等。

Visualizing heatmaps of class activation模块


# This is the "african elephant" entry in the prediction vector
african_elephant_output = model.output[:, 386]

# The is the output feature map of the `block5_conv3` layer,
# the last convolutional layer in VGG16
last_conv_layer = model.get_layer('block5_conv3')

# This is the gradient of the "african elephant" class with regard to
# the output feature map of `block5_conv3`
grads = K.gradients(african_elephant_output, last_conv_layer.output)[0]

其中african_elephant_output的维度为(?,)
last_conv_layer.output的维度为(?, 14, 14, 512)
grads的维度为(?, 14, 14, 512)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值