Bug Issue
The doc of keras.applications.MobileNetV3Small()
shows its description as below:
https://github.com/keras-team/keras/blob/1bdf25b830ba1c46aec5c04b43e0c4d1df51d683/keras/src/applications/mobilenet_v3.py#L109-L110
The model.compile()
function is coded here:
https://github.com/keras-team/keras/blob/1bdf25b830ba1c46aec5c04b43e0c4d1df51d683/keras/src/trainers/trainer.py#L41-L52
I found a phenomenon on keras.applications.MobileNetV3Small()
, see the repro below, with TensorFlow 2.19.0 and Keras nightly:
Repro 1 (With include_top == False)
import keras
import numpy as np
model = keras.applications.MobileNetV3Small(input_shape=(160, 160, 3), alpha=0.75, minimalistic=False, include_top=False, weights='imagenet', input_tensor=None, classes=1000, pooling=None, dropout_rate=0.2, classifier_activation='softmax', include_preprocessing=True)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# evaluate
x = np.zeros((1, 160, 160, 3))
y_fake = np.zeros((1, 1000))
y_fake[0, 0] = 1
try:
loss, acc = model.evaluate(x, y_fake, verbose=0)
print(f"Loss: {loss:.4f}, Accuracy: {acc:.4f}")
print("Evaluate successfully.")
except:
print("Compile successfully but Evaluate failed.")
Output 1
Compile successfully but Evaluate failed.
Repro 2 (With include_top == True)
import keras
import numpy as np
model = keras.applications.MobileNetV3Small(input_shape=(160, 160, 3), alpha=0.75, minimalistic=False, include_top=True, weights='imagenet', input_tensor=None, classes=1000, pooling=None, dropout_rate=0.2, classifier_activation='softmax', include_preprocessing=True)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# evaluate
x = np.zeros((1, 160, 160, 3))
y_fake = np.zeros((1, 1000))
y_fake[0, 0] = 1
try:
loss, acc = model.evaluate(x, y_fake, verbose=0)
print(f"Loss: {loss:.4f}, Accuracy: {acc:.4f}")
print("Evaluate successfully.")
except:
print("Compile successfully but Evaluate failed.")
Output 2
Loss: 8.2121, Accuracy: 0.0000
Evaluate successfully.
I'm not sure whether this is expected to do not check the applicability while compile the model with loss
and include_top
et al. specified.
Suggestions
- If not expected, add some check on the applicability, for this issue, the
output shape mismatch
is happened between the combination ofloss
andinclude_top
.
Thanks a lot!
Comment From: MalyalaKarthik66
I tried this and got the same error. It happens because when include_top=False, the output shape doesn’t match with categorical_crossentropy. Can I work on adding a check or warning for this?
Comment From: sonali-kumari1
Hi @MalyalaKarthik66 - Thank you for your interest in contributing to Keras! Please feel free to open a pull request and link it with this issue. Thanks!