Skip to content

Commit 2c6d894

Browse files
committed
update readme and mixup
1 parent 2c25d6e commit 2c6d894

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ BiliBili视频中的原仓库地址为:https://github.com/bubbliiiing/yolov4-t
2323
## 相关仓库
2424
| 模型 | 路径 |
2525
| :----- | :----- |
26-
YoloV3 | https://github.com/bubbliiiing/yolo3-keras  
27-
Efficientnet-Yolo3 | https://github.com/bubbliiiing/efficientnet-yolo3-keras  
26+
YoloV3 | https://github.com/bubbliiiing/yolo3-keras
27+
Efficientnet-Yolo3 | https://github.com/bubbliiiing/efficientnet-yolo3-keras
2828
YoloV4 | https://github.com/bubbliiiing/yolov4-keras
2929
YoloV4-tiny | https://github.com/bubbliiiing/yolov4-tiny-keras
3030
Mobilenet-Yolov4 | https://github.com/bubbliiiing/mobilenet-yolov4-keras
31-
YoloV5 | https://github.com/bubbliiiing/yolov5-keras
31+
YoloV5-V5.0 | https://github.com/bubbliiiing/yolov5-keras
32+
YoloV5-V6.1 | https://github.com/bubbliiiing/yolov5-v6.1-keras
3233
YoloX | https://github.com/bubbliiiing/yolox-keras
3334

3435
## 性能情况

train.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,29 @@
9191
# phi = 4为CA
9292
#-------------------------------#
9393
phi = 0
94-
#------------------------------------------------------#
95-
# Yolov4的tricks应用
96-
# mosaic 马赛克数据增强
97-
# 参考YoloX,由于Mosaic生成的训练图片,
98-
# 远远脱离自然图片的真实分布。
99-
# 本代码会在训练结束前的N个epoch自动关掉Mosaic
100-
# 100个世代会关闭30个世代(比例可在dataloader.py调整)
101-
# label_smoothing 标签平滑。一般0.01以下。如0.01、0.005
94+
#------------------------------------------------------------------#
95+
# mosaic 马赛克数据增强。
96+
# mosaic_prob 每个step有多少概率使用mosaic数据增强,默认50%。
97+
#
98+
# mixup 是否使用mixup数据增强,仅在mosaic=True时有效。
99+
# 只会对mosaic增强后的图片进行mixup的处理。
100+
# mixup_prob 有多少概率在mosaic后使用mixup数据增强,默认50%。
101+
# 总的mixup概率为mosaic_prob * mixup_prob。
102+
#
103+
# special_aug_ratio 参考YoloX,由于Mosaic生成的训练图片,远远脱离自然图片的真实分布。
104+
# 当mosaic=True时,本代码会在special_aug_ratio范围内开启mosaic。
105+
# 默认为前70%个epoch,100个世代会开启70个世代。
102106
#
103107
# 余弦退火算法的参数放到下面的lr_decay_type中设置
104-
#------------------------------------------------------#
105-
mosaic = False
108+
#------------------------------------------------------------------#
109+
mosaic = True
110+
mosaic_prob = 0.5
111+
mixup = True
112+
mixup_prob = 0.5
113+
special_aug_ratio = 0.7
114+
#------------------------------------------------------------------#
115+
# label_smoothing 标签平滑。一般0.01以下。如0.01、0.005。
116+
#------------------------------------------------------------------#
106117
label_smoothing = 0
107118

108119
#----------------------------------------------------------------------------------------------------------------------------#
@@ -343,8 +354,10 @@
343354
if epoch_step == 0 or epoch_step_val == 0:
344355
raise ValueError('数据集过小,无法进行训练,请扩充数据集。')
345356

346-
train_dataloader = YoloDatasets(train_lines, input_shape, anchors, batch_size, num_classes, anchors_mask, Init_Epoch, UnFreeze_Epoch, mosaic = mosaic, train = True)
347-
val_dataloader = YoloDatasets(val_lines, input_shape, anchors, batch_size, num_classes, anchors_mask, Init_Epoch, UnFreeze_Epoch, mosaic = False, train = False)
357+
train_dataloader = YoloDatasets(train_lines, input_shape, anchors, batch_size, num_classes, anchors_mask, Init_Epoch, UnFreeze_Epoch, \
358+
mosaic=mosaic, mixup=mixup, mosaic_prob=mosaic_prob, mixup_prob=mixup_prob, train=True, special_aug_ratio=special_aug_ratio)
359+
val_dataloader = YoloDatasets(val_lines, input_shape, anchors, batch_size, num_classes, anchors_mask, Init_Epoch, UnFreeze_Epoch, \
360+
mosaic=False, mixup=False, mosaic_prob=0, mixup_prob=0, train=False, special_aug_ratio=0)
348361

