Main Content

importTensorFlowNetwork

Import pretrainedTensorFlownetwork

    Description

    example

    net= importTensorFlowNetwork(modelFolder)imports a pretrained TensorFlow™ network from the foldermodelFolder, 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.importTensorFlowNetworkimports the layers defined in thesaved_model.pbfile and the learned weights contained in thevariablessubfolder, and returns the networknetas aDAGNetworkordlnetworkobject.

    importTensorFlowNetworkrequires theDeep Learning Toolbox™ Converter for TensorFlow Modelssupport package. If this support package is not installed, thenimportTensorFlowNetworkprovides a download link.

    Note

    importTensorFlowNetworktries 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, seeTensorFlow-Keras Layers Supported for Conversion into Built-In MATLAB Layers

    importTensorFlowNetworksaves the generated custom layers and the associated TensorFlow operators in the package+modelFolder

    importTensorFlowNetworkdoes 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, seeTips

    example

    net= importTensorFlowNetwork(modelFolder,Name,Value)imports the pretrained TensorFlow network with additional options specified by one or more name-value arguments. For example,'OutputLayerType','classification'imports the network as aDAGNetworkwith a classification output layer appended to the end of the imported network architecture.

    Examples

    collapse all

    Import a pretrained TensorFlow network in the saved model format as aDAGNetworkobject, and use the imported network to classify an image.

    Specify the model folder.

    if~exist('digitsDAGnet','dir') unzip('digitsDAGnet.zip')endmodelFolder ='./digitsDAGnet';

    Specify the class names.

    classNames = {'0','1','2','3','4','5','6','7','8','9'};

    Import a TensorFlow network in the saved model format. By default,importTensorFlowNetworkimports the network as aDAGNetworkobject. Specify the output layer type for an image classification problem.

    net = importTensorFlowNetwork(modelFolder,'OutputLayerType','classification','Classes',classNames)
    Importing the saved model... Translating the model, this may take a few minutes... Finished translation. Assembling network... Import finished.
    net = DAGNetwork with properties: Layers: [13×1 nnet.cnn.layer.Layer] Connections: [13×2 table] InputNames: {'input_1'} OutputNames: {'ClassificationLayer_activation_1'}

    Plot the network architecture.

    plot(net) title('DAG Network Architecture')

    Read the image you want to classify and display the size of the image. The image is a grayscale (one-channel) image with size 28-by-28 pixels.

    digitDatasetPath = fullfile(toolboxdir('nnet'),'nndemos','nndatasets','DigitDataset'); I = imread(fullfile(digitDatasetPath,'5','image4009.png')); size(I)
    ans =1×228 28

    Display the input size of the network. In this case, the image size matches the network input size. If they do not match, you must resize the image by usingimresize(I, netInputSize(1:2))

    net.Layers(1).InputSize
    ans =1×328 28 1

    Classify the image using the pretrained network.

    label = classify(net,I);

    Display the image and the classification result.

    imshow(I) title(['Classification result 'char(label)])

    Import a pretrained TensorFlow Network in the saved model format as adlnetworkobject, and use the imported network to predict class labels.

    Specify the model folder.

    if~exist('digitsDAGnet','dir') unzip('digitsDAGnet.zip')endmodelFolder ='./digitsDAGnet';

    Specify the class names.

    classNames = {'0','1','2','3','4','5','6','7','8','9'};

    Import a TensorFlow network in the saved model format as adlnetworkobject.

    net = importTensorFlowNetwork(modelFolder,'TargetNetwork','dlnetwork')
    Importing the saved model... Translating the model, this may take a few minutes... Finished translation. Assembling network... Import finished.
    net = dlnetwork with properties: Layers: [12×1 nnet.cnn.layer.Layer] Connections: [12×2 table] Learnables: [6×3 table] State: [0×3 table] InputNames: {'input_1'} OutputNames: {'activation_1'} Initialized: 1

    Read the image you want to classify and display the size of the image. The image is a grayscale (one-channel) image with size 28-by-28 pixels.

    digitDatasetPath = fullfile(toolboxdir('nnet'),'nndemos','nndatasets','DigitDataset'); I = imread(fullfile(digitDatasetPath,'5','image4009.png')); size(I)
    ans =1×228 28

    Display the input size of the network. In this case, the image size matches the network input size. If they do not match, you must resize the image by usingimresize(I, netInputSize(1:2))

    netInputSize = net.Layers(1).InputSize
    netInputSize =1×328 28 1

    Convert the image to adlarray.Format the images with the dimensions'SSCB'(spatial, spatial, channel, batch). In this case, the batch size is 1 and you can omit it ('SSC').

    I_dlarray = dlarray(single(I),'SSCB');

    Classify the sample image and find the predicted label.

    prob = predict(net,I_dlarray); [~,label] = max(prob);

    Display the image and the classification result.

    imshow(I) title(['Classification result 'classNames{label}])

    Import a pretrained TensorFlow network in the saved model format as aDAGNetworkobject, and use the imported network to classify an image. The imported network contains layers that are not supported for conversion into built-in MATLAB layers. The software automatically generates custom layers when you import these layers.

    This example uses the helper functionfindCustomLayers.To view the code for this function, seeHelper Function

    Specify the model folder.

    if~exist('digitsDAGnetwithnoise','dir') unzip('digitsDAGnetwithnoise.zip')endmodelFolder ='./digitsDAGnetwithnoise';

    Specify the class names.

    classNames = {'0','1','2','3','4','5','6','7','8','9'};

    Import a TensorFlow network in the saved model format. By default,importTensorFlowNetworkimports the network as aDAGNetworkobject. Specify the output layer type for an image classification problem.

    net = importTensorFlowNetwork(modelFolder,'OutputLayerType','classification','Classes',classNames);
    Importing the saved model... Translating the model, this may take a few minutes... Finished translation. Assembling network... Import finished.

    If the imported network contains layers not supported for conversion into built-in MATLAB layers, thenimportTensorFlowNetworkcan automatically generate custom layers in place of these layers.importTensorFlowNetworksaves each generated custom layer to a separate.mfile in the package+digitsDAGnetwithnoisein the current folder.

    Find the indices of the automatically generated custom layers using the helper functionfindCustomLayers, and display the custom layers.

    ind = findCustomLayers(net.Layers,'+digitsDAGnetwithnoise'); net.Layers(ind)
    ans = 2×1 Layer array with layers: 1 'gaussian_noise_1' GaussianNoise digitsDAGnetwithnoise.kGaussianNoise1Layer3766 2 'gaussian_noise_2' GaussianNoise digitsDAGnetwithnoise.kGaussianNoise2Layer3791

    Plot the network architecture.

    plot(net) title('DAG Network Architecture')

    Read the image you want to classify.

    digitDatasetPath = fullfile(toolboxdir('nnet'),'nndemos','nndatasets','DigitDataset'); I = imread(fullfile(digitDatasetPath,'5','image4009.png'));

    Classify the image using the pretrained network.

    label = classify(net,I);

    Display the image and the classification result.

    imshow(I) title(['Classification result 'char(label)])

    Helper Function

    本节提供的代码辅助功能ionfindCustomLayersused in this example.findCustomLayersreturns theindicesof the custom layers thatimportTensorFlowNetworkautomatically generates.

    functionindices = findCustomLayers(layers,PackageName) s = what(['.\'PackageName]); indices = zeros(1,length(s.m));fori = 1:length(layers)forj = 1:length(s.m)ifstrcmpi(class(layers(i)),[PackageName(2:end)'.'s.m{j}(1:end-2)]) indices(j) = i;endendendend

    Input Arguments

    collapse all

    Name of the folder containing the TensorFlow model, specified as a character vector or string scalar.modelFoldermust be in the current folder, or you must include a full or relative path to the folder.modelFoldermust contain the filesaved_model.pb, and the subfoldervariables.It can also contain the subfoldersassetsandassets.extra

    • The filesaved_model.pbcontains the layer graph architecture and training options (for example, optimizer, losses, and metrics).

    • The subfoldervariablescontains the weights learned by the pretrained TensorFlow network. By default,importTensorFlowNetworkimports the weights.

    • The subfolderassetscontains supplementary files (for example, vocabularies), which the layer graph can use.importTensorFlowNetworkdoes not import the files inassets

    • The subfolderassets.extracontains supplementary files (for example, information for users), which coexist with the layer graph.

    Example:'MobileNet'

    Example:'./MobileNet'

    Name-Value Arguments

    Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis 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 encloseNamein quotes.

    Example:importTensorFlowNetwork(modelFolder,'TargetNetwork','dagnetwork','OutputLayerType','classification')imports a network frommodelFolderas aDAGNetworkobject, saves the automatically generated custom layers in the package+modelFolderin the current folder, and appends a classification output layer to the end of the imported network architecture.

    Name of the package in whichimportTensorFlowNetworksaves custom layers, specified as a character vector or string scalar.importTensorFlowNetworksaves the custom layers package+PackageNamein the current folder. If you do not specify'PackageName', thenimportTensorFlowNetworksaves the custom layers in a package named+modelFolderin the current folder. For more information on packages, seePackages Create Namespaces

    importTensorFlowNetworktries 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.importTensorFlowNetworksaves each generated custom layer to a separate.mfile in+PackageName.To view or edit a custom layer, open the associated.mfile. For more information on custom layers, seeDeep Learning Custom Layers

    The package+PackageNamecan also contain the subpackage+ops.This subpackage contains MATLAB functions corresponding to TensorFlow operators (seeSupported TensorFlow Operators) that are used in the automatically generated custom layers.importTensorFlowNetworksaves the associated MATLAB function for each operator in a separate.mfile in the subpackage+ops.The object functions ofdlnetwork, such as thepredictfunction, use these operators when interacting with the custom layers.

    Example:“PackageName”、“MobileNet”

    Example:'PackageName','CustomLayers'

    Target type of Deep Learning Toolbox network, specified as'dagnetwork'or'dlnetwork'

    • Specify'TargetNetworkas'dagnetwork'to import the network as aDAGNetworkobject. In this case,netmust include an output layer specified by the TensorFlow saved model loss function or the name-value argument'OutputLayerType'

    • Specify'TargetNetworkas'dlnetwork'to import the network as adlnetworkobject. In this case,netdoes not include an output layer.

    Example:'TargetNetwork','dlnetwork'

    Type of output layer thatimportTensorFlowNetworkappends to the end of the imported network architecture, specified as'classification','regression', or'pixelclassification'.附加一个pixelClassificationLayer(Computer Vision Toolbox)object requires Computer Vision Toolbox™.

    • If you specify'TargetNetwork'as'dagnetwork'and the saved model inmodelFolderdoes not specify a loss function, you must assign a value to the name-value argument'OutputLayerType'.ADAGNetworkobject must have an output layer.

    • If you specify'TargetNetwork'as'dlnetwork',importTensorFlowNetworkignores the name-value argument'OutputLayerType'.Adlnetworkobject does not have an output layer.

    Example:'OutputLayerType','classification'

    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 thesaved_model.pbfile inmodelFolderdoes not specify the input size.

    Example:'ImageInputSize',[28 28]

    Classes of the output layer, specified as a categorical vector, string array, cell array of character vectors, or'auto'.If you specify a string array or cell array of character vectorsstr, thenimportTensorFlowNetworksets the classes of the output layer tocategorical(str,str).IfClassesis'auto', thenimportTensorFlowNetworksets the classes tocategorical(1:N), whereNis the number of classes.

    • If you specify'TargetNetwork'as'dagnetwork',importTensorFlowNetworkstores information on classes in the output layer of theDAGNetworkobject.

    • If you specify'TargetNetwork'as'dlnetwork',importTensorFlowNetworkignores the name-value argument'Classes'.Adlnetwork对象没有一个输出层来存储信息rmation on classes.

    Example:'Classes',{'0','1','3'}

    Example:'Classes',categorical({'dog','cat'})

    Data Types:char|categorical|string|cell

    Indicator to display import progress information in the command window, specified as a numeric or logical1(true) or0(false).

    Example:'Verbose','true'

    Output Arguments

    collapse all

    Pretrained TensorFlow network, returned as aDAGNetworkordlnetworkobject.

    • Specify'TargetNetworkas'dagnetwork'to import the network as aDAGNetworkobject. On theDAGNetworkobject, you then predict class labels by using theclassifyfunction.

    • Specify'TargetNetworkas'dlnetwork'to import the network as adlnetworkobject. On thedlnetworkobject, you then predict class labels by using thepredictfunction. Specify the input data as adlarrayusing the correct data format (for more information, see thefmtargument ofdlarray).

    Limitations

    • importTensorFlowNetworksupports TensorFlow versions v2.0 to 2.6.

    More About

    collapse all

    TensorFlow-Keras Layers Supported for Conversion into Built-InMATLABLayers

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

    TensorFlow-Keras Layer Corresponding Deep Learning Toolbox Layer
    Add additionLayer

    Activation, with activation names:

    • elu

    • gelu

    • relu

    • linear

    • softmax

    • sigmoid

    • swish

    • tanh

    Layers:

    Advanced activations:

    • ELU

    • Softmax

    • ReLU

    • LeakyReLU

    • PReLu*

    Layers:

    AveragePooling1D averagePooling1dLayerwithPaddingValuespecified as'mean'
    AveragePooling2D averagePooling2dLayerwithPaddingValuespecified as'mean'
    BatchNormalization batchNormalizationLayer
    Bidirectional(LSTM(__)) bilstmLayer
    Concatenate depthConcatenationLayer
    Conv1D convolution1dLayer
    Conv2D convolution2dLayer
    Conv2DTranspose transposedConv2dLayer
    CuDNNGRU gruLayer
    CuDNNLSTM lstmLayer
    Dense fullyConnectedLayer
    DepthwiseConv2D groupedConvolution2dLayer
    Dropout dropoutLayer
    Embedding wordEmbeddingLayer(Text Analytics Toolbox)
    Flatten nnet.keras.layer.FlattenCStyleLayer
    GlobalAveragePooling1D globalAveragePooling1dLayer
    GlobalAveragePooling2D globalAveragePooling2dLayer
    GlobalMaxPool1D globalMaxPooling1dLayer
    GlobalMaxPool2D globalMaxPooling2dLayer
    GRU gruLayer
    Input imageInputLayer,sequenceInputLayer, orfeatureInputLayer
    LSTM lstmLayer
    MaxPool1D maxPooling1dLayer
    MaxPool2D maxPooling2dLayer
    Multiply multiplicationLayer
    SeparableConv2D groupedConvolution2dLayerorconvolution2dLayer
    TimeDistributed sequenceFoldingLayerbefore the wrapped layer, andsequenceUnfoldingLayerafter the wrapped layer
    UpSampling2D resize2dLayer(Image Processing Toolbox)
    UpSampling3D resize3dLayer(Image Processing Toolbox)
    ZeroPadding1D nnet.keras.layer.ZeroPadding1DLayer
    ZeroPadding2D nnet.keras.layer.ZeroPadding2DLayer

    * For a PReLU layer,importTensorFlowNetworkreplaces 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, seeImport Keras PReLU Layer

    SupportedTensorFlow-Keras Loss Functions

    importTensorFlowNetworksupports the following Keras loss functions:

    • mean_squared_error

    • categorical_crossentropy

    • sparse_categorical_crossentropy

    • binary_crossentropy

    SupportedTensorFlowOperators

    importTensorFlowNetworksupports the following TensorFlow operators for conversion into MATLAB functions withdlarraysupport.

    TensorFlow Operator Corresponding MATLAB Function
    Add tfAdd
    AddN tfAddN
    AddV2 tfAdd
    Assert assert
    AvgPool tfAvgPool
    BatchMatMulV2 tfBatchMatMulV2
    BiasAdd tfBiasAdd
    BroadcastTo tfBroadcastTo
    Cast tfCast
    ConcatV2 tfCat
    Const None (translated to weights in custom layer)
    Conv2D tfConv2D
    DepthToSpace depthToSpace(Image Processing Toolbox)
    DepthwiseConv2dNative tfDepthwiseConv2D
    Exp exp
    ExpandDims tfExpandDims
    FusedBatchNormV3 tfBatchnorm
    GatherV2 tfGather
    GreaterEqual ge,>=
    Identity None (translated to value assignment in custom layer)
    IdentityN tfIdentityN
    L2Loss tfL2Loss
    LeakyRelu leakyrelu
    Less lt,<
    Log log
    MatMul tfMatMul
    MaxPool tfMaxPool
    Maximum tfMaximum
    Mean tfMean
    Minimum tfMinimum
    MirrorPad tfMirrorPad
    Mul tfMul
    Neg minus,-
    Pack tfStack
    Pad tfPad
    PadV2 tfPad
    PartitionedCall None (translated to function in custom layer methods)
    Pow power,.^
    Prod tfProd
    RandomStandardNormal tfRandomStandardNormal
    Range tfRange
    ReadVariableOp None (translated to value assignment in custom layer)
    RealDiv tfDiv
    Relu relu
    Relu6 reluandmin
    Reshape tfReshape
    ResizeNearestNeighbor dlresize(Image Processing Toolbox)
    Rsqrt sqrt
    Shape tfShape
    Sigmoid sigmoid
    Size tfSize
    Softmax softmax
    SpaceToDepth spaceToDepth(Image Processing Toolbox)
    Square .^2
    Sqrt sqrt
    SquaredDifference tfMulortfSub
    Squeeze tfSqueeze
    StatefulPartitionedCall None (translated to function in custom layer methods)
    StopGradient tfStopGradient
    StridedSlice tfStridedSliceortfSqueeze
    Sub tfSub
    Tanh tanh
    瓷砖 tfTile
    Transpose tfTranspose

    For more information on functions that operate ondlarrayobjects, seeList of Functions with dlarray Support

    Generate Code for Imported Network

    You can useMATLAB 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, seeDeep Learning Code Generation

    • UseMATLAB Coderwith 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, seeDeep Learning with MATLAB Coder(MATLAB Coder)

    • 使用GPU编码器与深度学习工具箱属te 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, seeDeep Learning with GPU Coder(GPU Coder)

    importTensorFlowNetworkreturns the networknetas aDAGNetworkordlnetworkobject. Both these objects support code generation. For more information onMATLAB Coderand GPU Coder support for Deep Learning Toolbox objects, seeSupported Classes(MATLAB Coder)andSupported 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 withMATLAB Coderand GPU Coder, seeSupported Layers(MATLAB Coder)andSupported 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, seeCode GenerationandGPU Code GenerationofimageInputLayer

    Use Imported Network on GPU

    importTensorFlowNetworkdoes not execute on a GPU. However,importTensorFlowNetworkimports a pretrained neural network for deep learning as aDAGNetworkordlnetworkobject, which you can use on a GPU.

    • If you import the network as aDAGNetworkobject, you can make predictions with the imported network on either a CPU or GPU by usingclassify.Specify the hardware requirements using the name-value argumentExecutionEnvironment.For networks with multiple outputs, use thepredictfunction forDAGNetworkobjects.

    • If you import the network as aDAGNetworkobject, you can make predictions with the imported network on either a CPU or GPU by usingpredict.Specify the hardware requirements using the name-value argumentExecutionEnvironment.If the network has multiple outputs, specify the name-value argumentReturnCategoricalastrue

    • If you import the network as adlnetworkobject, you can make predictions with the imported network on either a CPU or GPU by usingpredict.The functionpredictexecutes on the GPU if either the input data or network parameters are stored on the GPU.

      • If you useminibatchqueueto process and manage the mini-batches of input data, theminibatchqueueobject converts the output to a GPU array by default if a GPU is available.

      • Usedlupdateto convert the learnable parameters of adlnetworkobject to GPU arrays.

        net = dlupdate(@gpuArray,net)

    • You can train the imported network on either a CPU or GPU by usingtrainNetwork.To specify training options, including options for the execution environment, use thetrainingOptionsfunction. Specify the hardware requirements using the name-value argumentExecutionEnvironment.For more information on how to accelerate training, seeScale Up Deep Learning in Parallel, on GPUs, and in the Cloud

    Using a GPU requires Parallel Computing Toolbox™ and a supported GPU device. For information on supported devices, seeGPU Computing Requirements(Parallel Computing Toolbox)

    Tips

    • If the imported network contains a layer not supported for conversion into a built-in MATLAB layer (seeTensorFlow-Keras Layers Supported for Conversion into Built-In MATLAB Layers) andimportTensorFlowNetworkdoes not generate a custom layer, thenimportTensorFlowNetworkreturns an error. In this case, you can still useimportTensorFlowLayersto import the network architecture.

    • 使用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, useimresize.For example,imresize(image,[227,227,3])

      • To convert images from RGB to BGR format, useflip.For example,flip(image,3)

      For more information on preprocessing images for training and prediction, seePreprocess Images for Deep Learning

    • The members of the package+PackageName(custom layers and TensorFlow operators) are not accessible if the package parent folder is not on the MATLAB path. For more information, seePackages and the MATLAB Path

    • 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, seeArray Indexing.In MATLAB, to use an array of indices (ind) created in Python, convert the array toind+1

    • For more tips, seeTips on Importing Models from TensorFlow, PyTorch, and ONNX

    Alternative Functionality

    UseimportTensorFlowNetworkorimportTensorFlowLayersto import a TensorFlow network in the saved model format[2].Alternatively, if the network is in HDF5 or JSON format, useimportKerasNetworkorimportKerasLayersto import the network.

    References

    [1]TensorFlowhttps://www.tensorflow.org/

    [2]Using the SavedModel formathttps://www.tensorflow.org/guide/saved_model

    Version History

    Introduced in R2021a