-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit_ellipse_conic.py
226 lines (155 loc) · 4.24 KB
/
fit_ellipse_conic.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
#!/usr/bin/env python2.5
#
# Written (W) 2011-2013 Christian Widmer
# Copyright (C) 2011-2013 Max-Planck-Society, TU-Berlin, MSKCC
"""
@author: Christian Widmer
@summary: Procedures for fitting ellipses using
conic parameterization
"""
import numpy
import cvxmod
from util import conic_to_ellipse
def fit_ellipse_eps_insensitive(x, y):
"""
fit ellipse using epsilon-insensitive loss
"""
x = numpy.array(x)
y = numpy.array(y)
print "shapes", x.shape, y.shape
assert len(x) == len(y)
N = len(x)
D = 5
dat = numpy.zeros((N, D))
dat[:,0] = x*x
dat[:,1] = y*y
#dat[:,2] = y*x
dat[:,2] = x
dat[:,3] = y
dat[:,4] = numpy.ones(N)
print dat.shape
dat = cvxmod.matrix(dat)
#### parameters
# data
X = cvxmod.param("X", N, D)
# parameter for eps-insensitive loss
eps = cvxmod.param("eps", 1)
#### varibales
# parameter vector
theta = cvxmod.optvar("theta", D)
# dim = (N x 1)
s = cvxmod.optvar("s", N)
t = cvxmod.optvar("t", N)
# simple objective
objective = cvxmod.sum(t)
# create problem
p = cvxmod.problem(cvxmod.minimize(objective))
# add constraints
# (N x D) * (D X 1) = (N X 1)
p.constr.append(X*theta <= s)
p.constr.append(-X*theta <= s)
p.constr.append(s - eps <= t)
p.constr.append(0 <= t)
#p.constr.append(theta[4] == 1)
# trace constraint
p.constr.append(theta[0] + theta[1] == 1)
###### set values
X.value = dat
eps.value = 0.0
#solver = "mosek"
#p.solve(lpsolver=solver)
p.solve()
cvxmod.printval(theta)
theta_ = numpy.array(cvxmod.value(theta))
ellipse = conic_to_ellipse(theta_)
return ellipse
def fit_ellipse_linear(x, y):
"""
fit ellipse stack using absolute loss
"""
x = numpy.array(x)
y = numpy.array(y)
print "shapes", x.shape, y.shape
assert len(x) == len(y)
N = len(x)
D = 6
dat = numpy.zeros((N, D))
dat[:,0] = x*x
dat[:,1] = y*y
dat[:,2] = y*x
dat[:,3] = x
dat[:,4] = y
dat[:,5] = numpy.ones(N)
print dat.shape
dat = cvxmod.matrix(dat)
# norm
norm = numpy.zeros((N,N))
for i in range(N):
norm[i,i] = numpy.sqrt(numpy.dot(dat[i], numpy.transpose(dat[i])))
norm = cvxmod.matrix(norm)
#### parameters
# data
X = cvxmod.param("X", N, D)
Q_grad = cvxmod.param("Q_grad", N, N)
#### varibales
# parameter vector
theta = cvxmod.optvar("theta", D)
# dim = (N x 1)
s = cvxmod.optvar("s", N)
# simple objective
objective = cvxmod.sum(s)
# create problem
p = cvxmod.problem(cvxmod.minimize(objective))
# add constraints
# (N x D) * (D X 1) = (N x N) * (N X 1)
p.constr.append(X*theta <= Q_grad*s)
p.constr.append(-X*theta <= Q_grad*s)
#p.constr.append(theta[4] == 1)
# trace constraint
p.constr.append(theta[0] + theta[1] == 1)
###### set values
X.value = dat
Q_grad.value = norm
#solver = "mosek"
#p.solve(lpsolver=solver)
p.solve()
cvxmod.printval(theta)
theta_ = numpy.array(cvxmod.value(theta))
ellipse = conic_to_ellipse(theta_)
return ellipse
def fit_ellipse_squared(x, y):
"""
fit ellipoid using squared loss
"""
assert len(x) == len(y)
N = len(x)
D = 5
dat = numpy.zeros((N, D))
dat[:,0] = x*x
dat[:,1] = y*y
#dat[:,2] = x*y
dat[:,2] = x
dat[:,3] = y
dat[:,4] = 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.atoms.norm2(X*theta)
# create problem
p = cvxmod.problem(cvxmod.minimize(objective))
p.constr.append(theta[0] + theta[1] == 1)
###### set values
X.value = dat
#solver = "mosek"
#p.solve(lpsolver=solver)
p.solve()
cvxmod.printval(theta)
theta_ = numpy.array(cvxmod.value(theta))
ellipse = conic_to_ellipse(theta_)
return ellipse