主要内容

ImportKerasnetwork

Import pretrained Keras network and weights

描述

example

= importKerasNetwork(modelfile)进口预验证的Tensorflow™-Keras网络及其权重modelfile

此功能需要深度学习工具箱™ Converter for TensorFlow Modelssupport package. If this support package is not installed, the function provides a download link.

= importKerasNetwork(modelfile,姓名,Value)导入验证的TensorFlow-Keras网络及其权重,并提供一个或多个名称值对参数指定的其他选项。

例如,importKerasnetwork(Modelfile,'townfile',重量)imports the network from the model filemodelfile和重量文件的重量权重。在这种情况下,modelfilecan be in HDF5 or JSON format, and the weight file must be in HDF5 format.

例子

全部收缩

下载并安装用于TensorFlow模型支持软件包的深学习工具箱转换器。金宝app

类型ImportKerasnetwork在命令行。

ImportKerasnetwork

如果未安装用于TensorFlow模型支持软件包的深度学习工具箱转换器,则该功能提供了指向附加探索器中所需的支持软件包的链金宝app接。要安装支持包,请单击链接,然后金宝app单击安装。Check that the installation is successful by importing the network from the model file'digitsdagnet.h5'在命令行。如果安装了所需的支持软件包,则该金宝app功能返回dagnetwork目的。

Modelfile ='digitsdagnet.h5';网= importKerasNetwork(modelfile)
警告:保存的KERAS网络不包括类。类将设置为分类(1:n),其中n是网络分类输出层中的类数。要指定类,请使用“类”参数。
net =带有属性的dagnetwork:层:[13x1 nnet.cnn.layer.layer]连接:[13x2 table] inputNames:{'input_1'} outputnames:{'classification layer_activation_1'}

指定要导入的文件。文件digitsDAGnet.h5包含一个定向的无环形卷积神经网络,该神经网络对数字的图像进行了分类。

Modelfile ='digitsdagnet.h5';

导入网络。

网= importKerasNetwork(modelfile)
警告:保存的KERAS网络不包括类。类将设置为分类(1:n),其中n是网络分类输出层中的类数。要指定类,请使用“类”参数。
net =带有属性的dagnetwork:层:[13x1 nnet.cnn.layer.layer]连接:[13x2 table] inputNames:{'input_1'} outputnames:{'classification layer_activation_1'}

绘制网络体系结构。

情节(net)标题(“ DAG网络体系结构”)

Figure contains an axes object. The axes object with title DAG Network Architecture contains an object of type graphplot.

指定网络和重量文件以导入。

Modelfile ='digitsdagnet.json';权重='digitsDagnet.Weaters.h5';

这是一个针对数字数据训练的定向无环卷积神经网络。

导入网络体系结构并从单独的文件导入权重。.json文件没有输出层或成本函数的信息。导入文件时指定输出层类型。

网= importKerasNetwork(modelfile,“举重”,重量,...'outputlayerType','分类')
警告:保存的KERAS网络不包括类。类将设置为分类(1:n),其中n是网络分类输出层中的类数。要指定类,请使用“类”参数。
net =带有属性的dagnetwork:层:[13x1 nnet.cnn.layer.layer]连接:[13x2 table] inputNames:{'input_1'} outputnames:{'classification layer_activation_1'}

指定模型文件。

Modelfile ='digitsdagnet.h5';

指定类名。

classNames = {'0','1','2','3','4','5','6','7','8','9'};

导入带有类名的Keras网络。

网= importKerasNetwork(modelfile,“课”,classNames);

Read the image to classify.

