`logits` and `labels` must have the same shape

hello everyone,I am a student who is learning tensorflow when,i try to do image segment that my code is error, it say that logits and labels must have the same shape, received ((None, 640, 640, 1) vs (None, 640, 640, 3)). my intention is use rgb-image and mask to train model and segment test image,but i dont know where i false, please help me
that is the code
import torch
import tensorflow.keras as keras
import tensorflow as tf
import os
import cv2
import random
import numpy as np
from keras.layers import Flatten

from skimage import imread, imshow
from skimage.transform import resize
import matplotlib.pyplot as plt

sourse

source_folder = ‘E:\graduate\xunlian_image\xunlian’

get image

image_files = [os.path.join(source_folder, file_name) for file_name in os.listdir(source_folder)
if os.path.isfile(os.path.join(source_folder, file_name)) and file_name.lower().endswith((‘.png’, ‘.jpg’, ‘.jpeg’, ‘.gif’,‘.tif’))]

put into train

for image_file in image_files:
print(image_file)
print(len(image_files),image_files)
image_shape = cv2.imread(image_files[0]).shape
X_train = np.zeros((len(image_files), 640,640,3), dtype=np.uint8)
for i in range(len(image_files)):
img=cv2.imread(image_files[i],cv2.IMREAD_COLOR)
resized_image = cv2.resize(img, (640, 640))
X_train[i] = resized_image
#mask build
labelsource_folder = ‘E:\graduate\xunlian_image\xunlianlabel’

labelimage_files = [os.path.join(labelsource_folder, file_name) for file_name in os.listdir(labelsource_folder)
if os.path.isfile(os.path.join(labelsource_folder, file_name)) and file_name.lower().endswith((‘.png’, ‘.jpg’, ‘.jpeg’, ‘.gif’,‘.tif’))]

for labelimage_file in labelimage_files:
print(labelimage_file)
print(len(labelimage_files),labelimage_files)
image_shape = cv2.imread(labelimage_files[0]).shape
Y_train = np.zeros((len(labelimage_files), 640,640,3), dtype=bool)
for j in range(len(labelimage_files)):
img=cv2.imread(labelimage_files[j],cv2.IMREAD_COLOR)
resized_image = cv2.resize(img, (640, 640))
Y_train[j]=resized_image

#test data load
test_folder = ‘E:\graduate\ceshi_image’

test_files = [os.path.join(test_folder, file_name) for file_name in os.listdir(test_folder)
if os.path.isfile(os.path.join(test_folder, file_name)) and file_name.lower().endswith((‘.png’, ‘.jpg’, ‘.jpeg’, ‘.gif’,‘.tif’))]

for test_file in test_files:
print(test_file)
print(len(test_files),test_files)
testdata = np.zeros((len(test_files), 640,640,3), dtype=np.uint8)
for k in range(len(test_files)):
img=cv2.imread(test_files[k],cv2.IMREAD_COLOR)
resized_image = cv2.resize(img, (640, 640))
testdata[k]=resized_image
#cv2.imshow(‘testimage’,testdata[0])
#cv2.waitKey()
print(‘data have load in’)

IMG_HEIGHT=640
IMG_WIDTH=640
IMG_CHANNELS=3

Build the model

inputs = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
s = tf.keras.layers.Lambda(lambda x: x / 255)(inputs)

Contraction path

c1 = tf.keras.layers.Conv2D(16, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(s)
c1 = tf.keras.layers.Dropout(0.1)(c1)
c1 = tf.keras.layers.Conv2D(16, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)

c2 = tf.keras.layers.Conv2D(32, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(p1)
c2 = tf.keras.layers.Dropout(0.1)(c2)
c2 = tf.keras.layers.Conv2D(32, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(c2)
p2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)

c3 = tf.keras.layers.Conv2D(64, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(p2)
c3 = tf.keras.layers.Dropout(0.2)(c3)
c3 = tf.keras.layers.Conv2D(64, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(c3)
p3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)

c4 = tf.keras.layers.Conv2D(128, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(p3)
c4 = tf.keras.layers.Dropout(0.2)(c4)
c4 = tf.keras.layers.Conv2D(128, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(c4)
p4 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c4)

c5 = tf.keras.layers.Conv2D(256, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(p4)
c5 = tf.keras.layers.Dropout(0.3)(c5)
c5 = tf.keras.layers.Conv2D(256, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(c5)

Expansive path

u6 = tf.keras.layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding=‘same’)(c5)
u6 = tf.keras.layers.concatenate([u6, c4])
c6 = tf.keras.layers.Conv2D(128, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(u6)
c6 = tf.keras.layers.Dropout(0.2)(c6)
c6 = tf.keras.layers.Conv2D(128, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(c6)

u7 = tf.keras.layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding=‘same’)(c6)
u7 = tf.keras.layers.concatenate([u7, c3])
c7 = tf.keras.layers.Conv2D(64, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(u7)
c7 = tf.keras.layers.Dropout(0.2)(c7)
c7 = tf.keras.layers.Conv2D(64, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(c7)

u8 = tf.keras.layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding=‘same’)(c7)
u8 = tf.keras.layers.concatenate([u8, c2])
c8 = tf.keras.layers.Conv2D(32, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(u8)
c8 = tf.keras.layers.Dropout(0.1)(c8)
c8 = tf.keras.layers.Conv2D(32, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(c8)

u9 = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding=‘same’)(c8)
u9 = tf.keras.layers.concatenate([u9, c1], axis=3)
c9 = tf.keras.layers.Conv2D(16, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(u9)
c9 = tf.keras.layers.Dropout(0.1)(c9)
c9 = tf.keras.layers.Conv2D(16, (3, 3), activation=‘relu’, kernel_initializer=‘he_normal’, padding=‘same’)(c9)

outputs = tf.keras.layers.Conv2D(1, (1, 1), activation=‘sigmoid’)(c9)
model = tf.keras.Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer=‘adam’, loss=‘binary_crossentropy’, metrics=[‘accuracy’])
model.summary()

################################

Modelcheckpoint

callbacks = [
tf.keras.callbacks.EarlyStopping(patience=2, monitor=‘val_loss’),
tf.keras.callbacks.TensorBoard(log_dir=‘logs’)]

results = model.fit(X_train, Y_train, validation_split=0.1, batch_size=16, epochs=25, callbacks=callbacks)

Hi @hincheng_chen, The logits are the raw outputs of a model before they are transformed into probabilities. You’re getting this error as the last layer output shape and the label shape do not match. To resolve this error you have to convert your label shape to the model output shape or viceversa. For more details please refer to this gist. Thank You.

1 Like

Thank you very much!