File: confusionmatrix.cpp

package info (click to toggle)
cloudcompare 2.13.2%2Bgit20240821%2Bds-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 151,152 kB
  • sloc: cpp: 687,217; ansic: 165,269; python: 31,109; xml: 25,906; sh: 940; makefile: 509; java: 229; asm: 204; fortran: 160; javascript: 73; perl: 18
file content (313 lines) | stat: -rw-r--r-- 9,796 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "confusionmatrix.h"
#include "ui_confusionmatrix.h"

#include <CCConst.h>

#include <iterator>
#include <set>
#include <algorithm>

#include <QBrush>
#include <QFile>
#include <QTextStream>
#include <iostream>

#include <ccLog.h>

#include <ccLog.h>

static QColor GetColor(double value, double r1, double g1, double b1)
{
	double r0 = 255.0;
	double g0 = 255.0;
	double b0 = 255.0;
	if (value < 0.05)
	{
		value = 0.05;
	}
	else if (value > 0.95)
	{
		value = 0.95;
	}
	int r = static_cast<int>((r1 - r0) * value + r0);
	int g = static_cast<int>((g1 - g0) * value + g0);
	int b = static_cast<int>((b1 - b0) * value + b0);
//	ccLog::Warning("value " + QString::number(value) + " (" + QString::number(r) + ", " + QString::number(g) + ", " + QString::number(b) + ")");
	return QColor(r, g, b);
}

ConfusionMatrix::ConfusionMatrix(const std::vector<ScalarType> &actual, const std::vector<ScalarType> &predicted)
	: nbClasses(0)
	, ui(new Ui::ConfusionMatrix)
	, m_overallAccuracy(0.0f)
	
{
	ui->setupUi(this);
	this->setWindowFlag(Qt::WindowStaysOnTopHint);

	compute(actual, predicted);

	this->ui->tableWidget->resizeColumnsToContents();
	this->ui->tableWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
	QSize tableSize = this->ui->tableWidget->sizeHint();
	QSize widgetSize = QSize(tableSize.width() + 30, tableSize.height() + 50);
	this->setMinimumSize(widgetSize);
}

ConfusionMatrix::~ConfusionMatrix()
{
	delete ui;
	ui = nullptr;
}

void ConfusionMatrix::computePrecisionRecallF1Score(cv::Mat& matrix, cv::Mat& precisionRecallF1Score, cv::Mat& vec_TP_FN)
{
	int nbClasses = matrix.rows;

	// compute precision
	for (int predictedIdx = 0; predictedIdx < nbClasses; predictedIdx++)
	{
		float TP = 0;
		float FP = 0;
		for (int realIdx = 0; realIdx < nbClasses; realIdx++)
		{
			if (realIdx == predictedIdx)
				TP = matrix.at<int>(realIdx, realIdx);
			else
				FP += matrix.at<int>(realIdx, predictedIdx);
		}
		float TP_FP = TP + FP;
		if (TP_FP == 0)
			precisionRecallF1Score.at<float>(predictedIdx, PRECISION) = CCCoreLib::NAN_VALUE;
		else
			precisionRecallF1Score.at<float>(predictedIdx, PRECISION) = TP / TP_FP;
	}

	// compute recall
	for (int realIdx = 0; realIdx < nbClasses; realIdx++)
	{
		float TP = 0;
		float FN = 0;
		for (int predictedIdx = 0; predictedIdx< nbClasses; predictedIdx++)
		{
			if (realIdx == predictedIdx)
				TP = matrix.at<int>(realIdx, realIdx);
			else
				FN += matrix.at<int>(realIdx, predictedIdx);
		}
		float TP_FN = TP + FN;
		if (TP_FN == 0)
			precisionRecallF1Score.at<float>(realIdx, RECALL) = CCCoreLib::NAN_VALUE;
		else
			precisionRecallF1Score.at<float>(realIdx, RECALL) = TP / TP_FN;
		vec_TP_FN.at<int>(realIdx, 0) = TP_FN;
	}

	// compute F1-score
	for (int realIdx = 0; realIdx < nbClasses; realIdx++)
	{
		float den = precisionRecallF1Score.at<float>(realIdx, PRECISION)
				+ precisionRecallF1Score.at<float>(realIdx, RECALL);
		if (den == 0)
			precisionRecallF1Score.at<float>(realIdx, F1_SCORE) = CCCoreLib::NAN_VALUE;
		else
			precisionRecallF1Score.at<float>(realIdx, F1_SCORE) =
					2
					* precisionRecallF1Score.at<float>(realIdx, PRECISION)
					* precisionRecallF1Score.at<float>(realIdx, RECALL)
					/ den;
	}

}

