-
Notifications
You must be signed in to change notification settings - Fork 82
/
stopping_rule.cpp
151 lines (136 loc) · 7.29 KB
/
stopping_rule.cpp
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
#include "stopping_rule.h"
StoppingRule::StoppingRule()
{
}
void StoppingRule::operator()(vector<HCluster> &dendrogram, vector<int> &maxIdxs, vector<string> &lex, int min_word_lenght,
float weak_classifier_threshold, float cnn_classifier_threshold, bool nms)
{
//TODO the nms here is suboptimal... (only removes a few number of proposals and we do not gain much time by doing it here instead of in the global bbox nms) but its beautiful in the sense that exploits hierarchical organization
for (size_t i=0; i<dendrogram.size(); i++)
{
//cout << i << "(" << dendrogram[i].node1 << "," << dendrogram[i].node2 << ")"
// << dendrogram[i].cnn_recognition << " " << dendrogram[i].cnn_probability << endl;
if ((dendrogram[i].node1<0)&&(dendrogram[i].node2<0)) // both nodes are individual regions, so this is the max in the branch
{
dendrogram[i].branch_max_probability = dendrogram[i].cnn_probability;
if ( (dendrogram[i].probability >= weak_classifier_threshold)
&& (dendrogram[i].cnn_probability >= cnn_classifier_threshold)
&& (dendrogram[i].cnn_recognition.size() >= min_word_lenght)
&& ((lex.empty()) || (find (lex.begin(), lex.end(), dendrogram[i].cnn_recognition) != lex.end())) )
{
dendrogram[i].branch_maxIdx = i;
//cout << " MAX!" << endl;
} else {
dendrogram[i].branch_maxIdx = -1;
}
continue;
}
int node1 = dendrogram[i].node1;
int node2 = dendrogram[i].node2;
if ((dendrogram[i].inherit_cnn_probability == 1) && (node1>0))
{
dendrogram[i].cnn_probability = dendrogram[node1].cnn_probability;
dendrogram[i].cnn_recognition = dendrogram[node1].cnn_recognition;
dendrogram[i].rect = dendrogram[node1].rect;
}
else if ((dendrogram[i].inherit_cnn_probability == 2) && (node2>0))
{
dendrogram[i].cnn_probability = dendrogram[node2].cnn_probability;
dendrogram[i].cnn_recognition = dendrogram[node2].cnn_recognition;
dendrogram[i].rect = dendrogram[node2].rect;
}
if ((dendrogram[i].node1>=0)&&(dendrogram[i].node2<0)) // only look at child 1
{
if ( (dendrogram[i].cnn_probability*dendrogram[i].nfa >= dendrogram[node1].branch_max_probability)
&& (dendrogram[i].probability >= weak_classifier_threshold)
&& (dendrogram[i].cnn_probability >= cnn_classifier_threshold)
&& (dendrogram[i].cnn_recognition.size() >= min_word_lenght)
&& ((lex.empty()) || (find (lex.begin(), lex.end(), dendrogram[i].cnn_recognition) != lex.end())) )
{
for (size_t j=0; j<dendrogram[i].childs.size(); j++)
{
dendrogram[dendrogram[i].childs[j]].branch_max_probability = dendrogram[i].cnn_probability * dendrogram[i].nfa;
dendrogram[dendrogram[i].childs[j]].branch_maxIdx = i;
}
//cout << " MAX!" << endl;
//cout << " " << dendrogram[i].cnn_probability << " > "
// << dendrogram[node1].branch_max_probability << endl;
//cout << " all childs set to no max " << Mat(dendrogram[i].childs).t() << endl;
} else {
dendrogram[i].branch_max_probability = dendrogram[node1].branch_max_probability;
dendrogram[i].branch_maxIdx = dendrogram[node1].branch_maxIdx;
//cout << " no" << endl;
}
continue;
}
if ((dendrogram[i].node1<0)&&(dendrogram[i].node2>=0)) // only look at child 2
{
if ( (dendrogram[i].cnn_probability*dendrogram[i].nfa >= dendrogram[node2].branch_max_probability)
&& (dendrogram[i].probability >= weak_classifier_threshold)
&& (dendrogram[i].cnn_probability >= cnn_classifier_threshold)
&& (dendrogram[i].cnn_recognition.size() >= min_word_lenght)
&& ((lex.empty()) || (find (lex.begin(), lex.end(), dendrogram[i].cnn_recognition) != lex.end())) )
{
for (size_t j=0; j<dendrogram[i].childs.size(); j++)
{
dendrogram[dendrogram[i].childs[j]].branch_max_probability = dendrogram[i].cnn_probability*dendrogram[i].nfa;
dendrogram[dendrogram[i].childs[j]].branch_maxIdx = i;
}
//cout << " MAX!" << endl;
//cout << " " << dendrogram[i].cnn_probability << " > "
// << dendrogram[node2].branch_max_probability << endl;
//cout << " all childs set to no max " << Mat(dendrogram[i].childs).t() << endl;
} else {
dendrogram[i].branch_max_probability = dendrogram[node2].branch_max_probability;
dendrogram[i].branch_maxIdx = dendrogram[node2].branch_maxIdx;
//cout << " no" << endl;
}
continue;
}
// if here we must take a look at both childs
if ( (dendrogram[i].cnn_probability*dendrogram[i].nfa >= dendrogram[node1].branch_max_probability)
&& (dendrogram[i].cnn_probability*dendrogram[i].nfa >= dendrogram[node2].branch_max_probability)
&& (dendrogram[i].probability >= weak_classifier_threshold)
&& (dendrogram[i].cnn_probability >= cnn_classifier_threshold)
&& (dendrogram[i].cnn_recognition.size() >= min_word_lenght)
&& ((lex.empty()) || (find (lex.begin(), lex.end(), dendrogram[i].cnn_recognition) != lex.end())) )
{
for (size_t j=0; j<dendrogram[i].childs.size(); j++)
{
if (dendrogram[i].childs[j]<0) continue;
dendrogram[dendrogram[i].childs[j]].branch_max_probability = dendrogram[i].cnn_probability*dendrogram[i].nfa;
dendrogram[dendrogram[i].childs[j]].branch_maxIdx = i;
}
//cout << " MAX!" << endl;
//cout << " " << dendrogram[i].cnn_probability << " > "
// << dendrogram[node1].branch_max_probability << " > "
// << dendrogram[node2].branch_max_probability << endl;
//cout << " all childs set to no max " << Mat(dendrogram[i].childs).t() << endl;
} else if (dendrogram[node1].branch_max_probability > dendrogram[node2].branch_max_probability){
dendrogram[i].branch_max_probability = dendrogram[node1].branch_max_probability;
dendrogram[i].branch_maxIdx = dendrogram[node1].branch_maxIdx;
//cout << " no , node1 is better " << dendrogram[node1].branch_max_probability << endl;
if ((nms) && (dendrogram[node1].branch_maxIdx > 0) && (dendrogram[node2].branch_maxIdx >0))
{
if ((dendrogram[dendrogram[node1].branch_maxIdx].rect & dendrogram[dendrogram[node2].branch_maxIdx].rect)
== dendrogram[dendrogram[node2].branch_maxIdx].rect)
dendrogram[node2].branch_maxIdx = dendrogram[node1].branch_maxIdx;
}
} else {
dendrogram[i].branch_max_probability = dendrogram[node2].branch_max_probability;
dendrogram[i].branch_maxIdx = dendrogram[node2].branch_maxIdx;
//cout << " no , node2 is better " << dendrogram[node2].branch_max_probability << endl;
if ((nms) && (dendrogram[node1].branch_maxIdx > 0) && (dendrogram[node2].branch_maxIdx >0))
{
if ((dendrogram[dendrogram[node2].branch_maxIdx].rect & dendrogram[dendrogram[node1].branch_maxIdx].rect)
== dendrogram[dendrogram[node1].branch_maxIdx].rect)
dendrogram[node1].branch_maxIdx = dendrogram[node2].branch_maxIdx;
}
}
}
for (size_t i=0; i<dendrogram.size(); i++)
{
if (dendrogram[i].branch_maxIdx == i)
maxIdxs.push_back(i);
}
}