How to upload pretrained weights for a vgg16 dnn
7 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
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
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.
See Also
Categories
Find more on Build Deep Neural Networks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!