text.html 2.8 KB
Newer Older
ToTensor's avatar
ToTensor 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 
<p class="content_105">“好啦,本次课程到此就结束了。”咖哥说。</p> 
<p class="content_105">“等等,咖哥。”小冰喊道,“这么早下课?”</p> 
<p class="content_105">“嗯?小冰,你还有什么疑问?”咖哥回答。</p> 
<p class="content_105">小冰继续说:“是这样的,卷积网络,处理图像效果很好,但是我总觉得它是一个‘黑盒子’。所以我有点好奇,想看一看特征提取过程中,这个卷积网络里面到底发生了什么。”</p> 
<p class="content_105">咖哥说:“噢,这样啊。你这种疑惑还是挺常见的。人们也开发了几种方法来查看卷积网络的内部结构。我给你介绍一种比较简单的方法吧。通过这个叫作<span class="bold">中间激活</span>的方法,我们可以看到卷积过程中特征图的‘特征通道’。”</p> 
<p class="content">中间激活的实现代码如下:</p> 
<div class="content_106"> 
 <p class="content_105">from keras.models import load_model # 导入模型保存工具</p> 
 <p class="content_105">import matplotlib.pyplot as plt # 导入Matplotlib库</p> 
 <p class="content_105">model = load_model('../my_dog_cnn.h5')# 载入刚才保存的模型</p> 
 <p class="content_105"># 绘制特征通道</p> 
 <p class="content_105">layer_outputs = [layer.output for layer in model.layers[:16]]</p> 
 <p class="content_105">image = X_train[0]</p> 
 <p class="content_105">image = image.reshape(1, 150, 150, 3)</p> 
 <p class="content_105">activation_model = models.Model(inputs=model.input, outputs=layer_outputs)</p> 
 <p class="content_105">activations = activation_model.predict(image)</p> 
 <p class="content_105">first_layer_activation = activations[0]</p> 
 <p class="content_105">plt.matshow(first_layer_activation[0, :, :, 2], cmap='viridis')</p> 
 <p class="content_105">plt.matshow(first_layer_activation[0, :, :, 3], cmap='viridis')</p> 
</div> 
<p class="content">特征通道的示例如下图所示。</p> 
<div class="pic"> 
 <img src="http://csdn-ebook-resources.oss-cn-beijing.aliyuncs.com/images/b88b00f6ad14402ea66695d6809614da/figure-0237-0328.jpg"> 
 <p class="imgtitle">狗面部轮廓特征通道</p> 
</div> 
<div class="pic"> 
 <img src="http://csdn-ebook-resources.oss-cn-beijing.aliyuncs.com/images/b88b00f6ad14402ea66695d6809614da/figure-0237-0329.jpg"> 
 <p class="imgtitle">狗眼特征通道(抱着狗狗的人的眼睛也被激活)</p> 
</div> 
<p class="content">通过观察这些特征通道的中间激活图就能发现,卷积网络中的各个通道并不是漫无目地进行特征提取,而是各负其责,忽略不相关的噪声信息,专门聚焦于自己所负责的那部分特征,激活各个特征点。这些特征点(也就是小模式)进行组合,就实现了高效率的图像识别。</p>