Colab link is here:
The data is imported the following was
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
main_folder,
validation_split=0.1,
subset="training",
label_mode='categorical',
seed=123,
image_size=(dim, dim))
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
main_folder,
validation_split=0.1,
subset="validation",
label_mode='categorical',
seed=123,
image_size=(dim, dim))
The model is trained the following way
model = tf.keras.models.Sequential([
tf.keras.layers.experimental.preprocessing.Rescaling(1. / 255),
...
tf.keras.layers.Dense(2, activation='softmax')
])
model.compile(optimizer="adam", loss=tf.keras.losses.CategoricalCrossentropy(), metrics=['accuracy'])
I am struggling with getting the right predicted categories
and right true_categories
to get the classification report to work:
y_pred = model.predict(val_ds, batch_size=1)
predicted_categories = np.argmax(y_pred, axis=1)
true_categories = tf.concat([y for x, y in val_ds], axis=0).numpy()
true_categories_argmax = np.argmax(true_categories, axis=1)
print(classification_report(true_categories_argmax, predicted_categories))
At the moment the output of the epoch is contradicting the classification report
Epoch 22/75
144/144 [==============================] - 7s 48ms/step - loss: 0.0611 - accuracy: 0.9776 - val_loss: 0.0768 - val_accuracy: 0.9765
The validation set on the model returns
model.evaluate(val_ds)
[==============================] - 0s 16ms/step - loss: 0.0696 - accuracy: 0.9784
[0.06963862478733063, 0.9784313440322876]
while the classification report is very different:
precision recall f1-score support
0.0 0.42 0.44 0.43 221
1.0 0.56 0.54 0.55 289
accuracy 0.49 510
macro avg 0.49 0.49 0.49 510
weighted avg 0.50 0.49 0.50 510
Similiar questions here, here, here, here, here with no answers to this issue.