Commit fdee5406b9f1f58ba345f8489fa543cc4ed6da79

Authored by Josh Klontz
1 parent ec60aa41

removed downsample training

openbr/plugins/core/downsampletraining.cpp deleted
1 -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  
2 - * Copyright 2012 The MITRE Corporation *  
3 - * *  
4 - * Licensed under the Apache License, Version 2.0 (the "License"); *  
5 - * you may not use this file except in compliance with the License. *  
6 - * You may obtain a copy of the License at *  
7 - * *  
8 - * http://www.apache.org/licenses/LICENSE-2.0 *  
9 - * *  
10 - * Unless required by applicable law or agreed to in writing, software *  
11 - * distributed under the License is distributed on an "AS IS" BASIS, *  
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *  
13 - * See the License for the specific language governing permissions and *  
14 - * limitations under the License. *  
15 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */  
16 -  
17 -#include <openbr/plugins/openbr_internal.h>  
18 -  
19 -namespace br  
20 -{  
21 -  
22 -static TemplateList Downsample(const TemplateList &templates, int classes, int instances, float fraction, const QString &inputVariable, const QStringList &gallery, const QStringList &subjects)  
23 -{  
24 - // Return early when no downsampling is required  
25 - if ((classes == std::numeric_limits<int>::max()) &&  
26 - (instances == std::numeric_limits<int>::max()) &&  
27 - (fraction >= 1) &&  
28 - (gallery.isEmpty()) &&  
29 - (subjects.isEmpty()))  
30 - return templates;  
31 -  
32 - const bool atLeast = instances < 0;  
33 - instances = abs(instances);  
34 -  
35 - QList<QString> allLabels = File::get<QString>(templates, inputVariable);  
36 -  
37 - QList<QString> uniqueLabels = allLabels.toSet().toList();  
38 - qSort(uniqueLabels);  
39 -  
40 - QMap<QString,int> counts = templates.countValues<QString>(inputVariable, instances != std::numeric_limits<int>::max());  
41 -  
42 - if ((instances != std::numeric_limits<int>::max()) && (classes != std::numeric_limits<int>::max()))  
43 - foreach (const QString &label, counts.keys())  
44 - if (counts[label] < instances)  
45 - counts.remove(label);  
46 -  
47 - uniqueLabels = counts.keys();  
48 - if ((classes != std::numeric_limits<int>::max()) && (uniqueLabels.size() < classes))  
49 - qWarning("Downsample requested %d classes but only %d are available.", classes, uniqueLabels.size());  
50 -  
51 - QList<QString> selectedLabels = uniqueLabels;  
52 - if (classes < uniqueLabels.size()) {  
53 - std::random_shuffle(selectedLabels.begin(), selectedLabels.end());  
54 - selectedLabels = selectedLabels.mid(0, classes);  
55 - }  
56 -  
57 - TemplateList downsample;  
58 - for (int i=0; i<selectedLabels.size(); i++) {  
59 - const QString selectedLabel = selectedLabels[i];  
60 - QList<int> indices;  
61 - for (int j=0; j<allLabels.size(); j++)  
62 - if ((allLabels[j] == selectedLabel) && (!templates.value(j).file.get<bool>("FTE", false)) && (!templates.value(j).file.get<bool>("PossibleFTE", false)))  
63 - indices.append(j);  
64 -  
65 - std::random_shuffle(indices.begin(), indices.end());  
66 - const int max = atLeast ? indices.size() : std::min(indices.size(), instances);  
67 - for (int j=0; j<max; j++)  
68 - downsample.append(templates.value(indices[j]));  
69 - }  
70 -  
71 - if (fraction < 1) {  
72 - std::random_shuffle(downsample.begin(), downsample.end());  
73 - downsample = downsample.mid(0, downsample.size()*fraction);  
74 - }  
75 -  
76 - if (!gallery.isEmpty())  
77 - for (int i=downsample.size()-1; i>=0; i--)  
78 - if (!gallery.contains(downsample[i].file.get<QString>("Gallery")))  
79 - downsample.removeAt(i);  
80 -  
81 - if (!subjects.isEmpty())  
82 - for (int i=downsample.size()-1; i>=0; i--)  
83 - if (subjects.contains(downsample[i].file.get<QString>(inputVariable)))  
84 - downsample.removeAt(i);  
85 -  
86 - return downsample;  
87 -}  
88 -  
89 -/*!  
90 - * \ingroup transforms  
91 - * \brief DOCUMENT ME JOSH  
92 - * \author Josh Klontz \cite jklontz  
93 - */  
94 -class DownsampleTrainingTransform : public Transform  
95 -{  
96 - Q_OBJECT  
97 - Q_PROPERTY(br::Transform* transform READ get_transform WRITE set_transform RESET reset_transform STORED true)  
98 - Q_PROPERTY(int classes READ get_classes WRITE set_classes RESET reset_classes STORED false)  
99 - Q_PROPERTY(int instances READ get_instances WRITE set_instances RESET reset_instances STORED false)  
100 - Q_PROPERTY(float fraction READ get_fraction WRITE set_fraction RESET reset_fraction STORED false)  
101 - Q_PROPERTY(QString inputVariable READ get_inputVariable WRITE set_inputVariable RESET reset_inputVariable STORED false)  
102 - Q_PROPERTY(QStringList gallery READ get_gallery WRITE set_gallery RESET reset_gallery STORED false)  
103 - Q_PROPERTY(QStringList subjects READ get_subjects WRITE set_subjects RESET reset_subjects STORED false)  
104 - BR_PROPERTY(br::Transform*, transform, NULL)  
105 - BR_PROPERTY(int, classes, std::numeric_limits<int>::max())  
106 - BR_PROPERTY(int, instances, std::numeric_limits<int>::max())  
107 - BR_PROPERTY(float, fraction, 1)  
108 - BR_PROPERTY(QString, inputVariable, "Label")  
109 - BR_PROPERTY(QStringList, gallery, QStringList())  
110 - BR_PROPERTY(QStringList, subjects, QStringList())  
111 -  
112 -  
113 - Transform *simplify(bool &newTForm)  
114 - {  
115 - Transform *res = transform->simplify(newTForm);  
116 - return res;  
117 - }  
118 -  
119 - void project(const Template &src, Template &dst) const  
120 - {  
121 - transform->project(src,dst);  
122 - }  
123 -  
124 -  
125 - void train(const TemplateList &data)  
126 - {  
127 - if (!transform || !transform->trainable)  
128 - return;  
129 -  
130 - TemplateList downsampled = Downsample(data, classes, instances, fraction, inputVariable, gallery, subjects);  
131 -  
132 - transform->train(downsampled);  
133 - }  
134 -};  
135 -  
136 -BR_REGISTER(Transform, DownsampleTrainingTransform)  
137 -  
138 -} // namespace br  
139 -  
140 -#include "core/downsampletraining.moc"