You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+88-21
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
### This example shows how to call a TensorFlow™ model from MATLAB® using co-execution with Python®.
4
4
5
-

5
+

6
6
7
7
There are different options for accessing deep learning models within MATLAB. These include:
8
8
@@ -20,9 +20,10 @@ Co-execution between MATLAB and TensorFlow is when both frameworks are used toge
20
20
21
21
### Requirements
22
22
23
-
The example requires the following software:
23
+
The example requires the following to be installed:
24
24
25
25
*[MATLAB R2021b or later](https://www.mathworks.com/products/matlab.html)
26
+
*[MATLAB Live Editor task for Python](https://github.com/mathworks/MATLAB-Live-Task-for-Python) - Note: This must be installed prior to running the examples in this repo.
@@ -45,22 +46,84 @@ There are many benefits to co-execution. These include:
45
46
* Note: For versions R2022a or newer, MATLAB supports integration with TensorFlow Lite (TFLite) pretrained models. This enables the simulation of TensorFlow Lite models from both MATLAB and Simulink. For code generation, MATLAB generates code for pre and/or post-processing and generates a call to the TensorFlow Lite interpreter on a supported target.
46
47
* Datatype conversion and data reformatting - Only select data types in both frameworks are supported for co-execution. This is because not all data types from one framework can be mapped and translated into an equivalent type in the other framework. For a list of data types that can be used, see [MATLAB to Python Data Type Mapping](https://www.mathworks.com/help/matlab/matlab_external/passing-data-to-python.html),
47
48
48
-
##How can co-execution be performed?
49
+
# How can co-execution be performed?
49
50
50
-
The [example](https://github.com/matlab-deep-learning/Image-Classification-in-MATLAB-Using-TensorFlow/blob/main/ImageClassificationinMATLABusingTensorFlow.m) shows how MATLAB can co-execute with TensorFlow to classify images using a model from [tf.Keras.applications](https://www.tensorflow.org/api_docs/python/tf/keras/applications). The following steps outline what is covered in the example:
51
+
In this repo, 2 workflows for performing co-execution are presented.
52
+
53
+
[1.](#matlabcallingtflivetask)**MATLAB calling a TensorFlow model using a Live Editor task**
54
+
55
+
[2.](#matlabcallingtf)**MATLAB calling a TensorFlow model using MATLAB commands**
56
+
57
+
## MATLAB calling a TensorFlow model using Live Editor tasks <aname="matlabcallingtflivetask"/>
58
+
59
+
Using the MATLAB Live Editor task for Python enables users to write or copy & paste Python code into MATLAB.
60
+
61
+
Steps to use the MATLAB Live Editor task for Python are:
62
+
* Write or copy & paste Python code into the Live Editor task
63
+
* Define MATLAB input variables to be called in Python
64
+
* Define Python ouput variables to be called in MATLAB
65
+
66
+
Example code available here: [MATLAB calling TensorFlow model for Image Classification using a Live Editor task.mlx](https://insidelabs-git.mathworks.com/dwilling/matlab-calling-tensorflow-model-for-image-classification-using-a-live-task/-/blob/main/MATLAB%20calling%20TensorFlow%20model%20for%20Image%20Classification%20using%20a%20Live%20Editor%20Task.mlx)
67
+
68
+
***Read in image**
69
+
70
+
```matlab:Code(Display)
71
+
imgOriginal = imread("./images/banana.png");
72
+
imshow(imgOriginal)
73
+
```
74
+

75
+
76
+
Each pretrained model in tensorflow.keras.applications takes input images of different sizes. Therefore the image being classified needs to be resized.
TensorFlow orients image data in a different format to MATLAB. This requires conversion (HWCN TO NHWC)
83
+
```matlab:Code(Display)
84
+
imgforTF = permute(img, [4 1 2 3]);
85
+
batch_size = int32(1); % Tensorflow require inputs to be converted to int32.
86
+
```
87
+
88
+
***Import TensorFlow model using the Live Task for Python**
89
+
90
+

91
+
92
+

93
+
94
+
## MATLAB calling a TensorFlow model using MATLAB commands <aname="matlabcallingtf"/>
95
+
96
+
Example code available here: [ImageClassificationinMATLABusingTensorFlow.m](https://github.com/matlab-deep-learning/Image-Classification-in-MATLAB-Using-TensorFlow/blob/main/ImageClassificationinMATLABusingTensorFlow.m)
51
97
52
98
***Configuring python setup:**
53
99
54
100
The script [checkPythonSetup](https://github.com/matlab-deep-learning/Image-Classification-in-MATLAB-Using-TensorFlow/blob/main/checkPythonSetup.m) contains commands to help set up the python environment. You don't need to run these commands, unless the default Python configuration causes errors.
101
+
```matlab:Code(Display)
102
+
checkPythonSetup
103
+
```
55
104
56
105
For more information on setting up or troubleshooting the Python Environment in MATLAB see [Calling Python from MATLAB](https://www.mathworks.com/help/matlab/call-python-libraries.html)
% TensorFlow orients image data in a different format to MATLAB. This
121
+
% requires conversion (HWCN TO NHWC)
122
+
imgforTF = permute(img, [4 1 2 3]);
123
+
```
124
+
125
+
batch_size = int32(1); % Tensorflow require inputs to be converted to int32.
126
+
64
127
***Importing model directly into MATLAB:**
65
128
```matlab:Code(Display)
66
129
model = py.tensorflow.keras.applications.efficientnet_v2.EfficientNetV2L();
@@ -84,33 +147,37 @@ Note that many [pretrained models](https://www.mathworks.com/help/deeplearning/u
84
147
85
148
***Gathering and displaying the classification result in MATLAB:**
86
149
```matlab:Code(Display)
150
+
label = label{1}{1}{2}; % The label is stored in a nested cell. In the file layer of the cell there is a tuple (id, class, probability) - The predicted class label is the 2nd element of the tuple
87
151
labelStr = string(label);
88
152
imshow(imgOriginal);
89
153
title(labelStr,Interpreter="none");
90
154
```
91
-

155
+

156
+
157
+
158
+
92
159
93
160
94
161
## Comparison of Models accessible in MATLAB <aname="comparison-table"/>
95
162
| Capability | Models created using the [Deep Learning Toolbox](https://www.mathworks.com/products/deep-learning.html)| Models [Converted from other Frameworks](https://www.mathworks.com/help/deeplearning/deep-learning-import-and-export.html)| Co-execution |
| Integrates with pre and post processing with MATLAB ||||
98
-
| Requires installation of MATLAB products only ||||
99
-
| Supports debugging from MATLAB||||
100
-
| Offers best inference performance in MATLAB and Simulink||||
101
-
| Comes with many MATLAB application examples ||||
102
-
| Requires no datatype conversion and data reformatting ||||
103
-
| Provides largest coverage for embedded code generation with [MATLAB Coder](https://www.mathworks.com/products/matlab-coder.html), [GPU Coder](https://www.mathworks.com/products/gpu-coder.html) & [Deep Learning HDL Toolbox](https://www.mathworks.com/products/deep-learning-hdl.html)||||
104
-
| Requires no additional libraries for standalone deployment with [MATLAB Compiler](https://www.mathworks.com/products/compiler.html)||||
105
-
| Accesses popular models in a single line of code ||||
106
-
| Access to models from TensorFlow and PyTorch ||||
164
+
| Integrates with pre and post processing with MATLAB ||||
165
+
| Requires installation of MATLAB products only ||||
166
+
| Supports debugging from MATLAB||||
167
+
| Offers best inference performance in MATLAB and Simulink||||
168
+
| Comes with many MATLAB application examples ||||
169
+
| Requires no datatype conversion and data reformatting ||||
170
+
| Provides largest coverage for embedded code generation with [MATLAB Coder](https://www.mathworks.com/products/matlab-coder.html), [GPU Coder](https://www.mathworks.com/products/gpu-coder.html) & [Deep Learning HDL Toolbox](https://www.mathworks.com/products/deep-learning-hdl.html)||||
171
+
| Requires no additional libraries for standalone deployment with [MATLAB Compiler](https://www.mathworks.com/products/compiler.html)||||
172
+
| Accesses popular models in a single line of code ||||
173
+
| Access to models from TensorFlow and PyTorch ||||
107
174
108
175
109
176
Key:
110
177
111
-
 Most support and / or low effort
112
-
 Some support and / or some effort
113
-
 Little to no support and / or high effort
178
+
 Most support and / or low effort
179
+
 Some support and / or some effort
180
+
 Little to no support and / or high effort
0 commit comments