主要内容

从预训练的Keras层组装网络

这个示例展示了如何从预训练的Keras网络导入层,用自定义层替换不支持的层,并将这些层组装成一个网络,以便进行预测。万博1manbetx

导入Keras网络

从Keras网络模型导入层。美国的网络“digitsDAGnetwithnoise.h5”对数字图像进行分类。

文件名=“digitsDAGnetwithnoise.h5”;lgraph = importKerasLayers(文件名,“ImportWeights”,真正的);
警告:无法导入一些Keras层,因为深度学习工具箱不支持它们。万博1manbetx它们已被占位符层所取代。为了找到这些层,在返回的对象上调用findPlaceholderLayers函数。

Keras网络包含一些深度学习工具箱不支持的层。万博1manbetx的importKerasLayers函数显示警告并将不支持的层替换为占位符层。万博1manbetx

绘制层图使用情节

图表(lgraph)标题(“进口网络”

图中包含一个轴对象。标题为Imported Network的axes对象包含一个graphplot类型的对象。

替换占位符图层

要替换占位符层,首先确定要替换的层的名称。找到占位符层使用findPlaceholderLayers

placeholderLayers = findPlaceholderLayers(lgraph)
1' gaussian_noise_1' PLACEHOLDER LAYER 'GaussianNoise' Keras LAYER 2' gaussian_noise_2' PLACEHOLDER LAYER 'GaussianNoise' Keras层的占位符

显示这些层的Keras配置。

placeholderLayers。KerasConfiguration
ans =带字段的结构:可训练:1名:'gaussian_noise_1' stddev: 1.5000
ans =带字段的结构:可训练:1名:'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 =替换层(lgraph,“gaussian_noise_1”, gnLayer1);lgraph =替换层(lgraph,“gaussian_noise_2”, gnLayer2);

绘制更新后的图层图情节

图表(lgraph)标题(“替换层的网络”

图中包含一个轴对象。标题为Network with replace Layers的axes对象包含一个graphplot类型的对象。

指定类名

如果导入的分类层不包含类,则必须在预测之前指定这些类。如果您没有指定类,那么软件将自动将类设置为12、……N,在那里N是类的数量。

属性来查找分类层的索引层图的属性。

lgraph。层
ans = 15x1带有图层的图层数组:1“input_1”图像输入28 x28x1图片2的conv2d_1卷积20 7 x7x1旋转步[1]和填充“相同”3“conv2d_1_relu”ReLU ReLU 4 conv2d_2的卷积20 3 x3x1旋转步[1]和填充“相同”5“conv2d_2_relu”ReLU ReLU 6 new_gaussian_noise_1高斯噪声的高斯噪声标准差为1.5 7 new_gaussian_noise_2高斯噪声的高斯噪声标准差为0.7 8“max_pooling2d_1”马克斯池2 x2马克斯2[2]和池与进步padding 'same' 9 'max_pooling2d_2' Max Pooling 2x2 Max Pooling with stride[2 2]和padding 'same' 10 ' flat_1 ' Keras Flatten Flatten activation into 1-D假设C-style (row-major) order 11 ' flat_2 ' Keras Flatten Flatten activation into 1-D假设C-style (row-major) order 12 'concatenate_1' Depth concatenate_1' 2个输入的深度连接13 'dense_1' Fully Connected 10 Fully Connected layer 14 'activation_1' Softmax Softmax 15 'ClassificationLayer_activation_1' Classification输出crossentropyex

分类层有名称“ClassificationLayer_activation_1”.查看分类层,检查财产。

cLayer = lgraph.Layers(end)
cLayer = ClassificationOutputLayer与属性:名称:'ClassificationLayer_activation_1'类:'auto' ClassWeights: 'none' OutputSize: 'auto'超参数LossFunction: 'crossentropyex'

因为层的属性为“汽车”时,必须手动指定类。将类设置为01、……9,然后将导入的分类层替换为新的分类层。

粘土。class = string(0:9)
cLayer = ClassificationOutputLayer与属性:名称:'ClassificationLayer_activation_1'类:[0 1 2 3 4 5 6 7 8 9]ClassWeights: 'none' OutputSize: 10超参数LossFunction: 'crossentropyex'
lgraph =替换层(lgraph,“ClassificationLayer_activation_1”、粘土);

组装网络

使用组合图层图assembleNetwork.函数返回一个DAGNetwork对象,该对象已准备用于预测。

net =汇编网络(lgraph)
net = DAGNetwork with properties: Layers: [15x1 nnet.cnn.layer.Layer] Connections: [15x2 table] InputNames: {'input_1'} OutputNames: {'ClassificationLayer_activation_1'}

另请参阅

|||||||

相关的话题