Error while loading .keras model: Layer node index out of bounds

46 Views Asked by At

I added some layers to pretrained MobileNet model and trained it. Then saved model with

model.save('model.keras')

Also tried this just in case:

keras.saving.save_model(model, 'model.keras')

But when i load the model I get the error:

keras.models.load_model('model.keras')

ValueError: Layer node index out of bounds. inbound_layer = inbound_layer._inbound_nodes = ListWrapper([<Node operation=, id=1561281935184>]) inbound_node_index = 2

Other ways of loading don't change anything (like keras.saving.load_model("model.keras") or tf.keras.models.load_model("model.keras"). And tf.saved_model.load('model.keras') gives OSError)

Here's a model:

base_model = MobileNet(weights='imagenet',
                                include_top=False,
                                input_shape=TARGET_IMAGE_SIZE + (3,))
base_model.trainable = False
inputs = tf.keras.Input(shape=(ORIGINAL_IMAGE_SIZE + (3,)))
resized_inputs = Lambda(lambda image: tf.image.resize(image, (224, 224)), output_shape=(224, 224, 3))(inputs)
x = base_model(resized_inputs, training=False)
x = GlobalAveragePooling2D()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.25)(x)
outputs = Dense(NUM_CLASSES, activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

How to fix it?:( I'd like to deploy the model and load it for predictions without any problems.

By the way, this model.save('./model.h5') gives an error as well

Unable to synchronously create dataset (name already exists)

Maybe the problem is caused by the Lambda layer?

  • I have no problems with ...model = tf.keras.Model(inputs, outputs) and model_.load_weights('mb.weights.h5') then.
0

There are 0 best solutions below