필터 지우기
필터 지우기

How to upload pretrained weights for a vgg16 dnn

조회 수: 27 (최근 30일)
Peter
Peter 2022년 10월 27일
댓글: Peter 2022년 11월 1일
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?

답변 (1개)

Sivylla Paraskevopoulou
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
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.
Peter
Peter 2022년 11월 1일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by