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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.