forked from amueller/gco_python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgco_python.pyx
440 lines (362 loc) · 16.8 KB
/
gco_python.pyx
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# cython: experimental_cpp_class_def=True
import numpy as np
cimport numpy as np
from libcpp.map cimport map
from libcpp.pair cimport pair
DEF DEBUG_CHECKS = True # true if laborious parameter checks are needed
# This is the energy type; should match the EnergyType and
# EnergyTermType in GCOptimization.h
DEF NRG_TYPE_STR = int
IF NRG_TYPE_STR == int:
ctypedef np.int32_t NRG_DTYPE_t
ctypedef int NRG_TYPE
ctypedef map[pair[int,int],int] PW_MAP_T # map (s1, s2) -> strength
ELSE:
ctypedef np.float64_t NRG_DTYPE_t
ctypedef double NRG_TYPE
ctypedef map[pair[int,int],double] PW_MAP_T # map (s1, s2) -> strength
np.import_array()
cdef extern from "GCoptimization.h":
cdef cppclass GCoptimizationGridGraph:
cppclass SmoothCostFunctor:
NRG_TYPE compute(int s1, int s2, int l1, int l2)
GCoptimizationGridGraph(int width, int height, int n_labels)
void setDataCost(NRG_TYPE *)
void setSmoothCost(NRG_TYPE *)
NRG_TYPE expansion(int n_iterations)
NRG_TYPE swap(int n_iterations)
void setSmoothCostVH(NRG_TYPE* pairwise, NRG_TYPE* V, NRG_TYPE* H)
void setSmoothCostFunctor(SmoothCostFunctor* f)
int whatLabel(int node)
void setLabel(int node, int label)
NRG_TYPE compute_energy()
cdef cppclass GCoptimizationGeneralGraph:
GCoptimizationGeneralGraph(int n_vertices, int n_labels)
void setDataCost(NRG_TYPE *)
void setSmoothCost(NRG_TYPE *)
void setNeighbors(int, int)
void setNeighbors(int, int, NRG_TYPE)
NRG_TYPE expansion(int n_iterations)
NRG_TYPE swap(int n_iterations)
void setSmoothCostFunctor(GCoptimizationGridGraph.SmoothCostFunctor* f) # yep, it works
int whatLabel(int node)
void setLabel(int node, int label)
NRG_TYPE compute_energy()
cdef cppclass PottsFunctor(GCoptimizationGridGraph.SmoothCostFunctor):
NRG_TYPE strength_
__init__(NRG_TYPE strength):
this.strength_ = strength
NRG_TYPE compute(int s1, int s2, int l1, int l2):
return -this.strength_ if l1 == l2 else 0
cdef cppclass GeneralizedPottsFunctor(GCoptimizationGridGraph.SmoothCostFunctor):
PW_MAP_T data_
__init__(object data):
this.data_ = data
NRG_TYPE compute(int s1, int s2, int l1, int l2):
if l1 != l2:
return 0
else:
pair = tuple(sorted([s1,s2]))
return -this.data_[pair]
def cut_simple(np.ndarray[NRG_DTYPE_t, ndim=3, mode='c'] unary_cost,
np.ndarray[NRG_DTYPE_t, ndim=2, mode='c'] pairwise_cost, n_iter=5,
algorithm='expansion'):
"""
Apply multi-label graphcuts to grid graph.
Parameters
----------
unary_cost: ndarray, double, shape=(width, height, n_labels)
Unary potentials
pairwise_cost: ndarray, double, shape=(n_labels, n_labels)
Pairwise potentials for label compatibility
n_iter: int, (default=5)
Number of iterations
algorithm: string, `expansion` or `swap`, default=expansion
Whether to perform alpha-expansion or alpha-beta-swaps.
"""
if unary_cost.shape[2] != pairwise_cost.shape[0]:
raise ValueError("unary_cost and pairwise_cost have incompatible shapes.\n"
"unary_cost must be height x width x n_labels, pairwise_cost must be n_labels x n_labels.\n"
"Got: unary_cost: (%d, %d, %d), pairwise_cost: (%d, %d)"
%(unary_cost.shape[0], unary_cost.shape[1], unary_cost.shape[2],
pairwise_cost.shape[0], pairwise_cost.shape[1]))
if pairwise_cost.shape[1] != pairwise_cost.shape[0]:
raise ValueError("pairwise_cost must be a square matrix.")
cdef int h = unary_cost.shape[1]
cdef int w = unary_cost.shape[0]
cdef int n_labels = pairwise_cost.shape[0]
if (pairwise_cost != pairwise_cost.T).any():
raise ValueError("pairwise_cost must be symmetric.")
cdef GCoptimizationGridGraph* gc = new GCoptimizationGridGraph(h, w, n_labels)
gc.setDataCost(<NRG_TYPE*>unary_cost.data)
gc.setSmoothCost(<NRG_TYPE*>pairwise_cost.data)
cdef NRG_TYPE nrg
if algorithm == 'swap':
nrg = gc.swap(n_iter)
elif algorithm == 'expansion':
nrg = gc.expansion(n_iter)
else:
raise ValueError("algorithm should be either `swap` or `expansion`. Got: %s" % algorithm)
cdef np.npy_intp result_shape[2]
result_shape[0] = w
result_shape[1] = h
cdef np.ndarray[np.int32_t, ndim=2] result = np.PyArray_SimpleNew(2, result_shape, np.NPY_INT32)
cdef int * result_ptr = <int*>result.data
for i in xrange(w * h):
result_ptr[i] = gc.whatLabel(i)
del gc
return result, nrg
def cut_simple_gen_potts(np.ndarray[NRG_DTYPE_t, ndim=3, mode='c'] unary_cost,
object pairwise_cost, n_iter=5,
algorithm='expansion'):
"""
Apply multi-label graphcuts to grid graph.
Parameters
----------
unary_cost: ndarray, double, shape=(width, height, n_labels)
Unary potentials
pairwise_cost: dict: (site1, site2) -> strength, where site1 < site2.
Pixels are ordered by rows of the grid first
n_iter: int, (default=5)
Number of iterations
algorithm: string, `expansion` or `swap`, default=expansion
Whether to perform alpha-expansion or alpha-beta-swaps.
"""
cdef int h = unary_cost.shape[1]
cdef int w = unary_cost.shape[0]
cdef int n_labels = unary_cost.shape[2]
IF DEBUG_CHECKS:
cdef np.ndarray[np.int32_t, ndim=2] pix_nums = np.r_[:h*w].reshape(h,w)
edges = [tuple(sorted(pair)) for pair in zip(pix_nums[:,:-1].flatten(), pix_nums[:,1:].flatten())] + \
[tuple(sorted(pair)) for pair in zip(pix_nums[:-1,:].flatten(), pix_nums[1:,:].flatten())]
for edge in edges:
if edge not in pairwise_cost:
raise ValueError("Pairwise potential for the edge (%d,%d) is not given" % edge)
if pairwise_cost[edge] < 0:
raise ValueError("Pairwise potential for the edge (%d,%d) is negative, "
"which is not allowed in generalized Potts" % edge)
cdef GCoptimizationGridGraph* gc = new GCoptimizationGridGraph(h, w, n_labels)
gc.setDataCost(<NRG_TYPE*>unary_cost.data)
gc.setSmoothCostFunctor(<GeneralizedPottsFunctor*>new GeneralizedPottsFunctor(pairwise_cost))
cdef NRG_TYPE nrg
if algorithm == 'swap':
nrg = gc.swap(n_iter)
elif algorithm == 'expansion':
nrg = gc.expansion(n_iter)
else:
raise ValueError("algorithm should be either `swap` or `expansion`. Got: %s" % algorithm)
cdef np.npy_intp result_shape[2]
result_shape[0] = w
result_shape[1] = h
cdef np.ndarray[np.int32_t, ndim=2] result = np.PyArray_SimpleNew(2, result_shape, np.NPY_INT32)
cdef int * result_ptr = <int*>result.data
for i in xrange(w * h):
result_ptr[i] = gc.whatLabel(i)
del gc
return result, nrg
def cut_simple_vh(np.ndarray[NRG_DTYPE_t, ndim=3, mode='c'] unary_cost,
np.ndarray[NRG_DTYPE_t, ndim=2, mode='c'] pairwise_cost,
np.ndarray[NRG_DTYPE_t, ndim=2, mode='c'] costV,
np.ndarray[NRG_DTYPE_t, ndim=2, mode='c'] costH,
n_iter=5,
algorithm='expansion'):
"""
Apply multi-label graphcuts to grid graph.
Parameters
----------
unary_cost: ndarray, int32, shape=(width, height, n_labels)
Unary potentials
pairwise_cost: ndarray, int32, shape=(n_labels, n_labels)
Pairwise potentials for label compatibility
costV: ndarray, int32, shape=(width, height)
Vertical edge weights
costH: ndarray, int32, shape=(width, height)
Horizontal edge weights
n_iter: int, (default=5)
Number of iterations
algorithm: string, `expansion` or `swap`, default=expansion
Whether to perform alpha-expansion or alpha-beta-swaps.
"""
if unary_cost.shape[2] != pairwise_cost.shape[0]:
raise ValueError("unary_cost and pairwise_cost have incompatible shapes.\n"
"unary_cost must be height x width x n_labels, pairwise_cost must be n_labels x n_labels.\n"
"Got: unary_cost: (%d, %d, %d), pairwise_cost: (%d, %d)"
%(unary_cost.shape[0], unary_cost.shape[1], unary_cost.shape[2],
pairwise_cost.shape[0], pairwise_cost.shape[1]))
if pairwise_cost.shape[1] != pairwise_cost.shape[0]:
raise ValueError("pairwise_cost must be a square matrix.")
cdef int h = unary_cost.shape[1]
cdef int w = unary_cost.shape[0]
cdef int n_labels = pairwise_cost.shape[0]
if (pairwise_cost != pairwise_cost.T).any():
raise ValueError("pairwise_cost must be symmetric.")
if costV.shape[0] != w or costH.shape[0] != w or costV.shape[1] != h or costH.shape[1] != h:
raise ValueError("incorrect costV or costH dimensions.")
cdef GCoptimizationGridGraph* gc = new GCoptimizationGridGraph(h, w, n_labels)
gc.setDataCost(<NRG_TYPE*>unary_cost.data)
gc.setSmoothCostVH(<NRG_TYPE*>pairwise_cost.data, <NRG_TYPE*>costV.data, <NRG_TYPE*>costH.data)
cdef NRG_TYPE nrg
if algorithm == 'swap':
nrg = gc.swap(n_iter)
elif algorithm == 'expansion':
nrg = gc.expansion(n_iter)
else:
raise ValueError("algorithm should be either `swap` or `expansion`. Got: %s" % algorithm)
cdef np.npy_intp result_shape[2]
result_shape[0] = w
result_shape[1] = h
cdef np.ndarray[np.int32_t, ndim=2] result = np.PyArray_SimpleNew(2, result_shape, np.NPY_INT32)
cdef int * result_ptr = <int*>result.data
for i in xrange(w * h):
result_ptr[i] = gc.whatLabel(i)
del gc
return result, nrg
def energy_of_graph_assignment(np.ndarray[np.int32_t, ndim=2, mode='c'] edges,
np.ndarray[NRG_DTYPE_t, ndim=2, mode='c'] unary_cost,
np.ndarray[NRG_DTYPE_t, ndim=2, mode='c'] pairwise_cost,
np.ndarray[np.int32_t, ndim=1, mode='c'] assignment) :
"""
Calculate the energy of a particular assignment of labels to a graph
Parameters
----------
edges: ndarray, int32, shape(n_edges, 2 or 3)
Rows correspond to edges in graph, given as vertex indices.
if edges is n_edges x 3 then third parameter is used as edge weight
unary_cost: ndarray, int32, shape=(n_vertices, n_labels)
Unary potentials
pairwise_cost: ndarray, int32, shape=(n_labels, n_labels)
Pairwise potentials for label compatibility
assigment : ndarray, int32, shape= (n_vertices,)
Assignments of labels to nodes
"""
if (pairwise_cost != pairwise_cost.T).any():
raise ValueError("pairwise_cost must be symmetric.")
if unary_cost.shape[1] != pairwise_cost.shape[0]:
raise ValueError("unary_cost and pairwise_cost have incompatible shapes.\n"
"unary_cost must be height x width x n_labels, pairwise_cost must be n_labels x n_labels.\n"
"Got: unary_cost: (%d, %d), pairwise_cost: (%d, %d)"
%(unary_cost.shape[0], unary_cost.shape[1],
pairwise_cost.shape[0], pairwise_cost.shape[1]))
if pairwise_cost.shape[1] != pairwise_cost.shape[0]:
raise ValueError("pairwise_cost must be a square matrix.")
cdef int n_vertices = unary_cost.shape[0]
cdef int n_labels = pairwise_cost.shape[0]
cdef GCoptimizationGeneralGraph* gc = new GCoptimizationGeneralGraph(n_vertices, n_labels)
for e in edges:
if len(e) == 3:
gc.setNeighbors(e[0], e[1], e[2])
else:
gc.setNeighbors(e[0], e[1])
gc.setDataCost(<NRG_TYPE*>unary_cost.data)
gc.setSmoothCost(<NRG_TYPE*>pairwise_cost.data)
for i in xrange(n_vertices):
gc.setLabel(i, assignment[i])
nrg = gc.compute_energy()
return nrg
def cut_from_graph(np.ndarray[np.int32_t, ndim=2, mode='c'] edges,
np.ndarray[NRG_DTYPE_t, ndim=2, mode='c'] unary_cost,
np.ndarray[NRG_DTYPE_t, ndim=2, mode='c'] pairwise_cost, n_iter=5,
algorithm='expansion', np.ndarray[NRG_DTYPE_t, ndim=1, mode='c'] weights=None):
"""
Apply multi-label graphcuts to arbitrary graph given by `edges`.
Parameters
----------
edges: ndarray, int32, shape(n_edges, 2 or 3)
Rows correspond to edges in graph, given as vertex indices.
if edges is n_edges x 3 then third parameter is used as edge weight
unary_cost: ndarray, int32, shape=(n_vertices, n_labels)
Unary potentials
pairwise_cost: ndarray, int32, shape=(n_labels, n_labels)
Pairwise potentials for label compatibility
n_iter: int, (default=5)
Number of iterations
algorithm: string, `expansion` or `swap`, default=expansion
Whether to perform alpha-expansion or alpha-beta-swaps.
"""
if (pairwise_cost != pairwise_cost.T).any():
raise ValueError("pairwise_cost must be symmetric.")
if unary_cost.shape[1] != pairwise_cost.shape[0]:
raise ValueError("unary_cost and pairwise_cost have incompatible shapes.\n"
"unary_cost must be height x width x n_labels, pairwise_cost must be n_labels x n_labels.\n"
"Got: unary_cost: (%d, %d), pairwise_cost: (%d, %d)"
%(unary_cost.shape[0], unary_cost.shape[1],
pairwise_cost.shape[0], pairwise_cost.shape[1]))
if pairwise_cost.shape[1] != pairwise_cost.shape[0]:
raise ValueError("pairwise_cost must be a square matrix.")
if weights is not None and edges.shape[1] == 3:
raise ValueError("weights parameter is ambiguous when edges is a 3-column array.")
if weights is not None and weights.shape[0] != edges.shape[0]:
raise ValueError("weights vector should contain one weight per edge.")
cdef int n_vertices = unary_cost.shape[0]
cdef int n_labels = pairwise_cost.shape[0]
cdef GCoptimizationGeneralGraph* gc = new GCoptimizationGeneralGraph(n_vertices, n_labels)
if weights is None:
for e in edges:
if len(e) == 3:
gc.setNeighbors(e[0], e[1], e[2])
else:
gc.setNeighbors(e[0], e[1])
else:
for e,w in zip(edges, weights):
gc.setNeighbors(e[0], e[1], w)
gc.setDataCost(<NRG_TYPE*>unary_cost.data)
gc.setSmoothCost(<NRG_TYPE*>pairwise_cost.data)
cdef NRG_TYPE nrg
if algorithm == 'swap':
nrg = gc.swap(n_iter)
elif algorithm == 'expansion':
nrg = gc.expansion(n_iter)
else:
raise ValueError("algorithm should be either `swap` or `expansion`. Got: %s" % algorithm)
cdef np.npy_intp result_shape[1]
result_shape[0] = n_vertices
cdef np.ndarray[np.int32_t, ndim=1] result = np.PyArray_SimpleNew(1, result_shape, np.NPY_INT32)
cdef int * result_ptr = <int*>result.data
for i in xrange(n_vertices):
result_ptr[i] = gc.whatLabel(i)
del gc
return result, nrg
def cut_from_graph_gen_potts(
np.ndarray[NRG_DTYPE_t, ndim=2, mode='c'] unary_cost,
object pairwise_cost, n_iter=5,
algorithm='expansion'):
"""
Apply multi-label graphcuts to arbitrary graph given by `edges`.
Parameters
----------
unary_cost: ndarray, int32, shape=(n_vertices, n_labels)
Unary potentials
pairwise_cost: dict: (site1, site2) -> strength, where site1 < site2.
The order of nodes is the same as in unary_cost
n_iter: int, (default=5)
Number of iterations
algorithm: string, `expansion` or `swap`, default=expansion
Whether to perform alpha-expansion or alpha-beta-swaps.
"""
cdef int n_vertices = unary_cost.shape[0]
cdef int n_labels = unary_cost.shape[1]
cdef GCoptimizationGeneralGraph* gc = new GCoptimizationGeneralGraph(n_vertices, n_labels)
for edge, strength in pairwise_cost.items():
gc.setNeighbors(edge[0], edge[1])
if edge[0] >= edge[1]:
raise ValueError("The order of sites in the edge (%d,%d) should be ascending" % edge)
if strength < 0:
raise ValueError("Pairwise potential for the edge (%d,%d) is negative, "
"which is not allowed in generalized Potts" % edge)
gc.setDataCost(<NRG_TYPE*>unary_cost.data)
gc.setSmoothCostFunctor(<GeneralizedPottsFunctor*>new GeneralizedPottsFunctor(pairwise_cost))
cdef NRG_TYPE nrg
if algorithm == 'swap':
nrg = gc.swap(n_iter)
elif algorithm == 'expansion':
nrg = gc.expansion(n_iter)
else:
raise ValueError("algorithm should be either `swap` or `expansion`. Got: %s" % algorithm)
cdef np.npy_intp result_shape[1]
result_shape[0] = n_vertices
cdef np.ndarray[np.int32_t, ndim=1] result = np.PyArray_SimpleNew(1, result_shape, np.NPY_INT32)
cdef int * result_ptr = <int*>result.data
for i in xrange(n_vertices):
result_ptr[i] = gc.whatLabel(i)
del gc
return result, nrg