Home

Awesome

Image Classification in MATLAB Using TensorFlow

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

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

  1. Using models created in MATLAB using the Deep Learning Toolbox
  2. Converting models from other frameworks into MATLAB
  3. Co-executing models from other frameworks with MATLAB

This example provides an overview of how to perform 3. Co-execution. For a further example on co-execution see Hyperparameter Tuning in MATLAB using Experiment Manager & TensorFlow.

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:

For more information about installing Python, see Install Supported Python Implementation.

Why perform co-execution?

There are many benefits to co-execution. These include:

What are the limitations of co-execution?

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 <a name="matlabcallingtflivetask"/>

Using the MATLAB Live Editor task for Python enables users to write or copy & paste Python code into MATLAB.

Steps to use the MATLAB Live Editor task for Python are:

Example code available here: MATLAB calling TensorFlow model for Image Classification using a Live Editor task.mlx

imgOriginal = imread("./Images/banana.png");
imshow(imgOriginal)

Each pretrained model in tensorflow.keras.applications takes input Images of different sizes. Therefore the image being classified needs to be resized.

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.

MATLAB calling a TensorFlow model using MATLAB commands <a name="matlabcallingtf"/>

Example code available here: ImageClassificationinMATLABusingTensorFlow.m

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

imgOriginal = imread("./Images/banana.png");
imshow(imgOriginal)

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.

model = py.tensorflow.keras.applications.efficientnet_v2.EfficientNetV2L();  
% 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.

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");

Comparison of Models accessible in MATLAB <a name="comparison-table"/>

CapabilityModels created using the Deep Learning ToolboxModels Converted from other FrameworksCo-execution
Integrates with pre and post processing with MATLAB
Requires installation of MATLAB products only
Supports debugging from MATLAB
Offers best inference performance in MATLAB and Simulink
Comes with many MATLAB application examples
Requires no datatype conversion and data reformatting
Provides largest coverage for embedded code generation with MATLAB Coder, GPU Coder & Deep Learning HDL Toolbox
Requires no additional libraries for standalone deployment with MATLAB Compiler
Accesses popular models in a single line of code
Access to models from TensorFlow and PyTorch

Key:

Most support and / or low effort Some support and / or some effort Little to no support and / or high effort

Copyright 2023, The MathWorks, Inc.