coco数据集数据增强
时间: 2023-09-01 14:11:02 浏览: 44
COCO数据集是一个非常广泛使用的计算机视觉数据集,它包含了各种各样的图像和标注,可以用于图像分类、目标检测、图像分割等任务。在使用COCO数据集时,数据增强是一个非常有用的技巧,可以增加数据的多样性,提高模型的泛化能力。
常见的数据增强方法包括:随机裁剪、缩放、旋转、翻转、色彩变换等等。
下面是一个使用Python和OpenCV库实现数据增强的示例代码:
```python
import cv2
import numpy as np
def random_crop(img, boxes, labels):
"""随机裁剪"""
h, w, _ = img.shape
if len(boxes) == 0:
return img, boxes, labels
max_box = np.max(boxes, axis=0)
min_box = np.min(boxes, axis=0)
max_l_trans = min_box[0]
max_u_trans = min_box[1]
max_r_trans = w - max_box[2]
max_d_trans = h - max_box[3]
crop_xmin = int(np.maximum(0, min_box[0] - np.random.uniform(0, max_l_trans)))
crop_ymin = int(np.maximum(0, min_box[1] - np.random.uniform(0, max_u_trans)))
crop_xmax = int(np.minimum(w, max_box[2] + np.random.uniform(0, max_r_trans)))
crop_ymax = int(np.minimum(h, max_box[3] + np.random.uniform(0, max_d_trans)))
img = img[crop_ymin : crop_ymax, crop_xmin : crop_xmax]
boxes[:, [0, 2]] = boxes[:, [0, 2]] - crop_xmin
boxes[:, [1, 3]] = boxes[:, [1, 3]] - crop_ymin
boxes[:, [0, 2]] = np.clip(boxes[:, [0, 2]], 0, crop_xmax - crop_xmin)
boxes[:, [1, 3]] = np.clip(boxes[:, [1, 3]], 0, crop_ymax - crop_ymin)
labels = labels[np.where((boxes[:, 2] - boxes[:, 0]) > 0)]
boxes = boxes[np.where((boxes[:, 2] - boxes[:, 0]) > 0)]
return img, boxes, labels
def random_flip(img, boxes):
"""随机翻转"""
if np.random.uniform(0, 1) < 0.5:
img = cv2.flip(img, 1)
boxes[:, [0, 2]] = img.shape[1] - boxes[:, [2, 0]]
return img, boxes
def random_scale(img, boxes, labels):
"""随机缩放"""
scale = np.random.uniform(0.8, 1.2)
h, w, _ = img.shape
img = cv2.resize(img, (int(w * scale), int(h * scale)))
boxes[:, :4] *= scale
return img, boxes, labels
def random_rotate(img, boxes):
"""随机旋转"""
angle = np.random.uniform(-10, 10)
h, w, _ = img.shape
cx, cy = w // 2, h // 2
rot_mat = cv2.getRotationMatrix2D((cx, cy), angle, 1.0)
cos, sin = np.abs(rot_mat[0, 0]), np.abs(rot_mat[0, 1])
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
rot_mat[0, 2] += (nW / 2) - cx
rot_mat[1, 2] += (nH / 2) - cy
img = cv2.warpAffine(img, rot_mat, (nW, nH), flags=cv2.INTER_LINEAR)
boxes[:, :2] = np.dot(boxes[:, :2], rot_mat.T)
boxes[:, 2:4] = np.dot(boxes[:, 2:4], rot_mat.T)
boxes[:, 0::2] = np.clip(boxes[:, 0::2], 0, nW)
boxes[:, 1::2] = np.clip(boxes[:, 1::2], 0, nH)
return img, boxes
def random_distort(img):
"""随机色彩变换"""
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
img[..., 0] += np.random.randint(-10, 10)
img[..., 1] += np.random.randint(-10, 10)
img[..., 2] += np.random.randint(-10, 10)
img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
img = np.clip(img, 0, 255)
return img
def data_augmentation(img, boxes, labels):
"""数据增强"""
img, boxes, labels = random_crop(img, boxes, labels)
img, boxes = random_flip(img, boxes)
img, boxes, labels = random_scale(img, boxes, labels)
img, boxes = random_rotate(img, boxes)
img = random_distort(img)
return img, boxes, labels
```
上面的代码实现了随机裁剪、随机翻转、随机缩放、随机旋转、随机色彩变换等增强方法。你可以根据自己的需求进行调整和扩展。