-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransform.py
More file actions
69 lines (48 loc) · 2.26 KB
/
transform.py
File metadata and controls
69 lines (48 loc) · 2.26 KB
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
# this code is based on the project "Neural Networks Image Segmentation toolkit" : https://github.com/Cyril-Meyer/NeNISt
import numpy as np
import edt
def normalize_tanh(distance, scale=4.0):
return np.tanh(distance / scale)
def normalize_sign(distance, scale=2.0):
return 1 / (1 + np.exp(-(distance / scale)))
def normalize_linear_clip_0_1(distance, scale=8.0):
return np.clip((distance / scale), 0, 1)
def label_dt(label, anisotropy=None, normalize=normalize_tanh, normalize_scale_pos=5.0, normalize_scale_neg=5.0):
ndim = len(label.shape)
# default anisotropy: no anisotropy
if anisotropy == None:
anisotropy = (1.0,) * ndim
# check anisotropy shape
if not len(anisotropy) == ndim:
print("ERROR: label_dt invalid anisotropy tuple length")
distance_pos = edt.edt(label, anisotropy=anisotropy)
distance_pos = normalize(distance_pos, normalize_scale_pos)
distance_neg = -edt.edt(1 - label, anisotropy=anisotropy)
distance_neg = normalize(distance_neg, normalize_scale_neg)
distance = distance_neg
distance[label == 1] = distance_pos[label == 1]
return distance
def label_dt_f16(label, anisotropy=None, normalize=normalize_tanh, normalize_scale_pos=1.0, normalize_scale_neg=1.0):
ndim = len(label.shape)
# default anisotropy: no anisotropy
if anisotropy == None:
anisotropy = (1.0,) * ndim
# check anisotropy shape
if not len(anisotropy) == ndim:
print("ERROR: label_dt invalid anisotropy tuple length")
distance_pos = edt.edt(label, anisotropy=anisotropy).astype(np.float16)
distance_pos = normalize(distance_pos, normalize_scale_pos)
distance_neg = -edt.edt(1 - label, anisotropy=anisotropy).astype(np.float16)
distance_neg = normalize(distance_neg, normalize_scale_neg)
distance = distance_neg
distance[label == 1] = distance_pos[label == 1]
return distance
def label_edt(label, anisotropy=None):
ndim = len(label.shape)
# default anisotropy: no anisotropy
if anisotropy == None:
anisotropy = (1.0,) * ndim
# check anisotropy shape
if not len(anisotropy) == ndim:
print("ERROR: label_dt invalid anisotropy tuple length")
return edt.edt(label, anisotropy=anisotropy) - edt.edt(1 - label, anisotropy=anisotropy)