Search code examples
pythontensorflowtf.kerasvalueerror

Vale Error using very simple Network with Input and conv2d layers


using the following code to train a model on random data just to try some implementations throws an "easy" error I just cannot get my head around:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

def create_dataset(size=(224, 224)):
    array = np.random.randint(-128,127, (10, size[0], size[1], 3))
    array2 = np.argmax(array, axis=-1)
    array = np.split(array,size_dataset, axis=0)
    array2 = np.split(array2,size_dataset, axis=0)
    y= np.random.randint(-1,1, size_dataset)
    return array, array2

def create_model():
    encoder_input = keras.Input(shape=(224,224,3), name="start")
    encoder_output=keras.layers.Conv2D(filters=64,kernel_size=(3,3), activation='relu')(encoder_input)
    model=tf.keras.Model(inputs=encoder_input, outputs=encoder_output) 
    return model


if __name__=='__main__':
    data,target=create_dataset()
    model=create_model()
    model.compile(optimizer='adam',loss=tf.keras.losses.BinaryCrossentropy(),metrics='accuracy')
    model.fit(x=data, y=target, batch_size=16, epochs=2)

The error is as follows:

ValueError: Dimensions must be equal, but are 224 and 222 for '{{node binary_crossentropy/mul}} = Mul[T=DT_FLOAT](Cast, binary_crossentropy/Log)' with input shapes: [?,224,224], [?,222,222,64].

But what is the problem here. It seems that the input layer and the conv2d do not correclty work together. Maybe, it is just to late over here :-)

Thanks in advance


Solution

  • You need to pass padding='same' to Conv2D. By default it uses padding='valid' which means the output size is reduced by kernel size minus one. Here https://towardsdatascience.com/a-comprehensive-introduction-to-different-types-of-convolutions-in-deep-learning-669281e58215 it is explained in details.