Generating predictions from trained neural net in simulink

1 vue (au cours des 30 derniers jours)
Mike Jadwin
Mike Jadwin le 18 Août 2022
Commenté : David Willingham le 22 Août 2022
Hello,
Background: I have trained a neural network in python using tensorflow and keras, and saved the model in the h5 format. I have succesfully been able to load that model into the matlab environment and make the same predictions using a modified version of the following script:
% Load model into Matlab workspace
loaded_model = importKerasNetwork('Python_Trained_NN.h5');
% Normalize input data
X1_norm = X1 / max(X1);
X2_norm = X2 / max(X2);
X3_norm = X3 / max(X3);
X4_norm = X4 / max(X4);
% Create Input Vector
inputs = [X1_norm, X2_norm, X3_norm, X4_norm];
% Create zeros for predictions with length equal to the length of the time series data I am trying to make predictions on
test_preds = zeros(length(time), 1);
% % Make predictions using inputs
for i = 1:length(time)
test_preds(i) = predict(loaded_model, inputs(i, :));
end
I know there is probably a faster way to do this, but I am limited to matlab 2020a for my application and am unable to upgrade. Also, this works for now which was a good starting point at least. Please feel free to suggest a better way to do this.
Is there any way I can use a matlab function block to get the same behavior in simulink? Again, I am using matlab 2020a, so I do not have access to the full deep learning toolbox library. Right now I am trying the following.
function y = fcn(X1, X2, X3, X4)
%#codegen
inputs = [X1, X2, X3, X4];
y = predict(loaded_model, inputs);
But I get an error saying that 'loaded_model' is an undefined variable even though it is saved in the matlab workspace as a SeriesNetwork object.

Réponses (1)

David Willingham
David Willingham le 19 Août 2022
Hi Mike,
Great to hear your exploring workflows that include TensorFlow and MATLAB/Simulink.
There are more options for running the model in Simulink as of 21a. For example there is a stateful predict for LSTMs: Stateful Predict.
  4 commentaires
Mike Jadwin
Mike Jadwin le 22 Août 2022
My company prototypes our controls software in simulink, and we have many MIL type applications. Before I can get the approval to implement my new NN model into the production software, I will need to show it is capable of at least meeting the previous models performance. With each time step I need the model output to update based on the changing inputs.
David Willingham
David Willingham le 22 Août 2022
I have checked back with R2020b, and this example highlights how you can load your own network using the command in the MATLAB function block:
coder.loadDeepLearningNetwork
Note: this example is different in the current release as there are now dedicated blocks.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Predictive Maintenance Toolbox dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by