[Python] MobileNetV3Large with (224,224,1) input works, but channel mismatch error on testing...

Discussão em 'Python' iniciado por Stack, Agosto 27, 2025.

  1. Stack

    Stack Membro Participativo

    I created a Keras MobileNetV3Large model with ImageNet weights and explicitly defined the input layer as having shape (224,224,1) to accept grayscale images. When creating the model, no errors were raised.

    However, when I tested the model with actual (224,224,1) input images, an error related to channel mismatch occurred. To work around this, I preprocessed the images by copying the single channel three times to convert them from (224,224,1) to (224,224,3). After this, training with the original model defined on (224,224,1) input seemed to work without errors.

    Later, I changed the model input shape to (224,224,3), but I am still unsure why the code worked when the model was defined with a (224,224,1) input shape but got (224,224,3) images after preprocessing.

    tested on colab

    def grayscale_preprocessing(img):
    # Convert to grayscale using tf.image.rgb_to_grayscale
    # img is expected to be a tensor with shape (height, width, channels)
    img = tf.image.rgb_to_grayscale(img)
    return img


    # Data Augmentation settings
    train_datagen = ImageDataGenerator(
    rotation_range=40,
    width_shift_range=0.1,
    height_shift_range=0.1,
    fill_mode='nearest',
    vertical_flip =True,
    horizontal_flip =True,
    validation_split=0.2 ,
    preprocessing_function=grayscale_preprocessing # Apply grayscale preprocessing here
    base_model = keras.applications.MobileNetV3Large(
    include_top=False,
    weights="imagenet",
    pooling = 'avg',
    )
    base_model.trainable = False

    inputs = keras.Input(shape=(224, 224, 1))
    x = base_model(inputs, training=False)
    layer1 = keras.layers.Dense(128, activation='relu')(x)
    layer1 = keras.layers.Dropout(0.5)(layer1)
    layer2 = keras.layers.Dense(64, activation='relu')(layer1)
    layer2 = keras.layers.Dropout(0.5)(layer2)
    outputs = keras.layers.Dense(1)(layer2)
    new_layer_model = keras.Model(inputs, outputs)
    )


    Could someone please explain why Keras allows this kind of mismatch without raising errors at model creation and why training can proceed in this way? Also, does this approach actually allow proper training or is it prone to hidden issues? Thank you in advance!

    Continue reading...

Compartilhe esta Página