C2W2 'Cats vs Dogs with Data Augmentation' - Help with Error

Eagerly tried to solve this for 3 days - now I need help :slight_smile:

β€œYour model is not compatible with the dataset you defined earlier. Check that the loss function, last layer and label_mode are compatible with one another.”

I can share my notebook i upon request.

Thanks in advance …

This is what my aug + non_aug layers look like in the combined model:

model.summary(expand_nested=True)

model: β€œsequential_55”
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┑━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
β”‚ sequential_54 (Sequential) β”‚ ? β”‚ 0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” random_flip_18 β”‚ (None, 120, 120, 3) β”‚ 0 β”‚
β”‚ (RandomFlip) β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” random_rotation_18 β”‚ (None, 120, 120, 3) β”‚ 0 β”‚
β”‚ (RandomRotation) β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” random_translation_18 β”‚ (None, 120, 120, 3) β”‚ 0 β”‚
β”‚ (RandomTranslation) β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” random_zoom_18 β”‚ (None, 120, 120, 3) β”‚ 0 β”‚
β”‚ (RandomZoom) β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ sequential_53 (Sequential) β”‚ ? β”‚ 880,737 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” rescaling_19 (Rescaling) β”‚ (None, 120, 120, 3) β”‚ 0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” conv2d_76 (Conv2D) β”‚ (None, 118, 118, 16) β”‚ 448 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” max_pooling2d_76 β”‚ (None, 59, 59, 16) β”‚ 0 β”‚
β”‚ (MaxPooling2D) β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” conv2d_77 (Conv2D) β”‚ (None, 57, 57, 32) β”‚ 4,640 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” max_pooling2d_77 β”‚ (None, 28, 28, 32) β”‚ 0 β”‚
β”‚ (MaxPooling2D) β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” conv2d_78 (Conv2D) β”‚ (None, 26, 26, 64) β”‚ 18,496 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” max_pooling2d_78 β”‚ (None, 13, 13, 64) β”‚ 0 β”‚
β”‚ (MaxPooling2D) β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” conv2d_79 (Conv2D) β”‚ (None, 11, 11, 64) β”‚ 36,928 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” max_pooling2d_79 β”‚ (None, 5, 5, 64) β”‚ 0 β”‚
β”‚ (MaxPooling2D) β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” flatten_19 (Flatten) β”‚ (None, 1600) β”‚ 0 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” dense_38 (Dense) β”‚ (None, 512) β”‚ 819,712 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”” dense_39 (Dense) β”‚ (None, 1) β”‚ 513 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Total params: 880,737 (3.36 MB)
Trainable params: 880,737 (3.36 MB)
Non-trainable params: 0 (0.00 B)

I have not taken that course, so am not familiar with the details of that assignment, but notice that what you showed does not really address the point of what the error message is talking about. What we can see is that the output of the model is a single neuron β€œDense” layer, so presumably this is a binary classifier. Meaning that the labels should be 0 or 1, right? And the loss function needs to be one of the binary forms of β€œcross entropy” loss. You don’t show any information about what loss function you used or what the labels on the data look like.

The first rule of debugging is β€œbelieve the error message”. If you don’t understand what it is telling you, that is the first problem you need to solve. :nerd_face:

are you doing the assignment in Coursera provided environment?? @Joseph_Porrovecchio

Hi Paul,

I understand what its telling me - its just a little murky :slight_smile:

Here is some more relevant info:

label_mode=β€˜**binary’ and loss function is binary_crossentropy

=============================================
def train_val_datasets():

"""Creates datasets for training and validation.

Returns:
    (tf.data.Dataset, tf.data.Dataset): Training and validation datasets.
"""

### START CODE HERE ###

training_dataset = tf.keras.utils.image_dataset_from_directory( 
    directory=DATA_DIR,
    image_size=(120,120),
    batch_size=128,
    label_mode='binary',
    validation_split=.15,
    subset="training",
    seed=42 
)


validation_dataset = tf.keras.utils.image_dataset_from_directory( 
directory=DATA_DIR,
image_size=(120, 120),
batch_size=128,
label_mode='binary',
validation_split=0.15,
subset="validation",
seed=42

======================================

Create the compiled but untrained model

model = create_model()

model.compile(loss=β€˜binary_crossentropy’,
optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.0001),
metrics=[β€˜accuracy’]
)

Check parameter count against a reference solution