349362
#-------------------------------------------------------------------------------#
350363
# 训练参数的设置

utils/dataloader.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010

1111
class YoloDatasets(keras.utils.Sequence):
12-
def __init__(self, annotation_lines, input_shape, anchors, batch_size, num_classes, anchors_mask, epoch_now, epoch_length, mosaic, train, mosaic_ratio = 0.7):
12+
def __init__(self, annotation_lines, input_shape, anchors, batch_size, num_classes, anchors_mask, epoch_now, epoch_length, \
13+
mosaic, mixup, mosaic_prob, mixup_prob, train, special_aug_ratio = 0.7):
1314
self.annotation_lines = annotation_lines
1415
self.length = len(self.annotation_lines)
1516

@@ -21,10 +22,11 @@ def __init__(self, annotation_lines, input_shape, anchors, batch_size, num_class
2122
self.epoch_now = epoch_now - 1
2223
self.epoch_length = epoch_length
2324
self.mosaic = mosaic
25+
self.mosaic_prob = mosaic_prob
26+
self.mixup = mixup
27+
self.mixup_prob = mixup_prob
2428
self.train = train
25-
self.mosaic_ratio = mosaic_ratio
26-
27-
self.threshold = 4
29+
self.special_aug_ratio = special_aug_ratio
2830

2931
def __len__(self):
3032
return math.ceil(len(self.annotation_lines) / float(self.batch_size))
@@ -38,14 +40,16 @@ def __getitem__(self, index):
3840
# 训练时进行数据的随机增强
3941
# 验证时不进行数据的随机增强
4042
#---------------------------------------------------#
41-
if self.mosaic:
42-
if self.rand() < 0.5 and self.epoch_now < self.epoch_length * self.mosaic_ratio:
43-
lines = sample(self.annotation_lines, 3)
44-
lines.append(self.annotation_lines[i])
45-
shuffle(lines)
46-
image, box = self.get_random_data_with_Mosaic(lines, self.input_shape)
47-
else:
48-
image, box = self.get_random_data(self.annotation_lines[i], self.input_shape, random = self.train)
43+
if self.mosaic and self.rand() < self.mosaic_prob and self.epoch_now < self.epoch_length * self.special_aug_ratio:
44+
lines = sample(self.annotation_lines, 3)
45+
lines.append(self.annotation_lines[i])
46+
shuffle(lines)
47+
image, box = self.get_random_data_with_Mosaic(lines, self.input_shape)
48+
49+
if self.mixup and self.rand() < self.mixup_prob:
50+
lines = sample(self.annotation_lines, 1)
51+
image_2, box_2 = self.get_random_data(lines[0], self.input_shape, random = self.train)
52+
image, box = self.get_random_data_with_MixUp(image, box, image_2, box_2)
4953
else:
5054
image, box = self.get_random_data(self.annotation_lines[i], self.input_shape, random = self.train)
5155
image_data.append(preprocess_input(np.array(image, np.float32)))
@@ -368,6 +372,25 @@ def get_random_data_with_Mosaic(self, annotation_line, input_shape, max_boxes=50
368372
box_data[:len(new_boxes)] = new_boxes
369373
return new_image, box_data
370374

375+
def get_random_data_with_MixUp(self, image_1, box_1, image_2, box_2, max_boxes=500):
376+
new_image = np.array(image_1, np.float32) * 0.5 + np.array(image_2, np.float32) * 0.5
377+
378+
box_1_wh = box_1[:, 2:4] - box_1[:, 0:2]
379+
box_1_valid = box_1_wh[:, 0] > 0
380+
381+
box_2_wh = box_2[:, 2:4] - box_2[:, 0:2]
382+
box_2_valid = box_2_wh[:, 0] > 0
383+
384+
new_boxes = np.concatenate([box_1[box_1_valid, :], box_2[box_2_valid, :]], axis=0)
385+
#---------------------------------#
386+
# 将box进行调整
387+
#---------------------------------#
388+
box_data = np.zeros((max_boxes, 5))
389+
if len(new_boxes)>0:
390+
if len(new_boxes)>max_boxes: new_boxes = new_boxes[:max_boxes]
391+
box_data[:len(new_boxes)] = new_boxes
392+
return new_image, box_data
393+
371394
def preprocess_true_boxes(self, true_boxes, input_shape, anchors, num_classes):
372395
assert (true_boxes[..., 4]<num_classes).all(), 'class id must be less than num_classes'
373396
#-----------------------------------------------------------#

0 commit comments

Comments
 (0)