-
Notifications
You must be signed in to change notification settings - Fork 0
/
fit_ellipsoid.py
324 lines (219 loc) · 7.42 KB
/
fit_ellipsoid.py
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python2.5
#
# Written (W) 2011 Christian Widmer
# Copyright (C) 2011 Max-Planck-Society
"""
@author: Christian Widmer
@summary: Code for ellipsoid
"""
import scipy.optimize
import numpy
import random
import loss_functions
#import cvxmod
#from cvxmod.atoms import square
#import kdtree_cook as kdt
import scipy.spatial.distance as dist
from kdtree import KDTree
from util import ellipsoid
from data_processing import get_data_example
# global variables to use inside of solver
x = [0.0, -1.0, 1.0, 0.0, 0.0, 0.0]
y = [1.0, 0.0, 0.0, -1.0, 0.0, 0.0]
z = [0.0, 0.0, 0.0, 0.0, 1.0,-1.0]
i = [0.0, 0.0, 0.0, 0.0, 1.0,-1.0]
def fitting_obj_sample(param):
"""
computes residuals based on distance from ellipsoid
can be used with different loss-functions on residual
"""
obj = 0
# centers
cx = param[0]
cy = param[1]
cz = param[2]
rx = param[3]
ry = param[4]
rz = param[5]
sx, sy, sz = ellipsoid(cx, cy, cz, rx, ry, rz, 20)
num_samples = len(sx)
#plot_point_cloud(sx, sy, sz)
print "num_samples", num_samples
#import pdb
#pdb.set_trace()
#data = numpy.array(zip(sx, sy, sz)).T
#tree = kdt.kdtree( data, leafsize=1000 )
data = zip(sx, sy, sz)
tree = KDTree.construct_from_data(data)
num_queries = len(x)
print "num_queries", num_queries
global global_loss
global_loss = numpy.zeros(num_queries)
for idx in range(num_queries):
"""
Compute the unique root tbar of F(t) on (-e2*e2,+infinity);
x0 = e0*e0*y0/(tbar + e0*e0);
x1 = e1*e1*y1/(tbar + e1*e1);
x2 = e2*e2*y2/(tbar + e2*e2);
distance = sqrt((x0 - y0)*(x0 - y0) + (x1 - y1)*(x1 - y1) + (x2 - y2)*(x2 - y2))
"""
query = (x[idx], y[idx], z[idx])
nearest, = tree.query(query_point=query, t=1)
residual = dist.euclidean(query, nearest)
#obj += loss_functions.squared_loss(residual)
#obj += loss_functions.abs_loss(residual)
#obj += loss_functions.eps_loss(residual, 2)
#obj += loss_functions.eps_loss_bounded(residual, 2)
loss_xt = loss_functions.eps_loss_asym(residual, 2, 1.0, 0.2)
obj += loss_xt
global_loss[idx] = num_queries
#obj += eps_loss(residual, 2)*data_intensity[idx]
# add regularizer to keep radii close
reg = 10 * regularizer(param)
print "loss", obj
print "reg", reg
obj += reg
return obj
def regularizer(param):
"""
enforces similar radii
"""
rx = param[3]
ry = param[4]
rz = param[5]
reg = (rx - ry)**2 + (rx - rz)**2 + (ry - rz)**2
return reg
def fitting_obj(param):
"""
computes residuals based on distance from ellipsoid
can be used with different loss-functions on residual
"""
# centers
cx = param[0]
cy = param[1]
cz = param[2]
radius = param[3]
#a = param[3]
#b = param[4]
#c = param[5]
obj = 0
for idx in range(len(x)):
"""
Compute the unique root tbar of F(t) on (-e2*e2,+infinity);
x0 = e0*e0*y0/(tbar + e0*e0);
x1 = e1*e1*y1/(tbar + e1*e1);
x2 = e2*e2*y2/(tbar + e2*e2);
distance = sqrt((x0 - y0)*(x0 - y0) + (x1 - y1)*(x1 - y1) + (x2 - y2)*(x2 - y2))
"""
#residual = b*b*c*c*(cx - x[idx])**2
#residual += a*a*c*c*(cy - y[idx])**2
#residual += a*a*b*b*(cz - z[idx])**2
#residual = residual - a*a*b*b*c*c
residual = (cx - x[idx])**2 + (cy - y[idx])**2 + (cz - z[idx])**2
residual = numpy.sqrt(residual) - radius
tmp = loss_functions.squared_loss(residual)
#tmp = loss_functions.abs_loss(residual)
#tmp = loss_functions.eps_loss(residual, 1)
#tmp = loss_functions.eps_loss_asym(residual, 2, 1.0, 0.3)
# consider intensity
obj += tmp*i[idx]
return obj
def fit_ellipsoid(dx, dy, dz, di, num_points=None):
"""
fit ellipoid beased on data
"""
is_sphere = True
num_points = len(dx)
idx = range(num_points)
random.shuffle(idx)
subset_idx = idx[0:500]
global x,y,z,i
x = numpy.array(dx)[subset_idx]
y = numpy.array(dy)[subset_idx]
z = numpy.array(dz)[subset_idx]
i = numpy.array(di)[subset_idx]
print "num data points: %i" % (len(x))
if is_sphere:
x0 = numpy.array([0, 0, 0, 5])
else:
x0 = numpy.array([15, 15, 10, 5, 5, 5])
x0[0] = numpy.average(x)
x0[1] = numpy.average(y)
x0[2] = numpy.average(z)
print "center guess: x=%f, y=%f, z=%f" % (x0[0], x0[1], x0[2])
#x_opt = scipy.optimize.fmin(fitting_obj, x0)
epsilon = 0.5
bounds = []
bounds.append((0, None)) # cx
bounds.append((0, None)) # cy
bounds.append((0, None)) # cz
bounds.append((0, None)) # rx
if not is_sphere:
bounds.append((0, None)) # ry
bounds.append((0, None)) # rz
if is_sphere:
#x_opt, nfeval, rc = scipy.optimize.fmin_l_bfgs_b(fitting_obj, x0, bounds=bounds, approx_grad=True, iprint=5)
#x_opt = scipy.optimize.fmin(fitting_obj_sphere_sample, x0, xtol=epsilon, ftol=epsilon, disp=True, full_output=True)[0]
#x_opt = scipy.optimize.fmin(fitting_obj, x0, xtol=epsilon, ftol=epsilon, disp=True, full_output=True)[0]
x_opt, nfeval, rc = scipy.optimize.fmin_tnc(fitting_obj, x0, bounds=bounds, approx_grad=True, messages=5)
return x_opt[0], x_opt[1], x_opt[2], x_opt[3], x_opt[3], x_opt[3]
else:
#x_opt, nfeval, rc = scipy.optimize.fmin_l_bfgs_b(fitting_obj, x0, bounds=bounds, approx_grad=True, iprint=5)
x_opt = scipy.optimize.fmin(fitting_obj_sample, x0, xtol=epsilon, ftol=epsilon, disp=True, full_output=True)[0]
return x_opt[0], x_opt[1], x_opt[2], x_opt[3], x_opt[4], x_opt[5]
def fit_ellipsoid_cvx(x, y, z):
"""
fit ellipoid using squared loss
"""
#TODO not working. it is using non-linear solver, but takes forever
assert len(x) == len(y)
N = len(x)
D = 7
dat = numpy.zeros((N, D))
dat[:,0] = x*x
dat[:,1] = y*y
dat[:,2] = z*z
dat[:,3] = x
dat[:,4] = y
dat[:,5] = z
dat[:,6] = numpy.ones(N)
print dat.shape
dat = cvxmod.matrix(dat)
#### parameters
# data
X = cvxmod.param("X", N, D)
#### varibales
# parameter vector
theta = cvxmod.optvar("theta", D)
# simple objective
objective = cvxmod.sum(square(X*theta))
# create problem
p = cvxmod.problem(cvxmod.minimize(objective))
p.constr.append(theta[0] + theta[1] == 1)
#p.constr.append(theta[0] + theta[2] == 1)
#p.constr.append(theta[1] + theta[2] == 1)
###### set values
X.value = dat
#solver = "mosek"
#p.solve(lpsolver=solver)
p.solve()
w = numpy.array(cvxmod.value(theta))
#print weights
cvxmod.printval(theta)
## For clarity, fill in the quadratic form variables
A = numpy.zeros((3,3))
A[0,0] = w[0]
#A.ravel()[1:3] = 0 #w[2]
A[1,1] = w[1]
A[2,2] = w[2]
bv = w[3:6]
c = w[6]
## find parameters
from conic2ellipse import conic2ellipsoid
z, rx, ry, rz, alpha = conic2ellipsoid(A, bv, c)
return z, rx, ry, alpha
if __name__ == "__main__":
# debug
dx, dy, dz, di = get_data_example()
#print fit_stack(dx, dy, dz, di)
print fit_ellipsoid_cvx(dx, dy, dz)