필터 지우기
필터 지우기

Global variable python code simulink

조회 수: 7 (최근 30일)
jose daniel hoyos giraldo
jose daniel hoyos giraldo 2022년 9월 3일
댓글: jose daniel hoyos giraldo 2022년 9월 17일
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
Lucas García
Lucas García 2022년 9월 15일
편집: Lucas García 2022년 9월 15일
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
jose daniel hoyos giraldo
jose daniel hoyos giraldo 2022년 9월 17일
That works! ty so much

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by