importKerasLayers - (To be removed) Import layers from Keras network - MATLAB (original) (raw)
(To be removed) Import layers from Keras network
Syntax
Description
[layers](#mw%5F3d015d1d-ae71-40d6-ad1e-d402f52b2273) = importKerasLayers([modelfile](#d126e136001))
imports the layers of a TensorFlow™-Keras network from a model file. The function returns the layers defined in the HDF5 (.h5
) or JSON (.json
) file given by the file name modelfile
.
This function requires the Deep Learning Toolbox™ Converter for TensorFlow Models support package. If this support package is not installed, then the function provides a download link.
[layers](#mw%5F3d015d1d-ae71-40d6-ad1e-d402f52b2273) = importKerasLayers([modelfile](#d126e136001),[Name,Value](#namevaluepairarguments))
imports the layers from a TensorFlow-Keras network with additional options specified by one or more name-value pair arguments.
For example, importKerasLayers(modelfile,'ImportWeights',true)
imports the network layers and the weights from the model filemodelfile
.
Examples
Download and install the Deep Learning Toolbox Converter for TensorFlow Models support package.
Type importKerasLayers
at the command line.
If the Deep Learning Toolbox Converter for TensorFlow Models support package is not installed, then the function provides a link to the required support package in the Add-On Explorer. To install the support package, click the link, and then click Install. Check that the installation is successful by importing the layers from the model file 'digitsDAGnet.h5'
at the command line. If the required support package is installed, then the function returns a LayerGraph
object.
modelfile = 'digitsDAGnet.h5'; net = importKerasLayers(modelfile)
Warning: "importKerasLayers" is not recommended and will be removed in a future release. To import TensorFlow-Keras models, save using the SavedModel format and use importNetworkFromTensorFlow function.
net = LayerGraph with properties:
InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
Layers: [13×1 nnet.cnn.layer.Layer]
Connections: [13×2 table]
Import the network layers from the model file digitsDAGnet.h5
.
modelfile = 'digitsDAGnet.h5'; layers = importKerasLayers(modelfile)
layers = LayerGraph with properties:
Layers: [13×1 nnet.cnn.layer.Layer]
Connections: [13×2 table]
InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
Plot the network architecture.
Specify the network file to import.
modelfile = 'digitsDAGnet.h5';
Import network layers.
layers = importKerasLayers(modelfile)
layers = LayerGraph with properties:
Layers: [13×1 nnet.cnn.layer.Layer]
Connections: [13×2 table]
InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
Load a data set for training a classifier to recognize new digits.
folder = fullfile(toolboxdir('nnet'),'nndemos','nndatasets','DigitDataset'); imds = imageDatastore(folder, ... 'IncludeSubfolders',true, ... 'LabelSource','foldernames');
Partition the dataset into training and test sets.
numTrainFiles = 750; [imdsTrain,imdsTest] = splitEachLabel(imds,numTrainFiles,'randomize');
Set the training options.
options = trainingOptions('sgdm', ... 'MaxEpochs',10, ... 'InitialLearnRate',0.001);
Train network using training data.
net = trainNetwork(imdsTrain,layers,options);
Training on single CPU. |========================================================================================| | Epoch | Iteration | Time Elapsed | Mini-batch | Mini-batch | Base Learning | | | | (hh:mm:ss) | Accuracy | Loss | Rate | |========================================================================================| | 1 | 1 | 00:00:00 | 15.63% | 12.6982 | 0.0010 | | 1 | 50 | 00:00:06 | 63.28% | 1.2109 | 0.0010 | | 2 | 100 | 00:00:10 | 85.16% | 0.4196 | 0.0010 | | 3 | 150 | 00:00:15 | 96.09% | 0.1760 | 0.0010 | | 4 | 200 | 00:00:20 | 99.22% | 0.0453 | 0.0010 | | 5 | 250 | 00:00:25 | 100.00% | 0.0374 | 0.0010 | | 6 | 300 | 00:00:30 | 96.88% | 0.1221 | 0.0010 | | 7 | 350 | 00:00:35 | 100.00% | 0.0086 | 0.0010 | | 7 | 400 | 00:00:40 | 100.00% | 0.0166 | 0.0010 | | 8 | 450 | 00:00:45 | 100.00% | 0.0097 | 0.0010 | | 9 | 500 | 00:00:50 | 100.00% | 0.0046 | 0.0010 | | 10 | 550 | 00:00:55 | 100.00% | 0.0031 | 0.0010 | | 10 | 580 | 00:00:58 | 100.00% | 0.0059 | 0.0010 | |========================================================================================|
Run the trained network on the test set that was not used to train the network and predict the image labels (digits).
YPred = classify(net,imdsTest); YTest = imdsTest.Labels;
Calculate the accuracy.
accuracy = sum(YPred == YTest)/numel(YTest)
Specify the network file to import layers and weights from.
modelfile = 'digitsDAGnet.h5';
Import the network architecture and weights from the files you specified. To import the layer weights, specify 'ImportWeights'
to be true
. The function also imports the layers with their weights from the same HDF5 file.
layers = importKerasLayers(modelfile,'ImportWeights',true)
layers = LayerGraph with properties:
Layers: [13×1 nnet.cnn.layer.Layer]
Connections: [13×2 table]
InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
View the size of the weights in the second layer.
weights = layers.Layers(2).Weights; size(weights)
The function has imported the weights so the layer weights are non-empty.
Specify the network file to import layers from and the file containing weights.
modelfile = 'digitsDAGnet.json'; weights = 'digitsDAGnet.weights.h5';
Import the network architecture and weights from the files you specified. The .json file does not include an output layer. Specify the output layer, so that importKerasLayers adds an output layer at the end of the networks architecture.
layers = importKerasLayers(modelfile, ... 'ImportWeights',true, ... 'WeightFile',weights, ... 'OutputLayerType','classification')
layers = LayerGraph with properties:
Layers: [13×1 nnet.cnn.layer.Layer]
Connections: [13×2 table]
InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
Import layers from a Keras network that has parametric rectified linear unit (PReLU) layers.
A PReLU layer performs a threshold operation, where for each channel, any input value less than zero is multiplied by a scalar. The PReLU operation is given by
f(xi)={xiif xi>0aixiif xi≤0
where xi is the input of the nonlinear activation f on channel i, and ai is the scaling parameter controlling the slope of the negative part. The subscript i in ai indicates that the parameter can be a vector and the nonlinear activation can vary on different channels.
importKerasNetwork
and importKerasLayers
can import a network that includes PReLU layers. These functions support both scalar-valued and vector-valued scaling parameters. If a scaling parameter is a vector, then the functions replace the vector with the average of the vector elements. You can modify a PReLU layer to have a vector-valued scaling parameter after import.
Specify the network file to import.
modelfile = 'digitsDAGnetwithPReLU.h5';
digitsDAGnetwithPReLU
includes two PReLU layers. One has a scalar-valued scaling parameter, and the other has a vector-valued scaling parameter.
Import the network architecture and weights from modelfile
.
layers = importKerasLayers(modelfile,'ImportWeights',true);
Warning: Layer 'p_re_lu_1' is a PReLU layer with a vector-valued parameter. The function replaces the parameter with the average of the vector elements. You can change the parameter back to a vector after import.
The importKerasLayers
function displays a warning for the PReLu layer p_re_lu_1
. The function replaces the vector-valued scaling parameter of p_re_lu_1
with the average of the vector elements. You can change the parameter back to a vector. First, find the index of the PReLU layer by viewing the Layers
property.
ans = 13×1 Layer array with layers:
1 'input_1' Image Input 28×28×1 images
2 'conv2d_1' Convolution 20 7×7×1 convolutions with stride [1 1] and padding 'same'
3 'conv2d_2' Convolution 20 3×3×1 convolutions with stride [1 1] and padding 'same'
4 'p_re_lu_1' PReLU PReLU layer
5 'p_re_lu_2' PReLU PReLU layer
6 'max_pooling2d_1' Max Pooling 2×2 max pooling with stride [2 2] and padding 'same'
7 'max_pooling2d_2' Max Pooling 2×2 max pooling with stride [2 2] and padding 'same'
8 'flatten_1' Keras Flatten Flatten activations into 1-D assuming C-style (row-major) order
9 'flatten_2' Keras Flatten Flatten activations into 1-D assuming C-style (row-major) order
10 'concatenate_1' Depth concatenation Depth concatenation of 2 inputs
11 'dense_1' Fully Connected 10 fully connected layer
12 'dense_1_softmax' Softmax softmax
13 'ClassificationLayer_dense_1' Classification Output crossentropyex
layers
has two PReLU layers. Extract the fourth layer p_re_lu_1
, which originally had a vector-valued scaling parameter for a channel dimension.
tempLayer = layers.Layers(4)
tempLayer = PreluLayer with properties:
Name: 'p_re_lu_1'
RawAlpha: [20×1 single]
Learnable Parameters Alpha: 0.0044
Show all properties
The RawAlpha
property contains the vector-valued scaling parameter, and the Alpha
property contains a scalar that is an element average of the vector values. Reshape RawAlpha
to place the vector values in the third dimension, which corresponds to the channel dimension. Then, replace Alpha
with the reshaped RawAlpha
values.
tempLayer.Alpha = reshape(tempLayer.RawAlpha,[1,1,numel(tempLayer.RawAlpha)])
tempLayer = PreluLayer with properties:
Name: 'p_re_lu_1'
RawAlpha: [20×1 single]
Learnable Parameters Alpha: [1×1×20 single]
Show all properties
Replace the p_re_lu_1
layer in layers
with tempLayer
.
layers = replaceLayer(layers,'p_re_lu_1', tempLayer); layers.Layers(4)
ans = PreluLayer with properties:
Name: 'p_re_lu_1'
RawAlpha: [20×1 single]
Learnable Parameters Alpha: [1×1×20 single]
Show all properties
Now the p_re_lu_1
layer has a vector-valued scaling parameter.
Input Arguments
Name of the model file containing the network architecture, 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® path, or you must include a full or relative path to the file.
If modelfile
includes
- The network architecture and weights, then it must be in HDF5 (
.h5
) format. - Only the network architecture, then it can be in HDF5 or JSON (
.json
) format.
If modelfile
includes only the network architecture, then you can optionally supply the weights using the 'ImportWeights' and'WeightFile' name-value pair arguments. If you supply the weights, then the weights file must be in HDF5 format.
Example: 'digitsnet.h5'
Data Types: char
| string
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: importKerasLayers(modelfile,'OutputLayerType','classification')
imports the network layers from the model file modelfile
and adds an output layer for a classification problem at the end of the Keras layers.
Type of output layer that the function appends to the end of the imported network architecture when modelfile does not specify a loss function, specified as 'classification'
, 'regression'
, or'pixelclassification'
. Appending a pixelClassificationLayer (Computer Vision Toolbox) object requires Computer Vision Toolbox™.
If a network in modelfile
has multiple outputs, then you cannot specify the output layer types using this argument.importKerasLayers
inserts placeholder layers for the outputs. After importing, you can find and replace the placeholder layers by using findPlaceholderLayers and replaceLayer, respectively.
Example: 'OutputLayerType','regression'
Size of the input images for the network, specified as a vector of two or three numerical values corresponding to [height,width]
for grayscale images and [height,width,channels]
for color images, respectively. The network uses this information when the modelfile does not specify the input size.
If a network in modelfile
has multiple inputs, then you cannot specify the input sizes using this argument.importKerasLayers
inserts placeholder layers for the inputs. After importing, you can find and replace the placeholder layers by using findPlaceholderLayers and replaceLayer, respectively.
Example: 'ImageInputSize',[28 28]
Indicator to import weights as well as the network architecture, specified as either false
or true
.
- If
'ImportWeights'
istrue
andmodelfile includes the weights, thenimportKerasLayers
imports the weights frommodelfile
, which must have HDF5 (.h5
) format. - If
'ImportWeights'
istrue
andmodelfile
does not include the weights, then you must specify a separate file that includes weights, using the'WeightFile' name-value pair argument.
Example: 'ImportWeights',true
Data Types: logical
Weight file name, from which to import weights when modelfile does not include weights, specified as a character vector or a string scalar. To use this name-value pair argument, you also must set 'ImportWeights' to true
.
Weight file must be in the current folder, in a folder on the MATLAB path, or you must include a full or relative path to the file.
Example: 'WeightFile','weights.h5'
Data Types: char
| string
Output Arguments
Network architecture, returned as a Layer array object when the Keras network is of type Sequential
, or returned as aLayerGraph object when the Keras network is of type Model
.
Limitations
importKerasLayers
supports TensorFlow-Keras versions as follows:- The function fully supports TensorFlow-Keras versions up to 2.2.4.
- The function offers limited support for TensorFlow-Keras versions 2.2.5 to 2.4.0.
More About
importKerasLayers
supports the following TensorFlow-Keras layer types for conversion into built-in MATLAB layers, with some limitations.
* For a PReLU layer, importKerasLayers
replaces a vector-valued scaling parameter with the average of the vector elements. You can change the parameter back to a vector after import. For an example, see Import Keras PReLU Layer.
importKerasLayers
supports the following Keras loss functions:
mean_squared_error
categorical_crossentropy
sparse_categorical_crossentropy
binary_crossentropy
You can use MATLAB Coder™ or GPU Coder™ together with Deep Learning Toolbox to generate MEX, standalone CPU, CUDA® MEX, or standalone CUDA code for an imported network. For more information, see Generate Code and Deploy Deep Neural Networks.
- Use MATLAB Coder with Deep Learning Toolbox to generate MEX or standalone CPU code that runs on desktop or embedded targets. You can deploy generated standalone code that uses the Intel® MKL-DNN library or the ARM® Compute library. Alternatively, you can generate generic C or C++ code that does not call third-party library functions. For more information, see Deep Learning with MATLAB Coder (MATLAB Coder).
- Use GPU Coder with Deep Learning Toolbox to generate CUDA MEX or standalone CUDA code that runs on desktop or embedded targets. You can deploy generated standalone CUDA code that uses the CUDA deep neural network library (cuDNN), the TensorRT™ high performance inference library, or the ARM Compute library for Mali GPU. For more information, see Deep Learning with GPU Coder (GPU Coder).
importKerasLayers
returns the network architecturelayers as a Layer
or LayerGraph
object. For code generation, you must first convert the imported Layer
orLayerGraph
object to a network. Convert a Layer
orLayerGraph
object to a DAGNetwork
orSeriesNetwork
object by using assembleNetwork. Convert aLayer
or LayerGraph
object to adlnetwork
object by using dlnetwork. For more information on MATLAB Coder and GPU Coder support for Deep Learning Toolbox objects, see Supported Classes (MATLAB Coder) and Supported Classes (GPU Coder), respectively.
You can generate code for any imported network whose layers support code generation. For lists of the layers that support code generation with MATLAB Coder and GPU Coder, see Supported Layers (MATLAB Coder) and Supported Layers (GPU Coder), respectively. For more information on the code generation capabilities and limitations of each built-in MATLAB layer, see the Extended Capabilities section of the layer. For example, seeCode Generation and GPU Code Generation of imageInputLayer.
importKerasLayers
does not execute on a GPU. However,importKerasLayers
imports the layers of a pretrained neural network for deep learning as a Layer
array or LayerGraph
object, which you can use on a GPU.
- Convert the imported layers to a
DAGNetwork
object by usingassembleNetwork. On theDAGNetwork
object, you can then predict class labels on either a CPU or GPU by using classify. Specify the hardware requirements using the name-value argumentExecutionEnvironment
. For networks with multiple outputs, use the predict function and specify the name-value argument ReturnCategorical astrue
. - Convert the imported layers to a
dlnetwork
object by usingdlnetwork. On thedlnetwork
object, you can then predict class labels on either a CPU or GPU by using predict. The functionpredict
executes on the GPU if either the input data or network parameters are stored on the GPU.- If you use minibatchqueue to process and manage the mini-batches of input data, the
minibatchqueue
object converts the output to a GPU array by default if a GPU is available. - Use dlupdate to convert the learnable parameters of a
dlnetwork
object to GPU arrays.
net = dlupdate(@gpuArray,net)
- If you use minibatchqueue to process and manage the mini-batches of input data, the
- You can train the imported layers on either a CPU or GPU by using the trainnet and trainNetwork functions. To specify training options, including options for the execution environment, use the trainingOptions function. Specify the hardware requirements using the name-value argument
ExecutionEnvironment
. For more information on how to accelerate training, see Scale Up Deep Learning in Parallel, on GPUs, and in the Cloud.
Using a GPU requires a Parallel Computing Toolbox™ license and a supported GPU device. For information about supported devices, see GPU Computing Requirements (Parallel Computing Toolbox).
Tips
- If the network contains a layer that Deep Learning Toolbox Converter for TensorFlow Models does not support (see Supported Keras Layers), then
importKerasLayers
inserts a placeholder layer in place of the unsupported layer. To find the names and indices of the unsupported layers in the network, use thefindPlaceholderLayers function. You then can replace a placeholder layer with a new layer that you define. To replace a layer, use replaceLayer. - You can replace a placeholder layer with a new layer that you define.
- If the network is a series network, then replace the layer in the array directly. For example,
layer(2) = newlayer;
. - If the network is a DAG network, then replace the layer using replaceLayer.
- If the network is a series network, then replace the layer in the array directly. For example,
- You can import a Keras network with multiple inputs and multiple outputs (MIMO). UseimportKerasNetwork if the network includes input size information for the inputs and loss information for the outputs. Otherwise, use
importKerasLayers
. TheimportKerasLayers
function inserts placeholder layers for the inputs and outputs. After importing, you can find and replace the placeholder layers by using findPlaceholderLayers and replaceLayer, respectively. To learn about a deep learning network with multiple inputs and multiple outputs, see Multiple-Input and Multiple-Output Networks. - To use a pretrained network for prediction or transfer learning on new images, you must preprocess your images in the same way as the images that you use to train the imported model. The most common preprocessing steps are resizing images, subtracting image average values, and converting the images from BGR format to RGB format.
- To resize images, use imresize. For example,
imresize(image,[227 227 3])
. - To convert images from RGB to BGR format, use flip. For example,
flip(image,3)
.
For more information about preprocessing images for training and prediction, see Preprocess Images for Deep Learning.
- To resize images, use imresize. For example,
- MATLAB uses one-based indexing, whereas Python® uses zero-based indexing. In other words, the first element in an array has an index of 1 and 0 in MATLAB and Python, respectively. For more information about MATLAB indexing, see Array Indexing. In MATLAB, to use an array of indices (
ind
) created in Python, convert the array toind+1
. - For more tips, see Tips on Importing Models from TensorFlow, PyTorch, and ONNX.
Alternative Functionality
- Use
importKerasNetwork
orimportKerasLayers
to import a TensorFlow-Keras network in HDF5 or JSON format. If the TensorFlow network is in the saved model format, useimportTensorFlowNetwork
orimportTensorFlowLayers
. - If you import a custom TensorFlow-Keras layer or if the software cannot convert a TensorFlow-Keras layer into an equivalent built-in MATLAB layer, you can use
importTensorFlowNetwork
orimportTensorFlowLayers
, which try to generate a custom layer. For example,importTensorFlowNetwork
andimportTensorFlowLayers
generate a custom layer when you import a TensorFlow-KerasLambda
layer.
References
Version History
Introduced in R2017b
Starting in R2023b, the importKerasLayers
function warns. Use importNetworkFromTensorFlow instead. TheimportNetworkFromTensorFlow
function has these advantages overimportKerasLayers
:
- Imports a TensorFlow-Keras model into a dlnetwork object in a single step
- Provides a simplified workflow for importing models with unknown input and output information
- Has improved name-value arguments that you can use to more easily specify import options
- Supports the newer TensorFlow
SavedModel
format instead of the discouraged KerasH5
format