赞
踩
mixup是一种运用在计算机视觉中的对图像进行混类增强的算法,它可以将不同的图像进行混合,从而扩充训练数据集。
λ
=
B
e
t
a
(
α
,
β
)
\lambda = Beta(\alpha,\beta)
λ=Beta(α,β)
i
m
g
_
m
i
x
=
i
m
g
1
∗
λ
+
i
m
g
2
∗
(
1
−
λ
)
img\_mix = img_1*\lambda+img_2*(1-\lambda)
img_mix=img1∗λ+img2∗(1−λ)其中
λ
{\lambda}
λ是由参数为
α
{\alpha}
α,
β
{\beta}
β的贝塔分布计算出来的混合系数,
i
m
g
1
{img_1}
img1和
i
m
g
2
{img_2}
img2是进行混合的样本。无论如何设置
α
{\alpha}
α,
β
{\beta}
β的值,期望
α
α
+
β
{\frac{\alpha}{\alpha+\beta}}
α+βα 始终近似为0.5。
import random
import numpy as np
class BaseMixTransform:
"""This implementation is from mmyolo"""
def __init__(self, dataset, pre_transform=None, p=0.0) -> None:
# dataset中存储着读取的数据(图片、类别和真实框位置信息)
self.dataset = dataset
# 进行数据增强前进行的数据预处理
self.pre_transform = pre_transform
# 进行数据增强的概率
self.p = p
def __call__(self, labels):
# 随机进行数据增强
if random.uniform(0, 1) > self.p:
return labels
# 获取一个或三个其他图像的索引
indexes = self.get_indexes()
if isinstance(indexes, int):
indexes = [indexes]
# 获取图像信息用于 Mosaic 或者 MixUp 数据增强
mix_labels = [self.dataset.get_label_info(i) for i in indexes]
if self.pre_transform is not None:
for i, data in enumerate(mix_labels):
mix_labels[i] = self.pre_transform(data)
labels["mix_labels"] = mix_labels
# 进行 Mosaic 或 MixUp
labels = self._mix_transform(labels)
labels.pop("mix_labels", None)
return labels
def _mix_transform(self, labels):
raise NotImplementedError
def get_indexes(self):
raise NotImplementedError
class MixUp(BaseMixTransform):
def __init__(self, dataset, pre_transform=None, p=0.0) -> None:
super().__init__(dataset=dataset, pre_transform=pre_transform, p=p)
# 获取另一个图像的索引
def get_indexes(self):
return random.randint(0, len(self.dataset) - 1)
def _mix_transform(self, labels):
# Applies MixUp augmentation https://arxiv.org/pdf/1710.09412.pdf
# 混合系数
r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0
labels2 = labels["mix_labels"][0]
# 根据混合系数将两张图片mixup
labels["img"] = (labels["img"] * r + labels2["img"] * (1 - r)).astype(np.uint8)
labels["instances"] = Instances.concatenate([labels["instances"], labels2["instances"]], axis=0)
labels["cls"] = np.concatenate([labels["cls"], labels2["cls"]], 0)
return labels
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。