0
votes

I'm trying to use the Keras ResNet50 implementation for training a binary image classification model.

I want to test the model without using transfer learning but when i try to change the output layer using a simple dense layer with sigmoid activation for the binary classification i got errors regarding shape size.

My code is this:

baseModel= ResNet50(weights=None, include_top=False, classes=2, pooling=max)

output = baseModel.output
output = layers.Dense(1, activation='sigmoid')(output)

model = keras.models.Model(inputs=baseModel.input, outputs=output)

model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy',  metrics=['accuracy'])

Doing this i got this error:

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

If i add a flatten layer before the dense layer i got:

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

What I'm missing here? How i can change the imput shape for the dense layer?

1
Why not use ResNet50(weights=None, include_top=True, classes=1) ?jakub
Using the default top, without using the included weights doesn't include all the classes in the imageNet dataset for prediction?baboo
Ok, i better read the documentation, and the "classes" arguments is there for this purpose. For binary classification i should use 1 or 2?baboo
You can use 1 class with a sigmoid activation function, or 2 classes with a softmax activation function.jakub
In your code you have output = layers.Dense(1, activation='sigmoid')(out) but where is out defined? I suspect you meant output. Can you provide the first lines and last lines of model,summary? classes is: optional number of classes to classify images into, only to be specified if include_top is True, and if no weights argument is specified. You have Top=False so do not specify classesGerry P

1 Answers

1
votes

For ResNet you specified Top=False and pooling = 'max' so the Resent model has added a final max pooling layer to the model. So use the code below: You do not need to add a flatten layer, max pooling flattens the output for you.

out=basemodel.layers[-1].output 
output = layers.Dense(1, activation='sigmoid')(out)

You can use model.summary() to see the model structure. Also you should not use classes=2. When top is false classes should not be specified.