玖叶教程网

前端编程开发入门

MATLAB环境下基于AlexNet网络的Deep Dream图像生成

本文主要讲解如何使用预训练的卷积神经网络AlexNet生成Deep Dream图像

2015年Google发布了一个比较有意思的东西,Deep Dream,关于Deep Dream,可参考查看如下文章

Sherlock:一文理解Deep Dream

Google 开发的 Deep Dream 是如何用算法作画的?

Deep Dream是深度学习的一种特征可视化技术,能够生成强烈激活网络层的图像。通过可视化Deep Dream图像,可以突出显示深层网络学习到的图像特征,这些图像特征对于理解和调试网络非常有用

本文使用MATLAB的Neural Network Toolbox?, Neural Network Toolbox Model for AlexNet Network 来生成Deep Dream图像。

加载预训练网络

加载预训练的AlexNet 网络。如果Neural Network Toolbox Model _for AlexNet Network_没有安装,则MATLAB会提供下载链接。

net = alexnet;

生成图像

选择最后一个全连接层。首先查看网络层

net.Layers

ans =

25x1 Layer array with layers:

1 'data' Image Input 227x227x3 images with 'zerocenter' normalization
2 'conv1' Convolution 96 11x11x3 convolutions with stride [4 4] and padding [0 0 0 0]
3 'relu1' ReLU ReLU
4 'norm1' Cross Channel Normalization cross channel normalization with 5 channels per element
5 'pool1' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0]
6 'conv2' Convolution 256 5x5x48 convolutions with stride [1 1] and padding [2 2 2 2]
7 'relu2' ReLU ReLU
8 'norm2' Cross Channel Normalization cross channel normalization with 5 channels per element
9 'pool2' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0]
10 'conv3' Convolution 384 3x3x256 convolutions with stride [1 1] and padding [1 1 1 1]
11 'relu3' ReLU ReLU
12 'conv4' Convolution 384 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1]
13 'relu4' ReLU ReLU
14 'conv5' Convolution 256 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1]
15 'relu5' ReLU ReLU
16 'pool5' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0]
17 'fc6' Fully Connected 4096 fully connected layer
18 'relu6' ReLU ReLU
19 'drop6' Dropout 50% dropout
20 'fc7' Fully Connected 4096 fully connected layer
21 'relu7' ReLU ReLU
22 'drop7' Dropout 50% dropout
23 'fc8' Fully Connected 1000 fully connected layer
24 'prob' Softmax softmax
25 'output' Classification Output crossentropyex with 'tench', 'goldfish', and 998 other classes

全连接层为第23层

layer = 23;

通过设置channels选择要可视化的类

channels = [9 188 231 563 855 975];

类名存储在输出层的ClassNames属性中

net.Layers(end).ClassNames(channels)

ans =

  6×1 cell array

    {'hen'              }
    {'Yorkshire terrier'}
    {'Shetland sheepdog'}
    {'fountain'         }
    {'theater curtain'  }
    {'geyser'           }

使用deepDreamImage函数生成图像

I = deepDreamImage(net,layer,channels);

显示图像

figure
montage(I)

生成更加细节的图像

增加所谓的“金字塔级别”(pyramid levels)的数量和每个pyramid levels的迭代次数可以生成更加细节的图像。

将迭代次数设置为100

iterations = 100;

强烈激活“hen”类别(通道 9)

设置pyramid levels为4

levels = 4;

强烈激活“pot”类(通道 739)。

详细代码

https://mianbaoduo.com/o/bread/Y5malZxp

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言