float ConfusionMatrix::computeOverallAccuracy(cv::Mat& matrix)
{
	int nbClasses = matrix.rows;
	float totalTrue = 0;
	float totalFalse = 0;

	m_overallAccuracy = 0.0;

	for (int realIdx = 0; realIdx < nbClasses; realIdx++)
	{
		for (int predictedIdx = 0; predictedIdx< nbClasses; predictedIdx++)
		{
			if (realIdx == predictedIdx)
				totalTrue += matrix.at<int>(realIdx, realIdx);
			else
				totalFalse += matrix.at<int>(realIdx, predictedIdx);
		}
	}
	if ((totalTrue + totalFalse) != 0)
		m_overallAccuracy = totalTrue / (totalTrue + totalFalse);
	else
		m_overallAccuracy = CCCoreLib::NAN_VALUE;

	return m_overallAccuracy;
}

void ConfusionMatrix::compute(const std::vector<ScalarType>& actual, const std::vector<ScalarType>& predicted)
{
	int idxActual;
	int idxPredicted;
	int actualClass;
	int predictedClass;

	// get the set of classes with the contents of the actual classes
	std::set<ScalarType> classes(actual.begin(), actual.end());
	int nbClasses = static_cast<int>(classes.size());
	confusionMatrix = cv::Mat(nbClasses, nbClasses, CV_32S, cv::Scalar(0));
	precisionRecallF1Score = cv::Mat(nbClasses, 3, CV_32F, cv::Scalar(0));
	cv::Mat vec_TP_FN(nbClasses, 1, CV_32S, cv::Scalar(0));

	// fill the confusion matrix
	for (int i = 0; i < actual.size(); i++)
	{
		actualClass = actual.at(i);
		idxActual = std::distance(classes.begin(), classes.find(actualClass));
		predictedClass = predicted.at(i);
		idxPredicted = std::distance(classes.begin(), classes.find(predictedClass));
		confusionMatrix.at<int>(idxActual, idxPredicted)++;
	}

	// compute precision recall F1-score
	computePrecisionRecallF1Score(confusionMatrix, precisionRecallF1Score, vec_TP_FN);
	float overallAccuracy = computeOverallAccuracy(confusionMatrix);

	// display the overall accuracy
	this->ui->label_overallAccuracy->setText(QString::number(overallAccuracy, 'g', 2));

	std::set<ScalarType>::iterator itB = classes.begin();
	std::set<ScalarType>::iterator itE = classes.end();
	class_numbers.assign(itB, itE);

	// BUILD THE QTABLEWIDGET

	this->ui->tableWidget->setColumnCount(2+ nbClasses + 3); // +2 for titles, +3 for precision / recall / F1-score
	this->ui->tableWidget->setRowCount(2 + nbClasses);
	// create a font for the table widgets
	QFont font;
	font.setBold(true);
	QTableWidgetItem *newItem = nullptr;
	// set the row and column names
	this->ui->tableWidget->setSpan(0, 0, 2, 2); // empty area
	this->ui->tableWidget->setSpan(0, 2, 1, nbClasses); // 'Predicted' header
	this->ui->tableWidget->setSpan(2, 0, nbClasses, 1); // 'Actual' header
	this->ui->tableWidget->setSpan(0, 2 + nbClasses, 1, 3); // empty area
	// Predicted
	newItem = new QTableWidgetItem("Predicted");
	newItem->setFont(font);
	newItem->setBackground(Qt::lightGray);
	newItem->setTextAlignment(Qt::AlignCenter);
	this->ui->tableWidget->setItem(0, 2, newItem);
	// Real
	newItem = new QTableWidgetItem("Real");
	newItem->setFont(font);
	newItem->setBackground(Qt::lightGray);
	newItem->setTextAlignment(Qt::AlignCenter);
	this->ui->tableWidget->setItem(2, 0, newItem);
	// add precision / recall / F1-score headers
	newItem = new QTableWidgetItem("Precision");
	newItem->setToolTip("TP / (TP + FP)");
	newItem->setFont(font);
	this->ui->tableWidget->setItem(1, 2 + nbClasses + PRECISION, newItem);
	newItem = new QTableWidgetItem("Recall");
	newItem->setToolTip("TP / (TP + FN)");
	newItem->setFont(font);
	this->ui->tableWidget->setItem(1, 2 + nbClasses + RECALL, newItem);
	newItem = new QTableWidgetItem("F1-score");
	newItem->setToolTip("Harmonic mean of precision and recall (the closer to 1 the better)\n2 x precision x recall / (precision + recall)");
	newItem->setFont(font);
	this->ui->tableWidget->setItem(1, 2 + nbClasses + F1_SCORE, newItem);
	// add column names and row names
	for (int idx = 0; idx < class_numbers.size(); idx++)
	{
		QString str = QString::number(class_numbers[idx]);
		newItem = new QTableWidgetItem(str);
		newItem->setFont(font);
		this->ui->tableWidget->setItem(1, 2 + idx, newItem);
		newItem = new QTableWidgetItem(str);
		newItem->setFont(font);
		this->ui->tableWidget->setItem(2 + idx, 1, newItem);
	}

	// FILL THE QTABLEWIDGET

	// add the confusion matrix values
	for (int row = 0; row < nbClasses; row++)
		for (int column = 0; column < nbClasses; column++)
		{
			double val = confusionMatrix.at<int>(row, column);
			QTableWidgetItem *newItem = new QTableWidgetItem(QString::number(val));
			if (row == column)
			{
				newItem->setBackground(GetColor(val / vec_TP_FN.at<int>(row, 0), 0, 128, 255));
			}
			else
			{
				newItem->setBackground(GetColor(val / vec_TP_FN.at<int>(row, 0), 200, 50, 50));
			}
			this->ui->tableWidget->setItem(2 + row, + 2 + column, newItem);
		}

	// set precision / recall / F1-score values
	for (int realIdx=0; realIdx < nbClasses; realIdx++)
	{
		newItem = new QTableWidgetItem(QString::number(precisionRecallF1Score.at<float>(realIdx, PRECISION), 'g', 2));
		this->ui->tableWidget->setItem(2 + realIdx, 2 + nbClasses + PRECISION, newItem);
		newItem = new QTableWidgetItem(QString::number(precisionRecallF1Score.at<float>(realIdx, RECALL), 'g', 2));
		this->ui->tableWidget->setItem(2 + realIdx, 2 + nbClasses + RECALL, newItem);
		newItem = new QTableWidgetItem(QString::number(precisionRecallF1Score.at<float>(realIdx, F1_SCORE), 'g', 2));
		this->ui->tableWidget->setItem(2 + realIdx, 2 + nbClasses + F1_SCORE, newItem);
	}
}

