asendjasni
asendjasni

Reputation: 1074

Keras ValueError : the Dens' input dimension should be defined

I'm trying to use VGG16 with some modification on it. I followed this blog post from keras.io

Here the code I'm using to create the model:

def create_model():

    vgg16_model = vgg16.VGG16(weights='imagenet', include_top=False)
    
    print('[INFO] Model loaded.')

    x = vgg16_model.output
    x = Flatten()(x)
    x = Dense(256, activation="relu")(x)
    x = Dropout(0.5)(x)
    x = Dense(1, activation='linear')(x)

    model = Model(inputs=vgg16_model.inputs, outputs=x)
    return model

Calling the model = create_model() gives an error:

ValueError: The last dimension of the inputs to Dense should be defined. Found None.

What could be the problem?

Upvotes: 0

Views: 141

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22021

try to pass an input_shape when you use the vgg16.VGG16

def create_model():

    vgg16_model = vgg16.VGG16(input_shape=(224,224,3), weights='imagenet', include_top=False)
    
    print('[INFO] Model loaded.')

    x = vgg16_model.output
    x = Flatten()(x)
    x = Dense(256, activation="relu")(x)
    x = Dropout(0.5)(x)
    x = Dense(1, activation='linear')(x)

    model = Model(inputs=vgg16_model.inputs, outputs=x)
    return model

Upvotes: 1

Related Questions