我创建了模型并使用 model.save 保存。
然后我使用 tf.keras.modles.load_model 加载了权重和体系结构。
vgg16模型无需模型架构即可保存权重
消息错误是:
(ValueError: You are trying to load a weight file containing 15 layers into a model with 0 layers.)
此外,两者之间有什么区别
model.save 和 tf.keras.saved_model.save
答案 0 :(得分:0)
这似乎是Tensorflow或Keras的旧版本中的问题。 Github和Stackoverflow中也有相关问题。
但是此错误已在Tensorflow
,2.1
的最新稳定版本中修复。
下面提到的是简单的工作代码示例:
from tensorflow.keras.applications import VGG16
import tensorflow as tf
model = VGG16(include_top = False, weights = 'imagenet', input_shape = (224,224,3))
model.save('model.h5')
loaded_model1 = tf.keras.models.load_model('model.h5')
model.save('model')
loaded_model2 = tf.keras.models.load_model('model')
下面提到的是执行命令loaded_model1
和loaded_model2
时的输出。
Model: "vgg16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 150, 150, 3)] 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 150, 150, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 150, 150, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 75, 75, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 75, 75, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 75, 75, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 37, 37, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 37, 37, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 37, 37, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 37, 37, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 18, 18, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 18, 18, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 18, 18, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 18, 18, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 9, 9, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 9, 9, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 9, 9, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 9, 9, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 4, 4, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
如果即使在将Tensorflow版本升级到2.1之后仍遇到问题,请共享上述完整代码。我们可以进一步调查。