importKerasNetwork does not work: provide support for importing keras (with tensorflow backend) models with custom layers / custom activation functions

4 views (last 30 days)
I have a trained prediction model in keras (with tensorflow backend) using custom layers on Ubuntu 18.04 LTS,
and want to import this model in MATLAB using importKerasNetwork for further prediction (On Windows 10).
So, this is a cross-platform requirement.
when tried to import in MATLAB R2018a (also tried in later versions as well).
model = importKerasNetwork('model_file.h5'); % model_file.h5 is the model saved in keras (with tensorflow backend) using 'save' method
Error using importKerasNetwork (line 86)
Unable to import network because some network layers are not yet supported. To import layers and weights, call importKerasLayers with 'ImportWeights' set to true.
So, tried:
layers = importKerasLayers('model_file.h5', 'ImportWeights' , true);
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.
> In nnet.internal.cnn.keras.importKerasLayers (line 29)
In importKerasLayers (line 102)
In test_import_keras_model (line 4)
I cannot replace or remove my custom layers, because prediction will not be correct.
I also tried using assembleNetwork but it did'nt work.
Another, last option that I see is trying to train the prediction model using a custom activation function (required for my implementation).
Before, trying this, important question is whether there is any support in MATLAB to import keras models
with custom activation functions?
I request Mathworks, to kindly expedite the process of providing support for importing keras (tensorflow) models with:
A) Custom Layers and/or
B) Custom activation functions
Thank you.

Answers (1)

David Willingham
David Willingham on 7 Oct 2021
@Sandy Kumar, R2018a is too earlier of a version to try importing. Do you have access to the latest version R2021b or R2021a? In it we had upgraded support, with a generic tensorflow importer over just a keras specific one:
Sandy Kumar
Sandy Kumar on 19 Oct 2021
Dear David, I've mailed you the model file, please let me know whether you are able to import the model successfully. More details are in the mail. Please reply. Thank you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!