我想用load_model加载新的微调模型。但是ID不会加载,而是会创建一个空的新ID。该模型是在相同的环境中使用相同的keras和Python版本构建的。示例代码也是如此。
模型= load_weights也不起作用
from google.colab import drive
drive.mount('/content/gdrive')
from keras import models
from keras.models import load_model
import keras
print(keras.__version__)
model = load_model('/content/gdrive/My Drive/Bachelor/DATA/FullMoblieNet.h5')
那是我的输出
Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount("/content/gdrive", force_remount=True).
2.2.4
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-520e70aa6dbd> in <module>()
7 import keras
8 print(keras.__version__)
----> 9 model = load_model('/content/gdrive/My Drive/Bachelor/DATA/FullMoblieNet.h5')
1 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in _deserialize_model(f, custom_objects, compile)
256 raise ValueError('You are trying to load a weight file'
257 ' containing {} layers into a model with {} layers'
--> 258 .format(len(layer_names), len(filtered_layers))
259 )
260
ValueError: You are trying to load a weight file containing 51 layers into a model with 0 layers
我尝试将其放置在我的MobileNet中,但仍然失败,并出现相同的错误。
from google.colab import drive
drive.mount('/content/gdrive')
from keras import models
from keras.models import load_model
from keras import models
from keras import layers
from keras import optimizers
from keras.layers import Dense, Input, Layer
from keras.models import Model
from keras.applications.mobilenet import MobileNet
-----------------------New Code-------------------------------
mobile = MobileNet(input_shape=(224,224,3), alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=False, weights='imagenet')
model = models.Sequential()
for layer in mobile.layers[:-6]:
model.add(layer)
model.add(layers.Flatten())
model.add(layers.Dense(7, activation='softmax'))
for layer in model.layers[:-5]:
layer.trainable = False
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=['acc'])
--------------------------------new End-------------------
model = load_model('/content/gdrive/My Drive/Bachelor/DATA/FullMoblieNet.h5')
答案 0 :(得分:0)
好像您只保存了模型的权重。
因此,为了正确加载模型,您需要重建和重新编译相同的模型,然后将权重加载到模型中。
def your_model():
#model definition
model.compile()
return model
your_model = your_model()
your_model = load_model('/content/gdrive/My Drive/Bachelor/DATA/FullMoblieNet.h5')
应该可以,请保持联系!