-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounding_box_augmentation.py
280 lines (237 loc) · 9.67 KB
/
bounding_box_augmentation.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
from cv2 import cv2
from matplotlib import pyplot as plt
import albumentations as A
import glob
import os
import PIL
from numpy import isin
from custom_functional_transforms import *
import custom_functional_transforms
import torchvision.transforms.functional as my_f
from torchvision.transforms import Compose
from torchvision import transforms
from torch.utils.data import Dataset
from utils import *
from torchvision.utils import save_image
"""
one of the most common issues that data scientist are faced in real AI and Computer Vision
projects is data insufficiency. Deep Learning algorithms usually needs a lot of data to solve our problem
and data gathering is expensive, time-consuming, and in some cases impossible
therefore, data augmentation is an important task to generate massive data from a small dataset
in this project, you can apply many augmentation methods to your data to generate a massive and sufficient dataset
from your samll one. just change data_dir to your txt-jpg containing directory and run this script.
"""
os.system('cls')
data_dir = 'raccoon/raccoon'
classes = {
'raccoon': 0,
}
#! you can find more augmentation methods on PyTorch and Albumentation Docs
cfg = {
'format': 'yolo',
'target_size': (640, 640),
'bounding_box': [
# A.CenterCrop(100, 100),
# A.RandomCrop(100, 100),
CustomTransform(F.adjust_brightness, 3.0),
CustomTransform(F.adjust_contrast, 4.2),
CustomTransform(F.adjust_sharpness, 3.0),
# transforms.Grayscale(),
CustomTransform(my_f.adjust_saturation, 8),
CustomTransform(F.adjust_hue, -0.3),
CustomGaussianBlurTransform(None, 5),
],
'inner_bounding_box': [
transforms.RandomEqualize(1.0),
CustomTransform(F.adjust_brightness, 3.0),
CustomTransform(F.adjust_contrast, 4.2),
CustomTransform(F.adjust_sharpness, 3.0),
# transforms.Grayscale(),
CustomTransform(my_f.adjust_saturation, 8),
CustomTransform(F.adjust_hue, -0.3),
CustomGaussianBlurTransform(None, 5),
]
}
class MyDataset(Dataset):
def __init__(self, path, format):
assert format in ['yolo', 'coco', 'xml'], "format must be in yolo, coco or pascal"
self.path = path
self.format = format
self.data = []
#! inja bekhoon:
list = glob.glob(self.path+'*.jpg')
self.prefix = 'txt'
if self.format == 'yolo':
self.prefix = 'txt'
elif self.format == 'pascal':
self.prefix = 'xml'
elif self.format == 'coco':
self.prefix = 'json'
for i in list:
image = cv2.imread(i)
bbox = load_bbox(i[:-3]+self.prefix)
dict_ = {
'image': image,
'bbox': bbox
}
self.data.append(dict_)
def __len__(self):
return len(self.data)
def __getitem__(self, index):
return self.data[index]
class BoundingBoxAugmentation:
def __init__(self, cfg, save_dir='test_data'):
self.index = 0
self.format = cfg['format']
self.prefix = None
self.delimiter = None
if self.format == 'yolo':
self.prefix = '.txt'
self.delimiter = ' '
elif self.format == 'coco':
self.prefix = '.json'
#! soon
elif self.format == 'pascal':
self.prefix = '.xml'
#! soon
self.target_size = cfg['target_size']
self.cfg = cfg
self.transforms = self.create_transform()
self.save_dir = save_dir
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
def __call__(self, data):
index=0
img = data['image']
bbox = data['bbox']
for transform in self.transforms:
new_img = img
if isinstance(transform, transforms.Compose):
transformed_image = transform(new_img)
transformed_bboxs = bbox
else:
transformed = transform(image=new_img, bboxes=bbox)
transformed_image = transformed['image']
transformed_bboxs = transformed['bboxes']
image_save_path = self.save_dir + '/' + str(index) + str(self.index) + 'bbox' + '.jpg'
bbox_save_path = self.save_dir + '/' + str(index) + str(self.index) + 'bbox' + self.prefix
image_to_save = transforms.ToTensor()(transformed_image)
save_image(image_to_save, image_save_path)
with open(bbox_save_path, 'w') as f:
#! just implemented for yolo format because im currently use it
#! other formats will be added soon
for _, i in enumerate(transformed_bboxs):
vals = [str(val) for val in list(i[:-1])]
to_write = str(classes[i[-1]]) + self.delimiter + self.delimiter.join(vals) + '\n'
f.writelines(to_write)
index += 1
self.index += 1
def create_transform(self):
cfg = self.cfg
_transform = []
if 'bounding_box' in self.cfg.keys():
for augmentation in cfg['bounding_box']:
t = None
if type(augmentation) != custom_functional_transforms.CustomTransform and type(augmentation) != custom_functional_transforms.CustomGaussianBlurTransform:
t = A.Compose([
augmentation,
A.Resize(self.target_size[0], self.target_size[1])
],
bbox_params=A.BboxParams(format=self.format))
else:
t = transforms.Compose([
augmentation,
transforms.Resize((self.target_size[0], self.target_size[1]))
])
_transform.append(t)
return _transform
class InnerBoundingBoxAugmentation:
def __init__(self, cfg, save_dir='test_data'):
self.index = 0
self.format = cfg['format']
self.prefix = None
if self.format == 'yolo':
self.prefix = '.txt'
elif self.format == 'coco':
self.prefix = '.json'
elif self.format == 'pascal':
self.prefix = '.xml'
self.target_size = cfg['target_size']
self.cfg = cfg
self.transforms = self.create_transform()
self.save_dir = save_dir
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
def __call__(self, data):
index = 0
img = data['image']
bboxs = data['bbox']
dh, dw, _ = img.shape
for transform in self.transforms:
new_image = img
new_bboxs = bboxs
for bbox in new_bboxs:
x, y, w, h, c = bbox
l = int((x - w / 2) * dw)
r = int((x + w / 2) * dw)
t = int((y - h / 2) * dh)
b = int((y + h / 2) * dh)
if l < 0:
l = 0
if r > dw - 1:
r = dw - 1
if t < 0:
t = 0
if b > dh - 1:
b = dh - 1
cropped = new_image[t:b,l:r]
if isinstance(cropped, PIL.Image.Image):
new_cropped = transform(cropped)
else:
new_cropped = transform(transforms.ToPILImage()(cropped))
new_image[t:b,l:r] = new_cropped
image_save_path = self.save_dir + '/' + str(index) + str(self.index) + 'inner_bbox' + '.jpg'
bbox_save_path = self.save_dir + '/' + str(index) + str(self.index) + 'inner_bbox' + self.prefix
image_to_save = transforms.ToTensor()(new_image)
#! resize whole image to target size:
image_to_save = transforms.Resize(self.target_size)(image_to_save)
save_image(image_to_save, image_save_path)
with open(bbox_save_path, 'w') as f:
#! just implemented for yolo format because im currently use it
#! other formats will be added soon
for _, i in enumerate(bboxs):
vals = [str(val) for val in list(i[:-1])]
to_write = i[-1] + ' ' + ' '.join(vals)+'\n'
f.writelines(to_write)
index += 1
self.index += 1
def create_transform(self):
cfg = self.cfg
_transform = []
if 'inner_bounding_box' in self.cfg.keys():
for augmentation in cfg['inner_bounding_box']:
#! to change:
#! in the inner bounding box augmentation
#! we not use the last resize augmentation method
#! why? because we are going to change inner bbox content not the whole image
#! and in the end we apply resize augmentation on whole image but not bbox content
t = Compose([
augmentation,
])
_transform.append(t)
return _transform
def inner_box_augmentation():
dataset = MyDataset('raccoon/raccoon/', 'yolo')
inner_bbox_augmentation = InnerBoundingBoxAugmentation(cfg)
for i in range(len(dataset)):
inner_bbox_augmentation(dataset[i])
def bounding_box_augmentation():
dataset = MyDataset('raccoon/raccoon/', 'yolo')
bbox_augmentation = BoundingBoxAugmentation(cfg)
for i in range(len(dataset)):
bbox_augmentation(dataset[i])
def main():
inner_box_augmentation()
bounding_box_augmentation()
if __name__ == '__main__':
main()