mosaic 数据增强
Yolov4的mosaic 数据增强是参考CutMix数据增强,理论上类似,CutMix的理论可以参考这篇CutMix,但是mosaic利用了四张图片,据论文其优点是丰富检测物体的背景,且在BN计算的时候一下子会计算四张图片的数据,使得mini-batch大小不需要很大,那么一个GPU就可以达到比较好的效果。
实现方法
伪代码:
for data, target in batches:
w, h = (data[0]).shape
cut_x = random(0.2w, 0.8w)
cut_y = random(0.2h, 0.8h)
s1, s2, s3, s4 = area(block1) / wh, .....(分块所占比例)
d1 = data[random_index][0, :(h-cut_y), 0:cut_x, :]
d2 = data[random_index][1, (h-cut_y):, 0:cut_x, :]
d3 = data[random_index][2, (h-cut_y):, cut_x:, :]
d4 = data[random_index][3, :(h-cut_y), cut_x:, :]
x = concat(d1, d2, d3, d4)
y = target[random_index]*s1 + target[random_index]*s2 + target[random_index]*s3 + target[random_index]*s4
以上伪代码适用于分类的数据,检测的数据需要合并annotations,后面更新。
python代码在github
效果图:
对于用于检测的数据,首先对图片的处理和上面对分类数据处理一致,对于annotations需要对框的坐标在合成图中进行调整,超出边界的需要裁剪,效果图如下:
代码请见github,后期更新代码实现细节