unittests.parameter_count(model)

try:
model.evaluate(example_batch_images, example_batch_labels, verbose= true)
except:
print(β€œYour model is not compatible with the dataset you defined earlier. Check that the loss function, last layer and label_mode are compatible with one another.”)
else:
predictions = model.predict(example_batch_images, verbose=False)
print(f"predictions have shape: {predictions.shape}")

Hi Deepti,

Yes, I am using the provided colab env:

env

Hi, Joseph.

Thanks for the additional information. It all looks consistent and reasonable. The only further question would be how example_batch_images and example_batch_labels are defined. I assume the rules in the TF courses are the same as in the other specializations, so we aren’t really supposed to be publishing the solution code here. But my suggestion would be to print the shape of one of the example_batch_images and print the first few label values from example_batch_labels. Just in your own code. Are they what you expect?

But beyond that, I’m out of my depth here, since I can’t actually run any experiments on this assignment. Sorry!

Regards,
Paul

@Joseph_Porrovecchio

If these are the shapes of example_batch_images and example_batch_labels:

Shape of batch of images: (128, 120, 120, 3)
Shape of batch of labels: (128, 1)

please click my name and message your notebook as an attachment.

Thank you so much for your suggestions thus far Paul,

Maximum pixel value of images: 255.0

Shape of batch of images: (128, 120, 120, 3)
Shape of batch of labels: (128, 1)

Thank you Balaji!

Will do.

That makes sense for the pixel values (of course you’ll probably want to scale or normalize them).

But remember that the error message lists the label values as one of the things that may be wrong. What do those look like?

The error does make sense.

create_augmentation_model returns a Sequential model that does data augmentation. create_model should make use of create_augmentation_model to embed data augmentation within the final model and not include augmentation layers again.

See model_with_aug in C2_W2_Lab_2_horses_v_humans_augmentation.ipynb to understand how to embed a Sequential model like a layer inside another Sequential model.

Thank you very much for the suggestions. I made some modifications to my
β€˜create_model’ function as the suggestions in the β€œhorse vs humans” lab.

Now, Instead of an error, the Kernel crashes …

Any help would be appreciated!

[C2W2_Assignment (3).ipynb|attachment] Solution notebook posting on public post is against the honour code of this community, regardless if it is correct or not. You can share the errors/other details instead))

kernel crashing can happen if a cell is unable to run down the codes completely due to the assigned time limit for a particular cell.

Make sure you have a simple model created in create augmented model which is used in create model.

if not getting the hint, please DM codes of create augmented model as well as create model as screenshot by personal DM.

Regards
DP

1 Like

hi @Joseph_Porrovecchio

I noticed one more issue with your codes for create model, you seem to have import tensorflow and edited out the grade cell .

By doing so, you might fail the grader even after your codes are correct .

I also saw ###START AND END CODE HERE### MARKERS removed for create model.

These are all cause for grade failure.

Please make sure you grab a fresh copy and write codes only between assigned markers without editing or removing any other part of the grade cell or any cell for successful submission of your assignment.

Regards
DP

Thank you DP,

How do I grab a fresh copy ?

Hi DP,

Once of my main issues is that when I run the create_model () funtion, I get an error that I am not understanding :

 **NameError: name 'model_without_aug' is not defined**

model_with_aug = tf.keras.Sequential([data_augmentor, model_without_aug ])

![repeated posting codes despite warning can cause your post to be flagged, kindly follow community guidelines and code of conduct, sharing any assignment codes, notebook is not allowed on posts)

  • Open your current notebook.
  • Go to β€œFile->Rename”, and rename the notebook.
  • Go to the Lab Help menu (the question-mark inside a circle), and use β€œGet latest version”.
  • Go to β€œFile->Open” menu, and open the new notebook.
  • Use the β€œKernel->Restart & Clear All Output” command.

Now the new notebook is ready for you to use.

Note: Do not rename the new notebook. The grader always uses the notebook with the original file name.

Please stop posting codes on repeated mentioning, you are still posting codes in public post, this can effect your membership. for better use of discourse community please refer faq section code of conduct.

as I already mentioned in DM, your model needs changing, you do not require too complex layering. Always start with a simple model towards complex model creation, so you know why and how any layer, units or activation has an affect on your training.

I Hope you got the steps mentioned by Paul for getting fresh copy.

Regards
DP

Thank you Paul - will try that out.