0

I want to load new fine tuned model with load_model. But Id dont load but instad creating a empty new one. The Model was build in the same environment with the same keras and Python versions. Sample codes do the same.

model = load_weights is also not working

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')

Thats my output

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

I Try to put it my MobileNet construcktion but its still failing with the same error.

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')
Marv
  • 11
  • 3

1 Answers1

0

Looks like you have only saved the weights of your model.
So in order to correclty load your model you need to reconstruct and recompile the same model, and the load the weight into it.

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')

That should work, keep me in touch !

Thibault Bacqueyrisses
  • 2,281
  • 1
  • 6
  • 18
  • I see. But even with the model recompile I got the same error. I updated the question with the new code snippet – Marv Jul 12 '19 at 10:07