importKerasNetwork for custom loss function

11 views (last 30 days)
Dear Matlab community,
I'm trying to import a model that was trained using tensorflow/keras (see model.h5 in attachment, had to zip it) into Matlab using the importKerasNetwork function, but receive the error below. The model was trained in keras using a self-defined custom loss function and I think this is the part where the matlab import goes wrong: when I train the exact same model with a standard mse error, it imports perfectly...
>> net = importKerasNetwork('model.h5');
Warning: File 'model.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.
Warning: Loss function 'mixed_grad_loss_099' is not supported.
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: Missing output layer. The network must have at least one output layer.
Layer 'conv2d_87_OutputLayer_PLACEHOLDER': Unconnected output. Each layer output must be connected to the input of another layer.
Layer 'conv2d_87_OutputLayer_PLACEHOLDER': Layer validation failed. Error using 'forward' in Layer nnet.keras.layer.PlaceholderOutputLayer. The
function threw an error and could not be executed.
Error using nnet.internal.cnn.layer.util.CustomLayerLegacyStrategy/forward (line 42)
Networks containing PlaceholderLayers cannot be trained or used for prediction. Either remove or replace all PlaceholderLayers.
Note that importKerasLayers works perfectly and gives the following prompt in Matlab:
>> importKerasLayers('model.h5')
Warning: File 'model.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.
Warning: Loss function 'mixed_grad_loss_099' is not supported.
Warning: Keras network has multiple outputs and does not include loss information specifying the output layer types. importKerasLayers inserts
placeholder layers for the outputs. Find and replace the layers by using findPlaceholderLayers and replaceLayer, respectively.
ans =
LayerGraph with properties:
Layers: [47×1 nnet.cnn.layer.Layer]
Connections: [49×2 table]
InputNames: {'input_11'}
OutputNames: {1×0 cell}
Any ideas on how to bypass this behaviour so I can import my network into Matlab and perform inference there would be greatly appreciated!
Best,
Sam

Answers (1)

Madhav Thakker
Madhav Thakker on 21 Dec 2020
Hi Sam,
I understand that you are getting error when importing a trained network with custom loss function. The error as displayed in the log is caused because of missing output layer.
Missing output layer. The network must have at least one output layer.
You can use netron to visualize your model to see the missing output layer and try to resolve it using Keras.
Hope this helps.
  1 Comment
Ron Goldberg
Ron Goldberg on 13 Sep 2023
Hi, I just witnessed the exact same behaviour as described in the original question, for the same problem (model was trained using a customed loss function). I believe there is some wrong error prompt here, as in my case the output layer in the original model was surely defined, and I'm positive this was the case for the original inquiry too.
Perhaps the customized loss function causes an error while loading the model to Matlab and the 'missing output layer' error message is incorrectly prompted. Thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!