Home > Mobile >  ValueError when loading model that uses lambda resize layer in keras
ValueError when loading model that uses lambda resize layer in keras

Time:05-30

So basically, I trained a model with a resize layer. Here is my model:

model = Sequential()
model.add(keras.layers.Lambda(
    lambda image: tf.image.resize(
        image,
        (470,470),
        method = tf.image.ResizeMethod.BICUBIC,
        preserve_aspect_ratio = True
    )

))
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=shape))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.33))
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

Before using the first layer, I could save and load the model, but now when loading I get this error:


ValueError: The channel dimension of the inputs should be defined. The input_shape received is (None, None, None, None), where axis -1 (0-based) is the channel dimension, which found to be `None`.

I loaded my data first like this: model2 = load_model('catsanddogs.h5')

and also tried some solutions from a github issue that made it like this model =load_model('catsanddogs.h5',custom_objects={"tf":tf})

Does anyone know how to properly load this model?

CodePudding user response:

To anyone that has this issue in the future, instead of using a lambda layer, use a keras resizing layer. Also the h5 file that wouldn't load still works on huggingface.

  • Related