hyperparameter expansion to flips, perspective, mixup
This commit is contained in:
1 parent
6f08e8bcce
commit
127cbeb3f5
2 files changed
+32
-32
No files matched your search
+19
-22
@@ -484,11 +484,11 @@ class LoadImagesAndLabels(Dataset): # for training/testing
|
||||
shapes = None
|
||||
|
||||
# MixUp https://arxiv.org/pdf/1710.09412.pdf
|
||||
# if random.random() < 0.5:
|
||||
# img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels) - 1))
|
||||
# r = np.random.beta(0.3, 0.3) # mixup ratio, alpha=beta=0.3
|
||||
# img = (img * r + img2 * (1 - r)).astype(np.uint8)
|
||||
# labels = np.concatenate((labels, labels2), 0)
|
||||
if random.random() < hyp['mixup']:
|
||||
img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels) - 1))
|
||||
r = np.random.beta(8.0, 8.0) # mixup ratio, alpha=beta=8.0
|
||||
img = (img * r + img2 * (1 - r)).astype(np.uint8)
|
||||
labels = np.concatenate((labels, labels2), 0)
|
||||
|
||||
else:
|
||||
# Load image
|
||||
@@ -517,7 +517,8 @@ class LoadImagesAndLabels(Dataset): # for training/testing
|
||||
degrees=hyp['degrees'],
|
||||
translate=hyp['translate'],
|
||||
scale=hyp['scale'],
|
||||
shear=hyp['shear'])
|
||||
shear=hyp['shear'],
|
||||
perspective=hyp['perspective'])
|
||||
|
||||
# Augment colorspace
|
||||
augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v'])
|
||||
@@ -528,28 +529,23 @@ class LoadImagesAndLabels(Dataset): # for training/testing
|
||||
|
||||
nL = len(labels) # number of labels
|
||||
if nL:
|
||||
# convert xyxy to xywh
|
||||
labels[:, 1:5] = xyxy2xywh(labels[:, 1:5])
|
||||
|
||||
# Normalize coordinates 0 - 1
|
||||
labels[:, [2, 4]] /= img.shape[0] # height
|
||||
labels[:, [1, 3]] /= img.shape[1] # width
|
||||
labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh
|
||||
labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1
|
||||
labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1
|
||||
|
||||
if self.augment:
|
||||
# random left-right flip
|
||||
lr_flip = True
|
||||
if lr_flip and random.random() < 0.5:
|
||||
img = np.fliplr(img)
|
||||
if nL:
|
||||
labels[:, 1] = 1 - labels[:, 1]
|
||||
|
||||
# random up-down flip
|
||||
ud_flip = False
|
||||
if ud_flip and random.random() < 0.5:
|
||||
# flip up-down
|
||||
if random.random() < hyp['flipud']:
|
||||
img = np.flipud(img)
|
||||
if nL:
|
||||
labels[:, 2] = 1 - labels[:, 2]
|
||||
|
||||
# flip left-right
|
||||
if random.random() < hyp['fliplr']:
|
||||
img = np.fliplr(img)
|
||||
if nL:
|
||||
labels[:, 1] = 1 - labels[:, 1]
|
||||
|
||||
labels_out = torch.zeros((nL, 6))
|
||||
if nL:
|
||||
labels_out[:, 1:] = torch.from_numpy(labels)
|
||||
@@ -661,6 +657,7 @@ def load_mosaic(self, index):
|
||||
translate=self.hyp['translate'],
|
||||
scale=self.hyp['scale'],
|
||||
shear=self.hyp['shear'],
|
||||
perspective=self.hyp['perspective'],
|
||||
border=self.mosaic_border) # border to remove
|
||||
|
||||
return img4, labels4
|
||||
|
||||
Reference in new issue
Block a user