How to fix ("ValueError: You are trying to load a weight file containing 16 layers into a model with 0 layers")
How to fix ("ValueError: You are trying to load a weight file containing 16 layers into a model with 0 layers")
我是这样使用 vgg16 的:
model = VGG16()
data, labels = ReadImages(TRAIN_DIR)
vgg16 = VGG16()
model = Sequential()
#Converting VGG16 into Sequential model
for layer in vgg16.layers[:-1]:
model.add(layer)
#Freezing all layers except last layer for transfer learning
for layer in model.layers:
layer.trainable = False
#Adding custom softmax layer
model.add(Dense(1,activation='sigmoid'))
#Compiling our model
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(np.array(data), np.array(labels), batch_size=32, epochs=3)
model.save('model.h5')
当我试图在另一个 py 文件中加载这个模型时..:[=13=]
model = load_model('model.h5')
我已经尝试过 load_weights 并且也抛出错误
...returns 这个错误:
ValueError: You are trying to load a weight file containing 16 layers into a model with 0 layers
我应该怎么做才能加载此模型以进行预测?
版本:keras 2.2.4 tensorflow 1.14.0
已知问题:https://github.com/keras-team/keras/issues/10417
共有三个选项:1. 重新创建模型架构并使用“load_weights
”。如果您只想进行预测,这很好。 2. 降级到 Keras 版本 2.1.6.
3. 在此 link https://github.com/keras-team/keras/issues/10417#issuecomment-435620108 上可用的解决方法。我为 VGG16 改编了这个。这会更新 h5 文件。
def fix_layer0(filename, batch_input_shape, dtype):
with h5py.File(filename, 'r+') as f:
model_config = json.loads(f.attrs['model_config'].decode('utf-8'))
layer0 = model_config['config']['layers'][0]['config']
layer0['batch_input_shape'] = batch_input_shape
layer0['dtype'] = dtype
f.attrs['model_config'] = json.dumps(model_config).encode('utf-8')
fix_layer0('model.h5', [None, 224, 224, 3], 'float32')
loaded_model = load_model('model.h5')
此问题已在较新版本的 TensorFlow 中得到解决
只需升级您的 TensorFlow:
pip 安装 tensorflow --upgrade
我是这样使用 vgg16 的:
model = VGG16()
data, labels = ReadImages(TRAIN_DIR)
vgg16 = VGG16()
model = Sequential()
#Converting VGG16 into Sequential model
for layer in vgg16.layers[:-1]:
model.add(layer)
#Freezing all layers except last layer for transfer learning
for layer in model.layers:
layer.trainable = False
#Adding custom softmax layer
model.add(Dense(1,activation='sigmoid'))
#Compiling our model
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(np.array(data), np.array(labels), batch_size=32, epochs=3)
model.save('model.h5')
当我试图在另一个 py 文件中加载这个模型时..:[=13=]
model = load_model('model.h5')
我已经尝试过 load_weights 并且也抛出错误
...returns 这个错误:
ValueError: You are trying to load a weight file containing 16 layers into a model with 0 layers
我应该怎么做才能加载此模型以进行预测?
版本:keras 2.2.4 tensorflow 1.14.0
已知问题:https://github.com/keras-team/keras/issues/10417
共有三个选项:1. 重新创建模型架构并使用“load_weights
”。如果您只想进行预测,这很好。 2. 降级到 Keras 版本 2.1.6.
3. 在此 link https://github.com/keras-team/keras/issues/10417#issuecomment-435620108 上可用的解决方法。我为 VGG16 改编了这个。这会更新 h5 文件。
def fix_layer0(filename, batch_input_shape, dtype):
with h5py.File(filename, 'r+') as f:
model_config = json.loads(f.attrs['model_config'].decode('utf-8'))
layer0 = model_config['config']['layers'][0]['config']
layer0['batch_input_shape'] = batch_input_shape
layer0['dtype'] = dtype
f.attrs['model_config'] = json.dumps(model_config).encode('utf-8')
fix_layer0('model.h5', [None, 224, 224, 3], 'float32')
loaded_model = load_model('model.h5')
此问题已在较新版本的 TensorFlow 中得到解决
只需升级您的 TensorFlow: pip 安装 tensorflow --upgrade