validate.cpp
5.67 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
#include <QFutureSynchronizer>
#include <QtConcurrentRun>
#include "openbr_internal.h"
#include <openbr/core/qtutils.h>
namespace br
{
/*!
* \ingroup transforms
* \brief Cross validate a trainable transform.
* \author Josh Klontz \cite jklontz
* \note To use an extended gallery, add an allPartitions="true" flag to the gallery sigset for those images that should be compared
* against for all testing partitions.
*/
class CrossValidateTransform : public MetaTransform
{
Q_OBJECT
Q_PROPERTY(QString description READ get_description WRITE set_description RESET reset_description STORED false)
BR_PROPERTY(QString, description, "Identity")
QList<br::Transform*> transforms;
void train(const TemplateList &data)
{
int numPartitions = 0;
QList<int> partitions; partitions.reserve(data.size());
foreach (const File &file, data.files()) {
partitions.append(file.get<int>("Partition", 0));
numPartitions = std::max(numPartitions, partitions.last()+1);
}
while (transforms.size() < numPartitions)
transforms.append(make(description));
if (numPartitions < 2) {
transforms.first()->train(data);
return;
}
QFutureSynchronizer<void> futures;
for (int i=0; i<numPartitions; i++) {
TemplateList partitionedData = data;
for (int j=partitionedData.size()-1; j>=0; j--)
// Remove all templates from partition i
if (partitions[j] == i)
partitionedData.removeAt(j);
// Train on the remaining templates
futures.addFuture(QtConcurrent::run(transforms[i], &Transform::train, partitionedData));
}
futures.waitForFinished();
}
void project(const Template &src, Template &dst) const
{
// If the src partition is greater than the number of training partitions,
// assume that projection should be done using the same training data for all partitions.
int partition = src.file.get<int>("Partition", 0);
if (partition >= transforms.size()) partition = 0;
transforms[partition]->project(src, dst);
}
void store(QDataStream &stream) const
{
stream << transforms.size();
foreach (Transform *transform, transforms)
transform->store(stream);
}
void load(QDataStream &stream)
{
int numTransforms;
stream >> numTransforms;
while (transforms.size() < numTransforms)
transforms.append(make(description));
foreach (Transform *transform, transforms)
transform->load(stream);
}
};
BR_REGISTER(Transform, CrossValidateTransform)
/*!
* \ingroup distances
* \brief Cross validate a distance metric.
* \author Josh Klontz \cite jklontz
*/
class CrossValidateDistance : public Distance
{
Q_OBJECT
float compare(const Template &a, const Template &b) const
{
static const QString key("Partition"); // More efficient to preallocate this
const int partitionA = a.file.get<int>(key, 0);
const int partitionB = b.file.get<int>(key, 0);
return (partitionA != partitionB) ? -std::numeric_limits<float>::max() : 0;
}
};
BR_REGISTER(Distance, CrossValidateDistance)
/*!
* \ingroup distances
* \brief Checks target metadata against filters.
* \author Josh Klontz \cite jklontz
*/
class FilterDistance : public Distance
{
Q_OBJECT
float compare(const Template &a, const Template &b) const
{
(void) b; // Query template isn't checked
foreach (const QString &key, Globals->filters.keys()) {
bool keep = false;
const QString metadata = a.file.get<QString>(key, "");
if (Globals->filters[key].isEmpty()) continue;
if (metadata.isEmpty()) return -std::numeric_limits<float>::max();
foreach (const QString &value, Globals->filters[key]) {
if (metadata == value) {
keep = true;
break;
}
}
if (!keep) return -std::numeric_limits<float>::max();
}
return 0;
}
};
BR_REGISTER(Distance, FilterDistance)
/*!
* \ingroup distances
* \brief Checks target metadata against query metadata.
* \author Scott Klum \cite sklum
*/
class MetadataDistance : public Distance
{
Q_OBJECT
Q_PROPERTY(QStringList filters READ get_filters WRITE set_filters RESET reset_filters STORED false)
BR_PROPERTY(QStringList, filters, QStringList())
float compare(const Template &a, const Template &b) const
{
foreach (const QString &key, filters) {
QString aValue = a.file.get<QString>(key, QString());
QString bValue = b.file.get<QString>(key, QString());
// The query value may be a range. Let's check.
if (bValue.isEmpty()) bValue = QtUtils::toString(b.file.get<QPointF>(key, QPointF()));
if (aValue.isEmpty() || bValue.isEmpty()) continue;
bool keep = false;
bool ok;
QPointF range = QtUtils::toPoint(bValue,&ok);
if (ok) /* Range */ {
int value = range.x();
int upperBound = range.y();
while (value <= upperBound) {
if (aValue == QString::number(value)) {
keep = true;
break;
}
value++;
}
}
else if (aValue == bValue) keep = true;
if (!keep) return -std::numeric_limits<float>::max();
}
return 0;
}
};
BR_REGISTER(Distance, MetadataDistance)
} // namespace br
#include "validate.moc"