copy-pasted 数据增强是2019年论文《Augmentation for small object detection》提出的针对coco的数据集增强方式,主要是为了解决小目标数量过少的问题,顾名思义,把小目标复制然后再重新在图像的随机位置粘贴,以此来增加小目标在数据集中的数量。
由于最近在一个比赛中目标全为小目标,因此自己动手实现了这个数据增强的简化版。
代码如下,因为比赛中数据集中只有小目标,所以我无需区分大目标与小目标,因此这个和原论文的做法还是不同的;因此在代码中只是简单的将每一张图片中所有目标复制并随机粘贴,然后修改对应的label。
并不能适用于任意数据集,仅供参考。
其中sample是由数据集标注文件解析得到的一个字典,每个sample对应一张训练图片,以及它的所有目标的标注信息
import numpy as np
import cv2
import random
from matplotlib import pyplot as plt
img = cv2.imread("F:/1.jpg")
print(img.shape)
#为了测试,简单生成一个sample
sample = {'gt_bbox':np.array([[200,300,300,600]]),'image':img,'w':img.shape[1],'h':img.shape[0]}
def CopyAndPasted(sample):
img = sample['image']
num_bbox = sample['gt_bbox'].shape[0]
targetList = []
for i in range(num_bbox):
x_min = sample['gt_bbox'][i,0]
y_min = sample['gt_bbox'][i,1]
x_max = sample['gt_bbox'][i,2]
y_max = sample['gt_bbox'][i,3]
targetList.append(img[y_min:y_max,x_min:x_max,:])
w = sample['w']
h = sample['h']
new_gt_bbox = np.zeros((num_bbox, 4), dtype=np.float32)
for i,target in enumerate(targetList):
label_w = target.shape[1]
label_h = target.shape[0]
x_1 = random.randint(0,w-label_w)
y_1 = random.randint(0,h-label_h)
x_2 = x_1 + label_w
y_2 = y_1 + label_h
print(label_w,label_h)
print(x_1,y_1,x_2,y_2)
new_gt_bbox[i,:] = [x_1,y_1,x_2,y_2]
sample['image'][y_1:y_2,x_1:x_2,:] = target
sample['gt_bbox'] = np.vstack((sample['gt_bbox'],new_gt_bbox))
# sample['gt_class'] = np.vstack((sample['gt_class'],sample['gt_class']))
# sample['gt_score'] = np.vstack((sample['gt_score'],sample['gt_score']))
return sample
# 测试是否有效
s = CopyAndPasted(sample)
img = s['image']
for i in range(s['gt_bbox'].shape[0]):
box = s['gt_bbox'][i]
cv2.rectangle(img,(int(box[0]),int(box[1])),(int(box[2]),int(box[3])),(0,255,0),2)
cv2.imshow("img",img)
cv2.waitKey(0)