5
5
6
6
def get_subpatch (patch , coord , local_sub_patch_radius ):
7
7
start = np .maximum (0 , np .array (coord ) - local_sub_patch_radius )
8
- end = start + local_sub_patch_radius * 2 + 1
8
+ end = start + local_sub_patch_radius * 2 + 1
9
9
10
10
shift = np .minimum (0 , patch .shape - end )
11
11
12
12
start += shift
13
13
end += shift
14
14
15
- slices = [ slice (s , e ) for s , e in zip (start , end )]
15
+ slices = [slice (s , e ) for s , e in zip (start , end )]
16
16
17
17
return patch [tuple (slices )]
18
18
@@ -40,17 +40,19 @@ def normal_withoutCP(patch, coords, dims):
40
40
rand_coords = random_neighbor (patch .shape , coord )
41
41
vals .append (patch [tuple (rand_coords )])
42
42
return vals
43
+
43
44
return normal_withoutCP
44
45
45
46
46
47
def pm_uniform_withCP (local_sub_patch_radius ):
47
48
def random_neighbor_withCP_uniform (patch , coords , dims ):
48
49
vals = []
49
50
for coord in zip (* coords ):
50
- sub_patch = get_subpatch (patch , coord ,local_sub_patch_radius )
51
+ sub_patch = get_subpatch (patch , coord , local_sub_patch_radius )
51
52
rand_coords = [np .random .randint (0 , s ) for s in sub_patch .shape [0 :dims ]]
52
53
vals .append (sub_patch [tuple (rand_coords )])
53
54
return vals
55
+
54
56
return random_neighbor_withCP_uniform
55
57
56
58
@@ -60,6 +62,7 @@ def pixel_gauss(patch, coords, dims):
60
62
for coord in zip (* coords ):
61
63
vals .append (np .random .normal (patch [tuple (coord )], pixel_gauss_sigma ))
62
64
return vals
65
+
63
66
return pixel_gauss
64
67
65
68
@@ -71,6 +74,7 @@ def local_gaussian(patch, coords, dims):
71
74
axis = tuple (range (dims ))
72
75
vals .append (np .random .normal (np .mean (sub_patch , axis = axis ), np .std (sub_patch , axis = axis )))
73
76
return vals
77
+
74
78
return local_gaussian
75
79
76
80
@@ -80,17 +84,18 @@ def identity(patch, coords, dims):
80
84
for coord in zip (* coords ):
81
85
vals .append (patch [coord ])
82
86
return vals
87
+
83
88
return identity
84
89
85
90
86
91
def manipulate_val_data (X_val , Y_val , perc_pix = 0.198 , shape = (64 , 64 ), value_manipulation = pm_uniform_withCP (5 )):
87
92
dims = len (shape )
88
93
if dims == 2 :
89
- box_size = np .round (np .sqrt (100 / perc_pix )).astype (np .int )
94
+ box_size = np .round (np .sqrt (100 / perc_pix )).astype (np .int )
90
95
get_stratified_coords = dw .__get_stratified_coords2D__
91
96
rand_float = dw .__rand_float_coords2D__ (box_size )
92
97
elif dims == 3 :
93
- box_size = np .round (np .sqrt (100 / perc_pix )).astype (np .int )
98
+ box_size = np .round (np .sqrt (100 / perc_pix )).astype (np .int )
94
99
get_stratified_coords = dw .__get_stratified_coords3D__
95
100
rand_float = dw .__rand_float_coords3D__ (box_size )
96
101
@@ -99,7 +104,7 @@ def manipulate_val_data(X_val, Y_val, perc_pix=0.198, shape=(64, 64), value_mani
99
104
Y_val *= 0
100
105
for j in tqdm (range (X_val .shape [0 ]), desc = 'Preparing validation data: ' ):
101
106
coords = get_stratified_coords (rand_float , box_size = box_size ,
102
- shape = np .array (X_val .shape )[1 :- 1 ])
107
+ shape = np .array (X_val .shape )[1 :- 1 ])
103
108
for c in range (n_chan ):
104
109
indexing = (j ,) + coords + (c ,)
105
110
indexing_mask = (j ,) + coords + (c + n_chan ,)
@@ -112,17 +117,52 @@ def manipulate_val_data(X_val, Y_val, perc_pix=0.198, shape=(64, 64), value_mani
112
117
113
118
114
119
def autocorrelation (x ):
115
- """
116
- nD autocorrelation
117
- remove mean per-patch (not global GT)
118
- normalize stddev to 1
119
- value at zero shift normalized to 1...
120
- """
121
- x = (x - np .mean (x ))/ np .std (x )
122
- x = np .fft .fftn (x )
123
- x = np .abs (x )** 2
124
- x = np .fft .ifftn (x ).real
125
- x = x / x .flat [0 ]
126
- x = np .fft .fftshift (x )
127
- return x
128
-
120
+ """
121
+ nD autocorrelation
122
+ remove mean per-patch (not global GT)
123
+ normalize stddev to 1
124
+ value at zero shift normalized to 1...
125
+ """
126
+ x = (x - np .mean (x )) / np .std (x )
127
+ x = np .fft .fftn (x )
128
+ x = np .abs (x ) ** 2
129
+ x = np .fft .ifftn (x ).real
130
+ x = x / x .flat [0 ]
131
+ x = np .fft .fftshift (x )
132
+ return x
133
+
134
+
135
+ def tta_forward (x ):
136
+ """
137
+ Augments x 8-fold: all 90 deg rotations plus lr flip of the four rotated versions.
138
+
139
+ Parameters
140
+ ----------
141
+ x: data to augment
142
+
143
+ Returns
144
+ -------
145
+ Stack of augmented x.
146
+ """
147
+ x_aug = [x , np .rot90 (x , 1 ), np .rot90 (x , 2 ), np .rot90 (x , 3 )]
148
+ x_aug_flip = x_aug .copy ()
149
+ for x_ in x_aug :
150
+ x_aug_flip .append (np .fliplr (x_ ))
151
+ return x_aug_flip
152
+
153
+
154
+ def tta_backward (x_aug ):
155
+ """
156
+ Inverts `tta_forward` and averages the 8 images.
157
+
158
+ Parameters
159
+ ----------
160
+ x_aug: stack of 8-fold augmented images.
161
+
162
+ Returns
163
+ -------
164
+ average of de-augmented x_aug.
165
+ """
166
+ x_deaug = [x_aug [0 ], np .rot90 (x_aug [1 ], - 1 ), np .rot90 (x_aug [2 ], - 2 ), np .rot90 (x_aug [3 ], - 3 ),
167
+ np .fliplr (x_aug [4 ]), np .rot90 (np .fliplr (x_aug [5 ]), - 1 ), np .rot90 (np .fliplr (x_aug [6 ]), - 2 ), np .rot90 (np .fliplr (x_aug [7 ]), - 3 )]
168
+ return np .mean (x_deaug , 0 )
0 commit comments