我想用 load_model 加载新的微调模型。但是我不加载而是创建一个空的新的。该模型是在具有相同 keras 和 Python 版本的相同环境中构建的。示例代码也是如此。
model = 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')