-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaugmentation3DUtil.py
270 lines (187 loc) · 8.76 KB
/
augmentation3DUtil.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import SimpleITK as sitk
import numpy as np
import matplotlib.pyplot as plt
from enum import Enum
import random
class Transforms(Enum):
TRANSLATE = 0
ROTATE2D = 1
SHEAR = 2
FLIPHORIZONTAL = 3
FLIPVERTICAL = 4
class Augmentation3DUtil(object):
DIMENSION = 3
def __init__(self,imgs,masks=None):
"""
imgs : input should be list of sitk images (If masks are being provided the images must have same parameters such as spacing, origin, direction)
masks : as a list of sitk images of binary segmentation masks (if binary masks are involved)
A class implemented for the purpose of augmenting 3D volumes.
This class uses transformations in SimpleITK library to perform augmentations.
Currently rotation, translation and shear implemented
To use, define an object of this class and add transformation.
Note : Use the Transforms Enum class to define the transformation.
Ex.
au = Augmentation3DUtil(imgs,masks=masks)
au.add(Transforms.SHEAR,probability = 0.75, magnitude = (0.1,0.1))
au.add(Transforms.TRANSLATE,probability = 0.75, offset = (2,2,0))
au.add(Transforms.ROTATE2D,probability = 0.75, degrees = 15)
ret = au.process(10)
The above code produces 10 augmented samples by randomly combining
all the transformations defined with respect to their probabilities defined.
"""
self.transforms = []
if masks is not None:
for i in range(len(masks)):
mask = masks[i]
mask.SetDirection(imgs[0].GetDirection())
mask.SetOrigin(imgs[0].GetOrigin())
mask.SetSpacing(imgs[0].GetSpacing())
mask = self._flipimage(mask)
masks[i] = mask
self.masks = masks
for i in range(len(imgs)):
imgs[i] = self._flipimage(imgs[i])
imgs[i] = sitk.Cast(imgs[i],sitk.sitkFloat64)
self.img = imgs[0]
self.imgs = imgs
self.reference_image = None
self._define_reference_image()
def _define_reference_image(self):
img = self.imgs[0]
size = img.GetSize()
dimension = Augmentation3DUtil.DIMENSION
origin = img.GetOrigin()
direction = img.GetDirection()
spacing = img.GetSpacing()
reference_image = sitk.Image(size, img.GetPixelIDValue())
reference_image.SetOrigin(origin)
reference_image.SetSpacing(spacing)
reference_image.SetDirection(direction)
self.reference_image = reference_image
def _translate(self,kwargs):
offset = kwargs["offset"]
dimension = Augmentation3DUtil.DIMENSION
transform = sitk.TranslationTransform(dimension)
transform.SetOffset(offset)
return transform
def _affine_transform(self,transformationmatrix):
dimension = Augmentation3DUtil.DIMENSION
img = self.img
reference_image = self.reference_image
img_center = np.array(img.GetSize())/2.0
reference_center = np.array(reference_image.GetSize())/2.0
transform = sitk.AffineTransform(dimension)
transform.SetMatrix(img.GetDirection())
transform.SetTranslation(np.array(img.GetOrigin()) - np.array(reference_image.GetOrigin()))
centering_transform = sitk.TranslationTransform(dimension)
img_center = np.array(img.TransformContinuousIndexToPhysicalPoint(img_center))
reference_center = np.array(reference_image.TransformContinuousIndexToPhysicalPoint(reference_center))
centering_transform.SetOffset(np.array(transform.GetInverse().TransformPoint(img_center) - reference_center))
centered_transform = sitk.Transform(transform)
centered_transform.AddTransform(centering_transform)
flipped_transform = sitk.AffineTransform(dimension)
flipped_transform.SetCenter(reference_center)
flipped_transform.SetMatrix(transformationmatrix.ravel())
centered_transform.AddTransform(flipped_transform)
return centered_transform
def _flipHorizontal(self):
tranformationmatrix = np.array([-1,0,0,0,1,0,0,0,1]).astype(float)
return self._affine_transform(tranformationmatrix)
def _flipVertical(self):
tranformationmatrix = np.array([1,0,0,0,-1,0,0,0,1]).astype(float)
return self._affine_transform(tranformationmatrix)
def _rotate2D(self,kwargs):
degrees = kwargs["degrees"]
dimension = Augmentation3DUtil.DIMENSION
transform = sitk.AffineTransform(dimension)
radians = -np.pi * degrees / 180.
rotation = np.eye(dimension)
_rotation = np.array([[np.cos(radians), -np.sin(radians)],[np.sin(radians), np.cos(radians)]])
rotation[:2,:2] = _rotation
return self._affine_transform(rotation)
def _shear(self,kwargs):
magnitude = kwargs["magnitude"]
dimension = Augmentation3DUtil.DIMENSION
transform = sitk.AffineTransform(dimension)
new_transform = sitk.AffineTransform(transform)
matrix = np.array(transform.GetMatrix()).reshape((dimension,dimension))
matrix[0,1] = -magnitude[0]
matrix[1,0] = -magnitude[1]
new_transform.SetMatrix(matrix.ravel())
return new_transform
def _resample(self, img, transform, interpolator):
# Output image Origin, Spacing, Size, Direction are taken from the reference
# image in this call to Resample
reference_image = self.reference_image
default_value = 0
origin = reference_image.GetOrigin()
new_origin = transform.TransformPoint(origin)
resampled = sitk.Resample(img, reference_image, transform,
interpolator, default_value)
resampled.SetOrigin(new_origin)
resampled.SetSpacing(reference_image.GetSpacing())
resampled.SetDirection(reference_image.GetDirection())
return resampled
def _getTransform(self,transform):
if transform[0] == Transforms.TRANSLATE:
return self._translate(transform[2])
elif transform[0] == Transforms.ROTATE2D:
return self._rotate2D(transform[2])
elif transform[0] == Transforms.SHEAR:
return self._shear(transform[2])
elif transform[0] == Transforms.FLIPHORIZONTAL:
return self._flipHorizontal()
elif transform[0] == Transforms.FLIPVERTICAL:
return self._flipVertical()
def _composite(self,transforms):
dimension = Augmentation3DUtil.DIMENSION
composite = sitk.Transform(dimension, sitk.sitkComposite)
for transform in transforms:
composite.AddTransform(self._getTransform(transform))
imgs = self.imgs
masks = self.masks
augmented_mask = None
if masks is not None:
augmented_masks = []
for mask in masks:
augmented_mask = self._resample(mask,composite,interpolator=sitk.sitkNearestNeighbor)
augmented_masks.append(augmented_mask)
augmented_imgs = []
for img in imgs:
augmented = self._resample(img,composite,interpolator=sitk.sitkLinear)
augmented_imgs.append(augmented)
if masks is not None:
return (augmented_imgs,augmented_masks)
else:
return (augmented_imgs,None)
def add(self,transformname,probability,**kwargs):
self.transforms.append((transformname,probability,kwargs))
def process(self,samples):
imgs = self.imgs
masks = self.masks
if samples is None:
return (imgs,masks),None
augmentations = []
sampling = np.zeros((len(self.transforms),samples))
for i in range(len(self.transforms)):
per = self.transforms[i][1]
ind = tuple(random.sample(range(samples),int(per*samples)))
np.put(sampling[i],ind,1)
for i in range(samples):
if not sampling[:,i].sum() == 0:
ind = tuple(np.where(sampling[:,i]== 1)[0])
subtransforms = [self.transforms[i] for i in range(len(self.transforms)) if i in ind]
augmentations.append(self._composite(subtransforms))
return (imgs,masks),augmentations
def _flipimage(self,img):
arr = sitk.GetArrayFromImage(img)
direction = np.array(img.GetDirection()).reshape(3,3)
if direction[0].sum() == -1:
arr = np.flip(arr,2)
if direction[1].sum() == -1:
arr = np.flip(arr,1)
fimg = sitk.GetImageFromArray(arr)
fimg.SetDirection(np.eye(3).ravel())
fimg.SetOrigin(img.GetOrigin())
fimg.SetSpacing(img.GetSpacing())
return fimg