From 598460ec8118f171da209de195b0cbc1a1a01976 Mon Sep 17 00:00:00 2001
From: "Daniel.Frisinghelli" <daniel.frisinghelli@eurac.edu>
Date: Fri, 19 Feb 2021 16:49:56 +0100
Subject: [PATCH] Specify labels also for confusion matrix.

---
 pysegcnn/core/trainer.py | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/pysegcnn/core/trainer.py b/pysegcnn/core/trainer.py
index d0c2a57..792ebe2 100644
--- a/pysegcnn/core/trainer.py
+++ b/pysegcnn/core/trainer.py
@@ -2733,7 +2733,9 @@ class NetworkInference(BaseConfig):
 
                 # calculate confusion matrix
                 LOGGER.info('Computing confusion matrix ...')
-                conf_mat = confusion_matrix(y_true, y_pred)
+                conf_mat = confusion_matrix(
+                    y_true, y_pred,
+                    labels=np.asarray(list(self.use_labels.keys())))
 
                 # add confusion matrix to model output
                 output['cm'] = conf_mat
@@ -2779,11 +2781,15 @@ class NetworkInference(BaseConfig):
 
             # labels to predict: drop averages
             try:
+                # check if for some scenes micro average is calculated rather
+                # than accuracy due to missing class labels
                 labels = list(report.index.drop(['macro avg', 'weighted avg',
-                                                 'accuracy']))
+                                                 'micro_avg', 'accuracy']))
             except KeyError:
+                # micro average is not calculated for any scene: each scene
+                # contains all the classes
                 labels = list(report.index.drop(['macro avg', 'weighted avg',
-                                                 'micro avg']))
+                                                 'accuracy']))
 
             # plot classification report
             fig = plot_classification_report(report, labels)
-- 
GitLab