output.cpp
12.9 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright 2012 The MITRE Corporation *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <QByteArray>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QHash>
#include <QFile>
#include <QFileInfo>
#include <QList>
#ifndef BR_EMBEDDED
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#endif // BR_EMBEDDED
#include <QMutex>
#include <QPair>
#include <QVector>
#include <QtGlobal>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <limits>
#include <assert.h>
#include <openbr_plugin.h>
#include "core/bee.h"
#include "core/common.h"
#include "core/opencvutils.h"
#include "core/qtutils.h"
using namespace br;
/*!
* \ingroup outputs
* \brief Comma separated values output.
* \author Josh Klontz \cite jklontz
*/
class csvOutput : public MatrixOutput
{
Q_OBJECT
~csvOutput()
{
if (file.isNull() || targetFiles.isEmpty() || queryFiles.isEmpty()) return;
QFile out(file);
bool success = out.open(QFile::WriteOnly);
if (!success) qFatal("Output::saveCSV failed to open %s for writing.", qPrintable((QString)file));
out.write("File,");
out.write(qPrintable(targetFiles.names().join(",")));
out.write("\n");
for (int i=0; i<queryFiles.size(); i++) {
out.write(qPrintable(queryFiles[i].name));
for (int j=0; j<targetFiles.size(); j++) {
out.write(",");
out.write(qPrintable(toString(i,j)));
}
out.write("\n");
}
out.close();
}
};
BR_REGISTER(Output, csvOutput)
/*!
* \ingroup outputs
* \brief One score per row.
* \author Josh Klontz \cite jklontz
*/
class meltOutput : public MatrixOutput
{
Q_OBJECT
~meltOutput()
{
if (file.isNull() || targetFiles.isEmpty() || queryFiles.isEmpty()) return;
const bool genuineOnly = file.contains("Genuine") && !file.contains("Impostor");
const bool impostorOnly = file.contains("Impostor") && !file.contains("Genuine");
QHash<QString,QVariant> args = file.localMetadata();
args.remove("Genuine");
args.remove("Impostor");
QString keys; foreach (const QString &key, args.keys()) keys += "," + key;
QString values; foreach (const QVariant &value, args.values()) values += "," + value.toString();
QStringList lines;
if (file.baseName() != "terminal") lines.append(QString("Query,Target,Mask,Similarity%1").arg(keys));
QList<float> queryLabels = queryFiles.labels();
QList<float> targetLabels = targetFiles.labels();
for (int i=0; i<queryFiles.size(); i++) {
for (int j=(selfSimilar ? i+1 : 0); j<targetFiles.size(); j++) {
const bool genuine = queryLabels[i] == targetLabels[j];
if ((genuineOnly && !genuine) || (impostorOnly && genuine)) continue;
lines.append(QString("%1,%2,%3,%4%5").arg(queryFiles[i],
targetFiles[j],
QString::number(genuine),
QString::number(data.at<float>(i,j)),
values));
}
}
QtUtils::writeFile(file, lines);
}
};
BR_REGISTER(Output, meltOutput)
/*!
* \ingroup outputs
* \brief \ref simmat output.
* \author Josh Klontz \cite jklontz
*/
class mtxOutput : public MatrixOutput
{
Q_OBJECT
~mtxOutput()
{
if (file.isNull() || targetFiles.isEmpty() || queryFiles.isEmpty()) return;
BEE::writeSimmat(data, file.name);
}
};
BR_REGISTER(Output, mtxOutput)
/*!
* \ingroup outputs
* \brief Rank retrieval output.
* \author Josh Klontz \cite jklontz
*/
class rrOutput : public MatrixOutput
{
Q_OBJECT
~rrOutput()
{
if (file.isNull() || targetFiles.isEmpty() || queryFiles.isEmpty()) return;
const int limit = file.getInt("limit", 20);
const bool flat = file.getBool("flat");
const bool index = file.getBool("index");
const bool score = file.getBool("score");
const bool invert = file.getBool("invert");
QStringList lines;
for (int i=0; i<queryFiles.size(); i++) {
QStringList files;
if (!flat) files.append(queryFiles[i]);
typedef QPair<float,int> Pair;
foreach (const Pair &pair, Common::Sort(OpenCVUtils::matrixToVector(data.row(i)), !invert).mid(0, limit))
files.append((index ? QString::number(pair.second) : targetFiles[pair.second].name) +
(score ? "=" + QString::number(pair.first) : ""));
lines.append(files.join(flat ? "\n" : ","));
}
QtUtils::writeFile(file, lines);
}
};
BR_REGISTER(Output, rrOutput)
/*!
* \ingroup outputs
* \brief Text file output.
* \author Josh Klontz \cite jklontz
*/
class txtOutput : public MatrixOutput
{
Q_OBJECT
~txtOutput()
{
if (file.isNull() || targetFiles.isEmpty() || queryFiles.isEmpty()) return;
QStringList lines;
foreach (const File &file, queryFiles)
lines.append(file.name + " " + file.subject());
QtUtils::writeFile(file, lines);
}
};
BR_REGISTER(Output, txtOutput)
/*!
* \ingroup outputs
* \brief Output to the terminal.
* \author Josh Klontz \cite jklontz
*/
class EmptyOutput : public MatrixOutput
{
Q_OBJECT
static QString bufferString(const QString &string, int length)
{
if (string.size() >= length)
return string.left(length);
QString buffer; buffer.fill(' ', length-string.size());
return string+buffer;
}
~EmptyOutput()
{
if (targetFiles.isEmpty() || queryFiles.isEmpty()) return;
QString result;
if ((queryFiles.size() == 1) && (targetFiles.size() == 1)) {
result = toString(0,0) + "\n";
} else {
const int CELL_SIZE = 12;
result = bufferString(" ", CELL_SIZE) + " ";
foreach (const QString &targetName, targetFiles.names())
result += bufferString(targetName, CELL_SIZE) + " ";
result += "\n";
for (int i=0; i<queryFiles.size(); i++) {
result += bufferString(queryFiles[i].name, CELL_SIZE) + " ";
for (int j=0; j<targetFiles.size(); j++)
result += bufferString(toString(i,j), CELL_SIZE) + " ";
result += "\n";
}
}
printf("%s\n", qPrintable(result));
}
};
BR_REGISTER(Output, EmptyOutput)
/*!
* \ingroup outputs
* \brief The highest scoring matches.
* \author Josh Klontz \cite jklontz
*/
class tailOutput : public Output
{
Q_OBJECT
struct Comparison
{
br::File query, target;
float value;
Comparison(const br::File &_query, const br::File &_target, float _value)
: query(_query), target(_target), value(_value) {}
QString toString(bool args) const
{
return QString::number(value) + "," + (args ? target.flat() : (QString)target) + "," + (args ? query.flat() : (QString)query);
}
bool operator<(const Comparison &other) const
{
return value < other.value;
}
};
float threshold;
int atLeast, atMost;
bool args;
float lastValue;
QList<Comparison> comparisons;
QMutex comparisonsLock;
~tailOutput()
{
if (file.isNull() || comparisons.isEmpty()) return;
QStringList lines; lines.reserve(comparisons.size()+1);
lines.append("Value,Target,Query");
foreach (const Comparison &duplicate, comparisons)
lines.append(duplicate.toString(args));
QtUtils::writeFile(file, lines);
}
void initialize(const FileList &targetFiles, const FileList &queryFiles)
{
Output::initialize(targetFiles, queryFiles);
threshold = file.getFloat("threshold", -std::numeric_limits<float>::max());
atLeast = file.getInt("atLeast", 1);
atMost = file.getInt("atMost", std::numeric_limits<int>::max());
args = file.getBool("args");
lastValue = -std::numeric_limits<float>::max();
}
void set(float value, int i, int j)
{
// Return early for self similar matrices
if (selfSimilar && (i <= j)) return;
// Consider only values passing the criteria
if ((value < threshold) && (value <= lastValue) && (comparisons.size() >= atLeast))
return;
comparisonsLock.lock();
if (comparisons.isEmpty() || (value < comparisons.last().value)) {
// Special cases
comparisons.append(Comparison(queryFiles[i], targetFiles[j], value));
} else {
// General case
for (int k=0; k<comparisons.size(); k++) {
if (comparisons[k].value < value) {
comparisons.insert(k, Comparison(queryFiles[i], targetFiles[j], value));
break;
}
}
}
while (comparisons.size() > atMost)
comparisons.removeLast();
while ((comparisons.size() > atLeast) && (comparisons.last().value < threshold))
comparisons.removeLast();
lastValue = comparisons.last().value;
comparisonsLock.unlock();
}
};
BR_REGISTER(Output, tailOutput)
/*!
* \ingroup outputs
* \brief The highest scoring matches.
* \author Josh Klontz \cite jklontz
*/
class bestOutput : public Output
{
Q_OBJECT
typedef QPair< float, QPair<int, int> > BestMatch;
QList<BestMatch> bestMatches;
~bestOutput()
{
if (file.isNull() || bestMatches.isEmpty()) return;
qSort(bestMatches);
QStringList lines; lines.reserve(bestMatches.size()+1);
lines.append("Value,Target,Query");
for (int i=bestMatches.size()-1; i>=0; i--)
lines.append(QString::number(bestMatches[i].first) + "," + targetFiles[bestMatches[i].second.second] + "," + queryFiles[bestMatches[i].second.first]);
QtUtils::writeFile(file, lines);
}
void initialize(const FileList &targetFiles, const FileList &queryFiles)
{
Output::initialize(targetFiles, queryFiles);
bestMatches.reserve(queryFiles.size());
for (int i=0; i<queryFiles.size(); i++)
bestMatches.append(BestMatch(-std::numeric_limits<float>::max(), QPair<int,int>(-1, -1)));
}
void set(float value, int i, int j)
{
static QMutex lock;
// Return early for self similar matrices
if (selfSimilar && (i == j)) return;
if (value > bestMatches[i].first) {
lock.lock();
if (value > bestMatches[i].first)
bestMatches[i] = BestMatch(value, QPair<int,int>(i,j));
lock.unlock();
}
}
};
BR_REGISTER(Output, bestOutput)
/*!
* \ingroup outputs
* \brief Score histogram.
* \author Josh Klontz \cite jklontz
*/
class histOutput : public Output
{
Q_OBJECT
float min, max, step;
QVector<int> bins;
~histOutput()
{
if (file.isNull() || bins.isEmpty()) return;
QStringList counts;
foreach (int count, bins)
counts.append(QString::number(count));
const QString result = counts.join(",");
QtUtils::writeFile(file, result);
}
void initialize(const FileList &targetFiles, const FileList &queryFiles)
{
Output::initialize(targetFiles, queryFiles);
min = file.getFloat("min", -5);
max = file.getFloat("max", 5);
step = file.getFloat("step", 0.1);
bins = QVector<int>((max-min)/step, 0);
}
void set(float value, int i, int j)
{
(void) i;
(void) j;
if ((value < min) || (value >= max)) return;
bins[(value-min)/step]++; // This should technically be locked to ensure atomic increment
}
};
BR_REGISTER(Output, histOutput)
#include "output.moc"