# importKerasLayers

Import layers from Keras network

## Description

example

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.

## 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)
net =
LayerGraph with properties:

InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
Layers: [13x1 nnet.cnn.layer.Layer]
Connections: [13x2 table]

Import the network layers from the model file digitsDAGnet.h5.

modelfile = 'digitsDAGnet.h5';
layers = importKerasLayers(modelfile)
layers =
LayerGraph with properties:

InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
Layers: [13x1 nnet.cnn.layer.Layer]
Connections: [13x2 table]

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:

InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
Layers: [13x1 nnet.cnn.layer.Layer]
Connections: [13x2 table]

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:01 |       15.62% |      31.6977 |          0.0010 |
|       1 |          50 |       00:00:12 |       63.28% |       1.2109 |          0.0010 |
|       2 |         100 |       00:00:23 |       85.16% |       0.4126 |          0.0010 |
|       3 |         150 |       00:00:35 |       95.31% |       0.1752 |          0.0010 |
|       4 |         200 |       00:00:47 |       99.22% |       0.0465 |          0.0010 |
|       5 |         250 |       00:01:00 |      100.00% |       0.0366 |          0.0010 |
|       6 |         300 |       00:01:09 |       96.88% |       0.1214 |          0.0010 |
|       7 |         350 |       00:01:21 |      100.00% |       0.0086 |          0.0010 |
|       7 |         400 |       00:01:31 |      100.00% |       0.0166 |          0.0010 |
|       8 |         450 |       00:01:39 |      100.00% |       0.0096 |          0.0010 |
|       9 |         500 |       00:01:49 |      100.00% |       0.0047 |          0.0010 |
|      10 |         550 |       00:02:06 |      100.00% |       0.0031 |          0.0010 |
|      10 |         580 |       00:02:12 |      100.00% |       0.0060 |          0.0010 |
|========================================================================================|
Training finished: Max epochs completed.

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.9852

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:

InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
Layers: [13x1 nnet.cnn.layer.Layer]
Connections: [13x2 table]

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:

InputNames: {'input_1'}
OutputNames: {'ClassificationLayer_activation_1'}
Layers: [13x1 nnet.cnn.layer.Layer]
Connections: [13x2 table]

This example shows how to import the layers from a pretrained Keras network, replace the unsupported layers with custom layers, and assemble the layers into a network ready for prediction.

Import Keras Network

Import the layers from a Keras network model. The network in 'digitsDAGnetwithnoise.h5' classifies images of digits.

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.

The Keras network contains some layers that are not supported by Deep Learning Toolbox. The importKerasLayers function displays a warning and replaces the unsupported layers with placeholder layers.

Plot the layer graph using plot.

figure
plot(lgraph)
title("Imported Network")

Replace Placeholder Layers

To replace the placeholder layers, first identify the names of the layers to replace. Find the placeholder layers using 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

Display the Keras configurations of these layers.

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

Create two Gaussian noise layers with the same configurations as the imported Keras layers using the helper gaussianNoiseLayer function.

gnLayer1 = gaussianNoiseLayer(1.5,'new_gaussian_noise_1');
gnLayer2 = gaussianNoiseLayer(0.7,'new_gaussian_noise_2');

Replace the placeholder layers with the custom layers using replaceLayer.

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

Plot the updated layer graph using plot.

figure
plot(lgraph)
title("Network with Replaced Layers")

Specify Class Names

If the imported classification layer does not contain the classes, then you must specify these before prediction. If you do not specify the classes, then the software automatically sets the classes to 1, 2, ..., N, where N is the number of classes.

Find the index of the classification layer by viewing the Layers property of the layer graph.

lgraph.Layers
ans =
15x1 Layer array with layers:

1   'input_1'                            Image Input             28x28x1 images
2   'conv2d_1'                           2-D Convolution         20 7x7x1 convolutions with stride [1  1] and padding 'same'
3   'conv2d_1_relu'                      ReLU                    ReLU
4   'conv2d_2'                           2-D 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'                    2-D Max Pooling         2x2 max pooling with stride [2  2] and padding 'same'
9   'max_pooling2d_2'                    2-D 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

The classification layer has the name 'ClassificationLayer_activation_1'. View the classification layer and check the Classes property.

cLayer = lgraph.Layers(end)
cLayer =
ClassificationOutputLayer with properties:

Name: 'ClassificationLayer_activation_1'
Classes: 'auto'
ClassWeights: 'none'
OutputSize: 'auto'

Hyperparameters
LossFunction: 'crossentropyex'

Because the Classes property of the layer is 'auto', you must specify the classes manually. Set the classes to 0, 1, ..., 9, and then replace the imported classification layer with the new one.

cLayer.Classes = string(0:9)
cLayer =
ClassificationOutputLayer with properties:

Name: 'ClassificationLayer_activation_1'
Classes: [0    1    2    3    4    5    6    7    8    9]
ClassWeights: 'none'
OutputSize: 10

Hyperparameters
LossFunction: 'crossentropyex'

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

Assemble Network

Assemble the layer graph using assembleNetwork. The function returns a DAGNetwork object that is ready to use for prediction.

net = assembleNetwork(lgraph)
net =
DAGNetwork with properties:

