forked from aclapes/segmenthreetion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FusionPrediction.h
259 lines (174 loc) · 7.05 KB
/
FusionPrediction.h
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
//
// FusionPrediction.h
// segmenthreetion
//
// Created by Albert Clapés on 01/04/14.
//
//
#ifndef __segmenthreetion__FusionPrediction__
#define __segmenthreetion__FusionPrediction__
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include "CvExtraTools.h"
#include "ModalityGridData.hpp"
#include "GridMat.h"
#include "em.h"
#include <boost/thread.hpp>
using namespace std;
/*
* Simple fusion prediction
*/
class SimpleFusionPrediction
{
public:
SimpleFusionPrediction();
void setModalitiesData(vector<ModalityGridData> mgds);
// Cells' preconsensus
// want back some kind of consensued dists to margin in the fusion prediction?
void predict(vector<cv::Mat> allPredictions, vector<cv::Mat> allDistsToMargin,
cv::Mat& fusionPredictions, cv::Mat& fusionDistsToMargin);
void predict(vector<GridMat> allPredictions, vector<GridMat> allDistsToMargin, cv::Mat& fusionPredictions, cv::Mat& fusionDistsToMargin);
void predict(vector<GridMat> allDistsToMargin, cv::Mat& fusionPredictions, cv::Mat& fusionDistsToMargin);
cv::Mat getAccuracies();
private:
void predict(GridMat distsToMarginGrid, GridMat& fusionPredictions, GridMat& fusionDistsToMargin);
void computeGridConsensusPredictions(GridMat fusionPredictionsGrid,
GridMat fusionDistsToMarginGrid,
cv::Mat& consensusfusionPredictions,
cv::Mat& consensusfusionDistsToMargin);
// Attributes
vector<ModalityGridData> m_mgds;
cv::Mat m_tags;
cv::Mat m_partitions;
cv::Mat m_fusionPredictions;
cv::Mat m_fusionDistsToMargin;
};
/*
* Classifier-based prediction
*/
// Template definition NEEDED, but NOT USED. Use the specializations nextly defined
template<typename PredictorT, typename ClassifierT>
class ClassifierFusionPredictionBase
{
};
// <cv::EM40,ClassifierT> template parital-instantation
template<typename ClassifierT>
class ClassifierFusionPredictionBase<cv::EM40, ClassifierT>
{
public:
ClassifierFusionPredictionBase();
void setData(vector<ModalityGridData> mgds, vector<GridMat> distsToMargin, vector<cv::Mat> predictions);
void setModelSelection(bool flag);
void setModelSelectionParameters(int k, int seed, bool bGlobalBest);
void setValidationParameters(int k);
void setPartitions(cv::Mat partitions);
void setTrainMirrored(bool flag);
void setStackedPrediction(bool flag);
cv::Mat getAccuracies();
protected:
void formatData();
// Attributes
vector<ModalityGridData> m_mgds;
vector<cv::Mat> m_predictions;
vector<GridMat> m_distsToMargin;
cv::Mat m_data; // input data
cv::Mat m_responses; // output labels
ClassifierT* m_pClassifier;
cv::Mat m_fusionPredictions;
bool m_bModelSelection;
int m_testK;
int m_modelSelecK;
bool m_bGlobalBest;
bool m_bTrainMirrored;
int m_seed;
cv::Mat m_partitions;
bool m_bStackPredictions;
int m_narrowSearchSteps;
boost::mutex m_mutex;
};
// SVM template
// Template definition NEEDED, but NOT USED. Use the specializations nextly defined
template<typename PredictorT, typename ClassifierT>
class ClassifierFusionPrediction : public ClassifierFusionPredictionBase<PredictorT, ClassifierT>
{};
// <cv::EM40,CvSVM> template instantiation
template<>
class ClassifierFusionPrediction<cv::EM40,CvSVM> : public ClassifierFusionPredictionBase<cv::EM40,CvSVM>
{
public:
ClassifierFusionPrediction();
void setKernelType(int type);
void setCs(vector<float> cs);
void setGammas(vector<float> gammas);
void modelSelection(cv::Mat data, cv::Mat responses, cv::Mat params, cv::Mat& goodnesses);
void predict(cv::Mat& fusionPredictions);
private:
void _modelSelection(cv::Mat& descriptorsTr, cv::Mat& responsesTr, cv::Mat& descriptorsVal, cv::Mat& responsesVal, int k, cv::Mat& expandedParams, cv::Mat& accuracies);
// Attributes
int m_kernelType; // CvSVM::LINEAR or CvSVM::RBF
vector<float> m_cs;
vector<float> m_gammas;
int m_numItersSVM;
};
// <cv::EM40,CvBoost> template instantiation
template<>
class ClassifierFusionPrediction<cv::EM40,CvBoost> : public ClassifierFusionPredictionBase<cv::EM40,CvBoost>
{
public:
ClassifierFusionPrediction();
void setBoostType(int type);
void setNumOfWeaks(vector<float> numOfWeaks);
void setWeightTrimRate(vector<float> weightTrimRates);
void modelSelection(cv::Mat data, cv::Mat responses, cv::Mat params, cv::Mat& goodnesses);
void predict(cv::Mat& fusionPredictions);
private:
void _modelSelection(cv::Mat& descriptorsTr, cv::Mat& responsesTr, cv::Mat& descriptorsVal, cv::Mat& responsesVal, int k, cv::Mat& expandedParams, cv::Mat& accuracies);
// Attributes
int m_boostType;
vector<float> m_numOfWeaks;
vector<float> m_weightTrimRate;
};
// <cv::EM40,CvANN_MLP> template instantiation
template<>
class ClassifierFusionPrediction<cv::EM40,CvANN_MLP> : public ClassifierFusionPredictionBase<cv::EM40,CvANN_MLP>
{
public:
ClassifierFusionPrediction();
cv::Mat encode(cv::Mat vector);
cv::Mat decode(cv::Mat matrix);
void setActivationFunctionType(int type);
void setHiddenLayerSizes(vector<float> hiddenSizes);
//void setBackpropDecayWeightScales(vector<float> dwScales);
//void setBackpropMomentScales(vector<float> momScales);
void modelSelection(cv::Mat data, cv::Mat responses, cv::Mat params, cv::Mat& goodnesses);
void predict(cv::Mat& fusionPredictions);
private:
void _modelSelection(cv::Mat& descriptorsTr, cv::Mat& responsesTr, cv::Mat& descriptorsVal, cv::Mat& responsesVal, int k, cv::Mat& expandedParams, cv::Mat& accuracies);
// Attributes
int m_actFcnType; // activation function type
int m_numOfEpochs;
int m_numOfRepetitions; // deal with stochasticity introduced by the random weights initialization
vector<float> m_hiddenLayerSizes;
//vector<float> m_bpDwScales; // decay in weight (dw)
//vector<float> m_bpMomentScales;
};
// <cv::EM40,CvSVM> template instantiation
template<>
class ClassifierFusionPrediction<cv::EM40,CvRTrees> : public ClassifierFusionPredictionBase<cv::EM40,CvRTrees>
{
public:
ClassifierFusionPrediction();
void setMaxDepths(vector<float> depths);
void setMaxNoTrees(vector<float> n);
void setNoVars(vector<float> n);
void modelSelection(cv::Mat data, cv::Mat responses, cv::Mat params, cv::Mat& goodnesses);
void predict(cv::Mat& fusionPredictions);
private:
void _modelSelection(cv::Mat& descriptorsTr, cv::Mat& responsesTr, cv::Mat& descriptorsVal, cv::Mat& responsesVal, int k, cv::Mat& expandedParams, cv::Mat& accuracies);
// Attributes
vector<float> m_MaxDepths;
vector<float> m_MaxNoTrees;
vector<float> m_NoVars;
};
#endif /* defined(__segmenthreetion__FusionPrediction__) */