MATLAB實現整合系列第一期:MATLAB中寫Python,與TensorFlow,PyTorch整合

Fred Liu
8 min readApr 25, 2023

--

關於在MATLAB與Python混合編成,以及跟TensorFlow,PyTorch整合的流程,我們會用以下的程式碼與YouTube影片來做輔助說明。

YouTube連結:MATLAB Integration

GitHub連結:Python_MATLAB_Intergation

1.關於模型遷移與整合這件事(Model Exchage)

可以透過Youtube的影片”深度學習模型 TensorFlow/PyTorch/ONNX整合”,與GitHub中已下此檔案了解相關使用方式。

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Interoperability_TensorFlow_PyTorch_01.mlx 各種模型呼叫輸出轉換方式

  1. Export to TensorFlow
  2. Import TensorFlow 2
  3. Import KerasNetwork
  4. Import from PyTorch
  5. Interoperability Capabilities Summary

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

接著我們來談談這張圖,在2022b的MATLAB可以載入來自於TensorFlow與Pytorch與ONNX的模型,並且輸出模型成TensorFlow與ONNX的格式,以及透過ONNX來跟其他的深度學習框架去做轉移。

Export to TensorFlow

透過以下兩行程式碼,第一行是載入darknet19的模型,第二行則是做匯出的動作。

net = darknet19
exportNetworkToTensorFlow(net,'DarkNet19');

Import TensorFlow2

透過以下兩行程式碼,第一行是載入TensorFlow的Darknet19模型,第二行則是將模型匯入到MATLAB之中。

modelFolder = 'DarkNet19_savedmodel'
TFnet = importTensorFlowNetwork(modelFolder,'TargetNetwork','dlnetwork');

Import KerasNetwork

第一行載入h5格式的檔案,第二行則是將模型匯入至MATLAB中。

modelfileKeras = 'digitsDAGnet.h5'
Kerasnet = importKerasNetwork(modelfileKeras);

Import from PyTorch

第一段載入Pytorch的模型,第二段之後是在MATLAB中將模型加入輸入層

net = importNetworkFromPyTorch("traced_mnasnet1_0.pt");
InputSize = [224,224,3]
InputLayer = imageInputLayer(InputSize,Normalization="none");
net = addInputLayer(net,InputLayer,Initialize=true);;

2.關於在MATLAB寫Python Code

可以透過Youtube的影片”在MATLAB中使用Python Code”,與GitHub中已下此檔案了解相關使用方式。

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Interoperability_TensorFlow_PyTorch_02.mlx

  1. Pyrun(在MATLAB使用Pythoncode)
  2. Pyrunfile(在MATLAB使用Python File)
  3. MATLAB Live Task for Python Package

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

在MATLAB中可以透過Pyrun來在背景執行Python的程式碼,並且將結果回傳到MATLAB之中,也可以透過執行Pyrunfile來執行Python File,透過以上兩種方式來外嵌一些Python Library,以下我們來看看如何操作。

首先我們可以先查看一下MATLAB中的Python環境。

pyenv

呼叫Python Library

pyRow = int64(1);   % double -> int64 (Integer in python
pyCol = int64(10); % double -> int64 (Integer in python)
myRand = py.numpy.random.rand(pyRow,pyCol))

在MATLAB中載入影像並且做一些格式轉換

img = imread('image\r01.jpg')
figure,imshow(img)
%Image Resize
imageHWSize = 480;
img2 = imresize(img, [imageHWSize, imageHWSize]);
%調整通道順序(HWCN TO NHWC)
imgforTF = permute(img2, [4 1 2 3]);
batch_size = int32(1); % Tensorflow require inputs to be converted to int32.;

Pyrun(在MATLAB使用Pythoncode)

pythonCode4 = 
"from tensorflow.keras.applications import efficientnet_v2"
"import numpy as np"
"model = efficientnet_v2.EfficientNetV2L()"
"X = np.asarray(imgforTF)"
"X = efficientnet_v2.preprocess_input(X)"
"Y = model.predict(X, batch_size)"
"Y_decoded = efficientnet_v2.decode_predictions(Y)"
"label = Y_decoded[0][0][1]; score = Y_decoded[0][0][2]"
];
[model, Y, label, score] = pyrun(pythonCode4, ["model", "Y", "label", "score"], "imgforTF", imgforTF, "batch_size", batch_size);
figure,imshow(img);
title(string(label));[

Pyrunfile(在MATLAB使用Python File)

[model3, Y3, label3, score3] = pyrunfile("TensorFlow_using_DLmodel.py", ["model3","Y3","label3","score3"])

3.關於在Python中呼叫MATLAB

可以透過Youtube的影片”在Python中使用MATLAB”,與GitHub中已下此檔案了解相關使用方式。

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

Python_calling_MATLAB_Tim/Call Simulink Model From Python

  1. Python Call Simulink.ipynb

Python_calling_MATLAB_Tim/Using MATLAB App in Python_Image

  1. UsingAppInPython_Image.ipynb

Python_calling_MATLAB_Tim/Using MATLAB App in Python_Signal

  1. UsingAppInPython_Signal.ipynb

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

在這邊會介紹到怎麼樣在Python中使用MATLAB的APP,以及呼叫MATLAB的程式碼進行運作,還有開啟Simulink Model做互相溝通等等,這邊的操作會較多一點,在請大家透過程式碼與影片搭配了解。

以及其實也能將MATLAB打包成Python Package作為Python呼叫使用,因此這種混合編程與呼叫使用,模型轉換等方法就介紹到這邊,下回再跟大家介紹更多MATLAB與其他環境的整合,轉Code等應用。

--

--