ValueError: `logits` and `labels` must have the same shape, received ((None, 256, 256, 4) vs (None,)) ValueError: `logits` and `labels` must have the same shape, received ((None, 256, 256, 4) vs (None,))

def conv_block(input, num_filters):
x = Conv2D(num_filters, 3, padding=“same”)(input)
x = BatchNormalization()(x) #Not in the original network.
x = Activation(“relu”)(x)

x = Conv2D(num_filters, 3, padding="same")(x)
x = BatchNormalization()(x)  #Not in the original network
x = Activation("relu")(x)

return x

def encoder_block(input, num_filters):
x = conv_block(input, num_filters)
p = MaxPool2D((2, 2))(x)
return x, p

def decoder_block(input, skip_features, num_filters):
x = Conv2DTranspose(num_filters, (2, 2),strides=2,padding=“same”)(input)
x = Concatenate()([x, skip_features])
x = conv_block(x, num_filters)
return x

def build_unet(input_shape, n_classes):
inputs = Input(input_shape)

s1, p1 = encoder_block(inputs, 64)
s2, p2 = encoder_block(p1, 128)
s3, p3 = encoder_block(p2, 256)
s4, p4 = encoder_block(p3, 512)

b1 = conv_block(p4, 1024) #Bridge

d1 = decoder_block(b1, s4, 512)
d2 = decoder_block(d1, s3, 256)
d3 = decoder_block(d2, s2, 128)
d4 = decoder_block(d3, s1, 64)

if n_classes == 1:
  activation = 'sigmoid'
else:
  activation = 'softmax'

outputs = Conv2D(n_classes, 1, padding="same", activation=activation)(d4)  


model = Model(inputs, outputs, name="U-Net")


return model

my_unet = build_unet(input_shape=(256,256,3), n_classes=4)

print(my_unet.summary())

my_unet.compile(optimizer=tf.keras.optimizers.Adam(), loss=‘binary_crossentropy’, metrics=[‘accuracy’])

history = my_unet.fit(
train_ds,
batch_size=BATCH_SIZE,
validation_data=val_ds,
verbose=1,
epochs=50,

)

can somebody help me with this code

Hi @Amisha_Gouda, While passing labels during model.fit the labels should have the same shape as the input. But you are passing the shape as (None,). Please refer to this gist for the working code example. Thank You.