Layers: [15x1 nnet.cnn.layer.Layer]
Connections: [15x2 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

$\mathit{f}\left({\mathit{x}}_{\mathit{i}}\right)=\left\{\begin{array}{ll}{\mathit{x}}_{\mathit{i}}& \mathrm{if}\text{\hspace{0.17em}}{\mathit{x}}_{\mathit{i}}>0\\ {\mathit{a}}_{\mathit{i}}{\mathit{x}}_{\mathit{i}}& \mathrm{if}\text{\hspace{0.17em}}{\mathit{x}}_{\mathit{i}}\le 0\end{array}$

where ${\mathit{x}}_{\mathit{i}}$ is the input of the nonlinear activation $\mathit{f}$ on channel $i$, and ${a}_{i}$ is the scaling parameter controlling the slope of the negative part. The subscript $i$ in ${a}_{i}$ 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 'Alpha'. During import, the function replaces the parameter with the average of the vector elements. After import, it is possible to change the parameter back to a vector.

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 =
13x1 Layer array with layers:

1   'input_1'                       Image Input             28x28x1 images
2   'conv2d_1'                      2-D Convolution         20 7x7x1 convolutions with stride [1  1] and padding 'same'
3   'conv2d_2'                      2-D Convolution         20 3x3x1 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'               2-D Max Pooling         2x2 max pooling with stride [2  2] and padding 'same'
7   'max_pooling2d_2'               2-D Max Pooling         2x2 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: [20x1 single]

Learnable Parameters
Alpha: 0.0044

State Parameters
No properties.

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: [20x1 single]

Learnable Parameters
Alpha: [1x1x20 single]

State Parameters
No properties.

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: [20x1 single]

Learnable Parameters
Alpha: [1x1x20 single]

State Parameters
No properties.

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

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.

collapse all

### Supported Keras Layers

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

TensorFlow-Keras LayerCorresponding Deep Learning Toolbox Layer

Activation, with activation names:

• elu

• gelu

• relu

• linear

• softmax

• sigmoid

• swish

• tanh

Layers:

• ELU

• Softmax

• ReLU

• LeakyReLU

• PReLu*

Layers:

AveragePooling1DaveragePooling1dLayer with PaddingValue specified as 'mean'
AveragePooling2DaveragePooling2dLayer with PaddingValue specified as 'mean'
BatchNormalizationbatchNormalizationLayer
Bidirectional(LSTM(__))bilstmLayer
ConcatenatedepthConcatenationLayer
Conv1Dconvolution1dLayer
Conv2Dconvolution2dLayer
Conv2DTransposetransposedConv2dLayer
CuDNNGRUgruLayer
CuDNNLSTMlstmLayer
DensefullyConnectedLayer
DepthwiseConv2DgroupedConvolution2dLayer
DropoutdropoutLayer
EmbeddingwordEmbeddingLayer (Text Analytics Toolbox)
Flattennnet.keras.layer.FlattenCStyleLayer
GlobalAveragePooling1DglobalAveragePooling1dLayer
GlobalAveragePooling2DglobalAveragePooling2dLayer
GlobalMaxPool1DglobalMaxPooling1dLayer
GlobalMaxPool2DglobalMaxPooling2dLayer
GRUgruLayer
InputimageInputLayer, sequenceInputLayer, or featureInputLayer
LSTMlstmLayer
MaxPool1DmaxPooling1dLayer
MaxPool2DmaxPooling2dLayer
MultiplymultiplicationLayer
SeparableConv2DgroupedConvolution2dLayer or convolution2dLayer
TimeDistributedsequenceFoldingLayer before the wrapped layer, and sequenceUnfoldingLayer after the wrapped layer
UpSampling2Dresize2dLayer (Image Processing Toolbox)
UpSampling3Dresize3dLayer (Image Processing Toolbox)

* 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.

### Supported Keras Loss Functions

importKerasLayers supports the following Keras loss functions:

• mean_squared_error

• categorical_crossentropy

• sparse_categorical_crossentropy

• binary_crossentropy

### Generate Code for Imported Network Architecture

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 Code Generation.

• 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 architecture layers as a Layer or LayerGraph object. For code generation, you must first convert the imported Layer or LayerGraph object to a network. Convert a Layer or LayerGraph object to a DAGNetwork or SeriesNetwork object by using assembleNetwork. Convert a Layer or LayerGraph object to a dlnetwork 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, see Code Generation and GPU Code Generation of imageInputLayer.

### Use Imported Network Layers on GPU

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 using assembleNetwork. On the DAGNetwork object, you can then predict class labels on either a CPU or GPU by using classify. Specify the hardware requirements using the name-value argument ExecutionEnvironment. For networks with multiple outputs, use the predict function and specify the name-value argument ReturnCategorical as true.

• Convert the imported layers to a dlnetwork object by using dlnetwork. On the dlnetwork object, you can then predict class labels on either a CPU or GPU by using predict. The function predict 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)

• You can train the imported layers on either a CPU or GPU by using trainNetwork. 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 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.

• 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. The workflow for importing MIMO Keras networks is the same as the workflow for importing MIMO ONNX™ networks. For an example, see Import and Assemble ONNX Network with Multiple Outputs. 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 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 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 on 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 on 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