Import layers from TensorFlow network
returns the lgraph = importTensorFlowLayers(modelFolder)layers of a TensorFlow™ network from the folder modelFolder, which contains the
model in the saved model format (compatible only with TensorFlow 2). The function 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 package
+. modelFolder
importTensorFlowLayers does not automatically generate a custom layer for
each TensorFlow layer that is not supported for conversion into 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.
importTensorFlowLayers supports TensorFlow versions v2.0, v2.1, v.2.2, and v2.3.
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, then
importTensorFlowLayers 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.
importTensorFlowLayers can import TensorFlow networks created with the TensorFlow-Keras sequential or functional API.
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 images to RGB.
For more information on preprocessing images for training and prediction, see Preprocess Images for Deep Learning.
The members of the package + (custom
layers and TensorFlow operators) are not accessible if the package parent folder is not on the
MATLAB path. For more information, see Packages and the MATLAB Path.PackageName
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.
[1] TensorFlow. https://www.tensorflow.org/.
[2] Using the SavedModel format. https://www.tensorflow.org/guide/saved_model.
assembleNetwork | exportONNXNetwork | findPlaceholderLayers | importCaffeLayers | importCaffeNetwork | importKerasLayers | importKerasNetwork | importONNXFunction | importONNXLayers | importONNXNetwork | importTensorFlowNetwork | layerGraph | replaceLayer