{"id":9163,"date":"2022-03-18T10:15:01","date_gmt":"2022-03-18T14:15:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/deep-learning\/?p=9163"},"modified":"2024-04-22T12:21:29","modified_gmt":"2024-04-22T16:21:29","slug":"importing-models-from-tensorflow-pytorch-and-onnx","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/deep-learning\/2022\/03\/18\/importing-models-from-tensorflow-pytorch-and-onnx\/","title":{"rendered":"Importing Models from TensorFlow, PyTorch, and ONNX"},"content":{"rendered":"<em>The following post is from <a href=\"https:\/\/www.linkedin.com\/in\/sivylla-paraskevopoulou-05865a82\/\">Sivylla Paraskevopoulou<\/a>, Senior Technical Writer and <a href=\"https:\/\/www.linkedin.com\/in\/davidwillingham\/\">David Willingham<\/a>, Product Manager for Deep Learning Toolbox.<\/em>\r\n<blockquote>Read our <a href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2024\/04\/22\/convert-deep-learning-models-between-pytorch-tensorflow-and-matlab\/\">newest blog post<\/a> on how to convert (import and export) deep learning models between MATLAB, PyTorch, and TensorFlow.<\/blockquote>\r\n<h6><\/h6>\r\nHow do you import a model created in TensorFlow\u2122 or PyTorch\u2122 and convert it into MATLAB Code?\r\n<h6><\/h6>\r\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" width=\"399\" height=\"223\" class=\"alignnone size-full wp-image-9166\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/BlogImage1.png\" alt=\"\" \/><\/p>\r\n\r\n<h6><\/h6>\r\nFirst, keep in mind there are different options for working with deep learning models in MATLAB.\r\n<h6><\/h6>\r\n<ol>\r\n \t<li>Using models created in MATLAB with\u00a0Deep Learning Toolbox<\/li>\r\n \t<li><strong>Converting from other frameworks into MATLAB<\/strong><\/li>\r\n \t<li>Co-executing models from other frameworks with MATLAB<\/li>\r\n<\/ol>\r\nThis blog post focuses on <strong>option 2<\/strong>, but you can see the benefits of choosing between these 3 options using <a href=\"#ComparisonTable\"><strong>this comparison table<\/strong><\/a> below, and view all available models to directly import into MATLAB using the <a href=\"https:\/\/github.com\/matlab-deep-learning\/MATLAB-Deep-Learning-Model-Hub\">MATLAB Deep Learning Model Hub<\/a>.\r\n<h6><\/h6>\r\nHow complicated is it to convert a model? In many cases it's quite straightforward. In other cases, there may be a few additional steps before successfully converting a 3rd party model into MATLAB. The purpose of this blog is to give guidance on how to navigate these steps.\r\n<h6><\/h6>\r\n<strong>Here is what we will cover today:<\/strong>\r\n<h6><\/h6>\r\n<ul>\r\n \t<li><a href=\"#ImportingTF\">Importing TensorFlow Models<\/a>\r\n<ul>\r\n \t<li><a href=\"#DifferentTFModels1\">SavedModel Format<\/a><\/li>\r\n \t<li><a href=\"#DifferentTFModels2\">HDF5 Format<\/a><\/li>\r\n \t<li><a href=\"#MoreComplexTF\">Importing More Complex TensorFlow Models<\/a><\/li>\r\n<\/ul>\r\n<\/li>\r\n \t<li><a href=\"#ImportingPyTorch\">Importing PyTorch Models<\/a><\/li>\r\n \t<li><a href=\"#TensorFlowCompleteExample\">Example: Import Complex TensorFlow Model for Image Classification<\/a><\/li>\r\n<\/ul>\r\n<h6><\/h6>\r\nFollow along step by step in this post, and download the code here: <a href=\"https:\/\/github.com\/matlab-deep-learning\/Image-Classification-in-MATLAB-Using-Converted-TensorFlow-Model\">https:\/\/github.com\/matlab-deep-learning\/Image-Classification-in-MATLAB-Using-Converted-TensorFlow-Model<\/a>\r\n<h6><\/h6>\r\n<h2><em><span style=\"font-size: 18px;\"><a name=\"ImportingTF\"><\/a>Importing TensorFlow Models<\/span><\/em><\/h2>\r\nIt\u2019s important to note that there are 2 different versions of TensorFlow models that can be imported:\r\n<h6><\/h6>\r\n<ul>\r\n \t<li>TensorFlow 2.0, where the SavedModel format is used<\/li>\r\n \t<li>TensorFlow 1.0, where the HDF5 format is used<\/li>\r\n<\/ul>\r\n<em>(TensorFlow 2.0 supports both HDF5 and SavedModel formats, but the SavedModel format is recommended.)<\/em>\r\n<h6><\/h6>\r\nYour workflow in MATLAB will depend on the TensorFlow version in which the model was saved.\r\n<h6><\/h6>\r\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" width=\"493\" height=\"344\" class=\"alignnone size-full wp-image-9169\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/BlogImage2.png\" alt=\"\" \/><\/p>\r\n\r\n<h6><\/h6>\r\n<h6><\/h6>\r\n<strong>Importing TensorFlow Models using SavedModel Format<a name=\"DifferentTFModels1\"><\/a><\/strong>\r\n<h6><\/h6>\r\nWhen TensorFlow 2.0 became available (Sep 2019), the <a href=\"https:\/\/www.tensorflow.org\/guide\/saved_model\"><strong>SavedModel<\/strong><\/a> format was introduced and is now the preferred method for saving pretrained models. Here's the code to import this model format.\r\n<h6><\/h6>\r\nUse <a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/applications\/resnet_v2\/ResNet50V2\">tf.keras.applications.resnet_v2.ResNet50V2<\/a> to instantiate the ResNet50V2 model.\r\n<h6><\/h6>\r\nThe ResNet50V2 model is trained with images from the ImageNet database. Get the class names from SqueezeNet, which is also trained with ImageNet images.\r\n<h6><\/h6>\r\n<pre>squeezeNet = squeezenet;\r\nClassNames = squeezeNet.Layers(end).Classes;<\/pre>\r\n<h6><\/h6>\r\nImport ResNet50V2 in the SavedModel format by using the <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importtensorflownetwork.html\"><span style=\"font-family: courier;\">importTensorFlowNetwork<\/span><\/a> function.\r\n<h6><\/h6>\r\n<pre>net = importTensorFlowNetwork(\"ResNet50V2\",...\r\n   OutputLayerType=\"classification\",...\r\n   Classes=ClassNames)<\/pre>\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"379\" height=\"181\" class=\"alignnone size-full wp-image-9172\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage3.png\" alt=\"\" \/>\r\n<h6><\/h6>\r\n<strong>Importing TensorFlow Models using the\u00a0<\/strong><strong><em>HDF5 (Keras) Format<\/em><a name=\"DifferentTFModels2\"><\/a><\/strong>\r\n<h6><\/h6>\r\nImport ResNet50V2 in the HDF5 format by using the <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importkerasnetwork.html\"><span style=\"font-family: courier;\">importKerasNetwork<\/span><\/a> function.\r\n<h6><\/h6>\r\n<pre>net = importKerasNetwork(\"ResNet50V2.h5\",...\r\n  OutputLayerType=\"classification\",...\r\n  Classes=ClassNames)<\/pre>\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"572\" height=\"168\" class=\"alignnone size-full wp-image-9175\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage4.png\" alt=\"\" \/>\r\n<h6><\/h6>\r\nThe software throws a warning about the Keras version of the imported model. In the documentation page of each import and export function, there is a Limitations section where the supported TensorFlow or ONNX\u2122 versions are described. For example, see <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importtensorflownetwork.html#mw_a75fbbca-987b-4fdc-b3d5-d93af43457f5\">Limitations of importTensorFlowNetwork<\/a>.\r\n<h6><\/h6>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\">Tip: Even though you can successfully import ResNet50V2 with both <span style=\"font-family: courier;\">importKerasNetwork<\/span> and <span style=\"font-family: courier;\">importTensorFlowNetwork<\/span>, I recommend using <span style=\"font-family: courier;\">importTensorFlowNetwork<\/span>. Reasons for this become apparent when trying to import a more complex model with layers not supported for conversion into built-in MATLAB layers, as shown in the next section.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h6><\/h6>\r\n<h2><strong>Importing More Complex TensorFlow Models<\/strong><a name=\"MoreComplexTF\"><\/a><\/h2>\r\nThis section shows how to import the TensorFlow model <strong>PNASNetLargeV4<\/strong> which, unlike ResNet50V2, contains a complex feature-vector layer. This layer cannot be converted directly into a built-in MATLAB layer, so you'll need a custom layer. The good news is that <em>MATLAB will autogenerate custom layers for you<\/em>.\r\n<h6><\/h6>\r\nTo get the PNASNetLargeV4 model, run this code in Python\u00ae. You can find the code at\u00a0<a href=\"https:\/\/tfhub.dev\/google\/imagenet\/pnasnet_large\/feature_vector\/4\">https:\/\/tfhub.dev\/google\/imagenet\/pnasnet_large\/feature_vector\/4<\/a>.\r\n<pre>import tensorflow as tf\r\nimport tensorflow_hub as hub\r\nmodel = tf.keras.Sequential([\r\n  hub.KerasLayer(\"https:\/\/tfhub.dev\/google\/imagenet\/pnasnet_large\/feature_vector\/4\",\r\n  trainable=True),\r\n  tf.keras.layers.Dense(1000, activation='softmax')\r\n  ])\r\nmodel.build([None, 331, 331, 3])\r\n<\/pre>\r\n<h6><\/h6>\r\nThe PNASNetLargeV4 model is trained with images from the ImageNet database.\r\n<h6><\/h6>\r\n<strong>Importing using the\u00a0<\/strong><strong>HDF5 (Keras) Format <em>(not recommended)<\/em><\/strong>\r\n<h6><\/h6>\r\nWhen you import PNASNetLargeV4, the software cannot convert some of the TensorFlow layers into equivalent built-in MATLAB layers.\r\n<h6><\/h6>\r\nFirst, try to import the model in HDF5 format by using the <span style=\"font-family: courier;\">importKerasNetwork<\/span> function.\r\n<h6><\/h6>\r\n<pre>net = importKerasNetwork(\"PNASNetLargeV4.h5\",...\r\n  OutputLayerType=\"classification\",...\r\n  Classes=ClassNames)<\/pre>\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"550\" height=\"88\" class=\"alignnone size-full wp-image-9178\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage5.png\" alt=\"\" \/>\r\n<h6><\/h6>\r\nThe error message suggests using <span style=\"font-family: courier;\">importKerasLayers<\/span> to import the network architecture. <span style=\"font-family: courier;\">importKerasLayers<\/span> will import the model as a layer graph with placeholder layers. You must manually replace the placeholder layers to use the network for prediction or to retrain the network. You can replace a placeholder layer with a handwritten layer (<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/assemble-network-from-pretrained-keras-layers.html\">documentation example: Assemble Network from Pretrained Keras Layers<\/a>) or functionLayer (<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ug\/replace-unsupported-keras-layer-with-function-layer.html\">documentation example: Replace Unsupported Keras Layer with Function Layer<\/a>).\r\n<h6><\/h6>\r\n<h3><strong>Importing using the SavedModel Format <em>(recommended)<\/em><\/strong><\/h3>\r\n<h6><\/h6>\r\nAlternatively, you can use the <span style=\"font-family: courier;\">importTensorFlowNetwork<\/span> function. It differs from its Keras equivalent because it has a unique feature; it can <strong>autogenerate custom layers<\/strong>. During importing, <span style=\"font-family: courier;\">importTensorFlowNetwork<\/span> generates custom layers in place of the TensorFlow layers that are not supported for conversion into built-in MATLAB layers.\r\n<h6><\/h6>\r\n<pre>net = importTensorFlowNetwork(\"PNASNetLargeV4\",...\r\nOutputLayerType=\"classification\",...\r\nClasses=ClassNames)<\/pre>\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"371\" height=\"177\" class=\"alignnone size-full wp-image-9181\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage6.png\" alt=\"\" \/>\r\n<h6><\/h6>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\">Tip: A quick way to check if custom layers were generated for you is to inspect the package that was created during the import process.\u00a0 E.g., for PNASNetLargeV4, the import function generates one large custom layer (kKerasLayer2Layer29666.m):<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h6><\/h6>\r\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" width=\"258\" height=\"407\" class=\"alignnone size-full wp-image-9184\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage7.png\" alt=\"\" \/><\/p>\r\n\r\n<h6><\/h6>\r\n<h3><strong><em>What if there is no SavedModel version of a model, just HDF5?<\/em><\/strong><\/h3>\r\nIn this scenario, I would recommend to try and convert the TensorFlow model from HDF5 to saved model format. In most cases, this is the way to perform the conversion from within TensorFlow:\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-9187 size-medium\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage8_TF-300x74.png\" alt=\"\" width=\"300\" height=\"74\" \/>\r\n<h6><\/h6>\r\nHowever, you might encounter a use case that requires \"model surgery\". For example, if an unsupported layer is written in the older version, and you attempt to save it in the newer SavedModel version, TensorFlow may throw an error stating that it doesn't know how to convert the model. In this situation you must do one of the following:\r\n<h6><\/h6>\r\n<ol>\r\n \t<li>Manually re-code the layer in the newer TensorFlow SavedModel format prior to importing the model into MATLAB.<\/li>\r\n \t<li>Import the older model into MATLAB using <span style=\"font-family: courier;\">importKerasLayers<\/span>. It will create a placeholder layer in place of the custom layer, which the software could not import. Then, from within MATLAB you manually re-code the layer.<\/li>\r\n<\/ol>\r\nBoth options require you to write layers in either TensorFlow (option 1) or MATLAB (option 2).\r\n<h6><\/h6>\r\n<h2><strong>Importing PyTorch Models<\/strong><a name=\"ImportingPyTorch\"><\/a><\/h2>\r\nCurrently, Deep Learning Toolbox does not support importing models directly from PyTorch; However, you can import the model via ONNX.\r\n<h6><\/h6>\r\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" width=\"236\" height=\"300\" class=\"alignnone size-medium wp-image-9190\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage9-236x300.png\" alt=\"\" \/><\/p>\r\n\r\n<h6><\/h6>\r\nFirst, you need to convert the PyTorch model to the ONNX model format by following the instructions in <a href=\"https:\/\/pytorch.org\/tutorials\/advanced\/super_resolution_with_onnxruntime.html\">Exporting a Model from PyTorch to ONNX<\/a>. Then, you can import the ONNX model into MATLAB by using <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importonnxnetwork.html\">importONNXNetwork<\/a>. Here, we show you how to import the squeezenet.onnx model, which you can also find in the PyTorch Model Zoo.\r\n<h6><\/h6>\r\n<pre>net = importONNXNetwork(\"squeezeNet.onnx\");<\/pre>\r\n<h6><\/h6>\r\nThe\u00a0<span style=\"font-family: courier;\">importONNXNetwork<\/span>\u00a0function can also generate custom layers when the software cannot convert ONNX operators into equivalent built-in MATLAB layers. Import the shufflenet-9.onnx model with autogenerated custom layers. By default, <span style=\"font-family: courier;\">importONNXNetwork<\/span>\u00a0returns a DAGNetwork object that is ready to use for prediction.\r\n<h6><\/h6>\r\n<pre>net = importONNXNetwork(\"shufflenet-9.onnx\",PackageName=\"shuffleNet\");<\/pre>\r\n<h6><\/h6>\r\n<span style=\"font-family: courier;\">importONNXNetwork<\/span>\u00a0saves the custom layers in the package +shuffleNet, in the current folder, similarly to <span style=\"font-family: courier;\">importTensorFlowNetwork<\/span>.\r\n<h6><\/h6>\r\nYou can also export a trained Deep Learning Toolbox network to the ONNX model format by using the <a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/exportonnxnetwork.html\"><span style=\"font-family: courier;\">exportONNXNetwork<\/span><\/a> function.\r\n<h6><\/h6>\r\n<pre>exportONNXNetwork(net,\"myNet.onnx\")<\/pre>\r\n<h6><\/h6>\r\n<h1>Example: Import Complex TensorFlow Model for Image Classification<a name=\"TensorFlowCompleteExample\"><\/a><\/h1>\r\nThis example shows how to import a pretrained TensorFlow model in the SavedModel format, and use the imported network to classify an image. The TensorFlow model contains layers that are not supported for conversion into built-in MATLAB layers. The software automatically generates custom layers when you import these layers.\r\n<h6><\/h6>\r\n<h2><strong>Import Pretrained TensorFlow Model<\/strong><\/h2>\r\nUse <a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/applications\/efficientnet_v2\/EfficientNetV2L\">tf.keras.applications.efficientnet_v2.EfficientNetV2L<\/a> to instantiate the EfficientV2L model.\r\n<h6><\/h6>\r\nThe EfficientNetV2L model is trained with images from the ImageNet database. Get the class names from SqueezeNet, which is also trained with ImageNet images.\r\n<pre>squeezeNet = squeezenet;\r\nClassNames = squeezeNet.Layers(end).Classes;<\/pre>\r\nImport the TensorFlow model EfficientNetV2L in the SavedModel format by using the <span style=\"font-family: courier;\">importTensorFlowNetwork<\/span> function. Specify the output layer type for an image classification problem.\r\n<pre>net = importTensorFlowNetwork(\"EfficientNetV2L\",...\r\n  OutputLayerType=\"classification\",...\r\n  Classes=ClassNames);<\/pre>\r\n<h6><\/h6>\r\nFind the autogenerated custom layers.\r\n<h6><\/h6>\r\n<pre>PackageName = '+EfficientNetV2L';\r\ns = what(['.\\' PackageName]); <span class=\"comment\">%'<\/span>\r\nind = zeros(1,length(s.m));\r\n\r\nfor i = 1:length(net.Layers)\r\n  for j = 1:length(s.m)\r\n    if strcmpi(class(net.Layers(i)),[PackageName(2:end) '.' s.m{j(1:end-2)])\r\n      ind(j) = i;\r\n    end\r\n  end\r\nend\r\n\r\ndisplay(ind)<\/pre>\r\n<h6><\/h6>\r\nind = 2\r\n<h6><\/h6>\r\n<pre>display(net.Layers(ind))<\/pre>\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"276\" height=\"177\" class=\"alignnone size-full wp-image-9196\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage10.png\" alt=\"\" \/>\r\n<h6><\/h6>\r\nAnalyze the imported network.\r\n<pre>analyzeNetwork(net)<\/pre>\r\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-9199 size-full\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage11.png\" alt=\"\" width=\"941\" height=\"619\" \/><\/p>\r\nAs expected, the imported network, which contains autogenerated custom layers, shows no warnings or errors in the network analyzer. This means the imported network is ready to use for prediction.\r\n<h2><strong>Read and Preprocess Input Image<\/strong><\/h2>\r\nTensorFlow provides the <a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/applications\/efficientnet_v2\/preprocess_input\">tf.keras.applications.efficientnet_v2.preprocess_input<\/a> method to preprocess image input data for the EfficientNetV2L model. Here, we replicate the input preprocessing by resizing, rescaling, and normalizing the input image.\r\n<h6><\/h6>\r\nRead the image you want to classify and display the size of the image. The image is 792-by-1056 pixels and has three color channels (RGB).\r\n<h6><\/h6>\r\n<pre>Im = imread(\"peacock.jpg\");\r\nsize(Im)<\/pre>\r\n<h6><\/h6>\r\nResize the image to the input size of the network.\r\n<h6><\/h6>\r\n<pre>InputSize = net.Layers(1).InputSize;\r\nIm = imresize(Im,InputSize(1:2));<\/pre>\r\n<h6><\/h6>\r\nThe inputs to\u00a0 EfficientNetV2L require further preprocessing. Rescale the image. Normalize the image by subtracting the training images mean and dividing by the training images standard deviation. To find the values that you should use, see <a href=\"https:\/\/github.com\/keras-team\/keras-applications\/blob\/master\/keras_applications\/imagenet_utils.py\">https:\/\/github.com\/keras-team\/keras-applications\/blob\/master\/keras_applications\/imagenet_utils.py<\/a>.\r\n<h6><\/h6>\r\n<pre>ImProcessed = rescale(Im,0,1);\r\nmeanIm = [0.485 0.456 0.406];\r\nstdIm = [0.229 0.224 0.225];\r\nImProcessed = (ImProcessed-reshape(meanIm,[1 1 3])).\/reshape(stdIm,[1 1 3]);<\/pre>\r\n<h2><strong>Classify Image Using the Imported Network<\/strong><\/h2>\r\nClassify the image using the imported network. Show the image with the classification label.\r\n<h6><\/h6>\r\n<pre>label = classify(net,Im);\r\nimshow(Im)\r\ntitle(strcat(\"Predicted label: \",string(label)))<\/pre>\r\n<h6><\/h6>\r\n<p style=\"text-align: center;\"><img decoding=\"async\" loading=\"lazy\" width=\"234\" height=\"250\" class=\"alignnone size-full wp-image-9229\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/blogImage12.png\" alt=\"\" \/><\/p>\r\n\r\n<h6><\/h6>\r\nYou can also use the imported network with the Predict block of the Deep Learning Toolbox, to classify an image in Simulink. For an example, see the previous post: <a href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2021\/04\/16\/bringing-tensorflow-models-into-matlab\/?s_tid=srchtitle_tensorflow_1\">Bringing TensorFlow Models into MATLAB<\/a>. The fact that the imported network contains autogenerated custom layers doesn\u2019t hinder modeling in Simulink.\r\n<h6><\/h6>\r\n<h1>Summary<\/h1>\r\n<h6><\/h6>\r\nInteroperability with deep learning frameworks, such as TensorFlow and PyTorch, enables you to do more in MATLAB and Simulink. It gives the flexibility to take full advantage of the MATLAB ecosystem and integrate it with resources developed by the open-source community. It also enables you to combine workflows that include data-centric preprocessing, model tuning, model compression, model integration, and automatic code generation for models developed outside MATLAB.\r\n<h6><\/h6>\r\n<strong>Comparison of capabilities for working with deep learning models in MATLAB<a name=\"ComparisonTable\"><\/a><\/strong>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\"><strong>Capability<\/strong><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"133\">Models created using <strong><a href=\"https:\/\/www.mathworks.com\/products\/deep-learning.html\">Deep Learning Toolbox<\/a><\/strong><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"133\">Models\r\n<strong><a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/deep-learning-import-and-export.html\">Converted<\/a><\/strong><strong> from other FrameWorks<\/strong><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"133\"><strong>Co-Execution<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" height=\"50\">Integrates with pre and post processing in MATLAB<\/td>\r\n<td style=\"padding: 10px; border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" height=\"50\">Requires installation of products only<\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" height=\"50\">Supports debugging from MATLAB<\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" height=\"50\">Inference performance in MATLAB and Simulink<\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #faab43;\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" height=\"50\">Available MATLAB application examples<\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\" height=\"30\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" height=\"50\">Requires no datatype conversion and data reformatting<\/td>\r\n<td style=\"padding: 10px; border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #faab43;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding-left: 10px; padding-top: 5px; border: 1px solid black;\" height=\"50\">Coverage for embedded code generation with <a href=\"https:\/\/www.mathworks.com\/products\/matlab-coder.html\">MATLAB Coder<\/a>, <a href=\"https:\/\/www.mathworks.com\/products\/gpu-coder.html\">GPU Coder<\/a> &amp; <a href=\"https:\/\/www.mathworks.com\/products\/deep-learning-hdl.html\">Deep Learning HDL Toolbox<\/a><\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #faab43;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding-left: 10px; padding-top: 5px; border: 1px solid black;\" height=\"50\">Requires no additional libraries for standalone deployment from <a href=\"https:\/\/www.mathworks.com\/products\/compiler.html\">MATLAB Compiler<\/a><\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #faab43;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" height=\"50\">Access popular models in a single line of code<\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" height=\"50\">Access to open-source models from TensorFlow and PyTorch<\/td>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #faab43;\"><\/td>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td style=\"border: 1px solid black; background-color: #66bf5a;\"><\/td>\r\n<td style=\"padding-left: 5px;\">\u00a0 Most support<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"border: 1px solid black; background-color: #faab43;\"><\/td>\r\n<td style=\"padding-left: 5px;\">\u00a0 Limited support<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"border: 1px solid black; background-color: #cf2825;\"><\/td>\r\n<td style=\"padding-left: 5px;\">\u00a0 No support<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h6><\/h6>\r\n<h2>More About Support Packages<\/h2>\r\nYou must have support packages to run the import and export functions in Deep Learning Toolbox. If the support package is not installed, each function provides a download link to the corresponding support package in the Add-On Explorer. A recommended practice is to download the support package to the default location for the version of MATLAB you are running. You can also directly download the support packages from File Exchange.\r\n<h6><\/h6>\r\n<strong>Table: Support packages required to run import and export functions.<\/strong>\r\n<h6><\/h6>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"208\"><strong>Support Package<\/strong><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"208\"><strong>Functions<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"208\"><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64649-deep-learning-toolbox-converter-for-tensorflow-models\">Deep Learning Toolbox Converter for TensorFlow Models<\/a><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"208\"><a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importtensorflownetwork.html\">importTensorFlowNetwork<\/a>\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importtensorflowlayers.html\">importTensorFlowLayers<\/a>\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importkerasnetwork.html\">importKerasNetwork<\/a>\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importkeraslayers.html\">importKerasLayers<\/a><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"208\"><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/67296-deep-learning-toolbox-converter-for-onnx-model-format\">Deep Learning Toolbox Converter for ONNX Model Format<\/a><\/td>\r\n<td style=\"padding: 10px; border: 1px solid black;\" width=\"208\"><a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importonnxnetwork.html\">importONNXNetwork<\/a>\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importonnxlayers.html\">importONNXLayers<\/a>\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/importonnxfunction.html\">importONNXFunction<\/a>\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/help\/deeplearning\/ref\/exportonnxnetwork.html\">exportONNXNetwork<\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h6><\/h6>\r\n<strong>Table: Comparing high-level features of the three functions that can import deep learning networks.<\/strong>\r\n<h6><\/h6>\r\n<table style=\"height: 220px;\" width=\"705\">\r\n<tbody>\r\n<tr>\r\n<td style=\"border: 1px solid black;\" width=\"176\"><\/td>\r\n<td style=\"border: 1px solid black; padding: 10px;\" width=\"176\"><strong>importTensorFlowNetwork<\/strong><\/td>\r\n<td style=\"border: 1px solid black; padding: 10px;\" width=\"152\"><strong>importKerasNetwork<\/strong><\/td>\r\n<td style=\"border: 1px solid black; padding: 10px;\" width=\"153\"><strong>importONNXNetwork<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"border: 1px solid black; padding: 10px;\">Support for TensorFlow<\/td>\r\n<td style=\"border: 1px solid black; text-align: center; vertical-align: middle;\"><span style=\"font-size: 20px;\">\u2705*<\/span><\/td>\r\n<td style=\"border: 1px solid black; text-align: center; vertical-align: middle;\"><span style=\"font-size: 20px;\">\u2705**<\/span><\/td>\r\n<td style=\"border: 1px solid black;\" width=\"153\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"border: 1px solid black; padding: 10px;\">Support for PyTorch<\/td>\r\n<td style=\"border: 1px solid black; padding: 10px;\" width=\"176\"><\/td>\r\n<td style=\"border: 1px solid black; padding: 10px;\" width=\"152\"><\/td>\r\n<td style=\"border: 1px solid black; text-align: center; vertical-align: middle;\"><span style=\"font-size: 20px;\">\u2705<\/span><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"border: 1px solid black; padding: 10px;\">Support for autogeneration of custom layers<\/td>\r\n<td style=\"border: 1px solid black; text-align: center; vertical-align: middle;\"><span style=\"font-size: 20px;\">\u2705<\/span><\/td>\r\n<td style=\"border: 1px solid black;\" width=\"152\"><\/td>\r\n<td style=\"border: 1px solid black; text-align: center; vertical-align: middle;\"><span style=\"font-size: 20px;\">\u2705<\/span><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<span style=\"font-size: 12px;\">*\u00a0Currently supports TensorFlow versions 2.0-2.6 (thoroughly tested). In most cases, models saved in newer TensorFlow versions are also importable.\r\nRequires <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64649-deep-learning-toolbox-converter-for-tensorflow-models\"><em>Deep Learning Toolbox Converter for TensorFlow Models<\/em><\/a><\/span>\r\n<h6><\/h6>\r\n<span style=\"font-size: 12px;\">** Supports TensorFlow-Keras versions up to 2.2.4, with limited support for versions 2.2.5 to 2.4.0.\r\nRequires <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64649-deep-learning-toolbox-converter-for-tensorflow-models\"><em>Deep Learning Toolbox Converter for TensorFlow Models<\/em><\/a><\/span>\r\n<h6><\/h6>\r\nThe software in the support packages is updated monthly. These monthly updates can include new layer support for import and export, updated TensorFlow and ONNX version support, and bug fixes. Major features are released with the general release.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<script language=\"JavaScript\"> <!-- function grabCode_5ba588cb17fa4daa9efac0cb1b2643be() { \/\/ Remember the title so we can use it in the new page title = document.title; copyright = 'Copyright 2022 The MathWorks, Inc.'; window.open(\"https:\/\/github.com\/matlab-deep-learning\/Image-Classification-in-MATLAB-Using-Converted-TensorFlow-Model\", '_blank'); } --> <\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code<noscript>(requires JavaScript)<\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae R2020b<\/p>\r\n<!-- 5ba588cb17fa4daa9efac0cb1b2643be ##### SOURCE BEGIN ##### ##### SOURCE END ##### 5ba588cb17fa4daa9efac0cb1b2643be -->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2022\/03\/BlogImage1.png\" onError=\"this.style.display ='none';\" \/><\/div><p>The following post is from Sivylla Paraskevopoulou, Senior Technical Writer and David Willingham, Product Manager for Deep Learning Toolbox.\r\nRead our newest blog post on how to convert (import and... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2022\/03\/18\/importing-models-from-tensorflow-pytorch-and-onnx\/\">read more >><\/a><\/p>","protected":false},"author":156,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/9163"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/users\/156"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/comments?post=9163"}],"version-history":[{"count":84,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/9163\/revisions"}],"predecessor-version":[{"id":14877,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/9163\/revisions\/14877"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/media?parent=9163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/categories?post=9163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/tags?post=9163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}