Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding data_augmentation layer causes errors with model_1 in 05_transfer_learning #683

Open
ilsid opened this issue Jan 14, 2025 Discussed in #680 · 1 comment
Open

Comments

@ilsid
Copy link

ilsid commented Jan 14, 2025

Discussed in #680

Originally posted by MikaATuomaala December 17, 2024
Hi,

I have problems with "05_transfer_learning". Following code used for model_1:

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

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

x = data_augmentation(inputs, training=True)

where data_augmentation is defined as follows:

data_augmentation = keras.Sequential([
    layers.RandomFlip("horizontal"),
    layers.RandomRotation(0.2),
    layers.RandomZoom(0.2),
    layers.RandomHeight(0.2),
    layers.RandomHeight(0.2),
    # preprocessing.rescasle(1./255) # Resnet50v2 may need this
], name = "data_augmentation")

produces following error:

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={'training': 'True', 'mask': 'None'}

As far as I can tell, the code is same as in the updated course notes. And I have tried to copy-paste from the updated notes, but that doesn't seem to make any difference.

Tensorflow version is 2.17.1 and the runtime type is T4 GPU

Any ideas what could be causing this? And better yet, how to fix it?

Thanks,
-Mika

@ilsid
Copy link
Author

ilsid commented Jan 14, 2025

I faced the same issue.
Looks like "View a random image" check sets up the input shape based on the passed image's size

# Augment the image
augmented_img = data_augmentation(tf.expand_dims(img, axis=0))

So, you've got (None, 384, 512, 3) shape for the first random image.

I found the following workaround

cloned_da_model = tf.keras.models.clone_model(data_augmentation)
augmented_img = cloned_da_model(tf.expand_dims(img, axis=0))

The original data_augmentation remains untouched and the cloned one is used for the image augmentation

Regards,
Illia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant