How to upload pretrained weights for a vgg16 dnn

6 views (last 30 days)
Peter
Peter on 27 Oct 2022
Commented: Peter on 1 Nov 2022
I want to upload an h5 weights file for the vgg16 model. There are options to import a whole keras model, or just the layers but not, apparently, just the weights. So I thought, maybe I could create a vgg16 model in matlab, export it, change the weights file and reimport it. However, matlab won't even reimport its own exported file:
net =vgg16;
exportNetworkToTensorFlow(net,package);
net = importTensorFlowNetwork(package); %no change made to the saved model
Importing the saved model...
Error using tf2mex
Unable to import the model because SavedModel appears to be corrupt or missing.
Can anyone suggest a way to import the weights?

Answers (1)

Sivylla Paraskevopoulou
Sivylla Paraskevopoulou on 28 Oct 2022
The importTensorFlowNetwork function expects the input modelFolder to be a saved_model.pb file (SaveModel format) and I think that's what the error is telling you. To save a TensorFlow model in the SavedModel format, run this command in Python:
model.save("modelName")
A couple more things...
(1) The subfolder variables in the SavedModel folder contains the weights learned by the pretrained TensorFlow network. By default, importTensorFlowNetwork imports the weights.
(2) The exported model to TensorFlow might be slight different than the original MATLAB network. Thus, exporting and re-import might or might not work seamleassly.
  4 Comments
Sivylla Paraskevopoulou
Sivylla Paraskevopoulou on 31 Oct 2022
Hi Peter,
I am sorry you are experiencing difficulties. I just run the following code, which I think is what you are trying to do, and everything run fine. For reference, I am running with Python version 3.9.7.
MATLAB code:
net = squeezenet;
exportNetworkToTensorFlow(net,"SqueezeNet")
Python code:
import SqueezeNet
model = SqueezeNet.load_model()
model.save("exported_squeezenet")
MATLAB code:
net_imported = importTensorFlowNetwork("exported_squeezenet",OutputLayerType="classification")
Regardless, exporting and re-importing is not a workflow I would recommend. It is unclear to me what you are trying to accomplish.
  • Do you have a TensorFlow model that you are trying to import? If yes, use the importTensorFlowNetwork function and make sure that your model is in SavedModel format.
  • If you are primarily a MATLAB user, there many available MATLAB pretrained networks. Check out the MATLAB Deep Learning Model Hub.
Peter
Peter on 1 Nov 2022
Thanks for trying this. When I try it now, it does work. Who knows? I'm not aware of changing anything.
What I actually want to do is something like this, in matlab:
net=vgg16;
net_import_weights('weights.h5');
where weights.h5 come from a vgg16 net trained differently from the default. I only have the weights file, not the structure that goes with it.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!