通过下面的图片来理解最大激活的概念:

在识别大象的过程中,哪些特征比较重要?
下面是一些较容易想到的特征。
这就是人类凭直觉判别大象的方式。但是,使用卷积神经网络优化随机图像,并尝试将其归类为大象时,会得到什么结果呢?
卷积神经网络中,每个卷积层都在前一层的输出中寻找相似的模式。当输入包含其正在寻找的模式时,就能实现最大激活。
在激活最大化技术中,更新每一层的输入,使该过程中的损失达到最小值。
应该怎么做呢?首先需要计算激活损失相对于输入的梯度,并据此更新输入。

以下为所述方法的代码:
- #importing the required modules
- from vis.visualization import visualize_activation
- from vis.utils import utils
- from keras import activations
- from keras import applications
- import matplotlib.pyplot as plt
- %matplotlib inline
- plt.rcParams['figure.figsize'] = (18,6)
- #creating a VGG16 model using fully connected layers also because then we can
- #visualize the patterns for individual category
- from keras.applications import VGG16
- model = VGG16(weights='imagenet',include_top=True)
-
- #finding out the layer index using layer name
- #the find_layer_idx function accepts the model and name of layer as parameters and return the index of respective layer
- layer_idx = utils.find_layer_idx(model,'predictions')
- #changing the activation of the layer to linear
- model.layers[layer_idx].activation = activations.linear
- #applying modifications to the model
- model = utils.apply_modifications(model)
- #Indian elephant
- img3 = visualize_activation(model,layer_idx,filter_indices=385,max_iter=5000,verbose=True)
- plt.imshow(img3)
(编辑:西安站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|