Python Keras Convolutional autoencoder. ValueError: logits and labels must have the same shape

Viewed 99

I took most of this code from here. I am very new to autoencoders.

Heres the code:

input_img = keras.Input(shape=(64, 55,1))

x = keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = keras.layers.MaxPooling2D((2, 2), padding='same')(x)

x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(16, (3, 3), activation='relu',padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
decoded = keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

onlyfiles = [f for f in listdir("newnew") if isfile(join("newnew", f))][:2000]
lis=[]
for i in onlyfiles:
    image = Image.open('newnew/'+i)
    data = np.asarray(image)
    lis.append(data)
    
lis = np.array(lis)
lis = lis.astype("float32")
lis = lis / np.max(lis)
print(lis.shape)
lis = lis.reshape((len(lis),64,55,1))
print(lis.shape)

test = lis[:100]
train = lis[100:]

autoencoder.fit(train,train,
                epochs=50,
                batch_size=128,
                shuffle=True,
                validation_data=(test,test))

As I was just trying to get the code to run, I limited it to only 2000 images. For context, the images are greyscale and already 55x64.

The error message I am recieving is:

ValueError: logits and labels must have the same shape ((None, 64, 56, 1) vs (None, 64, 55, 1))

After reading similar problems on stack overflow, I have already made one change to the website I took the autoencoder from, by adding an additional padding='same' to one of the lines which was missing it. I believe the root of the problem is somewhere in the layers because originally before adding the extra "padding=same", the error message was saying:

ValueError: logits and labels must have the same shape ((None, 60, 52, 1) vs (None, 64, 55, 1))

Unfortunately as I say I'm new to autoencoders and still don't fully understand all the numbers in layers. Any help would be appreciated :)

1 Answers

Because the output of an autoencoder is the same as the input, the input and output shapes should be the same. The input shape in your model is (None, 64, 55,1) and the output shape is (None, 64, 56, 1), resulting in the error.

To overcome this, add a Cropping2D layer to the model, as shown below.

input_img = keras.Input(shape=(64, 55,1))

x = keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = keras.layers.MaxPooling2D((2, 2), padding='same')(x)

x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(16, (3, 3), activation='relu',padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
decoded = keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
decoded= keras.layers.Cropping2D(((0,0),(0,1)))(decoded)
autoencoder = keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy') 
autoencoder.summary()

Output:

Model: "model_11"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_15 (InputLayer)       [(None, 64, 55, 1)]       0         
                                                                 
 conv2d_98 (Conv2D)          (None, 64, 55, 16)        160       
                                                                 
 max_pooling2d_42 (MaxPoolin  (None, 32, 28, 16)       0         
 g2D)                                                            
                                                                 
 conv2d_99 (Conv2D)          (None, 32, 28, 8)         1160      
                                                                 
 max_pooling2d_43 (MaxPoolin  (None, 16, 14, 8)        0         
 g2D)                                                            
                                                                 
 conv2d_100 (Conv2D)         (None, 16, 14, 8)         584       
                                                                 
 max_pooling2d_44 (MaxPoolin  (None, 8, 7, 8)          0         
 g2D)                                                            
                                                                 
 conv2d_101 (Conv2D)         (None, 8, 7, 8)           584       
                                                                 
 up_sampling2d_42 (UpSamplin  (None, 16, 14, 8)        0         
 g2D)                                                            
                                                                 
 conv2d_102 (Conv2D)         (None, 16, 14, 8)         584       
                                                                 
 up_sampling2d_43 (UpSamplin  (None, 32, 28, 8)        0         
 g2D)                                                            
                                                                 
 conv2d_103 (Conv2D)         (None, 32, 28, 16)        1168      
                                                                 
 up_sampling2d_44 (UpSamplin  (None, 64, 56, 16)       0         
 g2D)                                                            
                                                                 
 conv2d_104 (Conv2D)         (None, 64, 56, 1)         145       
                                                                 
 cropping2d_13 (Cropping2D)  (None, 64, 55, 1)         0         
                                                                 
=================================================================
Total params: 4,385
Trainable params: 4,385
Non-trainable params: 0
_________________________________________________________________ 
Related