How to upload pretrained weights for a vgg16 dnn
조회 수: 6 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Sivylla Paraskevopoulou
2022년 10월 28일
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
Sivylla Paraskevopoulou
2022년 10월 31일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Build Deep Neural Networks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!