Main Content

importKerasLayers

(To be removed) Import layers from Keras network

importKerasLayers will be removed in a future release. Use importNetworkFromTensorFlow instead. (since R2023b) For more information about updating your code, see Version History.

Description

layers = importKerasLayers(modelfile) 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.

example

layers = importKerasLayers(modelfile,Name,Value) 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 file modelfile.

example

Examples

collapse all

Download and install the Deep Learning Toolbox Converter for TensorFlow Models support package.

Type importKerasLayers at the command line.

importKerasLayers

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.

plot(layers)

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)
accuracy = 0.9856

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)
ans = 1×4

     7     7     1    20

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)={xiifxi>0aixiifxi0

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.

layers.Layers
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

collapse all

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

collapse all

Specify optional pairs of arguments as Name1=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' is true and modelfile includes the weights, then importKerasLayers imports the weights from modelfile, which must have HDF5 (.h5) format.

  • If 'ImportWeights' is true and modelfile 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

collapse all

Network architecture, returned as a Layer array object when the Keras network is of type Sequential, or returned as a LayerGraph 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

collapse all

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 the findPlaceholderLayers 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.

  • You can import a Keras network with multiple inputs and multiple outputs (MIMO). Use importKerasNetwork if the network includes input size information for the inputs and loss information for the outputs. Otherwise, use importKerasLayers. The importKerasLayers 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.

  • 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 to ind+1.

  • For more tips, see Tips on Importing Models from TensorFlow, PyTorch, and ONNX.

Alternative Functionality

  • Use importKerasNetwork or importKerasLayers to import a TensorFlow-Keras network in HDF5 or JSON format. If the TensorFlow network is in the saved model format, use importTensorFlowNetwork or importTensorFlowLayers.

  • 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 or importTensorFlowLayers, which try to generate a custom layer. For example, importTensorFlowNetwork and importTensorFlowLayers generate a custom layer when you import a TensorFlow-Keras Lambda layer.

References

[1] Keras: The Python Deep Learning library. https://keras.io.

Version History

Introduced in R2017b

collapse all