Converting Neural Network Model file from Python to Matlab

Dear all
We have developed a CNN model in Python and would like to convert the model to Matlab.
We have exported the Python model to a h5 format file.
However we hare having trouble about having Matlab to import this file.
So far we have used importkerasnetwork matlab function and get the following error:
Warning: File 'new_model_doceis.h5' was saved in Keras version '2.7.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 nnet.internal.cnn.keras.LayerTranslator/importWeights (line 223)
Assertion failed.
Error in nnet.internal.cnn.keras.LayerSpec.fromBaseLayer (line 32)
this.Weights = importWeights(this.Translator, this.Name, SubmodelName, WeightFile, H5Info);
Error in nnet.internal.cnn.keras.KerasLayerInsideModel/expandLayer (line 26)
LSpec = nnet.internal.cnn.keras.LayerSpec.fromBaseLayer(this, ImportWeights, SubmodelName,
HDF5Filename, H5Info);
Error in nnet.internal.cnn.keras.KerasModelConfig>@(L)expandLayer(L,ImportWeights,SubmodelName,HDF5Filename,H5Info)
(line 18)
[ExpandedLayerSpecs, ExpandedNameTables] = cellfun(@(L)expandLayer(L, ImportWeights, SubmodelName,
HDF5Filename, H5Info), ...
Error in nnet.internal.cnn.keras.KerasModelConfig/flattenLayer (line 18)
[ExpandedLayerSpecs, ExpandedNameTables] = cellfun(@(L)expandLayer(L, ImportWeights, SubmodelName,
HDF5Filename, H5Info), ...
Error in nnet.internal.cnn.keras.KerasSequentialModel/flatten (line 21)
[LayerSpecs, NameTable] = flattenLayer(this.Config, ImportWeights, '', HDF5Filename, H5Info);
Error in nnet.internal.cnn.keras.AssembledModel (line 38)
[this.LayerSpecs, this.InputLayerIndices, this.OutputTensors] = flatten(KM.Model, ImportWeights,
WeightFile, H5Info);
Error in nnet.internal.cnn.keras.importKerasNetwork (line 25)
AM = nnet.internal.cnn.keras.AssembledModel(KM, WeightFile);
Error in importKerasNetwork (line 91)Network = nnet.internal.cnn.keras.importKerasNetwork(modelfile, varargin{:});
Is there a way to solve this problem?
Thanks in advance
Para

回答 (1 件)

David Willingham
David Willingham 2021 年 12 月 7 日

0 投票

Hi,
The importer that had the most up to date functionality as it’s based on networks created in TensorFlow 2.0:
https://www.mathworks.com/help/deeplearning/ref/importtensorflownetwork.html
Was your networked created in Tensorflow 2.0?
David

4 件のコメント

Paramonte
Paramonte 2021 年 12 月 7 日
hi, thank you for your answer.
I have followed your sugestion regarding using Tensorflow 2.0 and got this message:
Importing the saved model...
Warning: The SavedModel 'C:\Users\sotao\Documents\backup\EHG\filipa_cardoso\CNN\modelCNNFSST' was saved in TensorFlow version '2.6.0'. Import of SavedModel versions
newer than '2.3.0' is not supported. The imported model may not exactly match the model saved in the SavedModel.
Error using jsondecode
JSON syntax error: expected value but found end of text.
Error in nnet.internal.cnn.tensorflow.savedmodel.InternalTrackableGraph (line 44)
KerasLayerConfig = jsondecode(obj.NodeStruct{i}.user_object.metadata);
Error in nnet.internal.cnn.tensorflow.savedmodel.TFKerasManager (line 32)
obj.InternalTrackableGraph = savedmodel.InternalTrackableGraph(object_graph_def);
Error in nnet.internal.cnn.tensorflow.savedmodel.TFSavedModel (line 32)
obj.KerasManager = savedmodel.TFKerasManager(smstruct.meta_graphs.object_graph_def, obj.SavedModelPath, kerasImporterOptions, importNetwork);
Error in nnet.internal.cnn.tensorflow.importTensorFlowNetwork (line 21)
sm = savedmodel.TFSavedModel(path, options, true);
Error in importTensorFlowNetwork (line 107)
Network = nnet.internal.cnn.tensorflow.importTensorFlowNetwork(modelFolder, varargin{:});
Error in python_doceis_2022_v1 (line 6)
model_python=importTensorFlowNetwork(ddd);
David Willingham
David Willingham 2021 年 12 月 8 日
Can you update the support package? There was an update November 18 that includes support for TF 2.6
Paramonte
Paramonte 2021 年 12 月 8 日
The neural network was created with the TensorFlow version 2.6.1
David Willingham
David Willingham 2021 年 12 月 8 日
I'd still recommend updating the support package:
Then we can check if the network created in TF 2.6.1 can be imported.

サインインしてコメントする。

製品

リリース

R2021a

質問済み:

2021 年 12 月 7 日

コメント済み:

2021 年 12 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by