What’s New in Interoperability with TensorFlow and PyTorch
source link: https://blogs.mathworks.com/deep-learning/2022/10/04/whats-new-in-interoperability-with-tensorflow-and-pytorch/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
What’s New in Interoperability with TensorFlow and PyTorch
For deep learning, MATLAB allows users to create and train models in MATLAB or leverage models trained in open source via model conversion. Prior to MATLAB R2022b, support for model conversion included: import from and export to ONNX™, and import from TensorFlow™. We are excited to share that as of MATLAB R2022b, users can now export models to TensorFlow as Python® code and can import models from PyTorch® (starting with support for image classification).
Export to TensorFlow
The support package Deep Learning Toolbox Converter for TensorFlow Models just added the capability to export from MATLAB to TensorFlow, by using the exportNetworkToTensorFlow function.
There are many reasons to be excited about the new exportNetworkToTensorFlow function:
|
Here I am showing the basic workflow of how to export a deep learning network to TensorFlow, load it as a TensorFlow model, and save it in SavedModel format.
MATLAB Code: Load a pretrained network. The Pretrained Deep Neural Networks documentation page shows you all options of how to get a pretrained network. You can alternatively create your own network. net = darknet19; Export the network net to TensorFlow. The exportNetworkToTensorFlow function saves the TensorFlow model in the Python package DarkNet19. exportNetworkToTensorFlow(net,"DarkNet19") |
The DarkNet19 package contains four files:
- The _init_.py file, which defines the DarkNet19 folder as a regular Python package.
- The model.py file, which contains the code that defines the untrained TensorFlow-Keras model.
- The README.txt file, which provides instructions on how to load the TensorFlow model and save it in HDF5 or SavedModel format.
- The weights.h5 file which contains the model weights in HDF5 format.
Figure: The exported TensorFlow model is saved in the regular Python package DarkNet19.
Python Code: Load the exported TensorFlow model from the DarkNet19 package. import DarkNet19 model = DarkNet19.load_model() Save the exported model in the SavedModel format. model.save("DarkNet19_savedmodel") |
Import from PyTorch
In R2022b we introduced the Deep Learning Toolbox Converter for PyTorch Models support package. This initial release supports importing image classification models. Support for other model types will be added in future releases.
Use the importNetworkFromPyTorch function to import a PyTorch model. Make sure that the PyTorch model that you are importing is pretrained and traced. I am showing you here how to import an image classification model from PyTorch and initialize it.
Python Code: Load a pretrained image classification model from the TorchVision library. import torch from torchvision import models model = models.mnasnet1_0(pretrained=True) Trace the PyTorch model. For more information on how to trace a PyTorch model, go to Torch documentation: Tracing a function. X = torch.rand(1,3,224,224) traced_model = torch.jit.trace(model.forward,X) Save the PyTorch model. traced_model.save("traced_mnasnet1_0.pt") |
MATLAB Code: Import the PyTorch model into MATLAB by using the importNetworkTFromPyTorch function. The function imports the model as an uninitialized dlnetwork object without an input layer. net = importNetworkFromPyTorch("traced_mnasnet1_0.pt"); Specify the input size of the imported network and create an image input layer. Then, add the image input layer to the imported network and initialize the network by using the addInputLayer function (also new in R2022b). InputSize = [224 224 3]; InputLayer = imageInputLayer(InputSize,Normalization="none"); net = addInputLayer(net,InputLayer,Initialize=true); |
You might have noticed in the above code that the input dimensions in PyTorch and MATLAB have a different order. For more information, see Input Dimension Ordering for Deep Learning Platforms.
For more details on how to import a PyTorch model, how to initialize the imported model, and how to perform workflows (such as prediction and training) on the imported model, see the Examples of the importNetworkTFromPyTorch documentation page.
Interoperability Capabilities Summary
The interoperability support packages allow you to connect Deep Learning Toolbox with TensorFlow, Pytorch, and ONNX. Use the import and export functions to access models available in open-source repositories and collaborate with colleagues who work in other deep learning frameworks.
More information:
- To find all the available import and export functions (and their documentation), go to Deep Learning Import and Export.
- To learn more about how to import and export networks, see Interoperability Between Deep Learning Toolbox, TensorFlow, PyTorch, and ONNX.
- For answers to common questions about importing models, see Tips on Importing Models from TensorFlow, PyTorch, and ONNX.
- If you are working just in MATLAB, you can probably find a suitable network in our constantly-updated model repository: MATLAB Deep Learning Model Hub.
- Check out our previous blog post Importing Models from TensorFlow, PyTorch, and ONNX; you will find useful tips on importing and an example you can download (focus on importing from TensorFlow).
Recommend
-
103
开源 | 伯克利AI分布式框架Ray,兼容TensorFlow、PyTorch与MXNet
-
76
PyTorch一周年战绩总结:是否比TensorFlow来势凶猛?PyTorch一周年战绩总结:是否比TensorFlow来势凶猛?
-
84
有一个令人震惊的事实,即人工智能和机器学习的工具和技术在近期迅速兴起。深度学习,或者说“注射了激素的机器学习”,数据科学家和机器学习专家在这个领域有数不胜数等可用的库和框架。很多这样的框架都是基于 P…
-
57
So, you want to learn deep learning? Whether you want to start applying it to your business, base your next side project on it, or simply gain marketable skills – picking the right deep learning framework to learn is the...
-
39
-
45
机器之心 原...
-
57
-
46
据 CNBC 报道,微软虽然还在继续维护其开源的认知工具包 CNTK ,但在最近几个月里,该公司改变了方向,选择与 Facebook 更紧密地合作,为 PyTorch 的开发贡献代码。TensorFlow是目前最受...
-
7
An Observation on TensorFlow and PyTorch Startup Time Oct 18, 2019 I'm in the process of designing a STANDARD IN processing system implemented in Python using some fairly simple data processing and now some Py...
-
34
Importing Models from TensorFlow, PyTorch, and ONNX » Deep Learning The following post is from Sivylla Paraskevopoulou, Senior Technical Writer and
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK