MATLAB Answers


Keras TensorFlow importer: can't upload weights from .h5 file using importKerasNetwork.

Asked by Ajpaezm on 7 Feb 2019
Latest activity Commented on by Don Mathis on 26 Feb 2019
Hi, I have a .h5 file with a Keras TensorFlow model that was built using Sequential API. The model is carrying weights, and though Layers are being succesfully uploaded through importKerasNetwork() function, I can't seem to upload the weights with it.
What could I be doing wrong? Is there a way to debug this issue?
I tried this:
test_2=importKerasNetwork('myFile.h5', 'WeightFile', 'myFile.h5')
No success whatsover.
Would it be recommendable to have the layers in an JSON file and the weights in a .h5 file?
Thanks in advance for all the help.


Show 1 older comment
Thanks so much for your interest in this Don,
In this comment I have uploaded the .h5 file.
Don, I am having the same problem - I can't import my weights. If I use use importKerasNetwork, I get:
"Error using importKerasNetwork (line 93) Unable to import network because some network layers are not yet supported. To import layers and weights, call importKerasLayers with 'ImportWeights' set to true."
If I use "importKerasLayers(modelfile, 'ImportWeights', true)", I am able to see some of my layers, however there are a fair deal of layers that are "placeholder" layers, and I am not able to see the weghts at all. I get the message:
"Warning: Keras network takes vector inputs. Pass images with height=1 and channels=1. Warning: Loss function 'binary_crossentropy' is not yet supported. Warning: Unable to import some Keras layers, because they are not yet supported by the Deep Learning Toolbox. They have been replaced by placeholder layers. To find these layers, call the function findPlaceholderLayers on the returned object. "
Are the weights not importing because it's unable to identify the layers? (I have 12 layers, 8 of them have been returned as placeholders since Matlab doesn't suport 1dCNN , max, or average pooling...)
Darci, if you're using R2018b, you can download the latest version of the keras importer. There was an update in the last month or so. 'binary_crossentropy' is supported now. The placeholder layers should contain the weights, inside the KerasConfiguration field. Unfortunately, Conv1D is not yet supported by the importer.

Sign in to comment.

1 Answer

Answer by Don Mathis on 8 Feb 2019
Edited by Don Mathis on 8 Feb 2019
 Accepted Answer

It works for me when I use the latest R2018b update of the tensorflow-keras importer. What version of MATLAB are you using? And do you get an error message?
I get the attached network in MATLAB.


Yes, is the same outcome I have.
Doing further reading on this article there's no message or notification that tells me I successfully uploaded the weights of the model, right? I thought it Matlab told us (just like it says "SeriesNetwork with properties: Layers: [something] Weights: [Something]".
How could I know if they were indeed uploaded?
Thanks for all the help so far Don and sorry for al these questions.
importKerasNetwork returns the network. So you can look at it:
>> n = importKerasNetwork('OVO_LSTM_model-01.h5')
n =
SeriesNetwork with properties:
Layers: [10×1 nnet.cnn.layer.Layer]
>> n.Layers
ans =
10x1 Layer array with layers:
1 'SequenceInputLayer' Sequence Input Sequence input with 26 dimensions
2 'lstm_1' LSTM LSTM with 128 hidden units
3 'dense_1' Fully Connected 128 fully connected layer
4 'dense_1_relu' ReLU ReLU
5 'dense_2' Fully Connected 128 fully connected layer
6 'dense_2_relu' ReLU ReLU
7 'dense_3' Fully Connected 32 fully connected layer
8 'dense_3_relu' ReLU ReLU
9 'dense_4' Fully Connected 4 fully connected layer
10 'RegressionLayer' Regression Output mean-squared-error
>> n.Layers(5)
ans =
FullyConnectedLayer with properties:
Name: 'dense_2'
InputSize: 128
OutputSize: 128
Learnable Parameters
Weights: [128×128 single]
Bias: [128×1 single]
Show all properties
Thanks so much for clearing this out for me Don! You've helped me tons!

Sign in to comment.