Use imported keras network to predict in Simulink

3 visualizaciones (últimos 30 días)
Chuan Yu
Chuan Yu el 16 de Nov. de 2020
Respondida: Peter Man el 3 de Feb. de 2022
Hi,
I am trying to imported my trained series network from python into Simulink to do prediction following this previous link:https://www.mathworks.com/matlabcentral/answers/592243-import-keras-tensorflow-model-into-simulink. However I am still getting some errors. Can you help me to look into why? I have tested the imported keras network to do prediction on a single sample in a separate m file (PT_NN_Prediction.m). It works fine. However, when I use above link to create a matlab function to call the imported keras network to do prediction in Simulink. It does not work with some errors poped up.
PT_NN_modeling.slx is the simulink model to test the imported keras network prediction.
PTNN.m function is the function that is used to generate the series network.
2020-11-16_17h59_55.png is a screenshot of the errors I got.
And I am using Matlab2019b.
Thanks,
Chuan Yu
  3 comentarios
Chuan Yu
Chuan Yu el 19 de Nov. de 2020
Hi, I have attached one more zip file with those two files zipped in. Let me know if you need more information. Thanks!
Chuan Yu
Chuan Yu el 9 de Feb. de 2021
Hi,
It has been some time since I posted my question here, can someone help to look into my question and give some answers?
Thanks,
Chuan

Iniciar sesión para comentar.

Respuestas (1)

Peter Man
Peter Man el 3 de Feb. de 2022
Hi Chuan Yu,
In 19b, there isn't a layer specifically for vector inputs, which is why the imported network contains its input layer as imageInputLayer. That means the network is expecting an image - which is an array of dimensions height x weight x channels, (channels is 3 for rgb and 1 for greyscale). You can emulate a vector input by making your vector input be essentially a height=1 and channel=1 image i.e. the input to the network can be [1 2 3] for example. I see that you have essentially this in your attached file PT_NN_Prediction.m.
Now, as for your Simulink model, your model function is expecting an image input too. That means instead of passing in 3 separate scalar inputs, you need a single vector input. Furthermore, you need to ensure that vector input is the correct orientation (i.e. [1,2,3] not [1;2;3]) which means your MATLAB function probably needs to do a transpose on the input e.g.
function y = NNPrediction(x)
persistent mynet;
if isempty(mynet)
mynet = coder.loadDeepLearningNetwork('PTNN','myPTNN');
end
disp(x);
y = predict(mynet,x'); % x might be passed in as [1;2;3] rather than [1,2,3]
end
If you upgrade to a later version (R2021a), if you import your network, the input layer would instead be a featureInputLayer rather than an imageInputLayer. The featureInputLayer is designed for vectorial inputs.

Categorías

Más información sobre Deep Learning Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by