我正在尝试迁移学习,并从ImageNet数据集中下载了VGG19模型的权重。它作为“.mat”文件下载。起初,我试图将整个.mat文件加载为
import tensorflow as tf
from tensorflow.keras.applications.vgg19 import VGG19
model_path = os.path.join('trained_model', 'imagenet-vgg-verydeep-19.mat')
pre_trained_model = VGG19(weights = None, include_top = False)
pre_trained_model.load_weights(model_path)
但是,我得到了以下错误
OSError: Unable to open file (file signature not found)
然后我尝试了set_weights()
方法而不是load_weights()
,如下所示
pre_trained_model = VGG19(weights = None, include_top = False)
pre_trained_model.set_weights(model_path)
然后我得到了下面的错误
ValueError: You called `set_weights(weights)` on layer "vgg19" with a weight list of length 42,
but the layer was expecting 32 weights. Provided weights: trained_model\imagenet-vgg-verydeep-19.mat...
我也试过了
pre_trained_model = VGG19(weights = None, include_top = True)
pre_trained_model.set_weights(model_path)
但是,我仍然会犯同样的错误
我已经从https://www.kaggle.com/teksab/imagenetvggverydeep19mat下载了权重
这行
pre_trained_model = VGG19(weights = None, include_top = False)
include_top = False
表示模型不加载完全连接的层。您是否检查过Kaggle上的预训练模型是否使用include_top = True?
无法加载权重,因为图层大小不同
以下是您可以使用的方法: How to read .mat file format in tensorflow?
相关问题 更多 >
编程相关推荐