/
Train_KFan.cpp
236 lines (182 loc) · 5.56 KB
/
Train_KFan.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
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
//
// k-Fan matching and training code, as described in the paper:
//
// Crandall, Felzenszwalb, Huttenlocher, "Spatial
// Priors for Part-based Recognition using Statistical Models," CVPR 2005.
//
//
// Author: David Crandall, 2003-2005
// crandall@cs.cornell.edu
//
// Please do not redistribute this code.
//
//
//
//
#include <Train_KFan.h>
using namespace std;
template<class T>
T Train_KFan<T>::glikelihood(const _DMatrix<T> &sigma, const _DMatrix<T> &mu,
const _DMatrix<T> &data) const
{
assert(data.cols() == 2);
T det_sigma = sigma[0][0] * sigma[1][1] - sigma[0][1] * sigma[1][0];
T log_c = T( -(1/2.0) * data.cols() * data.rows() * log(2*M_PI) -
(1/2.0) * data.rows() * log((double)det_sigma));
_DMatrix<T> sigma_inv(2,2);
sigma_inv[0][0] = -sigma[1][1];
sigma_inv[1][1] = -sigma[0][0];
sigma_inv[0][1] = sigma[0][1];
sigma_inv[1][0] = sigma[1][0];
sigma_inv = sigma_inv / (-det_sigma);
T e=0;
for(int i=0; i<data.rows(); i++)
{
_DMatrix<T> _data = data.extract_row(i);
e += ((_data - mu) * sigma_inv * (_data - mu).transpose() / 2.0)[0][0];
}
return( log_c - e);
}
template<class T>
KFan<T> Train_KFan<T>::train(const _DMatrix<T> &part_locations, int dist_part_count)
{
switch(dist_part_count)
{
case 1:
{
KFan<T> best_kfan;
_DMatrix<int> best_dist_parts;
T best_l=-1e100;
_DMatrix<int> dist_parts(1,1);
for(int i=0; i<dist_part_count; i++)
{
dist_parts[0][0] = i;
KFan<T> kfan = train(part_locations, dist_parts);
if(kfan.model_likelihood > best_l)
{
best_l = kfan.model_likelihood;
best_dist_parts = dist_parts;
best_kfan = kfan;
}
}
return best_kfan;
}
case 0:
{
KFan<T> result;
result.parts_count = part_locations.cols()/2;
result.K = 0;
return result;
}
default:
throw std::string("Train_KFan doesn't currently support that value of K");
}
return KFan<T>();
}
template<class T>
KFan<T> Train_KFan<T>::train(const _DMatrix<T> &part_locations,
const _DMatrix<int> &dist_parts)
{
int part_count = part_locations.cols() / 2;
int img_count = part_locations.rows();
int dist_part_count = dist_parts.cols();
// first learn a gaussian for the distinguished parts
_DMatrix<T> d_D(img_count, dist_part_count * 2);
_DMatrix<T> d_D_diff(img_count, (dist_part_count-1)*2);
for(int j=0; j<img_count; j++)
{
T *d_D_row = d_D[j];
T *part_loc_row = part_locations[j];
for(int i=0; i<dist_part_count; i++)
{
d_D_row[i*2] = part_loc_row[dist_parts[0][i]*2];
d_D_row[i*2+1] = part_loc_row[dist_parts[0][i]*2+1];
}
if(dist_part_count > 1)
{
T *d_D_diff_row = d_D_diff[j];
for(int i=1; i<dist_part_count; i++)
{
d_D_diff_row[i*2] = d_D_row[i*2] - d_D_row[0];
d_D_diff_row[i*2+1] = d_D_row[i*2+1] - d_D_row[1];
}
}
}
T l2=0;
_DMatrix<T> sigma_123, mu_123;
if(dist_part_count > 1)
{
sigma_123 = d_D_diff.covariance();
mu_123 = d_D_diff.means();
l2 = glikelihood(sigma_123, mu_123, d_D_diff);
}
T l = l2;
_DMultiDMatrix<T> sigma(3, part_count, 2, 2);
_DMultiDMatrix<T> mu(3, part_count, 2, 1);
_DMultiDMatrix<T> mu_x2(3, part_count, 2, (dist_part_count-1)*2);
sigma=0;
mu=0;
mu_x2=0;
// now learn gaussians for (p1, p2, ... , i) tuples for all other
// parts i
for(int i=0; i<part_count; i++)
{
bool d_part = false;
for(int j=0; j<dist_part_count; j++)
if(i == dist_parts[0][j])
d_part = true;
if(d_part) continue;
_DMatrix<T> this_D(img_count, dist_part_count * 2);
for(int j=0; j<img_count; j++)
{
// for(int k=1,k2=0; k<dist_part_count-1; k++,k2++)
// {
// this_D[j][k2*2] = d_D[j][k2*2] - d_D[j][0];
// this_D[j][k2*2+1] = d_D[j][k2*2+1] - d_D[j][1];
// }
// this_D[j][(dist_part_count-1)*2] = part_locations[j][i*2] -
// d_D[j][0];
// this_D[j][(dist_part_count-1)*2+1] = part_locations[j][i*2+1] -
// d_D[j][1];
// for(int k=0, k2=1; k<part_count-1; k++, k2++)
// {
int k2=i;
int k=0;
this_D[j][k*2] = part_locations[j][k2*2] - d_D[j][0];
this_D[j][k*2+1] = part_locations[j][k2*2+1] - d_D[j][1];
// }
}
_DMatrix<T> sigma_123i = this_D.covariance();
_DMatrix<T> mu_123i = (this_D.means()).transpose();
l=l+glikelihood(sigma_123i, mu_123i.transpose(), this_D) - l2;
// now compute parameters for P(i|dist_parts), which is also normal
int low_dist = 0, high_dist = (dist_part_count-1)*2-1;
int low_i = high_dist+1, high_i = high_dist+2;
_DMatrix<T> S_11 = sigma_123i.extract(DRect(low_i, low_i, high_i, high_i));
if(dist_part_count != 1)
{
_DMatrix<T> S_22 = sigma_123i.extract(DRect(low_dist, low_dist, high_dist, high_dist));
_DMatrix<T> S_12 = sigma_123i.extract(DRect(low_i, low_dist, high_i, high_dist));
_DMatrix<T> S_21 = S_12.transpose();
sigma.get(i) = S_11 - S_12 * S_22.inverse() * S_21;
mu_x2.get(i) = S_12 * S_22.inverse();
mu.get(i) = mu_123i.extract(DRect(low_i,0, high_i, mu_123i.cols()-1)) -
S_12 * S_22.inverse() *
mu_123i.extract(DRect(low_dist, 0, high_dist, mu_123i.cols()-1));
}
else
{
sigma.get(i) = S_11;
mu.get(i) = mu_123i.extract(DRect(low_i,0, high_i, mu_123i.cols()-1));
}
}
l = l / img_count;
KFan<T> kfan(part_count, dist_part_count, dist_parts, sigma, mu,
sigma_123, mu_123, mu_x2);
kfan.model_likelihood = l;
return kfan;
}
#define DECLARE(x) \
template class Train_KFan<x>;
DECLARE(double)
DECLARE(float)