Skip to content

Commit 80971bc

Browse files
Added Live Edit task for Python example
1 parent 66e855c commit 80971bc

3 files changed

+88
-21
lines changed

Images/liveeditortask.png

177 KB
Loading

README.md

+88-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### This example shows how to call a TensorFlow™ model from MATLAB® using co-execution with Python®.
44

5-
![](Images/mainImage.png)
5+
![](images/mainImage.png)
66

77
There are different options for accessing deep learning models within MATLAB. These include:
88

@@ -20,9 +20,10 @@ Co-execution between MATLAB and TensorFlow is when both frameworks are used toge
2020

2121
### Requirements
2222

23-
The example requires the following software:
23+
The example requires the following to be installed:
2424

2525
* [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.
2627
* [Tensorflow 2.8](https://www.tensorflow.org/install)
2728
* [Python 3.8+](https://www.python.org/downloads/)
2829

@@ -45,22 +46,84 @@ There are many benefits to co-execution. These include:
4546
* 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.
4647
* 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),
4748

48-
## How can co-execution be performed?
49+
# How can co-execution be performed?
4950

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 <a name="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+
![](images/banana.png)
75+
76+
Each pretrained model in tensorflow.keras.applications takes input images of different sizes. Therefore the image being classified needs to be resized.
77+
78+
```matlab:Code(Display)
79+
imageHWSize = 480;
80+
img = imresize(imgOriginal, [imageHWSize, imageHWSize]);
81+
```
82+
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+
![](images/liveeditortask.png)
91+
92+
![](images/bananaClassified.png)
93+
94+
## MATLAB calling a TensorFlow model using MATLAB commands <a name="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)
5197

5298
* **Configuring python setup:**
5399

54100
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+
```
55104

56105
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)
57106

58107
* **Loading an image:**
59108
```matlab:Code(Display)
60-
imgOriginal = imread("./Images/banana.png");
109+
imgOriginal = imread("./images/banana.png");
61110
imshow(imgOriginal)
62111
```
63-
![](Images/banana.png)
112+
![](images/banana.png)
113+
114+
* **Preparing inputs:**
115+
116+
```matlab:Code(Display)
117+
imageHWSize = 480;
118+
img = imresize(imgOriginal, [imageHWSize, imageHWSize]);
119+
120+
% 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+
64127
* **Importing model directly into MATLAB:**
65128
```matlab:Code(Display)
66129
model = py.tensorflow.keras.applications.efficientnet_v2.EfficientNetV2L();
@@ -84,33 +147,37 @@ Note that many [pretrained models](https://www.mathworks.com/help/deeplearning/u
84147

85148
* **Gathering and displaying the classification result in MATLAB:**
86149
```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
87151
labelStr = string(label);
88152
imshow(imgOriginal);
89153
title(labelStr,Interpreter="none");
90154
```
91-
![](Images/bananaClassified.png)
155+
![](images/bananaClassified.png)
156+
157+
158+
92159

93160

94161
## Comparison of Models accessible in MATLAB <a name="comparison-table"/>
95162
| 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 |
96163
| ------------- | ------------- | ------------- | ------------- |
97-
| Integrates with pre and post processing with MATLAB | ![](Images/000000.png) | ![](Images/000000.png) | ![](Images/000000.png) |
98-
| Requires installation of MATLAB products only | ![](Images/000000.png) | ![](Images/000000.png) | ![](Images/000002.png) |
99-
| Supports debugging from MATLAB| ![](Images/000000.png) | ![](Images/000000.png) | ![](Images/000002.png) |
100-
| Offers best inference performance in MATLAB and Simulink| ![](Images/000000.png) | ![](Images/000000.png) | ![](Images/000002.png) |
101-
| Comes with many MATLAB application examples | ![](Images/000000.png) | ![](Images/000000.png) | ![](Images/000002.png) |
102-
| Requires no datatype conversion and data reformatting | ![](Images/000000.png) | ![](Images/000001.png) | ![](Images/000002.png) |
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) | ![](Images/000000.png) | ![](Images/000001.png) | ![](Images/000002.png) |
104-
| Requires no additional libraries for standalone deployment with [MATLAB Compiler](https://www.mathworks.com/products/compiler.html) | ![](Images/000000.png) | ![](Images/000001.png) | ![](Images/000002.png) |
105-
| Accesses popular models in a single line of code | ![](Images/000000.png) | ![](Images/000002.png) | ![](Images/000002.png) |
106-
| Access to models from TensorFlow and PyTorch | ![](Images/000002.png) | ![](Images/000001.png) | ![](Images/000000.png) |
164+
| Integrates with pre and post processing with MATLAB | ![](images/000000.png) | ![](images/000000.png) | ![](images/000000.png) |
165+
| Requires installation of MATLAB products only | ![](images/000000.png) | ![](images/000000.png) | ![](images/000002.png) |
166+
| Supports debugging from MATLAB| ![](images/000000.png) | ![](images/000000.png) | ![](images/000002.png) |
167+
| Offers best inference performance in MATLAB and Simulink| ![](images/000000.png) | ![](images/000000.png) | ![](images/000002.png) |
168+
| Comes with many MATLAB application examples | ![](images/000000.png) | ![](images/000000.png) | ![](images/000002.png) |
169+
| Requires no datatype conversion and data reformatting | ![](images/000000.png) | ![](images/000001.png) | ![](images/000002.png) |
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) | ![](images/000000.png) | ![](images/000001.png) | ![](images/000002.png) |
171+
| Requires no additional libraries for standalone deployment with [MATLAB Compiler](https://www.mathworks.com/products/compiler.html) | ![](images/000000.png) | ![](images/000001.png) | ![](images/000002.png) |
172+
| Accesses popular models in a single line of code | ![](images/000000.png) | ![](images/000002.png) | ![](images/000002.png) |
173+
| Access to models from TensorFlow and PyTorch | ![](images/000002.png) | ![](images/000001.png) | ![](images/000000.png) |
107174

108175

109176
Key:
110177

111-
![](Images/000000.png) Most support and / or low effort
112-
![](Images/000001.png) Some support and / or some effort
113-
![](Images/000002.png) Little to no support and / or high effort
178+
![](images/000000.png) Most support and / or low effort
179+
![](images/000001.png) Some support and / or some effort
180+
![](images/000002.png) Little to no support and / or high effort
114181

115182
Copyright 2022, The MathWorks, Inc.
116-
183+
Copyright 2022, The MathWorks, Inc.

0 commit comments

Comments
 (0)