Skip to content

Commit a4ac001

Browse files
committed
Mosaic hotfix and improved error reporting
1 parent d556b6e commit a4ac001

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

datasets/coco.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,20 @@ def __getitem__(self, idx):
8888
images = (i1, i2, i3, i4)
8989
anns = (a1, a2, a3, a4)
9090

91-
img_tensor, anns_partial = preprocess_images_mosaic(images, anns, netblock, self.input_dim, force_cpu=True)
91+
try:
92+
img_tensor, anns_partial = preprocess_images_mosaic(images, anns, netblock, self.input_dim, force_cpu=True)
93+
except:
94+
print("CocoDataset: Mosaic exception on images:", (img_id, id2, id3, id4))
95+
raise
9296

9397
# Normal preprocessing
9498
else:
95-
img_tensor, anns_partial = preprocess_image_train(image, anns, self.netblock, self.input_dim,
96-
augment=self.augment, letterbox=self.letterbox, force_cpu=True)
99+
try:
100+
img_tensor, anns_partial = preprocess_image_train(image, anns, self.netblock, self.input_dim,
101+
augment=self.augment, letterbox=self.letterbox, force_cpu=True)
102+
except:
103+
print("CocoDataset: Augmentation exception on image:", img_id)
104+
raise
97105

98106
n_anns = len(anns_partial)
99107
if(n_anns > self.max_anns):

utilities/augmentations.py

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,33 @@ def create_mosaic(images, netblock, target_dim, images_annotations=None, jitter=
133133

134134
# Jittering image beforehand
135135
jitter_embedding = (pleft, pright, ptop, pbot)
136-
jittered_img = jitter_image_precalc(img, jitter_embedding, target_dim, annotations=annotations, image_info=image_info)
137136

138-
# Do rest of the augmentations
139-
aug_image = augment_image(jittered_img, netblock, target_dim, annotations=annotations, image_info=image_info, jitter=False)
140-
141-
place_image_mosaic(mosaic_img, aug_image, image_info, cut_x, cut_y, i, annotations=annotations)
137+
try:
138+
jittered_img = jitter_image_precalc(img, jitter_embedding, target_dim, annotations=annotations, image_info=image_info)
139+
except:
140+
print("Error with jittering image inside create_mosaic:")
141+
print("jitter_embedding:", jitter_embedding)
142+
raise
143+
144+
try:
145+
# Do rest of the augmentations
146+
aug_image = augment_image(jittered_img, netblock, target_dim, annotations=annotations, image_info=image_info, jitter=False)
147+
except:
148+
print("Mosaic image index:", i)
149+
raise
150+
151+
try:
152+
# Place the image on the mosaic
153+
place_image_mosaic(mosaic_img, aug_image, image_info, cut_x, cut_y, i, annotations=annotations)
154+
except:
155+
print("Error with placing mosaic image:")
156+
print("placement image shape:", mosaic_img.shape)
157+
print("aug image shape:", aug_image.shape)
158+
print("jitter_embedding:", jitter_embedding)
159+
print("cut_x:", cut_x)
160+
print("cut_y:", cut_y)
161+
print("image num:", i)
162+
raise
142163

143164
# Combining all annotations together into one tensor
144165
if(images_annotations is not None):
@@ -217,8 +238,6 @@ def place_image_mosaic(placement_image, image, image_info, cut_x, cut_y, i_num,
217238
embed_w_fix = abs(avail_w - avail_w_fix)
218239
embed_h_fix = abs(avail_h - avail_h_fix)
219240

220-
# print(offset_x_fix, offset_y_fix, actual_w, actual_h, avail_w_fix, avail_h_fix)
221-
222241
pleft = pleft_fix
223242
avail_w = avail_w_fix
224243
ptop = ptop_fix
@@ -250,6 +269,7 @@ def place_image_mosaic(placement_image, image, image_info, cut_x, cut_y, i_num,
250269
pleft = pleft
251270
ptop = ptop
252271

272+
# Placing the mosaic
253273
placement_image[placem_y1:placem_y2, placem_x1:placem_x2] = image[ptop:pbot, pleft:pright]
254274

255275
# Mapping annotations
@@ -383,7 +403,15 @@ def jitter_image(image, jitter, resize_coef, target_dim, annotations=None, image
383403
oh = image.shape[CV2_H_DIM]
384404

385405
precalc = get_jitter_embedding(ow, oh, jitter, resize_coef)
386-
jitter_img = jitter_image_precalc(image, precalc, target_dim, annotations=annotations, image_info=image_info)
406+
407+
try:
408+
jitter_img = jitter_image_precalc(image, precalc, target_dim, annotations=annotations, image_info=image_info)
409+
except:
410+
print("Error with jittering image:")
411+
print("ow:", ow)
412+
print("oh:", oh)
413+
print("jitter precalc:", precalc)
414+
raise
387415

388416
return jitter_img
389417

@@ -432,7 +460,7 @@ def jitter_image_precalc(image, precalc, target_dim, annotations=None, image_inf
432460
dst_w_norm = 1.0
433461
dst_h_norm = 1.0
434462
else:
435-
# Just how darknet does it, it sort of reflects the dimension placement
463+
# Negation to guarantee placement lines up on the destination image
436464
dst_x1 = max(0, -pleft)
437465
dst_y1 = max(0, -ptop)
438466
dst_x2 = dst_x1 + crop_w
@@ -457,10 +485,11 @@ def jitter_image_precalc(image, precalc, target_dim, annotations=None, image_inf
457485
new_img = image_resize(new_img, new_dim)
458486

459487
# Needed by image info and annotations
460-
start_x = round(dst_x1_norm * target_dim)
461-
start_y = round(dst_y1_norm * target_dim)
462-
embed_w = round(dst_w_norm * target_dim)
463-
embed_h = round(dst_h_norm * target_dim)
488+
# Will take the floor to ensure we never go outside the image bounds
489+
start_x = int(dst_x1_norm * target_dim)
490+
start_y = int(dst_y1_norm * target_dim)
491+
embed_w = int(dst_w_norm * target_dim)
492+
embed_h = int(dst_h_norm * target_dim)
464493

465494
# Setting annotations
466495
if(annotations is not None):
@@ -543,7 +572,13 @@ def hsv_shift_image(image, hue, saturation, exposure, image_info=None):
543572
"""
544573

545574
precalc = get_hsv_shifting(hue, saturation, exposure)
546-
new_img = hsv_shift_image_precalc(image, precalc, image_info=image_info)
575+
576+
try:
577+
new_img = hsv_shift_image_precalc(image, precalc, image_info=image_info)
578+
except:
579+
print("Error with HSV shifting:")
580+
print("HSV precalc:", precalc)
581+
raise
547582

548583
return new_img
549584

@@ -653,7 +688,7 @@ def flip_image(image, annotations=None, image_info=None):
653688

654689
# image_info
655690
if(image_info is not None):
656-
half_ow = round(image.shape[CV2_W_DIM] / 2.0)
691+
half_ow = image.shape[CV2_W_DIM] / 2.0
657692
pleft = image_info.aug_pleft
658693
embed_w = image_info.aug_embed_w
659694

@@ -663,7 +698,7 @@ def flip_image(image, annotations=None, image_info=None):
663698
pleft += half_ow
664699
pleft -= embed_w
665700

666-
image_info.aug_pleft = pleft
701+
image_info.aug_pleft = int(pleft)
667702
image_info.set_augmentation(new_img)
668703

669704

0 commit comments

Comments
 (0)