Home > database >  InvalidArgumentError: Incompatible shapes: [29] vs. [29,7,7,2]
InvalidArgumentError: Incompatible shapes: [29] vs. [29,7,7,2]

Time:11-23

so I'm new right here and in Python also. I'm trying to make my own network. I found some pictures of docs and cats 15x15 and unfortunatly couldn't make this basic network...

So, these are libraries which I'm using

    from tensorflow.keras.models import Sequential
    from tensorflow.keras import utils
    from tensorflow.keras.datasets import mnist  
    from tensorflow.keras.layers import Dense
    import numpy as np
    import matplotlib.pyplot as plt
    import tensorflow as tf
    import keras
    from tensorflow.keras.layers import Conv2D
    from tensorflow.keras.layers import MaxPooling2D
    from tensorflow.keras.layers import GlobalMaxPooling2D

Body

train_dataset = tf.keras.preprocessing.image_dataset_from_directory(
        'drive/MyDrive/cats vs dogs/cats vs dogs/training',
        color_mode="rgb",
        batch_size=32,
        image_size=(150, 150),
        shuffle=True,
        seed=42,
        validation_split=0.1,
        subset='training',
        interpolation="bilinear",
        follow_links=False,
        )

validation_dataset = tf.keras.preprocessing.image_dataset_from_directory(
    'drive/MyDrive/cats vs dogs/cats vs dogs/training',
    color_mode="rgb",
    batch_size=32,
    image_size=(150, 150),
    shuffle=True,
    seed=42,
    validation_split=0.1,
    subset='validation',
    interpolation="bilinear",
    follow_links=False,
    )
test_dataset = tf.keras.preprocessing.image_dataset_from_directory(
    'drive/MyDrive/cats vs dogs/cats vs dogs/test',
     batch_size = 32,
     image_size = (150, 150),
     interpolation="bilinear"
     )

model = Sequential()
model.add(keras.Input(shape=(150, 150, 3)))
model.add(Conv2D(32, 5, strides=2, activation="relu"))
model.add(Conv2D(32, 3, activation="relu"))
model.add(MaxPooling2D(3))
model.add(Dense(250, activation='sigmoid'))
model.add(Dense(100))
model.add(MaxPooling2D(3))
model.add(Dense(2))
model.summary()

model.compile(loss='binary_crossentropy', 
          optimizer='adam',
          metrics=['accuracy'])

history = model.fit(train_dataset, validation_data=validation_dataset, epochs=5, verbose=2)

And I get this error

Incompatible shapes: [29] vs. [29,7,7,2]
 [[node gradient_tape/binary_crossentropy/mul_1/BroadcastGradientArgs
 (defined at /usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/optimizer_v2.py:464)
 ]] [Op:__inference_train_function_4364]

 Errors may have originated from an input operation.
 Input Source operations connected to node 
 gradient_tape/binary_crossentropy/mul_1/BroadcastGradientArgs:
 In[0] gradient_tape/binary_crossentropy/mul_1/Shape:   
 In[1] gradient_tape/binary_crossentropy/mul_1/Shape_1

I was trying to change from binary_crossentropy to categorical_crossentrapy but it didn't help, I suppose my mistake is in datasets or inputs but I don't know how to solve it :(

Really hope to find help here!

[my architecture][1] [1]: https://i.stack.imgur.com/w4Y9N.png

CodePudding user response:

You need to flatten your prediction somewhere, otherwise you are outputing an image (29 samples of size 7x7 with 2 channels), while you simply want a flat 2 dimensional logits (so shape 29x2). The architecture you are using is somewhat odd, did you mean to have flattening operation before first Dense layer, and then no "maxpooling2d" (as it makes no sense for flattened signal)? Mixing relu and sigmoid activations is also quite non standard, I would encourage you to start with established architectures rather than try to compose your own to get some intuitions.

model = Sequential()
model.add(keras.Input(shape=(150, 150, 3)))
model.add(Conv2D(32, 5, strides=2, activation="relu"))
model.add(Conv2D(32, 3, activation="relu"))
model.add(MaxPooling2D(3))
model.add(Flatten())
model.add(Dense(250, activation="relu"))
model.add(Dense(100, activation="relu"))
model.add(Dense(2))
model.summary()
  • Related