For reference, the following table provides a comparison for all options.
What is co-execution?
Co-execution between MATLAB and TensorFlow is when both frameworks are used together, in executing a single application. Co-execution can achieve this by passing data to and from each framework. It requires MATLAB and Tensorflow to be installed on the same machine.
Requirements
The example requires the following to be installed:
There are many benefits to co-execution. These include:
You can leverage the features and capabilities of both frameworks together in a single application. For example, MATLAB performs the pre-and post-processing of data when training a model in TensorFlow.
Teams working in multiple frameworks can build applications together. This reduces the need to:
Rely on a single language
Manually re-code code from one language to another.
Force some team members to work in their non-preferred environment.
What are the limitations of co-execution?
Performance - Due to data passed between frameworks, there is a latency introduced. In most situations, the latency overhead is negligible, e.g., when performing model training. In other situations, e.g., performing inference at high throughputs, the latency impact is higher.
Automatic code generation to embedded-devices. MATLAB can automatically generate code for the following: C/C++ using MATLAB Coder, CUDA using GPU Coder and, VHDL using Deep Learning HDL Toolbox, including the deep learning model and pre and post-processing steps. This deployment option is not available when co-execution with TensorFlow.
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.
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,
How can co-execution be performed?
In this repo, 2 workflows for performing co-execution are presented.
1.MATLAB calling a TensorFlow model using a Live Editor task
2.MATLAB calling a TensorFlow model using MATLAB commands
MATLAB calling a TensorFlow model using Live Editor tasks
Each pretrained model in tensorflow.keras.applications takes input Images of different sizes. Therefore the image being classified needs to be resized.
The script checkPythonSetup contains commands to help set up the python environment. You don't need to run these commands, unless the default Python configuration causes errors.
checkPythonSetup
For more information on setting up or troubleshooting the Python Environment in MATLAB see Calling Python from MATLAB
imageHWSize = 480;
img = imresize(imgOriginal, [imageHWSize, imageHWSize]);
% TensorFlow orients image data in a different format to MATLAB. This
% requires conversion (HWCN TO NHWC)
imgforTF = permute(img, [4 1 2 3]);
batch_size = int32(1); % Tensorflow require inputs to be converted to int32.
Importing model directly into MATLAB:
model = py.tensorflow.keras.applications.efficientnet_v2.EfficientNetV2L();
Calling a TensorFlow pretrained model for image classification:
% converting input from MATLAB array into Python array.
X = py.numpy.asarray(imgforTF);
% call preprocessing function that is required for the image input in Keras.
X = py.tensorflow.keras.applications.efficientnet_v2.preprocess_input(X);
% classify image
Y = model.predict(X, batch_size);
% label of classification output
label = py.tensorflow.keras.applications.efficientnet_v2.decode_predictions(Y);
Note that many pretrained models are available for use directly in MATLAB without the need for co-execution.
Gathering and displaying the classification result in MATLAB:
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
labelStr = string(label);
imshow(imgOriginal);
title(labelStr,Interpreter="none");
请发表评论