Search code examples
pythontensorflowkerasdeep-learningresnet

Exception training Resnet50: "The shape of the input to "Flatten" is not fully defined"


I want to use keras.applications.resnet50 to train a Resnet for a two class problem using the following setup:

from keras.layers import Dropout, Flatten, Dense
from keras.applications.resnet50 import ResNet50
from keras.models import Model

resNet = ResNet50(include_top=False, weights=None)
y = resNet.output
y = Flatten()(y)
y = Dense(2, activation='softmax')(y)
model = Model(inputs=resNet.input, outputs=y)
opt = keras.optimizers.Adam()
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
epochs = 15
model.fit(train_tensors, train_targets, 
          validation_data=(valid_tensors, valid_targets),
          epochs=epochs, batch_size=10, callbacks=[checkpointer], verbose=1)

Running the code throws the error

Exception: The shape of the input to "Flatten" is not fully defined 

So there must be something wrong with the input tensor to the output layer, which in my case is a one-hot encoded vector, i.e. a one-dimensional array of size 2. What am I doing wrong?


Solution

  • You get

    Exception: The shape of the input to "Flatten" is not fully defined

    because you have not set the input shape in your resnet network. Try:

    resNet = ResNet50(include_top=False, weights=None, input_shape=(224, 224, 3)) 
    

    Also since you are using binary_crossentropy with sigmoid activation in you output layer you should use only 1 neuron not 2, like this:

    y = Dense(1, activation='sigmoid')(y)