void ConfusionMatrix::setSessionRun(QString session, int run)
{
	QString label;

	label = session + " / " + QString::number(run);

	this->ui->label_sessionRun->setText(label);
}

bool ConfusionMatrix::save(QString filePath)
{
	QFile file(filePath);

	if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
	{
		ccLog::Error("impossible to open file: " + filePath);
		return false;
	}

	QTextStream stream(&file);
	stream << "# columns: predicted classes\n# rows: actual classes\n";
	stream << "# last three colums: precision / recall / F1-score\n";
	for (auto class_number : class_numbers)
	{
		stream << class_number << " ";
	}
	stream << Qt::endl;
	for (int row = 0; row < confusionMatrix.rows; row++)
	{
		stream << class_numbers.at(row) << " ";
		for (int col = 0; col < confusionMatrix.cols; col++)
		{
			stream << confusionMatrix.at<int>(row, col) << " ";
		}
		stream << precisionRecallF1Score.at<float>(row, PRECISION) << " ";
		stream << precisionRecallF1Score.at<float>(row, RECALL) << " ";
		stream << precisionRecallF1Score.at<float>(row, F1_SCORE) << Qt::endl;
	}

	file.close();

	return true;

}

float ConfusionMatrix::getOverallAccuracy()
{
	return m_overallAccuracy;
}