matlab keras,基于预训练的 Keras 层组合网络

导入 Keras 网络

从 Keras 网络模型中导入层。'digitsDAGnetwithnoise.h5' 中的网络可对数字图像进行分类。

filename = 'digitsDAGnetwithnoise.h5';

lgraph = importKerasLayers(filename,'ImportWeights',true);

Warning: Unable to import some Keras layers, because they are not supported by the Deep Learning Toolbox. They have been replaced by placeholder layers. To find these layers, call the function findPlaceholderLayers on the returned object.

Keras 网络包含一些 Deep Learning Toolbox 不支持的层。importKerasLayers 函数会显示警告,并用占位符层替换不支持的层。

使用 plot 绘制层次图。

figure

plot(lgraph)

title("Imported Network")

580439aaa3c6650251148be85801fef8.png

替换占位层

要替换占位层,请首先标识要替换的层的名称。使用 findPlaceholderLayers 查找占位层。

placeholderLayers = findPlaceholderLayers(lgraph)

placeholderLayers =

2x1 PlaceholderLayer array with layers:

1 'gaussian_noise_1' PLACEHOLDER LAYER Placeholder for 'GaussianNoise' Keras layer

2 'gaussian_noise_2' PLACEHOLDER LAYER Placeholder for 'GaussianNoise' Keras layer

显示这些层的 Keras 配置。

placeholderLayers.KerasConfiguration

ans = struct with fields:

trainable: 1

name: 'gaussian_noise_1'

stddev: 1.5000

ans = struct with fields:

trainable: 1

name: 'gaussian_noise_2'

stddev: 0.7000

定义自定义高斯噪声层。要创建此层,请将文件 gaussianNoiseLayer.m 保存在当前文件夹中。然后,创建两个高斯噪声层,它们的配置与导入的 Keras 层相同。

gnLayer1 = gaussianNoiseLayer(1.5,'new_gaussian_noise_1');

gnLayer2 = gaussianNoiseLayer(0.7,'new_gaussian_noise_2');

使用 replaceLayer 将占位层替换为自定义层。

lgraph = replaceLayer(lgraph,'gaussian_noise_1',gnLayer1);

lgraph = replaceLayer(lgraph,'gaussian_noise_2',gnLayer2);

使用 plot 绘制更新后的层次图。

figure

plot(lgraph)

title("Network with Replaced Layers")

527046de35c21914280134d34cf86e0b.png

指定类名称

如果导入的分类层不包含类,则必须在进行预测之前指定这些类。如果不指定类,软件会自动将类设置为 1、2、...、N,其中 N 是类的数量。

通过查看层次图的 Layers 属性,查找分类层的索引。

lgraph.Layers

ans =

15x1 Layer array with layers:

1 'input_1' Image Input 28x28x1 images

2 'conv2d_1' Convolution 20 7x7x1 convolutions with stride [1 1] and padding 'same'

3 'conv2d_1_relu' ReLU ReLU

4 'conv2d_2' Convolution 20 3x3x1 convolutions with stride [1 1] and padding 'same'

5 'conv2d_2_relu' ReLU ReLU

6 'new_gaussian_noise_1' Gaussian Noise Gaussian noise with standard deviation 1.5

7 'new_gaussian_noise_2' Gaussian Noise Gaussian noise with standard deviation 0.7

8 'max_pooling2d_1' Max Pooling 2x2 max pooling with stride [2 2] and padding 'same'

9 'max_pooling2d_2' Max Pooling 2x2 max pooling with stride [2 2] and padding 'same'

10 'flatten_1' Keras Flatten Flatten activations into 1-D assuming C-style (row-major) order

11 'flatten_2' Keras Flatten Flatten activations into 1-D assuming C-style (row-major) order

12 'concatenate_1' Depth concatenation Depth concatenation of 2 inputs

13 'dense_1' Fully Connected 10 fully connected layer

14 'activation_1' Softmax softmax

15 'ClassificationLayer_activation_1' Classification Output crossentropyex

分类层的名称为 'ClassificationLayer_activation_1'。查看分类层并检查 Classes 属性。

cLayer = lgraph.Layers(end)

cLayer =

ClassificationOutputLayer with properties:

Name: 'ClassificationLayer_activation_1'

Classes: 'auto'

OutputSize: 'auto'

Hyperparameters

LossFunction: 'crossentropyex'

由于层的 Classes 属性为 'auto',因此您必须手动指定类。将类设置为 0、1、...、9,然后将导入的分类层替换为新层。

cLayer.Classes = string(0:9)

cLayer =

ClassificationOutputLayer with properties:

Name: 'ClassificationLayer_activation_1'

Classes: [0 1 2 3 4 5 6 7 8 9]

OutputSize: 10

Hyperparameters

LossFunction: 'crossentropyex'

lgraph = replaceLayer(lgraph,'ClassificationLayer_activation_1',cLayer);

组合网络

使用 assembleNetwork 组合层次图。此函数将返回一个可以用于预测的 DAGNetwork 对象。

net = assembleNetwork(lgraph)

net =

DAGNetwork with properties:

Layers: [15x1 nnet.cnn.layer.Layer]

Connections: [15x2 table]

InputNames: {'input_1'}

OutputNames: {'ClassificationLayer_activation_1'}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值