|
a |
|
b/notebooks/Check_Augmentations.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": null, |
|
|
6 |
"metadata": {}, |
|
|
7 |
"outputs": [], |
|
|
8 |
"source": [ |
|
|
9 |
"import numpy as np\n", |
|
|
10 |
"\n", |
|
|
11 |
"import sys\n", |
|
|
12 |
"sys.path.append('..')\n", |
|
|
13 |
"\n", |
|
|
14 |
"import nibabel as nib\n", |
|
|
15 |
"from matplotlib import pyplot as plt\n", |
|
|
16 |
"\n", |
|
|
17 |
"from fetal_net.augment import augment_data" |
|
|
18 |
] |
|
|
19 |
}, |
|
|
20 |
{ |
|
|
21 |
"cell_type": "code", |
|
|
22 |
"execution_count": null, |
|
|
23 |
"metadata": {}, |
|
|
24 |
"outputs": [], |
|
|
25 |
"source": [ |
|
|
26 |
"def slice_it(arr, inds):\n", |
|
|
27 |
" return arr[inds[0][0]:inds[0][1], inds[1][0]: inds[1][1], inds[2][0]:inds[2][1]]" |
|
|
28 |
] |
|
|
29 |
}, |
|
|
30 |
{ |
|
|
31 |
"cell_type": "code", |
|
|
32 |
"execution_count": null, |
|
|
33 |
"metadata": {}, |
|
|
34 |
"outputs": [], |
|
|
35 |
"source": [ |
|
|
36 |
"vol = nib.load('../../Datasets/fetus_window_1_99/255/volume.nii')\n", |
|
|
37 |
"mask = nib.load('../../Datasets/fetus_window_1_99/255/truth.nii')\n", |
|
|
38 |
"vol.shape" |
|
|
39 |
] |
|
|
40 |
}, |
|
|
41 |
{ |
|
|
42 |
"cell_type": "code", |
|
|
43 |
"execution_count": null, |
|
|
44 |
"metadata": {}, |
|
|
45 |
"outputs": [], |
|
|
46 |
"source": [ |
|
|
47 |
"patch_corner = [70, 70, 30]\n", |
|
|
48 |
"patch_shape = [128,128,5]\n", |
|
|
49 |
"data_range = [(start, start + size) for start, size in zip(patch_corner, patch_shape)]\n", |
|
|
50 |
"data_range" |
|
|
51 |
] |
|
|
52 |
}, |
|
|
53 |
{ |
|
|
54 |
"cell_type": "code", |
|
|
55 |
"execution_count": null, |
|
|
56 |
"metadata": {}, |
|
|
57 |
"outputs": [], |
|
|
58 |
"source": [ |
|
|
59 |
"truth_index = 2\n", |
|
|
60 |
"truth_size = 1\n", |
|
|
61 |
"truth_range = data_range[:2] + [(patch_corner[2] + truth_index,\n", |
|
|
62 |
" patch_corner[2] + truth_index + truth_size)]\n", |
|
|
63 |
"truth_range" |
|
|
64 |
] |
|
|
65 |
}, |
|
|
66 |
{ |
|
|
67 |
"cell_type": "markdown", |
|
|
68 |
"metadata": {}, |
|
|
69 |
"source": [ |
|
|
70 |
"# Gaussian Filter" |
|
|
71 |
] |
|
|
72 |
}, |
|
|
73 |
{ |
|
|
74 |
"cell_type": "code", |
|
|
75 |
"execution_count": null, |
|
|
76 |
"metadata": {}, |
|
|
77 |
"outputs": [], |
|
|
78 |
"source": [ |
|
|
79 |
"data = vol.get_fdata()\n", |
|
|
80 |
"truth = mask.get_fdata()\n", |
|
|
81 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
82 |
" gaussian_filter={\n", |
|
|
83 |
" 'max_sigma': 1.5,\n", |
|
|
84 |
" 'prob': 1,\n", |
|
|
85 |
" }, poisson_noise=1)\n", |
|
|
86 |
"plt.figure(figsize = (16,14))\n", |
|
|
87 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')" |
|
|
88 |
] |
|
|
89 |
}, |
|
|
90 |
{ |
|
|
91 |
"cell_type": "markdown", |
|
|
92 |
"metadata": {}, |
|
|
93 |
"source": [ |
|
|
94 |
"# Shot Noise" |
|
|
95 |
] |
|
|
96 |
}, |
|
|
97 |
{ |
|
|
98 |
"cell_type": "code", |
|
|
99 |
"execution_count": null, |
|
|
100 |
"metadata": {}, |
|
|
101 |
"outputs": [], |
|
|
102 |
"source": [ |
|
|
103 |
"data = vol.get_fdata()\n", |
|
|
104 |
"truth = mask.get_fdata()\n", |
|
|
105 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
106 |
" poisson_noise=0.5)\n", |
|
|
107 |
"plt.figure(figsize = (16,14))\n", |
|
|
108 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')" |
|
|
109 |
] |
|
|
110 |
}, |
|
|
111 |
{ |
|
|
112 |
"cell_type": "markdown", |
|
|
113 |
"metadata": {}, |
|
|
114 |
"source": [ |
|
|
115 |
"# contrast deviation" |
|
|
116 |
] |
|
|
117 |
}, |
|
|
118 |
{ |
|
|
119 |
"cell_type": "code", |
|
|
120 |
"execution_count": null, |
|
|
121 |
"metadata": {}, |
|
|
122 |
"outputs": [], |
|
|
123 |
"source": [ |
|
|
124 |
"data = vol.get_fdata()\n", |
|
|
125 |
"truth = mask.get_fdata()\n", |
|
|
126 |
"print(data.min(), data.max())\n", |
|
|
127 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
128 |
" contrast_deviation={'min_factor': 0.2, 'max_factor': 0.8})\n", |
|
|
129 |
"plt.figure(figsize = (16,14))\n", |
|
|
130 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')" |
|
|
131 |
] |
|
|
132 |
}, |
|
|
133 |
{ |
|
|
134 |
"cell_type": "markdown", |
|
|
135 |
"metadata": {}, |
|
|
136 |
"source": [ |
|
|
137 |
"# intensity_multiplication_range" |
|
|
138 |
] |
|
|
139 |
}, |
|
|
140 |
{ |
|
|
141 |
"cell_type": "code", |
|
|
142 |
"execution_count": null, |
|
|
143 |
"metadata": {}, |
|
|
144 |
"outputs": [], |
|
|
145 |
"source": [ |
|
|
146 |
"data = vol.get_fdata()\n", |
|
|
147 |
"truth = mask.get_fdata()\n", |
|
|
148 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
149 |
" intensity_multiplication_range=[0.8, 1.2])\n", |
|
|
150 |
"plt.figure(figsize = (16,14))\n", |
|
|
151 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')" |
|
|
152 |
] |
|
|
153 |
}, |
|
|
154 |
{ |
|
|
155 |
"cell_type": "markdown", |
|
|
156 |
"metadata": {}, |
|
|
157 |
"source": [ |
|
|
158 |
"# piecewise_affine" |
|
|
159 |
] |
|
|
160 |
}, |
|
|
161 |
{ |
|
|
162 |
"cell_type": "code", |
|
|
163 |
"execution_count": null, |
|
|
164 |
"metadata": {}, |
|
|
165 |
"outputs": [], |
|
|
166 |
"source": [ |
|
|
167 |
"data = vol.get_fdata()\n", |
|
|
168 |
"truth = mask.get_fdata()\n", |
|
|
169 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
170 |
" piecewise_affine={'scale': 0.5})\n", |
|
|
171 |
"plt.figure(figsize = (16,14))\n", |
|
|
172 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')" |
|
|
173 |
] |
|
|
174 |
}, |
|
|
175 |
{ |
|
|
176 |
"cell_type": "markdown", |
|
|
177 |
"metadata": {}, |
|
|
178 |
"source": [ |
|
|
179 |
"# elastic_transform" |
|
|
180 |
] |
|
|
181 |
}, |
|
|
182 |
{ |
|
|
183 |
"cell_type": "code", |
|
|
184 |
"execution_count": null, |
|
|
185 |
"metadata": {}, |
|
|
186 |
"outputs": [], |
|
|
187 |
"source": [ |
|
|
188 |
"data = vol.get_fdata()\n", |
|
|
189 |
"truth = mask.get_fdata()\n", |
|
|
190 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
191 |
" elastic_transform={'alpha': 5, 'sigma': 1})\n", |
|
|
192 |
"plt.figure(figsize = (16,14))\n", |
|
|
193 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')" |
|
|
194 |
] |
|
|
195 |
}, |
|
|
196 |
{ |
|
|
197 |
"cell_type": "markdown", |
|
|
198 |
"metadata": {}, |
|
|
199 |
"source": [ |
|
|
200 |
"# scale_deviation" |
|
|
201 |
] |
|
|
202 |
}, |
|
|
203 |
{ |
|
|
204 |
"cell_type": "code", |
|
|
205 |
"execution_count": null, |
|
|
206 |
"metadata": {}, |
|
|
207 |
"outputs": [], |
|
|
208 |
"source": [ |
|
|
209 |
"data = vol.get_fdata()\n", |
|
|
210 |
"truth = mask.get_fdata()\n", |
|
|
211 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
212 |
" scale_deviation=[0.1, 0.1, 0.0])\n", |
|
|
213 |
"plt.figure(figsize = (16,14))\n", |
|
|
214 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')" |
|
|
215 |
] |
|
|
216 |
}, |
|
|
217 |
{ |
|
|
218 |
"cell_type": "markdown", |
|
|
219 |
"metadata": {}, |
|
|
220 |
"source": [ |
|
|
221 |
"# rotate" |
|
|
222 |
] |
|
|
223 |
}, |
|
|
224 |
{ |
|
|
225 |
"cell_type": "code", |
|
|
226 |
"execution_count": null, |
|
|
227 |
"metadata": {}, |
|
|
228 |
"outputs": [], |
|
|
229 |
"source": [ |
|
|
230 |
"data = vol.get_fdata()\n", |
|
|
231 |
"truth = mask.get_fdata()\n", |
|
|
232 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
233 |
" rotate_deviation=[0, 0, 1800])\n", |
|
|
234 |
"plt.figure(figsize = (16,14))\n", |
|
|
235 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')\n", |
|
|
236 |
"\n", |
|
|
237 |
"#print(slice_it(truth, truth_range).shape)\n", |
|
|
238 |
"#print(truth2.shape)\n", |
|
|
239 |
"plt.figure(figsize = (16,14))\n", |
|
|
240 |
"plt.imshow(np.c_[slice_it(truth, truth_range)[..., 0], truth2[..., 0]], cmap='gray')" |
|
|
241 |
] |
|
|
242 |
}, |
|
|
243 |
{ |
|
|
244 |
"cell_type": "markdown", |
|
|
245 |
"metadata": {}, |
|
|
246 |
"source": [ |
|
|
247 |
"# Flip" |
|
|
248 |
] |
|
|
249 |
}, |
|
|
250 |
{ |
|
|
251 |
"cell_type": "code", |
|
|
252 |
"execution_count": null, |
|
|
253 |
"metadata": {}, |
|
|
254 |
"outputs": [], |
|
|
255 |
"source": [ |
|
|
256 |
"data = vol.get_fdata()\n", |
|
|
257 |
"truth = mask.get_fdata()\n", |
|
|
258 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
259 |
" flip=[0, 0, 1])\n", |
|
|
260 |
"plt.figure(figsize = (16,14))\n", |
|
|
261 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')\n", |
|
|
262 |
"\n", |
|
|
263 |
"#print(slice_it(truth, truth_range).shape)\n", |
|
|
264 |
"#print(truth2.shape)\n", |
|
|
265 |
"plt.figure(figsize = (16,14))\n", |
|
|
266 |
"plt.imshow(np.c_[slice_it(truth, truth_range)[..., 0], truth2[..., 0]], cmap='gray')" |
|
|
267 |
] |
|
|
268 |
}, |
|
|
269 |
{ |
|
|
270 |
"cell_type": "markdown", |
|
|
271 |
"metadata": {}, |
|
|
272 |
"source": [ |
|
|
273 |
"# Translate" |
|
|
274 |
] |
|
|
275 |
}, |
|
|
276 |
{ |
|
|
277 |
"cell_type": "code", |
|
|
278 |
"execution_count": null, |
|
|
279 |
"metadata": {}, |
|
|
280 |
"outputs": [], |
|
|
281 |
"source": [ |
|
|
282 |
"data = vol.get_fdata()\n", |
|
|
283 |
"truth = mask.get_fdata()\n", |
|
|
284 |
"data2, truth2, _ = augment_data(data, truth, data.min(), data.max(), data_range=data_range, truth_range=truth_range,\n", |
|
|
285 |
" translate_deviation=[0, 0, 10])\n", |
|
|
286 |
"plt.figure(figsize = (16,14))\n", |
|
|
287 |
"plt.imshow(np.c_[slice_it(data, data_range)[..., 2], data2[..., 2]], cmap='gray')\n", |
|
|
288 |
"\n", |
|
|
289 |
"#print(slice_it(truth, truth_range).shape)\n", |
|
|
290 |
"#print(truth2.shape)\n", |
|
|
291 |
"plt.figure(figsize = (16,14))\n", |
|
|
292 |
"plt.imshow(np.c_[slice_it(truth, truth_range)[..., 0], truth2[..., 0]], cmap='gray')" |
|
|
293 |
] |
|
|
294 |
}, |
|
|
295 |
{ |
|
|
296 |
"cell_type": "code", |
|
|
297 |
"execution_count": null, |
|
|
298 |
"metadata": {}, |
|
|
299 |
"outputs": [], |
|
|
300 |
"source": [ |
|
|
301 |
"z_trans = 9\n", |
|
|
302 |
"\n", |
|
|
303 |
"data_range2 = data_range.copy()\n", |
|
|
304 |
"data_range2[-1] = np.add(data_range2[-1], z_trans)\n", |
|
|
305 |
"\n", |
|
|
306 |
"plt.figure(figsize = (16,14))\n", |
|
|
307 |
"plt.imshow(np.c_[slice_it(data, data_range2)[..., 2], data2[..., 2]], cmap='gray')\n", |
|
|
308 |
"\n", |
|
|
309 |
"truth_range2 = truth_range\n", |
|
|
310 |
"truth_range2[-1] = np.add(truth_range2[-1], z_trans)\n", |
|
|
311 |
"plt.figure(figsize = (16,14))\n", |
|
|
312 |
"plt.imshow(np.c_[slice_it(truth, truth_range2)[..., 0], truth2[..., 0]], cmap='gray')" |
|
|
313 |
] |
|
|
314 |
}, |
|
|
315 |
{ |
|
|
316 |
"cell_type": "code", |
|
|
317 |
"execution_count": null, |
|
|
318 |
"metadata": {}, |
|
|
319 |
"outputs": [], |
|
|
320 |
"source": [] |
|
|
321 |
} |
|
|
322 |
], |
|
|
323 |
"metadata": { |
|
|
324 |
"kernelspec": { |
|
|
325 |
"display_name": "Python 3", |
|
|
326 |
"language": "python", |
|
|
327 |
"name": "python3" |
|
|
328 |
}, |
|
|
329 |
"language_info": { |
|
|
330 |
"codemirror_mode": { |
|
|
331 |
"name": "ipython", |
|
|
332 |
"version": 3 |
|
|
333 |
}, |
|
|
334 |
"file_extension": ".py", |
|
|
335 |
"mimetype": "text/x-python", |
|
|
336 |
"name": "python", |
|
|
337 |
"nbconvert_exporter": "python", |
|
|
338 |
"pygments_lexer": "ipython3", |
|
|
339 |
"version": "3.6.6" |
|
|
340 |
} |
|
|
341 |
}, |
|
|
342 |
"nbformat": 4, |
|
|
343 |
"nbformat_minor": 2 |
|
|
344 |
} |