importTensorFlowLayers
importTensorFlowLayers
will be removed in a future release. Use importNetworkFromTensorFlow
instead. (since R2023b) For more information about
updating your code, see Version History.
Syntax
Description
returns the layers of a TensorFlow™ network from the folder lgraph
= importTensorFlowLayers(modelFolder
)modelFolder
, which contains the
model in the saved model format (compatible only with TensorFlow 2). The function can import TensorFlow networks created with the TensorFlow-Keras sequential or functional API. importTensorFlowLayers
imports
the layers defined in the saved_model.pb
file and the learned weights
contained in the variables
subfolder, and returns
lgraph
as a LayerGraph
object.
importTensorFlowLayers
requires the Deep Learning Toolbox™ Converter for TensorFlow Models support package. If this support package is not installed, then
importTensorFlowLayers
provides a download link.
Note
importTensorFlowLayers
tries to generate a custom layer when you import a
custom TensorFlow layer or when the software cannot convert a TensorFlow layer into an equivalent built-in MATLAB® layer. For a list of layers for which the software supports conversion,
see TensorFlow-Keras Layers Supported for Conversion into Built-In MATLAB Layers.
importTensorFlowLayers
saves the generated custom layers and the
associated TensorFlow operators in the namespace
+
. modelFolder
importTensorFlowLayers
does not automatically generate a custom layer for
each TensorFlow layer that is not supported for conversion into a built-in MATLAB layer. For more information on how to handle unsupported layers, see Tips.
imports the layers and weights from a TensorFlow network with additional options specified by one or more name-value arguments.
For example, lgraph
= importTensorFlowLayers(modelFolder
,Name,Value
)'OutputLayerType','classification'
appends a classification
output layer to the end of the imported network architecture.
Examples
Input Arguments
Output Arguments
More About
Tips
If the imported network contains a layer not supported for conversion into a built-in MATLAB layer (see TensorFlow-Keras Layers Supported for Conversion into Built-In MATLAB Layers) and
importTensorFlowLayers
does not automatically generate a custom layer, thenimportTensorFlowLayers
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, usereplaceLayer
.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.
For more information about preprocessing images for training and prediction, see Preprocess Images for Deep Learning.
The members of the namespace
+
(custom layers and TensorFlow operators) are not accessible if the namespace parent folder is not on the MATLAB path. For more information, see Namespaces and the MATLAB Path.Namespace
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 importTensorFlowNetwork
or
importTensorFlowLayers
to import a TensorFlow network in the saved model format [2]. Alternatively, if the
network is in HDF5 or JSON format, use importKerasNetwork
or
importKerasLayers
to import the network.
References
[1] TensorFlow. https://www.tensorflow.org/.
[2] Using the SavedModel format. https://www.tensorflow.org/guide/saved_model.