-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCtypesPermutohedralLattice.py
executable file
·79 lines (51 loc) · 2.14 KB
/
CtypesPermutohedralLattice.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
import numpy as np
from ctypes import *
import ctypes.util
import cv2
from concurrent.futures import ThreadPoolExecutor, wait
permdll = ctypes.CDLL('./ctypes_shared_lib/permuto.so', mode=ctypes.RTLD_LOCAL)
c_constr = permdll.PermBuild
c_constr.restype = c_void_p
c_init = permdll.PermInit
c_init.argtypes = [c_void_p, POINTER(c_float), c_int, c_int]
c_compute = permdll.PermCompute
c_compute.argtypes = [c_void_p, POINTER(c_float), POINTER(c_float), c_int, c_int]
c_delete = permdll.PermDelete
c_delete.argtypes = [c_void_p]
class CtypesPermutohedralLattice(object):
def __init__(self, features):
self.obj = c_constr()
cf, h, w = features.shape
features = np.reshape(features, (cf, h*w))
features = np.ascontiguousarray(features, dtype='float32')
f_p = features.ctypes.data_as(POINTER(c_float))
c_init(self.obj, f_p, cf, h*w)
self.norm_factor = self.compute(np.ones((1, h, w)), normalize=False)
print np.min( self.norm_factor), np.max( self.norm_factor)
def compute(self, x, normalize=True):
cx, h, w = x.shape
x = np.reshape(x, (cx, h*w))
x = np.ascontiguousarray(x, dtype='float32')
out = np.ones_like(x)
out = np.ascontiguousarray(out, dtype='float32')
x_p = x.ctypes.data_as(POINTER(c_float))
out_p = out.ctypes.data_as(POINTER(c_float))
c_compute(self.obj, x_p, out_p, cx, h*w)
out = np.reshape(out, (cx, h, w))
if normalize:
out /= self.norm_factor
return out
def compute_stacked(self, x_list, normalize=True):
c_vec = np.array([x.shape[0] for x in x_list])
stacked_tensor = x_list[0]
for i in range(1, len(x_list)):
stacked_tensor = np.concatenate([stacked_tensor, x_list[i]], axis=0)
stacked_tensor_filtered = self.compute(stacked_tensor, normalize)
output_list = []
c_counter = 0
for i in range(0, c_vec.shape[0]):
output_list.append(stacked_tensor_filtered[c_counter:c_counter + c_vec[i]])
c_counter += c_vec[i]
return output_list
def __del__(self):
c_delete(self.obj)