Cannot import trained network

I trained a network using tensorflow 2.0 in windows 10. It works fine and can output the predicted result. However, when I try to use "net = importKerasNetwork('model.h5')", MATLAB has the following error:
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_8_OutputLayer_PLACEHOLDER': Unconnected output. Each layer output must be connected to the input
of another layer.
Layer 'conv2d_8_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.
Any possible way to revise it? Thanks a lot.

Answers (2)

Divyam Gupta
Divyam Gupta on 8 Jun 2021
Hi Yongbo,
I understand that you're having an issue in importing a trained network. The error that I notice is that your network is missing an output layer. Please refer to the following answer on MATLAB Central which discusses a similar issue that you are facing: https://www.mathworks.com/matlabcentral/answers/673738-importkerasnetwork-for-custom-loss-function
Hope this helps.
Abhishek Gupta
Abhishek Gupta on 9 Jun 2021
Hi,
As per my understanding, you are getting an error while importing a network.
If your network doesn't have a loss layer, then you need to import your network using importKerasLayers and add a custom output layer at the end, or you can convert the layerGraph to dlnetwork.
Referring to the following documentation, which might help you in resolving the issue: -

3 Comments

Thank you for your response. I convert the layerGraph to dlnetwork. And then use the model for prediction:
lgraph = importTensorFlowLayers('D:\models');
dlnet = dlnetwork(lgraph);
I = imread('D:\1.jpg');
dlY = predict(dlnet,I);
My input is a RGB image (AXBX3) and output is a depth image (AXBX1). But I get a new error:
dlnetwork with properties:
Layers: [622×1 nnet.cnn.layer.Layer]
Connections: [707×2 table]
Learnables: [694×3 table]
State: [338×3 table]
InputNames: {'input_1'}
OutputNames: {'conv2d_8'}
Initialized: 1
Error using dlnetwork/predict (line 564)
Invalid argument at position 2. Value must be of type dlarray or be convertible to dlarray.
You are getting this error because the second argument of your predict function is not of type dlarray. For more information related to the input data, check out this documentation link: -https://www.mathworks.com/help/deeplearning/ref/dlnetwork.predict.html#function_dlnetwork_sep_predict_sep_mw_bfae26e4-e62e-4b46-80e2-3d3f8305b520
Thank you very much for your answer. I have revised the input and get the predict result. However, I find that the output of the network is totally different from the result from tensor flow using python. The trained network "lgraph" from tensorflow 2.4.1. Any possible way to solve this problem:
lgraph = importTensorFlowLayers('D:\models');
I = imread('D:\1.jpg');
dlX = dlarray(single(I),'SSCB');
dlnet = dlnetwork(lgraph);
dlY = predict(dlnet,dlX);
x = extractdata(dlY);

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 5 Jun 2021

Commented:

on 19 Jun 2021

Community Treasure Hunt

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

Start Hunting!