I am using Keras (on top of TF 2.3) to train an image classifier. In some cases I have more than two classes, but often there are just two classes (either "good" or "bad"). I am using the
tensorflow.keras.applications.VGG16 class as base model with a custom classifier on top, like this:
input_layer = layers.Input(shape=(self.image_size, self.image_size, 3), name="model_input") base_model = VGG16(weights="imagenet", include_top=False, input_tensor=input_layer) model_head = base_model.output model_head = layers.AveragePooling2D(pool_size=(4, 4))(model_head) model_head = layers.Flatten()(model_head) model_head = layers.Dense(256, activation="relu")(model_head) model_head = layers.Dropout(0.5)(model_head) model_head = layers.Dense(len(self.image_classes), activation="softmax")(model_head)
As you can see in the last (output) layer I am using a
softmax activation function. Then I compile the whole model with the
categorical_crossentropy loss function and train with one-hot-encoded image data (labels).
All in all the model performs quite well, I am happy with the results, I achieve over 99% test and validation accuracy with our data set. There is one thing I don't understand though:
When I call
predict() on the Keras model and look at the prediction results, then these are always either 0 or 1 (or at least very, very close to that, like 0.000001 and 0.999999). So my classifier seems to be quite sure whether an image belongs to either class "good" or "bad" (for example, if I am using only two classes). I was under the assumption, however, that usually these predictions are not that clear, more in terms of "the model thinks with a probability of 80% that this image belongs to class A" - but as said in my case it's always 100% sure.
Any ideas why this might be the case?