AzumaSeren100 commited on
Commit
d827b45
1 Parent(s): 501fc19

Upload 4 files

Browse files
monotonic_align/__init__.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from numpy import zeros, int32, float32
2
+ from torch import from_numpy
3
+
4
+ from .core import maximum_path_jit
5
+
6
+ def maximum_path(neg_cent, mask):
7
+ device = neg_cent.device
8
+ dtype = neg_cent.dtype
9
+ neg_cent = neg_cent.data.cpu().numpy().astype(float32)
10
+ path = zeros(neg_cent.shape, dtype=int32)
11
+
12
+ t_t_max = mask.sum(1)[:, 0].data.cpu().numpy().astype(int32)
13
+ t_s_max = mask.sum(2)[:, 0].data.cpu().numpy().astype(int32)
14
+ maximum_path_jit(path, neg_cent, t_t_max, t_s_max)
15
+ return from_numpy(path).to(device=device, dtype=dtype)
monotonic_align/__pycache__/__init__.cpython-39.pyc ADDED
Binary file (724 Bytes). View file
 
monotonic_align/__pycache__/core.cpython-39.pyc ADDED
Binary file (953 Bytes). View file
 
monotonic_align/core.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numba
2
+
3
+
4
+ @numba.jit(numba.void(numba.int32[:,:,::1], numba.float32[:,:,::1], numba.int32[::1], numba.int32[::1]), nopython=True, nogil=True)
5
+ def maximum_path_jit(paths, values, t_ys, t_xs):
6
+ b = paths.shape[0]
7
+ max_neg_val=-1e9
8
+ for i in range(int(b)):
9
+ path = paths[i]
10
+ value = values[i]
11
+ t_y = t_ys[i]
12
+ t_x = t_xs[i]
13
+
14
+ v_prev = v_cur = 0.0
15
+ index = t_x - 1
16
+
17
+ for y in range(t_y):
18
+ for x in range(max(0, t_x + y - t_y), min(t_x, y + 1)):
19
+ if x == y:
20
+ v_cur = max_neg_val
21
+ else:
22
+ v_cur = value[y-1, x]
23
+ if x == 0:
24
+ if y == 0:
25
+ v_prev = 0.
26
+ else:
27
+ v_prev = max_neg_val
28
+ else:
29
+ v_prev = value[y-1, x-1]
30
+ value[y, x] += max(v_prev, v_cur)
31
+
32
+ for y in range(t_y - 1, -1, -1):
33
+ path[y, index] = 1
34
+ if index != 0 and (index == y or value[y-1, index] < value[y-1, index-1]):
35
+ index = index - 1