Clear Filters
Clear Filters

Keras Tensorflow import trained model with shared weights not supported Matlab 2021a

53 views (last 30 days)
Hello to everyone.
I have trained a Siamese CNN model with shared weights with Keras Tensorflow and saved it in h5 format. I want to import it in Matlab for inference. I am not interested to be able to train it further in Matlab.
When I try to import it using
net = importKerasNetwork(modelfile)
I get the following error.
Error using nnet.internal.cnn.keras.KerasDAGModelConfig (line 14)
Unable to import network. Weight sharing is not supported.
Error in nnet.internal.cnn.keras.KerasDAGModel (line 15)
this.Config = nnet.internal.cnn.keras.KerasDAGModelConfig(Struct.config);
Error in nnet.internal.cnn.keras.ParsedKerasModel (line 28)
this.Model = nnet.internal.cnn.keras.KerasDAGModel(KerasModelConfig, isTFModel);
Error in nnet.internal.cnn.keras.importKerasNetwork (line 23)
KM = nnet.internal.cnn.keras.ParsedKerasModel(ModelConfig, TrainingConfig);
Error in importKerasNetwork (line 76)
Network = nnet.internal.cnn.keras.importKerasNetwork(modelfile, varargin{:});
I have also tried
net=importKerasLayers(modelfile,'ImportWeights',true)
buy also got
Error using nnet.internal.cnn.keras.KerasDAGModelConfig (line 14)
Unable to import network. Weight sharing is not supported.
Error in nnet.internal.cnn.keras.KerasDAGModel (line 15)
this.Config = nnet.internal.cnn.keras.KerasDAGModelConfig(Struct.config);
Error in nnet.internal.cnn.keras.ParsedKerasModel (line 28)
this.Model = nnet.internal.cnn.keras.KerasDAGModel(KerasModelConfig, isTFModel);
Error in nnet.internal.cnn.keras.importKerasLayers (line 14)
KM = nnet.internal.cnn.keras.ParsedKerasModel(ModelConfig, TrainingConfig);
Error in importKerasLayers (line 76)
Layers = nnet.internal.cnn.keras.importKerasLayers(modelfile, varargin{:});
I am using Matlab 2021a. Does a newer Release support weight sharing? In case it doesn't is there any work around. For example save the weights from Python to numpy or .mat files and then create the architecture in Matlab and load the weights?
I mention once again that I don't want to train the model further in Matlab, I just need it for inference.

Answers (1)

prabhat kumar sharma
prabhat kumar sharma on 3 Jun 2024 at 9:24
Edited: prabhat kumar sharma on 3 Jun 2024 at 9:27
I understand you are looking to use the .h5 weights of your pretrained model in matlab. But As far as I know weight sharing is not supported in matlab.
As a workarround you can do the following steps:
1.Export Weights from Keras
model.save_weights('model_weights.h5')
2. Recreate Architecture in MATLAB
You will need to manually code the architecture. Here's a simplified example of defining a layer in MATLAB:
layers = [
imageInputLayer([28 28 1], 'Name', 'input')
convolution2dLayer(3, 16, 'Padding', 'same', 'Name', 'conv1')
% Add more layers according to your Requirement.
];
3.Load the Weights into MATLAB Model:
  • Load the weights into MATLAB. If you saved them in .npz format, you can use MATLAB's "load" function for .mat files or a library like numpy in Python to extract the weights and then load them into MATLAB.
  • Assign the weights to the corresponding layers in your MATLAB model. This step can be a bit tedious, as you'll need to ensure that the weights are correctly matched to each layer, especially considering the shared weights aspect of your Siamese network
  • For .h5 files, you might need a custom function to parse and load the weights into MATLAB, as MATLAB does not directly support loading Keras weights into its layers. If you choose to convert the weights into .mat format, you can easily load them using MATLAB's "load" function but would still need to manually assign each weight to the corresponding layer in your MATLAB model.
You can additionally refer "importNetworkFromTensorFlow" Documentation: Import TensorFlow network as MATLAB network - MATLAB importNetworkFromTensorFlow - MathWorks India
I hope this workarround will help you!

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!