Import a pretrained Keras network and weights
imports a pretrained TensorFlow-Keras network and its weights with additional options specified by one or more
name-value pair arguments.net
= importKerasNetwork(modelfile
,Name,Value
)
For example, importKerasNetwork(modelfile,'WeightFile',weights)
imports the network from the model file modelfile
and weights from the
weight file weights
. In this case, modelfile
can be in
HDF5 or JSON format, and the weight file must be in HDF5 format.
importKerasNetwork
can import a network with the following Keras layer
types, with some limitations. If the network contains any other type of layer, then the
software returns an error message. In this case, you can still use importKerasLayers
to import the network architecture and
weights.
Supported Keras Layer | Corresponding Deep Learning Toolbox Layer |
---|---|
Add | additionLayer |
| Layers: |
Advanced activations:
| Layers:
|
AveragePooling2D | averagePooling2dLayer |
BatchNormalization | batchNormalizationLayer |
Bidirectional(LSTM(__)) | bilstmLayer |
Concatenate | depthConcatenationLayer |
Conv2D | convolution2dLayer |
Conv2DTranspose | transposedConv2dLayer |
CuDNNLSTM | lstmLayer |
Dense | fullyConnectedLayer |
DepthwiseConv2D | groupedConvolution2dLayer |
Dropout | dropoutLayer |
Embedding | wordEmbeddingLayer (Text Analytics Toolbox) |
Flatten | nnet.keras.layer.FlattenCStyleLayer |
GlobalAveragePooling2D | globalAveragePooling2dLayer |
GlobalMaxPooling2D | globalMaxPooling2dLayer |
GRU | gruLayer |
Input | imageInputLayer |
LSTM | lstmLayer |
MaxPooling2D | maxPooling2dLayer |
Multiply | multiplicationLayer |
SeparableConv2D | groupedConvolution2dLayer or convolution2dLayer |
UpSampling2D | resize2dLayer (Image Processing Toolbox) |
UpSampling3D | resize3dLayer (Image Processing Toolbox) |
ZeroPadding2D | nnet.keras.layer.ZeroPadding2DLayer |
*For a PReLU layer, importKerasNetwork
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.
importKerasNetwork
supports the following Keras loss functions:
mean_squared_error
categorical_crossentropy
sparse_categorical_crossentropy
binary_crossentropy
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 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 as the images used to train the imported model were preprocessed. Resizing images, subtracting the average image, and converting the images from RGB to BGR format are the most common preprocessing operations.
For more information on preprocessing images for training and prediction, see Preprocess Images for Deep Learning.
[1] Keras: The Python Deep Learning library. https://keras.io.
exportONNXNetwork
| importCaffeLayers
| importCaffeNetwork
| importKerasLayers
| importONNXLayers
| importONNXNetwork