digitdatasetpath = fullfile(toolboxDir(toolboxdir)('nnet'),'nndemos','nndatasets',“ digitdataset”);i = imread(fullfile(digitdatasetpeth,'5','image4009.png');

Classify the image using the pretrained network.

label =分类(net,i);

显示图像和分类结果。

imshow(i)标题([“分类结果:”char(标签)])

Figure contains an axes object. The axes object with title Classification result: 5 contains an object of type image.

输入Arguments

全部收缩

包含网络拱模型文件的名称itecture, and possibly the weights, specified as a character vector or a string scalar. The file must be in the current folder, in a folder on the MATLAB®路径,否则您必须包括文件的完整或相对路径。

如果modelfile包括

  • 网络体系结构和权重,然后必须在HDF5中(.h5) format.

  • 只有网络体系结构,然后可以在HDF5或JSON中(。json) format.

如果modelfile仅包括网络体系结构,然后您必须在HDF5文件中提供权重,“举重”name-value pair argument.

例子:'digitsnet.h5'

数据类型:char|细绳

姓名-Value Arguments

Specify optional comma-separated pairs of姓名,Value参数。姓名是the argument name and价值是相应的值。姓名必须出现在引号中。您可以按任何顺序指定几个名称和值对参数NAME1,Value1,...,Namen,Valuen

例子:importKerasnetwork(Modelfile,'outputLayerType',“分类”,“类”,类)从模型文件导入网络modelfile,在KERAS层的末端添加一个用于分类问题的输出层,并指定课程作为输出层的类。

姓名of file containing weights, specified as a character vector or a string scalar.举重必须在当前文件夹中,在MATLAB路径上的文件夹中,否则必须包含文件的完整或相对路径。

例子:“举重”,“ wights.h5”

当功能附加到导入网络体系结构末尾的输出层的类型modelfiledoes not specify a loss function, specified as'分类',“回归”, 或者'pixelclassification'。附加pixelClassificationLayer(Computer Vision Toolbox)对象需要计算机视觉工具箱™。

如果网络中的网络modelfilehas multiple outputs, then you cannot specify the output layer types using this argument. UseImportKeraslayers反而。ImportKeraslayers插入输出的占位符层。导入后,您可以通过使用FindplaceholderlayersreplaceLayer, respectively.

例子:“输出layertype”,“回归”

网络的输入图像的大小,指定为两个或三个数值值的向量[高度宽度]用于灰度图像和[height,width,channels]对于颜色图像。当网络使用此信息时modelfiledoes not specify the input size.

如果网络中的网络modelfile有多个输入,那么您无法使用此参数指定输入尺寸。利用ImportKeraslayers反而。ImportKeraslayers插入输入的占位符层。导入后,您可以通过使用FindplaceholderlayersreplaceLayer, respectively.

例子:“ ImageSunputsize”,[28 28]

输出层的类,指定为分类向量,字符串数组,字符向量的单元格数或'auto'。如果指定字符串阵列或字符向量的单元格数组str,然后该软件将输出层的类设置为分类(str,str)。如果课程'auto', then the function sets the classes to分类(1:n), 在哪里N是the number of classes.

数据类型:char|分类|细绳|细胞

输出参数

全部收缩

预估计的Keras网络作为以下返回:

  • 如果KERAS网络类型Sequential, then是一个SeriesNetwork目的。

  • 如果KERAS网络类型模型, then是一个dagnetwork目的。

限制

  • ImportKerasnetwork金宝app支持TensorFlow-keras版本如下:

    • 该函数完全支持TensorFlow-k金宝apperas版本最多2.2.4。

    • 该功能为TensorFlow-Keras版本2.2.5至金宝app2.4.0提供了有限的支持。

More About

全部收缩

Supported Keras Layers

ImportKerasnetworksupports the following TensorFlow-Keras layer types for conversion into built-in MATLAB layers, with some limitations.

TensorFlow-Keras Layer Corresponding Deep Learning Toolbox Layer
Add 加法器

激活,具有激活名称:

  • 'elu'

  • 'relu'

  • “线性”

  • 'SoftMax'

  • 'sigmoid'

  • 'swish'

  • 'tanh'

层:

高级激活:

  • ELU

  • SoftMax

  • relu

  • Leakyyrelu

  • prelu*

层:

AveragePooling1D 平均pooling1dlayerwithPaddingValuespecified as'意思是'
AveragePooling2D 平均泵2DlayerwithPaddingValuespecified as'意思是'
批处理规范化 batchnormalizationlayer
双向(LSTM(__)) Bilstmlayer
加入 depthConcatenationLayer
Conv1D 卷积1Dlayer
Conv2D 卷积2Dlayer
conv2dtranspose transposedconv2dlayer
cudnngru gruLayer
cudnnlstm lstmlayer
Dense fullyConnectedLayer
DepthwiseConv2D groupedConvolution2dLayer
退出 dropoutlayer
Embedding Wordembeddinglayer(文本分析工具箱)
扁平 nnet.keras.layer.flattencstylayer
globalaveragePooling1d globalAveragePooling1dLayer
globalageraperpooling2d globalaveragePooling2Dlayer
globalmaxpool1d globalMaxPooling1dLayer
globalmaxpool2d GlobalMaxPooling2Dlayer
GRU gruLayer
输入 ImageInputlayer,sequenceInputlayer, 或者featureinputlayer
LSTM lstmlayer
maxpool1d maxPooling1dLayer
maxpool2d maxpooling2dlayer
Multiply 乘法层
SeparableConv2D groupedConvolution2dLayer或者卷积2Dlayer
TimeDistributed sequenceFoldingLayerbefore the wrapped layer, andsequenceUnfoldingLayer包裹层之后
UpSampling2D resize2dlayer(图像处理工具箱)
UpSampling3D resize3dlayer(图像处理工具箱)
ZeroPadding1D nnet.keras.layer.zeropadding1dlayer
zeropadding2d nnet.keras.layer.zeropadding2dlayer

* For a PReLU layer,ImportKerasnetwork用向量元素的平均值代替矢量值缩放参数。导入后,您可以将参数更改回向量。例如,请参阅导入keras prelu层

金宝app支持的Keras损失功能

ImportKerasnetwork金宝app支持以下凯拉斯损失功能:

  • Mean_squared_error

  • 分类_crossentropy

  • Sparse_Categorical_CrossentRopy

  • binary_crossentropy

在GPU上使用导入网络

ImportKerasnetwork不在GPU上执行。然而,ImportKerasnetwork进口一个预处理的神经网络,以深入学习dagnetwork或者SeriesNetwork对象,您可以在GPU上使用。

  • 您可以通过使用CPU或GPU的导入网络进行预测分类。使用名称值参数指定硬件要求执行环境。对于具有多个输出的网络,请使用预测功能。

  • 您可以通过使用CPU或GPU的导入网络进行预测预测。使用名称值参数指定硬件要求执行环境。如果网络有多个输出,请指定名称值参数返回类别as真的

  • 您可以使用CPU或GPU上的导入网络训练trainNetwork。To specify training options, including options for the execution environment, use the训练功能。使用名称值参数指定硬件要求执行环境。有关如何加速培训的更多信息,请参阅并行扩展深度学习,在GPU和云中扩展

使用GPU需要并行计算工具箱™和支持的GPU设备。金宝app有关支持设备的信息,请参阅金宝app释放的G金宝appPU支持(并行计算工具箱)

提示

  • 如果网络包含一个图层深度学习工具箱转换器用于张量流模型不支持金宝appSupported Keras Layers), 然后ImportKerasnetwork返回错误消息。在这种情况下,您仍然可以使用ImportKeraslayers导入网络体系结构和权重。

  • 您可以导入具有多个输入和多个输出(MIMO)的KERAS网络。利用ImportKerasnetwork如果网络包括输入大小信息,以获取输入和输出的损失信息。否则,请使用ImportKeraslayers。这ImportKeraslayersfunction inserts placeholder layers for the inputs and outputs. After importing, you can find and replace the placeholder layers by usingFindplaceholderlayersreplaceLayer, respectively. The workflow for importing MIMO Keras networks is the same as the workflow for importing MIMO ONNX™ networks. For an example, see导入和组装具有多个输出的ONNX网络。要了解具有多个输入和多个输出的深度学习网络,请参见多输入和多输出网络

  • To use a pretrained network for prediction or transfer learning on new images, you must preprocess your images in the same way the images that were used to train the imported model were preprocessed. The most common preprocessing steps are resizing images, subtracting image average values, and converting the images from BGR images to RGB.

    • 要调整图像大小,请使用精加工。例如,Imresize(图像,[227,227,3])

    • 要将图像从RGB转换为BGR格式,请使用flip。例如,翻转(图像,3)

    有关用于培训和预测的预处理图像的更多信息,请参见Preprocess Images for Deep Learning

替代功能

  • 利用ImportKerasnetwork或者ImportKeraslayersto import a TensorFlow-Keras network in HDF5 or JSON format. If the TensorFlow network is in the saved model format, useImportTensorFlownetwork或者ImportTensorFlowlayers

  • 如果导入自定义TensorFlow-keras层或软件无法将TensorFlow-Keras层转换为等效的内置MATLAB层,则可以使用ImportTensorFlownetwork或者ImportTensorFlowlayers,尝试生成自定义层。例如,ImportTensorFlownetworkImportTensorFlowlayers导入TensorFlow-keras时生成自定义层Lambda层。

兼容性考虑

展开全部

不建议从R2018B开始

参考

[1]Keras:Python深度学习库https://keras.io

Introduced in R2017b