forked from lozuwa/impy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplyAugmentation.py
executable file
·247 lines (238 loc) · 9.44 KB
/
ApplyAugmentation.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
"""
Author: Rodrigo Loza
Email: [email protected]
Description: Methods that apply a type of data augmentation.
"""
import numpy as np
try:
from .BoundingBoxAugmenters import *
except:
from BoundingBoxAugmenters import *
try:
from .ColorAugmenters import *
except:
from ColorAugmenters import *
try:
from .GeometricAugmenters import *
except:
from GeometricAugmenters import *
bndboxAugmenter = BoundingBoxAugmenters()
colorAugmenter = ColorAugmenters()
geometricAugmenter = GeometricAugmenters()
def applyGeometricAugmentation(frame = None, augmentationType = None, parameters = None):
"""
Applies a geometric augmentation making sure all the parameters exist or are
correct.
Args:
augmentationType: A string that contains a type of augmentation.
parameters: A hashmap that contains parameters for the respective type
of augmentation.
Returns:
A tensor that contains a frame with the respective transformation.
"""
# Logic
if (augmentationType == "scale"):
# Apply scaling
if (not ("size" in parameters)):
raise Exception("ERROR: Scale requires parameter size.")
if (not ("interpolationMethod" in parameters)):
print("WARNING: Interpolation method for scale will be set to default value.")
frame = geometricAugmenter.scale(frame = frame,
size = parameters["size"],
interpolationMethod = parameters["interpolationMethod"])
elif (augmentationType == "crop"):
# Apply crop
if (not ("size" in parameters)):
parameters["size"] = None
print("WARNING: Size for crop will be set to default value.")
frame = geometricAugmenter.crop(frame = frame,
size = parameters["size"])
elif (augmentationType == "translate"):
# Apply pad
if (not ("offset" in parameters)):
raise Exception("Pad requires parameter offset.")
bndboxes = geometricAugmenter.translate(frame = frame,
offset = parameters["offset"])
elif (augmentationType == "jitterBoxes"):
# Apply jitter boxes
if (not ("size" in parameters)):
raise Exception("JitterBoxes requires parameter size.")
if (not ("quantity" in parameters)):
parameters["quantity"] = 10
print("WARNING: Quantity for jitter boxes will be set to its default value.")
if (not ("color" in parameters)):
parameters["color"] = [255,255,255]
print("WARNING: Color for jitter boxes will be set to its default value.")
frame = geometricAugmenter.jitterBoxes(frame = frame,
size = parameters["size"],
quantity = parameters["quantity"],
color = parameters["color"])
elif (augmentationType == "horizontalFlip"):
# Apply horizontal flip
frame = geometricAugmenter.horizontalFlip(frame = frame)
elif (augmentationType == "verticalFlip"):
# Apply vertical flip
frame = geometricAugmenter.verticalFlip(frame = frame)
elif (augmentationType == "rotation"):
# Apply rotation
if (not ("theta" in parameters)):
theta = None
#raise Exception("ERROR: Rotation requires parameter theta.")
else:
theta = parameters["theta"]
frame = geometricAugmenter.rotation(frame = frame,
bndbox = [0, 0, frame.shape[1], frame.shape[0]],
theta = theta)
return frame
def applyColorAugmentation(frame = None, augmentationType = None, parameters = None):
"""
Applies a color augmentation making sure all the parameters exist or are
correct.
Args:
augmentationType: A string that contains a type of augmentation.
parameters: A hashmap that contains parameters for the respective type
of augmentation.
Returns:
A tensor that contains a frame with the respective transformation.
"""
# Logic.
if (augmentationType == "invertColor"):
if (not ("CSpace" in parameters)):
parameters["CSpace"] = None
frame = colorAugmenter.invertColor(frame = frame, \
CSpace = parameters["CSpace"])
elif (augmentationType == "histogramEqualization"):
if (not ("equalizationType" in parameters)):
parameters["equalizationType"] = None
frame = colorAugmenter.histogramEqualization(frame = frame, \
equalizationType = parameters["equalizationType"])
elif (augmentationType == "changeBrightness"):
if (not ("coefficient" in parameters)):
raise AttributeError("coefficient for changeBrightness must be specified.")
frame = colorAugmenter.changeBrightness(frame = frame, \
coefficient = parameters["coefficient"])
elif (augmentationType == "sharpening"):
if (not ("weight" in parameters)):
parameters["weight"] = None
frame = colorAugmenter.sharpening(frame = frame, weight = parameters["weight"])
elif (augmentationType == "addGaussianNoise"):
if (not ("coefficient" in parameters)):
parameters["coefficient"] = None
frame = colorAugmenter.addGaussianNoise(frame = frame, \
coefficient = parameters["coefficient"])
elif (augmentationType == "gaussianBlur"):
if (not ("sigma" in parameters)):
parameters["sigma"] = None
if (not ("kernelSize" in parameters)):
parameters["kernelSize"] = None
frame = colorAugmenter.gaussianBlur(frame = frame, \
kernelSize = parameters["kernelSize"], sigma = parameters["sigma"])
elif (augmentationType == "averageBlur"):
if (not ("kernelSize" in parameters)):
parameters["kernelSize"] = None
frame = colorAugmenter.averageBlur(frame = frame, \
kernelSize = parameters["kernelSize"])
elif (augmentationType == "medianBlur"):
if (not ("coefficient" in parameters)):
parameters["coefficient"] = None
frame = colorAugmenter.medianBlur(frame = frame, \
coefficient = parameters["coefficient"])
elif (augmentationType == "bilateralBlur"):
if (not ("d" in parameters)):
parameters["d"] = None
if (not ("sigmaColor" in parameters)):
parameters["sigmaColor"] = None
if (not ("sigmaSpace" in parameters)):
parameters["sigmaSpace"] = None
frame = colorAugmenter.bilateralBlur(frame = frame, d = parameters["d"], \
sigmaColor = parameters["sigmaColor"], sigmaSpace = parameters["sigmaSpace"])
elif (augmentationType == "shiftColors"):
frame = colorAugmenter.shiftColors(frame = frame)
elif (augmentationType == "fancyPCA"):
frame = colorAugmenter.fancyPCA(frame = frame)
else:
raise Exception("Color augmentation type not supported: {}."\
.format(augmentationType))
# Return result
return frame
def applyBoundingBoxAugmentation(frame = None, boundingBoxes = None, augmentationType = None, parameters = None):
"""
Applies a bounding box augmentation making sure all the parameters exist or are
correct.
Args:
boundingBoxes: A list of lists of integers that contains coordinates.
augmentationType: A string that contains a type of augmentation.
parameters: A hashmap that contains parameters for the respective type
of augmentation.
Returns:
A tensor that contains a frame with the respective transformation.
"""
# Local variables.
bndboxes = boundingBoxes
# Logic.
if (augmentationType == "scale"):
# Apply scaling.
if (not ("size" in parameters)):
raise Exception("Scale requires parameter size.")
if (not ("zoom" in parameters)):
parameters["zoom"] = None
if (not ("interpolationMethod" in parameters)):
parameters["interpolationMethod"] = None
frame, bndboxes = bndboxAugmenter.scale(frame = frame,
boundingBoxes = boundingBoxes,
size = parameters["size"],
zoom = parameters["zoom"],
interpolationMethod = parameters["interpolationMethod"])
elif (augmentationType == "crop"):
# Apply crop.
if (not ("size" in parameters)):
parameters["size"] = None
bndboxes = bndboxAugmenter.crop(boundingBoxes = boundingBoxes,
size = parameters["size"])
elif (augmentationType == "pad"):
# Apply pad.
if (not ("size" in parameters)):
raise Exception("Pad requires parameter size.")
bndboxes = bndboxAugmenter.pad(boundingBoxes = boundingBoxes,
frameHeight = frame.shape[0],
frameWidth = frame.shape[1],
size = parameters["size"])
elif (augmentationType == "jitterBoxes"):
# Apply jitter boxes.
if (not ("size" in parameters)):
raise Exception("JitterBoxes requires parameter size.")
if (not ("quantity" in parameters)):
parameters["quantity"] = None
frame = bndboxAugmenter.jitterBoxes(frame = frame,
boundingBoxes = boundingBoxes,
size = parameters["size"],
quantity = parameters["quantity"])
elif (augmentationType == "horizontalFlip"):
# Apply horizontal flip.
frame = bndboxAugmenter.horizontalFlip(frame = frame,
boundingBoxes = boundingBoxes)
elif (augmentationType == "verticalFlip"):
# Apply vertical flip.
frame = bndboxAugmenter.verticalFlip(frame = frame,
boundingBoxes = boundingBoxes)
elif (augmentationType == "rotation"):
# Apply rotation.
if (not ("theta" in parameters)):
theta = None
#raise Exception("ERROR: Rotation requires parameter theta.")
else:
theta = parameters["theta"]
frame = bndboxAugmenter.rotation(frame = frame,
boundingBoxes = boundingBoxes,
theta = theta)
elif (augmentationType == "dropout"):
# Apply dropout.
if (not ("size" in parameters)):
raise Exception("Dropout requires parameter size.")
if (not ("threshold" in parameters)):
parameters["threshold"] = None
frame = bndboxAugmenter.dropout(frame = frame,
boundingBoxes = boundingBoxes,
size = parameters["size"],
threshold = parameters["threshold"])
return frame, bndboxes