forked from conradry/copy-paste-aug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_paste.py
313 lines (262 loc) · 10.6 KB
/
copy_paste.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import os
import cv2
import random
import numpy as np
import albumentations as A
from copy import deepcopy
from skimage.filters import gaussian
def image_copy_paste(img, paste_img, alpha, blend=True, sigma=1):
if alpha is not None:
if blend:
alpha = gaussian(alpha, sigma=sigma, preserve_range=True)
img_dtype = img.dtype
alpha = alpha[..., None]
img = paste_img * alpha + img * (1 - alpha)
img = img.astype(img_dtype)
return img
def mask_copy_paste(mask, paste_mask, alpha):
raise NotImplementedError
def masks_copy_paste(masks, paste_masks, alpha):
if alpha is not None:
#eliminate pixels that will be pasted over
masks = [
np.logical_and(mask, np.logical_xor(mask, alpha)).astype(np.uint8) for mask in masks
]
masks.extend(paste_masks)
return masks
def extract_bboxes(masks):
bboxes = []
h, w = masks[0].shape
for mask in masks:
yindices = np.where(np.any(mask, axis=0))[0]
xindices = np.where(np.any(mask, axis=1))[0]
if yindices.shape[0]:
y1, y2 = yindices[[0, -1]]
x1, x2 = xindices[[0, -1]]
y2 += 1
x2 += 1
y1 /= w
y2 /= w
x1 /= h
x2 /= h
else:
y1, x1, y2, x2 = 0, 0, 0, 0
bboxes.append((y1, x1, y2, x2))
return bboxes
def bboxes_copy_paste(bboxes, paste_bboxes, masks, paste_masks, alpha, key):
if key == 'paste_bboxes':
return bboxes
elif paste_bboxes is not None:
masks = masks_copy_paste(masks, paste_masks=[], alpha=alpha)
adjusted_bboxes = extract_bboxes(masks)
#only keep the bounding boxes for objects listed in bboxes
mask_indices = [box[-1] for box in bboxes]
adjusted_bboxes = [adjusted_bboxes[idx] for idx in mask_indices]
#append bbox tails (classes, etc.)
adjusted_bboxes = [bbox + tail[4:] for bbox, tail in zip(adjusted_bboxes, bboxes)]
#adjust paste_bboxes mask indices to avoid overlap
if len(masks) > 0:
max_mask_index = len(masks)
else:
max_mask_index = 0
paste_mask_indices = [max_mask_index + ix for ix in range(len(paste_bboxes))]
paste_bboxes = [pbox[:-1] + (pmi,) for pbox, pmi in zip(paste_bboxes, paste_mask_indices)]
adjusted_paste_bboxes = extract_bboxes(paste_masks)
adjusted_paste_bboxes = [apbox + tail[4:] for apbox, tail in zip(adjusted_paste_bboxes, paste_bboxes)]
bboxes = adjusted_bboxes + adjusted_paste_bboxes
return bboxes
def keypoints_copy_paste(keypoints, paste_keypoints, alpha):
#remove occluded keypoints
if alpha is not None:
visible_keypoints = []
for kp in keypoints:
x, y = kp[:2]
tail = kp[2:]
if alpha[int(y), int(x)] == 0:
visible_keypoints.append(kp)
keypoints = visible_keypoints + paste_keypoints
return keypoints
class CopyPaste(A.DualTransform):
def __init__(
self,
blend=True,
sigma=3,
pct_objects_paste=0.1,
max_paste_objects=None,
p=0.5,
always_apply=False
):
super(CopyPaste, self).__init__(always_apply, p)
self.blend = blend
self.sigma = sigma
self.pct_objects_paste = pct_objects_paste
self.max_paste_objects = max_paste_objects
self.p = p
self.always_apply = always_apply
@staticmethod
def get_class_fullname():
return 'copypaste.CopyPaste'
@property
def targets_as_params(self):
return [
"masks",
"paste_image",
#"paste_mask",
"paste_masks",
"paste_bboxes",
#"paste_keypoints"
]
def get_params_dependent_on_targets(self, params):
image = params["paste_image"]
masks = None
if "paste_mask" in params:
#handle a single segmentation mask with
#multiple targets
#nothing for now.
raise NotImplementedError
elif "paste_masks" in params:
masks = params["paste_masks"]
assert(masks is not None), "Masks cannot be None!"
bboxes = params.get("paste_bboxes", None)
keypoints = params.get("paste_keypoints", None)
#number of objects: n_bboxes <= n_masks because of automatic removal
n_objects = len(bboxes) if bboxes is not None else len(masks)
#paste all objects if no restrictions
n_select = n_objects
if self.pct_objects_paste:
n_select = int(n_select * self.pct_objects_paste)
if self.max_paste_objects:
n_select = min(n_select, self.max_paste_objects)
#no objects condition
if n_select == 0:
return {
"param_masks": params["masks"],
"paste_img": None,
"alpha": None,
"paste_mask": None,
"paste_masks": None,
"paste_bboxes": None,
"paste_keypoints": None,
"objs_to_paste": []
}
#select objects
objs_to_paste = np.random.choice(
range(0, n_objects), size=n_select, replace=False
)
#take the bboxes
if bboxes:
bboxes = [bboxes[idx] for idx in objs_to_paste]
#the last label in bboxes is the index of corresponding mask
mask_indices = [bbox[-1] for bbox in bboxes]
#create alpha by combining all the objects into
#a single binary mask
masks = [masks[idx] for idx in mask_indices]
alpha = masks[0] > 0
for mask in masks[1:]:
alpha += mask > 0
return {
"param_masks": params["masks"],
"paste_img": image,
"alpha": alpha,
"paste_mask": None,
"paste_masks": masks,
"paste_bboxes": bboxes,
"paste_keypoints": keypoints
}
@property
def ignore_kwargs(self):
return [
"paste_image",
"paste_mask",
"paste_masks"
]
def apply_with_params(self, params, force_apply=False, **kwargs): # skipcq: PYL-W0613
if params is None:
return kwargs
params = self.update_params(params, **kwargs)
res = {}
for key, arg in kwargs.items():
if arg is not None and key not in self.ignore_kwargs:
target_function = self._get_target_function(key)
target_dependencies = {k: kwargs[k] for k in self.target_dependence.get(key, [])}
target_dependencies['key'] = key
res[key] = target_function(arg, **dict(params, **target_dependencies))
else:
res[key] = None
return res
def apply(self, img, paste_img, alpha, **params):
return image_copy_paste(
img, paste_img, alpha, blend=self.blend, sigma=self.sigma
)
def apply_to_mask(self, mask, paste_mask, alpha, **params):
return mask_copy_paste(mask, paste_mask, alpha)
def apply_to_masks(self, masks, paste_masks, alpha, **params):
return masks_copy_paste(masks, paste_masks, alpha)
def apply_to_bboxes(self, bboxes, paste_bboxes, param_masks, paste_masks, alpha, key, **params):
return bboxes_copy_paste(bboxes, paste_bboxes, param_masks, paste_masks, alpha, key)
def apply_to_keypoints(self, keypoints, paste_keypoints, alpha, **params):
raise NotImplementedError
#return keypoints_copy_paste(keypoints, paste_keypoints, alpha)
def get_transform_init_args_names(self):
return (
"blend",
"sigma",
"pct_objects_paste",
"max_paste_objects"
)
def copy_paste_class(dataset_class):
def _split_transforms(self):
split_index = None
for ix, tf in enumerate(list(self.transforms.transforms)):
if tf.get_class_fullname() == 'copypaste.CopyPaste':
split_index = ix
if split_index is not None:
tfs = list(self.transforms.transforms)
pre_copy = tfs[:split_index]
copy_paste = tfs[split_index]
post_copy = tfs[split_index+1:]
#replicate the other augmentation parameters
bbox_params = None
keypoint_params = None
paste_additional_targets = {}
if 'bboxes' in self.transforms.processors:
bbox_params = self.transforms.processors['bboxes'].params
paste_additional_targets['paste_bboxes'] = 'bboxes'
if self.transforms.processors['bboxes'].params.label_fields:
msg = "Copy-paste does not support bbox label_fields! "
msg += "Expected bbox format is (a, b, c, d, label_field)"
raise Exception(msg)
if 'keypoints' in self.transforms.processors:
keypoint_params = self.transforms.processors['keypoints'].params
paste_additional_targets['paste_keypoints'] = 'keypoints'
if keypoint_params.label_fields:
raise Exception('Copy-paste does not support keypoint label fields!')
if self.transforms.additional_targets:
raise Exception('Copy-paste does not support additional_targets!')
#recreate transforms
self.transforms = A.Compose(pre_copy, bbox_params, keypoint_params, additional_targets=None)
self.post_transforms = A.Compose(post_copy, bbox_params, keypoint_params, additional_targets=None)
self.copy_paste = A.Compose(
[copy_paste], bbox_params, keypoint_params, additional_targets=paste_additional_targets
)
else:
self.copy_paste = None
self.post_transforms = None
def __getitem__(self, idx):
#split transforms if it hasn't been done already
if not hasattr(self, 'post_transforms'):
self._split_transforms()
img_data = self.load_example(idx)
if self.copy_paste is not None:
paste_idx = random.randint(0, self.__len__() - 1)
paste_img_data = self.load_example(paste_idx)
for k in list(paste_img_data.keys()):
paste_img_data['paste_' + k] = paste_img_data[k]
del paste_img_data[k]
img_data = self.copy_paste(**img_data, **paste_img_data)
img_data = self.post_transforms(**img_data)
img_data['paste_index'] = paste_idx
return img_data
setattr(dataset_class, '_split_transforms', _split_transforms)
setattr(dataset_class, '__getitem__', __getitem__)
return dataset_class