Error occurs when I use importKerasNetwork function to import my LSTM RNN into MATLAB

10 views (last 30 days)
Hello,
I was trying to import my trained model from python Keras to MATLAB, the input of the network is a 3 timesteps, 48 features matrix so each sample of my rnn is a (3,48,1) matrix, which is passed into a LSTM layer and then a Dense layer. this simple RNN works fine in python so I saved the trained model in.h5 file and tried to import it to MATLAB using importKerasNetwork function. However, an error occured.
the command I used is the following:
path = strcat('./data/','model','.h5');
importKerasNetwork(path)
the error shows:
Warning: File './Traffic_data/model_0_0.h5' was saved in Keras version '2.4.0'. Import of Keras versions newer
than '2.2.4' is not supported. The imported model may not exactly match the model saved in the Keras file.
Error using assembleNetwork (line 47)
Invalid network.
Error in nnet.internal.cnn.keras.importKerasNetwork (line 35)
Network = assembleNetwork(LayersOrGraph);
Error in importKerasNetwork (line 91)
Network = nnet.internal.cnn.keras.importKerasNetwork(modelfile, varargin{:});
Caused by:
Network: Incompatible layer types. The network contains layer types not supported with recurrent layers.
Detected recurrent layers:
layer 'lstm_5' (LSTM)
Detected incompatible layers:
layer 'lstm_5_input' (Image Input)
Layer 'lstm_5': Input size mismatch. LSTM layers must have scalar input size, but input size (3×48×1) was
received. Try using a flatten layer before the LSTM layer.
I wondering How could I fix it. If anyone could give some insight on this I would greatly appreciate it.
Thank you.

Accepted Answer

Rishik Ramena
Rishik Ramena on 2 Dec 2020
You can try importing the layers of your model using the importKerasLayers function to import the model layerwise into MATLAB. You can then edit the layers prior to the lstm layer to fix this.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!