yolov5中的Mosaic数据增强代码
时间: 2023-10-19 08:08:09 浏览: 201
以下是YOLOv5中实现Mosaic数据增强的代码:
```python
def mosaic(self, img, boxes, labels):
img_size = img.shape[:2]
xc, yc = [int(random.uniform(img_size[i] * 0.4, img_size[i] * 0.6)) for i in range(2)] # mosaic中心点坐标
indices = [random.randint(0, len(self.img_files) - 1) for _ in range(3)] # 随机选择三张图像进行拼接
mosaic_img = np.zeros((2 * yc, 2 * xc, 3), dtype=np.uint8)
mosaic_boxes, mosaic_labels = [], []
for i, index in enumerate([0, 1, 2, 3]):
img_path = self.img_files[indices[index]]
img, _, _ = self.load_image(img_path)
img_boxes = boxes[indices[index]].copy()
img_labels = labels[indices[index]].copy()
if i == 0:
# 左上角
x1a, y1a, x2a, y2a = self.rand_bbox(img_size, xc, yc)
x1b, y1b, x2b, y2b = self.rand_bbox(img_size, xc, yc)
mosaic_img[:yc, :xc, :] = img[y1a:y2a, x1a:x2a, :]
mosaic_boxes.append(self.box_transform(img_boxes, (x1a, y1a, x2a, y2a)))
mosaic_labels.append(img_labels)
elif i == 1:
# 右上角
x1a, y1a, x2a, y2a = self.rand_bbox(img_size, xc, yc)
x1b, y1b, x2b, y2b = self.rand_bbox(img_size, xc, yc)
mosaic_img[:yc, xc:, :] = img[y1a:y2a, x1b:x2b, :]
mosaic_boxes.append(self.box_transform(img_boxes, (x1b - xc, y1a, x2b - xc, y2a)))
mosaic_labels.append(img_labels)
elif i == 2:
# 左下角
x1a, y1a, x2a, y2a = self.rand_bbox(img_size, xc, yc)
x1b, y1b, x2b, y2b = self.rand_bbox(img_size, xc, yc)
mosaic_img[yc:, :xc, :] = img[y1b:y2b, x1a:x2a, :]
mosaic_boxes.append(self.box_transform(img_boxes, (x1a, y1b - yc, x2a, y2b - yc)))
mosaic_labels.append(img_labels)
else:
# 右下角
x1a, y1a, x2a, y2a = self.rand_bbox(img_size, xc, yc)
x1b, y1b, x2b, y2b = self.rand_bbox(img_size, xc, yc)
mosaic_img[yc:, xc:, :] = img[y1b:y2b, x1b:x2b, :]
mosaic_boxes.append(self.box_transform(img_boxes, (x1b - xc, y1b - yc, x2b - xc, y2b - yc)))
mosaic_labels.append(img_labels)
return mosaic_img, np.concatenate(mosaic_boxes, 0), np.concatenate(mosaic_labels, 0)
def rand_bbox(self, img_size, xc, yc):
w, h = img_size
size = min(w, h)
x1 = random.randint(0, xc)
y1 = random.randint(0, yc)
x2 = random.randint(xc, size)
y2 = random.randint(yc, size)
return x1, y1, x2, y2
def box_transform(self, boxes, box):
x1, y1, x2, y2 = box
w, h = x2 - x1, y2 - y1
boxes[:, [0, 2]] -= x1
boxes[:, [1, 3]] -= y1
boxes[:, [0, 2]] /= w
boxes[:, [1, 3]] /= h
boxes[:, [0, 2]] = np.clip(boxes[:, [0, 2]], 0, 1)
boxes[:, [1, 3]] = np.clip(boxes[:, [1, 3]], 0, 1)
boxes[:, 2:4] -= boxes[:, :2]
boxes[:, :2] += (x1, y1)
boxes[:, 2:4] += (x1, y1)
return boxes
```
阅读全文