importTensorFlowNetwork
importTensorFlowNetwork
will be removed in a future release. Use importNetworkFromTensorFlow
instead. (since R2023b) For more information about
updating your code, see Version History.
Syntax
Description
imports a pretrained TensorFlow™ network from the folder net
= importTensorFlowNetwork(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. importTensorFlowNetwork
imports
the layers defined in the saved_model.pb
file and the learned weights
contained in the variables
subfolder, and returns the network
net
as a DAGNetwork
or dlnetwork
object.
importTensorFlowNetwork
requires the Deep Learning Toolbox™ Converter for TensorFlow Models support package. If this support package is not installed, then
importTensorFlowNetwork
provides a download link.
Note
importTensorFlowNetwork
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.
importTensorFlowNetwork
saves the generated custom layers and the
associated TensorFlow operators in the namespace
+
. modelFolder
importTensorFlowNetwork
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 pretrained TensorFlow network with additional options specified by one or more name-value arguments.
For example, net
= importTensorFlowNetwork(modelFolder
,Name,Value
)'OutputLayerType','classification'
imports the network as a
DAGNetwork
with a classification output layer appended 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
importTensorFlowNetwork
does not generate a custom layer, thenimportTensorFlowNetwork
returns an error. In this case, you can still useimportTensorFlowLayers
to import the network architecture.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.
Version History
Introduced in R2021aSee Also
importNetworkFromTensorFlow
| importTensorFlowLayers
| exportNetworkToTensorFlow
| importNetworkFromPyTorch
| importKerasNetwork
| importKerasLayers
| importONNXNetwork
| importONNXLayers
| exportONNXNetwork
Topics
- Interoperability Between Deep Learning Toolbox, TensorFlow, PyTorch, and ONNX
- Tips on Importing Models from TensorFlow, PyTorch, and ONNX
- Inference Comparison Between TensorFlow and Imported Networks for Image Classification
- Pretrained Deep Neural Networks
- Deploy Imported TensorFlow Model with MATLAB Compiler