| 
                         上述代码的输出结果如下所示,包含了block5_conv1层的不同参数: 
- {'name': 'block5_conv1', 
 -  'trainable': True, 
 -  'filters': 512, 
 -  'kernel_size': (3, 3), 
 -  'strides': (1, 1), 
 -  'padding': 'same', 
 -  'data_format': 'channels_last', 
 -  'dilation_rate': (1, 1), 
 -  'activation': 'relu', 
 -  'use_bias': True, 
 -  'kernel_initializer': {'class_name': 'VarianceScaling', 
 -   'config': {'scale': 1.0, 
 -    'mode': 'fan_avg', 
 -    'distribution': 'uniform', 
 -    'seed': None}}, 
 -  'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 
 -  'kernel_regularizer': None, 
 -  'bias_regularizer': None, 
 -  'activity_regularizer': None, 
 -  'kernel_constraint': None, 
 -  'bias_constraint': None} 
 
  
’block5_conv1’层的可训练参数值是真实的,这意味着之后可以通过进一步模型训练来更新权重。 
过滤器——卷积神经网络构件的可视化 
过滤器是卷积神经网络的基本组成部分。如下图所示,不同的过滤器会从图像中提取不同类型的特征: 
  
如图所示,每个卷积层都由多个过滤器组成。回顾上一节中提到的‘block5_conv1’层的参数概要显示了该层含有512个过滤器,确实是这个道理。 
通过下列编码,可以绘制每VGG16模块的第一个卷积层的首个过滤器: 
- layers = model.layers 
 - layer_ids = [1,4,7,11,15] 
 - #plot the filters 
 - fig,ax = plt.subplots(nrows=1,ncols=5) 
 - for i in range(5): 
 -     ax[i].imshow(layers[layer_ids[i]].get_weights()[0][:,:,:,0][:,:,0],cmap='gray') 
 -     ax[i].set_title('block'+str(i+1)) 
 -     ax[i].set_xticks([]) 
 -     ax[i].set_yticks([]) 
 
  
  
以上输出结果即为不同层的过滤器。由于VGG16只使用3×3过滤器,因此所有过滤器形状大小都相同。 
激活最大化——将模型所期望的进行可视化 
通过下面的图片来理解最大激活的概念: 
  
在识别大象的过程中,哪些特征比较重要? 
下面是一些较容易想到的特征。 
这就是人类凭直觉判别大象的方式。但是,使用卷积神经网络优化随机图像,并尝试将其归类为大象时,会得到什么结果呢? 
卷积神经网络中,每个卷积层都在前一层的输出中寻找相似的模式。当输入包含其正在寻找的模式时,就能实现最大激活。 
在激活最大化技术中,更新每一层的输入,使该过程中的损失达到最小值。 
应该怎么做呢?首先需要计算激活损失相对于输入的梯度,并据此更新输入。 
  
以下为所述方法的代码: 
- #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) 
 
                          (编辑:91站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |