Skip to content

Error running video 165. Model 1: Feature extraction transfer learning on 1% of the data with data augmentation #696

Open
@warnercamacho

Description

@warnercamacho

I follow folling code:

Setup input shape and base model, freezing the base model layers

input_shape = (224, 224, 3)
base_model = tf.keras.applications.efficientnet_v2.EfficientNetV2B0(include_top=False)
base_model.trainable = False

Create input layer

inputs = layers.Input(shape=input_shape, name="input_layer")

Add in data augmentation Sequential model as a layer

x = data_augmentation(inputs)

Give base_model inputs (after augmentation) and don't train it

x = base_model(x, training=False)

Pool output features of base model

x = layers.GlobalAveragePooling2D(name="global_average_pooling_layer")(x)

Put a dense layer on as the output

outputs = layers.Dense(10, activation="softmax", name="output_layer")(x)

Make a model with inputs and outputs

model_1 = keras.Model(inputs, outputs)

Compile the model

model_1.compile(loss="categorical_crossentropy",
optimizer=tf.keras.optimizers.Adam(),
metrics=["accuracy"])

Fit the model

history_1_percent = model_1.fit(train_data_1_percent,
epochs=5,
steps_per_epoch=len(train_data_1_percent),
validation_data=test_data,
validation_steps=int(0.25* len(test_data)), # validate for less steps
# Track model training logs
callbacks=[create_tensorboard_callback("transfer_learning", "1_percent_data_aug")])

It produces following error:

ValueError Traceback (most recent call last)
in <cell line: 0>()
8
9 # Add in data augmentation Sequential model as a layer
---> 10 x = data_augmentation(inputs)
11
12 # Give base_model inputs (after augmentation) and don't train it

1 frames
/usr/local/lib/python3.11/dist-packages/keras/src/layers/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
243 if spec_dim is not None and dim is not None:
244 if spec_dim != dim:
--> 245 raise ValueError(
246 f'Input {input_index} of layer "{layer_name}" is '
247 "incompatible with the layer: "

ValueError: Exception encountered when calling Sequential.call().

Input 0 of layer "functional_1" is incompatible with the layer: expected shape=(None, 384, 512, 3), found shape=(None, 224, 224, 3)

Arguments received by Sequential.call():
• args=('<KerasTensor shape=(None, 224, 224, 3), dtype=float32, sparse=False, name=input_layer>',)
• kwargs={'mask': 'None'}

Somebody have found a solution to this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions