Global variable python code simulink

Hello everyone.
I'm trying to run a python code which uses a neural network in sumlink. It works using the "trick" of "coder.extrinsic" to use pyrunfile("testSimulink.py")
The problem is that this code loads a neural network to analyze an input image, so every time the block in simulink is called, it re loads the neural network, which is too slow for real time applications.
Do you think any idea how to keep the neural network loaded as a global variable or background in the python environment?
The extrinsinc function I'm running looks like:
function y = NNextrinsic(colored)
delete('own\*')
imwrite(colored,'own/myGray.png')
pyrunfile("testSimulink.py")
A = imread("test.png");
A = imresize(A, [720,1280]);
y = A;
And the simulink block is a MatlabFunction block which looks like:
function y = ANNDepth(colored)
z=uint8(ones(720,1280,3));
coder.extrinsic('NNextrinsic');
z=NNextrinsic(colored);
y = z;
Thanks!

4 Comments

Hi Jose,
Could you please provide a bit more explanation of what you are trying to do (and if possible, why, so we can try to suggest a solution).
My understanding is that you have a Python code that does some sort of image classification using a neural net, is that correct?
You want to use that image classification algorithm in your Simulink model, right?
You mention that your current way of doing this is too slow for "real-time application". What do you actually mean by "real-time application"? Would you be eventually interested in generating code from a Simulink model for deployment to GPU or a microprocessor?
In general, the way to get the fastest performance would be to import your neural net into MATLAB and then use a Simulink block fo running inference on your neural net.
If you don't need to generate code eventually, and only need to run simulations on your desktop, then the approach of calling Python from Simulink would be a viable option as well.
Could you please clarify?
Thanks.
Arkadiy
Hello, thanks for answering.
I have a .h5 neural network with custom layers such as " 'BilinearUpSampling2D'" so I cant convert it into a DAGNetwork to use the simulink block. Hence, I have a python code which loads the neural network and predict the depth from an image. I'm calling this python function every frame in simulink, so it takes too long because it loads the neural network each frame, I was thinking in keep these neural network saved someway in memory -RAM- to access it easily. I cant use global variables or similar because once the python code runs in simulink, it closes.
Thank you
Hi Jose Daniel,
Here is something you might be able to try. Inside your MATLAB Function Block, you may use pyrun to load the model but declare the variable as persistent. Something like:
persistent model;
if isempty(model)
model = pyrun(<Python code to load model>, 'model');
end
Then, you may pass the model variable to your Python script in pyrunfile.
Hope this helps.
Lucas

Sign in to comment.

Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!