Main Content

Visualize Features of a Convolutional Neural Network

This example shows how to visualize the features learned by convolutional neural networks.

Convolutional neural networks use特征至classify images. The network learns these features itself during the training process. What the network learns during training is sometimes unclear. However, you can use thedeepDreamImage函数f形象化eatures learned.

convolutionallayers output a 3D activation volume, where slices along the third dimension correspond to a single filter applied to the layer input. The channels output by完全连接网络末端的层对应于早期层学到的功能的高级组合。

您可以通过使用可视化学到的功能的外观deepDreamImage生成强烈激活网络层特定通道的图像。

这example requires Deep Learning Toolbox™ and Deep Learning Toolbox Model对于Googlenet网络support package.

Load Pretrained Network

Load a pretrained GoogLeNet network.

net = googlenet;

可视化早期卷积层

Googlenet网络中有多个卷积层。网络开头的卷积层具有较小的接收场大小,并学习了较小的低级特征。网络末端的层具有更大的接收场大小并学习更大的功能。

使用analyzeNetwork,,,,view the network architecture and locate the convolutional layers.

analyzeNetwork(net)

Features on Convolutional Layer 1

layer成为第一个卷积层。该层是网络中的第二层,被命名'conv1-7x7_s2'

层= 2;名称= net.layers(layer).Name
name = 'conv1-7x7_s2'

可视化使用此层学到的前36个功能deepDreamImageby setting频道成为指数的向量1:36。放'PyramidLevels'到1,以使图像不缩放。要一起显示图像,您可以使用imtile

deepDreamImageuses a compatible GPU, by default, if available. Otherwise it uses the CPU. Using a GPU requires Parallel Computing Toolbox™ and a supported GPU device. For information on supported devices, seeGPU Support by Release(并行计算工具箱)

频道= 1:36; I = deepDreamImage(net,name,channels,...'PyramidLevels',,,,1);
| ========================================== ||迭代|激活|金字塔水平|||力量||| ========================================== | | 1 | 0.26 | 1 | | 2 | 6.99 | 1 | | 3 | 14.24 | 1 | | 4 | 21.49 | 1 | | 5 | 28.74 | 1 | | 6 | 35.99 | 1 | | 7 | 43.24 | 1 | | 8 | 50.50 | 1 | | 9 | 57.75 | 1 | | 10 | 65.00 | 1 | |==============================================|
图i = imtile(i,'ThumbnailSize',[64 64]);imshow(I) title(['层 ',姓名,' 特征'],'Interpreter',,,,'没有任何'

这些图像主要包含边缘和颜色,这表明过滤器在层'conv1-7x7_s2'是边缘探测器和颜色过滤器。

Features on Convolutional Layer 2

第二个卷积层被命名'conv2-3x3_reduce',对应于第6层。可视化通过设置该层学到的前36个功能频道成为指数的向量1:36

To suppress detailed output on the optimization process, set'Verbose''错误的'in the call to深梦图。

层= 6;名称= net.layers(layer).Name
名称='conv2-3x3_reduce'
频道= 1:36; I = deepDreamImage(net,name,channels,...'Verbose',,,,false,...'PyramidLevels',,,,1); figure I = imtile(I,'ThumbnailSize',[64 64]);imshow(i)name = net.layers(layer).name;标题(['层 ',姓名,' 特征'],'Interpreter',,,,'没有任何'

Filters for this layer detect more complex patterns than the first convolutional layer.

可视化更深的卷积层

这deeper layers learn high-level combinations of features learned by the earlier layers.

增加金字塔水平和每个金字塔水平的迭代次数可以产生更详细的图像,但以其他计算为代价。您可以使用'NumIterations'选择并使用'PyramidLevels' option.

layer = 97; name = net.Layers(layer).Name
名称='inception_4e-1x1'
频道= 1:6; I = deepDreamImage(net,name,channels,...'Verbose',,,,false,..."NumIterations",20,...'PyramidLevels',2);图i = imtile(i,'ThumbnailSize',[250 250]);imshow(i)name = net.layers(layer).name;标题(['层 ',姓名,' 特征'],'Interpreter',,,,'没有任何'

Notice that the layers which are deeper into the network yield more detailed filters which have learned complex patterns and textures.

可视化完全连接的层

To produce images that resemble each class the most closely, select the fully connected layer, and set频道至be the indices of the classes.

选择完全连接的层(第142层)。

层= 142;名称= net.layers(layer).Name
名称='Loss3分类器'

通过设置选择要可视化的类频道至be the indices of those class names.

频道= [114 293 341 484 563 950];

这些课程存储在课程输出层的属性(最后一层)。您可以通过选择中的条目来查看所选类的名称频道

net.layers(end).classes(频道)
ans =6×1分类snail tiger zebra castle fountain strawberry

Generate detailed images that strongly activate these classes. Set'NumIterations'到100deepDreamImage产生更详细的图像。从完全连接的图层生成的图像对应于图像类。

I = deepDreamImage(net,name,channels,...'Verbose',,,,false,...'NumIterations',100,...'PyramidLevels',2);图i = imtile(i,'ThumbnailSize',[250 250]);imshow(i)name = net.layers(layer).name;标题(['层 ',姓名,' 特征'])

图像生成的图像强烈激活所选类。为“斑马”类生成的图像包含不同的斑马条纹,而为“城堡”类生成的图像包含炮塔和窗户。

也可以看看

||||

Related Topics