-
Notifications
You must be signed in to change notification settings - Fork 78
/
datasets.py
437 lines (391 loc) · 16.1 KB
/
datasets.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import torch
import cv2
import numpy as np
import os
import glob as glob
import random
from xml.etree import ElementTree as et
from torch.utils.data import Dataset, DataLoader
from utils.transforms import (
get_train_transform,
get_valid_transform,
get_train_aug,
transform_mosaic
)
from tqdm.auto import tqdm
# the dataset class
class CustomDataset(Dataset):
def __init__(
self,
images_path,
labels_path,
img_size,
classes,
transforms=None,
use_train_aug=False,
train=False,
mosaic=1.0,
square_training=False
):
self.transforms = transforms
self.use_train_aug = use_train_aug
self.images_path = images_path
self.labels_path = labels_path
self.img_size = img_size
self.classes = classes
self.train = train
self.square_training = square_training
self.mosaic_border = [-img_size // 2, -img_size // 2]
self.image_file_types = ['*.jpg', '*.jpeg', '*.png', '*.ppm', '*.JPG']
self.all_image_paths = []
self.log_annot_issue_x = True
self.mosaic = mosaic
self.log_annot_issue_y = True
# get all the image paths in sorted order
for file_type in self.image_file_types:
self.all_image_paths.extend(glob.glob(os.path.join(self.images_path, file_type)))
self.all_annot_paths = glob.glob(os.path.join(self.labels_path, '*.xml'))
self.all_images = [image_path.split(os.path.sep)[-1] for image_path in self.all_image_paths]
self.all_images = sorted(self.all_images)
# Remove all annotations and images when no object is present.
self.read_and_clean()
def read_and_clean(self):
print('Checking Labels and images...')
images_to_remove = []
problematic_images = []
for image_name in tqdm(self.all_images, total=len(self.all_images)):
possible_xml_name = os.path.join(self.labels_path, os.path.splitext(image_name)[0]+'.xml')
if possible_xml_name not in self.all_annot_paths:
print(f"⚠️ {possible_xml_name} not found... Removing {image_name}")
images_to_remove.append(image_name)
continue
# Check for invalid bounding boxes
tree = et.parse(possible_xml_name)
root = tree.getroot()
invalid_bbox = False
for member in root.findall('object'):
xmin = float(member.find('bndbox').find('xmin').text)
xmax = float(member.find('bndbox').find('xmax').text)
ymin = float(member.find('bndbox').find('ymin').text)
ymax = float(member.find('bndbox').find('ymax').text)
if xmin >= xmax or ymin >= ymax:
invalid_bbox = True
break
if invalid_bbox:
problematic_images.append(image_name)
images_to_remove.append(image_name)
# Remove problematic images and their annotations
self.all_images = [img for img in self.all_images if img not in images_to_remove]
self.all_annot_paths = [
path for path in self.all_annot_paths
if not any(os.path.splitext(os.path.basename(path))[0] + ext in images_to_remove
for ext in self.image_file_types)
]
# Print warnings for problematic images
if problematic_images:
print("\n⚠️ The following images have invalid bounding boxes and will be removed:")
for img in problematic_images:
print(f"⚠️ {img}")
print(f"Removed {len(images_to_remove)} problematic images and annotations.")
def resize(self, im, square=False):
if square:
im = cv2.resize(im, (self.img_size, self.img_size))
else:
h0, w0 = im.shape[:2] # orig hw
r = self.img_size / max(h0, w0) # ratio
if r != 1: # if sizes are not equal
im = cv2.resize(im, (int(w0 * r), int(h0 * r)))
return im
def load_image_and_labels(self, index):
image_name = self.all_images[index]
image_path = os.path.join(self.images_path, image_name)
# Read the image.
image = cv2.imread(image_path)
# Convert BGR to RGB color format.
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)
image_resized = self.resize(image, square=self.square_training)
image_resized /= 255.0
# Capture the corresponding XML file for getting the annotations.
annot_filename = os.path.splitext(image_name)[0] + '.xml'
annot_file_path = os.path.join(self.labels_path, annot_filename)
boxes = []
orig_boxes = []
labels = []
# Get the height and width of the image.
image_width = image.shape[1]
image_height = image.shape[0]
# Box coordinates for xml files are extracted and corrected for image size given.
# try:
tree = et.parse(annot_file_path)
root = tree.getroot()
for member in root.findall('object'):
# Map the current object name to `classes` list to get
# the label index and append to `labels` list.
labels.append(self.classes.index(member.find('name').text))
# xmin = left corner x-coordinates
xmin = float(member.find('bndbox').find('xmin').text)
# xmax = right corner x-coordinates
xmax = float(member.find('bndbox').find('xmax').text)
# ymin = left corner y-coordinates
ymin = float(member.find('bndbox').find('ymin').text)
# ymax = right corner y-coordinates
ymax = float(member.find('bndbox').find('ymax').text)
xmin, ymin, xmax, ymax = self.check_image_and_annotation(
xmin,
ymin,
xmax,
ymax,
image_width,
image_height,
orig_data=True
)
orig_boxes.append([xmin, ymin, xmax, ymax])
# Resize the bounding boxes according to the
# desired `width`, `height`.
xmin_final = (xmin/image_width)*image_resized.shape[1]
xmax_final = (xmax/image_width)*image_resized.shape[1]
ymin_final = (ymin/image_height)*image_resized.shape[0]
ymax_final = (ymax/image_height)*image_resized.shape[0]
xmin_final, ymin_final, xmax_final, ymax_final = self.check_image_and_annotation(
xmin_final,
ymin_final,
xmax_final,
ymax_final,
image_resized.shape[1],
image_resized.shape[0],
orig_data=False
)
boxes.append([xmin_final, ymin_final, xmax_final, ymax_final])
# except:
# pass
# Bounding box to tensor.
boxes_length = len(boxes)
boxes = torch.as_tensor(boxes, dtype=torch.float32)
# Area of the bounding boxes.
area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0]) if boxes_length > 0 else torch.as_tensor(boxes, dtype=torch.float32)
# No crowd instances.
iscrowd = torch.zeros((boxes.shape[0],), dtype=torch.int64) if boxes_length > 0 else torch.as_tensor(boxes, dtype=torch.float32)
# Labels to tensor.
labels = torch.as_tensor(labels, dtype=torch.int64)
return image, image_resized, orig_boxes, \
boxes, labels, area, iscrowd, (image_width, image_height)
def check_image_and_annotation(
self,
xmin,
ymin,
xmax,
ymax,
width,
height,
orig_data=False
):
"""
Check that all x_max and y_max are not more than the image
width or height.
"""
if ymax > height:
ymax = height
if xmax > width:
xmax = width
if xmax - xmin <= 1.0:
if orig_data:
# print(
# '\n',
# '!!! xmax is equal to xmin in data annotations !!!'
# 'Please check data'
# )
# print(
# 'Increasing xmax by 1 pixel to continue training for now...',
# 'THIS WILL ONLY BE LOGGED ONCE',
# '\n'
# )
self.log_annot_issue_x = False
xmin = xmin - 1
if ymax - ymin <= 1.0:
if orig_data:
# print(
# '\n',
# '!!! ymax is equal to ymin in data annotations !!!',
# 'Please check data'
# )
# print(
# 'Increasing ymax by 1 pixel to continue training for now...',
# 'THIS WILL ONLY BE LOGGED ONCE',
# '\n'
# )
self.log_annot_issue_y = False
ymin = ymin - 1
return xmin, ymin, xmax, ymax
def load_cutmix_image_and_boxes(self, index, resize_factor=512):
"""
Adapted from: https://www.kaggle.com/shonenkov/oof-evaluation-mixup-efficientdet
"""
s = self.img_size
yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border) # mosaic center x, y
indices = [index] + [random.randint(0, len(self.all_images) - 1) for _ in range(3)]
# Create empty image with the above resized image.
# result_image = np.full((h, w, 3), 1, dtype=np.float32)
result_boxes = []
result_classes = []
for i, index in enumerate(indices):
_, image_resized, orig_boxes, boxes, \
labels, area, iscrowd, dims = self.load_image_and_labels(
index=index
)
h, w = image_resized.shape[:2]
if i == 0:
# Create empty image with the above resized image.
result_image = np.full((s * 2, s * 2, image_resized.shape[2]), 114/255, dtype=np.float32) # base image with 4 tiles
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image)
x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image)
elif i == 1: # top right
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
elif i == 2: # bottom left
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, max(xc, w), min(y2a - y1a, h)
elif i == 3: # bottom right
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
result_image[y1a:y2a, x1a:x2a] = image_resized[y1b:y2b, x1b:x2b]
padw = x1a - x1b
padh = y1a - y1b
if len(orig_boxes) > 0:
boxes[:, 0] += padw
boxes[:, 1] += padh
boxes[:, 2] += padw
boxes[:, 3] += padh
result_boxes.append(boxes)
result_classes += labels
final_classes = []
if len(result_boxes) > 0:
result_boxes = np.concatenate(result_boxes, 0)
np.clip(result_boxes[:, 0:], 0, 2 * s, out=result_boxes[:, 0:])
result_boxes = result_boxes.astype(np.int32)
for idx in range(len(result_boxes)):
if ((result_boxes[idx, 2] - result_boxes[idx, 0]) * (result_boxes[idx, 3] - result_boxes[idx, 1])) > 0:
final_classes.append(result_classes[idx])
result_boxes = result_boxes[
np.where((result_boxes[:, 2] - result_boxes[:, 0]) * (result_boxes[:, 3] - result_boxes[:, 1]) > 0)
]
# Resize the mosaic image to the desired shape and transform boxes.
result_image, result_boxes = transform_mosaic(
result_image, result_boxes, self.img_size
)
return result_image, torch.tensor(result_boxes), \
torch.tensor(np.array(final_classes)), area, iscrowd, dims
def __getitem__(self, idx):
if not self.train: # No mosaic during validation.
image, image_resized, orig_boxes, boxes, \
labels, area, iscrowd, dims = self.load_image_and_labels(
index=idx
)
if self.train:
mosaic_prob = random.uniform(0.0, 1.0)
if self.mosaic >= mosaic_prob:
image_resized, boxes, labels, \
area, iscrowd, dims = self.load_cutmix_image_and_boxes(
idx, resize_factor=(self.img_size, self.img_size)
)
else:
image, image_resized, orig_boxes, boxes, \
labels, area, iscrowd, dims = self.load_image_and_labels(
index=idx
)
# Prepare the final `target` dictionary.
target = {}
target["boxes"] = boxes
target["labels"] = labels
target["area"] = area
target["iscrowd"] = iscrowd
image_id = torch.tensor([idx])
target["image_id"] = image_id
if self.use_train_aug: # Use train augmentation if argument is passed.
train_aug = get_train_aug()
sample = train_aug(image=image_resized,
bboxes=target['boxes'],
labels=labels)
image_resized = sample['image']
target['boxes'] = torch.Tensor(sample['bboxes']).to(torch.int64)
else:
sample = self.transforms(image=image_resized,
bboxes=target['boxes'],
labels=labels)
image_resized = sample['image']
target['boxes'] = torch.Tensor(sample['bboxes']).to(torch.int64)
# Fix to enable training without target bounding boxes,
# see https://discuss.pytorch.org/t/fasterrcnn-images-with-no-objects-present-cause-an-error/117974/4
if np.isnan((target['boxes']).numpy()).any() or target['boxes'].shape == torch.Size([0]):
target['boxes'] = torch.zeros((0, 4), dtype=torch.int64)
return image_resized, target
def __len__(self):
return len(self.all_images)
def collate_fn(batch):
"""
To handle the data loading as different images may have different number
of objects and to handle varying size tensors as well.
"""
return tuple(zip(*batch))
# Prepare the final datasets and data loaders.
def create_train_dataset(
train_dir_images,
train_dir_labels,
img_size,
classes,
use_train_aug=False,
mosaic=1.0,
square_training=False
):
train_dataset = CustomDataset(
train_dir_images,
train_dir_labels,
img_size,
classes,
get_train_transform(),
use_train_aug=use_train_aug,
train=True,
mosaic=mosaic,
square_training=square_training
)
return train_dataset
def create_valid_dataset(
valid_dir_images,
valid_dir_labels,
img_size,
classes,
square_training=False
):
valid_dataset = CustomDataset(
valid_dir_images,
valid_dir_labels,
img_size,
classes,
get_valid_transform(),
train=False,
square_training=square_training
)
return valid_dataset
def create_train_loader(
train_dataset, batch_size, num_workers=0, batch_sampler=None
):
train_loader = DataLoader(
train_dataset,
batch_size=batch_size,
# shuffle=True,
num_workers=num_workers,
collate_fn=collate_fn,
sampler=batch_sampler
)
return train_loader
def create_valid_loader(
valid_dataset, batch_size, num_workers=0, batch_sampler=None
):
valid_loader = DataLoader(
valid_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
collate_fn=collate_fn,
sampler=batch_sampler
)
return valid_loader