forked from falvaro/seshat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparel.cc
178 lines (134 loc) · 4.78 KB
/
sparel.cc
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
/*Copyright 2014 Francisco Alvaro
This file is part of SESHAT.
SESHAT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SESHAT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SESHAT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cfloat>
#include "sparel.h"
using namespace std;
//Aux functions
Hypothesis *leftmost(Hypothesis *h) {
if (h->pt) {
return h;
}
Hypothesis *izq = leftmost(h->hi);
Hypothesis *der = leftmost(h->hd);
return izq->parent->x < der->parent->x ? izq : der;
}
Hypothesis *rightmost(Hypothesis *h) {
if (h->pt) {
return h;
}
Hypothesis *izq = rightmost(h->hi);
Hypothesis *der = rightmost(h->hd);
return izq->parent->s > der->parent->s ? izq : der;
}
//Percentage of the area of region A that overlaps with region B
float solape(CellCYK *a, CellCYK *b) {
int x = max(a->x, b->x);
int y = max(a->y, b->y);
int s = min(a->s, b->s);
int t = min(a->t, b->t);
if (s >= x && t >= y) {
float aSolap = (s - x + 1.0)*(t - y + 1.0);
float aTotal = (a->s - a->x + 1.0)*(a->t - a->y + 1.0);
return aSolap / aTotal;
}
return 0.0;
}
//
//SpaRel methods
//
SpaRel::SpaRel(GMM *gmm, Sample *m) {
model = gmm;
mue = m;
}
void SpaRel::smooth(float *post) {
for (int i = 0; i < NRELS; i++) {
post[i] = (post[i] + 0.02) / (1.00 + NRELS * 0.02);
}
}
void SpaRel::getFeas(Hypothesis *a, Hypothesis *b, float *sample, int ry) {
//Normalization factor: combined height
float F = max(a->parent->t, b->parent->t) - min(a->parent->y, b->parent->y) + 1;
sample[0] = (b->parent->t - b->parent->y + 1) / F;
sample[1] = (a->rcen - b->lcen) / F;
sample[2] = ((a->parent->s + a->parent->x) / 2.0 - (b->parent->s + b->parent->x) / 2.0) / F;
sample[3] = (b->parent->x - a->parent->s) / F;
sample[4] = (b->parent->x - a->parent->x) / F;
sample[5] = (b->parent->s - a->parent->s) / F;
sample[6] = (b->parent->y - a->parent->t) / F;
sample[7] = (b->parent->y - a->parent->y) / F;
sample[8] = (b->parent->t - a->parent->t) / F;
}
double SpaRel::compute_prob(Hypothesis *h1, Hypothesis *h2, int k) {
//Set probabilities according to spatial constraints
if (k <= 2) {
//Check left-to-right order constraint in Hor/Sub/Sup relationships
Hypothesis *rma = rightmost(h1);
Hypothesis *lmb = leftmost(h2);
if (lmb->parent->x < rma->parent->x || lmb->parent->s <= rma->parent->s) {
return 0.0;
}
}
//Compute probabilities
float sample[NFEAT];
getFeas(h1, h2, sample, mue->RY);
//Get spatial relationships probability from the model
model->posterior(sample, probs);
//Slightly smooth probabilities because GMM classifier can provide
//to biased probabilities. Thsi way we give some room to the
//language model (the 2D-SCFG grammar)
smooth(probs);
return probs[k];
}
SpaRel::~SpaRel() {
}
double SpaRel::getHorProb(Hypothesis *ha, Hypothesis *hb) {
return compute_prob(ha, hb, 0);
}
double SpaRel::getSubProb(Hypothesis *ha, Hypothesis *hb) {
return compute_prob(ha, hb, 1);
}
double SpaRel::getSupProb(Hypothesis *ha, Hypothesis *hb) {
return compute_prob(ha, hb, 2);
}
double SpaRel::getVerProb(Hypothesis *ha, Hypothesis *hb, bool strict) {
//Pruning
if (hb->parent->y < (ha->parent->y + ha->parent->t) / 2
|| abs((ha->parent->x + ha->parent->s) / 2 - (hb->parent->x + hb->parent->s) / 2) > 2.5 * mue->RX
|| (hb->parent->x > ha->parent->s || hb->parent->s < ha->parent->x))
return 0.0;
if (!strict) {
return compute_prob(ha, hb, 3);
}
//Penalty for strict relationships
float penalty = abs(ha->parent->x - hb->parent->x) / (3.0 * mue->RX)
+ abs(ha->parent->s - hb->parent->s) / (3.0 * mue->RX);
if (penalty > 0.95) {
penalty = 0.95;
}
return (1.0 - penalty) * compute_prob(ha, hb, 3);
}
double SpaRel::getInsProb(Hypothesis *ha, Hypothesis *hb) {
if (solape(hb->parent, ha->parent) < 0.5 ||
hb->parent->x < ha->parent->x || hb->parent->y < ha->parent->y) {
return 0.0;
}
return compute_prob(ha, hb, 4);
}
double SpaRel::getMrtProb(Hypothesis *ha, Hypothesis *hb) {
return compute_prob(ha, hb, 5);
}