Update data_augmentation.py
Browse files- data_augmentation.py +328 -365
data_augmentation.py
CHANGED
|
@@ -1,366 +1,329 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
self.
|
| 28 |
-
self.
|
| 29 |
-
self.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
out
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
def
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
def
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
self
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
modality: 模态类型 ('image', 'audio', 'video')
|
| 330 |
-
labels: 标签(可选)
|
| 331 |
-
"""
|
| 332 |
-
# 1. 模态特定的增强 (Intra-sample augmentation)
|
| 333 |
-
if modality == 'image' and self.image_aug is not None:
|
| 334 |
-
data = self.image_aug(data)
|
| 335 |
-
elif modality == 'audio' and self.audio_aug is not None:
|
| 336 |
-
data = self.audio_aug(data)
|
| 337 |
-
elif modality == 'video' and self.video_aug is not None:
|
| 338 |
-
data = self.video_aug(data)
|
| 339 |
-
|
| 340 |
-
# 2. 混合增强 (Inter-sample augmentation)
|
| 341 |
-
if self.training and labels is not None:
|
| 342 |
-
# 随机选择 MixUp 或 CutMix,或者都不选
|
| 343 |
-
# 策略:如果有 CutMix 且是图片,50%概率 CutMix;否则看有没有 MixUp
|
| 344 |
-
|
| 345 |
-
apply_mixup = False
|
| 346 |
-
apply_cutmix = False
|
| 347 |
-
|
| 348 |
-
p = random.random()
|
| 349 |
-
|
| 350 |
-
# 简单的互斥逻辑:如果有CutMix且是图像,一半概率CutMix,一半概率MixUp(如果有)
|
| 351 |
-
if self.cutmix is not None and modality == 'image':
|
| 352 |
-
if p < 0.5:
|
| 353 |
-
apply_cutmix = True
|
| 354 |
-
elif self.mixup is not None:
|
| 355 |
-
apply_mixup = True
|
| 356 |
-
elif self.mixup is not None:
|
| 357 |
-
# 非图像或无CutMix,则只考虑MixUp
|
| 358 |
-
if p < 0.5: # 假设 50% 概率应用 MixUp
|
| 359 |
-
apply_mixup = True
|
| 360 |
-
|
| 361 |
-
if apply_cutmix:
|
| 362 |
-
data, labels, _ = self.cutmix(data, labels)
|
| 363 |
-
elif apply_mixup:
|
| 364 |
-
data, labels, _ = self.mixup(data, labels)
|
| 365 |
-
|
| 366 |
return data, labels
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from typing import Optional, Tuple, List
|
| 5 |
+
import random
|
| 6 |
+
import math
|
| 7 |
+
|
| 8 |
+
class RandAugment(nn.Module):
|
| 9 |
+
"""RandAugment for images"""
|
| 10 |
+
def __init__(self, n: int = 2, m: int = 10):
|
| 11 |
+
super().__init__()
|
| 12 |
+
self.n = n
|
| 13 |
+
self.m = m
|
| 14 |
+
|
| 15 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 16 |
+
"""随机应用n个增强操作"""
|
| 17 |
+
# 确保输入是 [B, C, H, W],如果是 [C, H, W] 则增加维度
|
| 18 |
+
is_batched = x.ndim == 4
|
| 19 |
+
if not is_batched:
|
| 20 |
+
x = x.unsqueeze(0)
|
| 21 |
+
|
| 22 |
+
augmentations = [
|
| 23 |
+
self._auto_contrast,
|
| 24 |
+
self._equalize,
|
| 25 |
+
self._solarize,
|
| 26 |
+
self._color,
|
| 27 |
+
self._contrast,
|
| 28 |
+
self._brightness,
|
| 29 |
+
self._sharpness,
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
for _ in range(self.n):
|
| 33 |
+
aug = random.choice(augmentations)
|
| 34 |
+
x = aug(x)
|
| 35 |
+
|
| 36 |
+
if not is_batched:
|
| 37 |
+
x = x.squeeze(0)
|
| 38 |
+
|
| 39 |
+
return x
|
| 40 |
+
|
| 41 |
+
def _auto_contrast(self, x: torch.Tensor) -> torch.Tensor:
|
| 42 |
+
# 针对每个样本分别计算 min/max
|
| 43 |
+
# x: [B, C, H, W]
|
| 44 |
+
B, C, H, W = x.shape
|
| 45 |
+
x_flat = x.view(B, C, -1)
|
| 46 |
+
min_val = x_flat.min(dim=2, keepdim=True)[0].view(B, C, 1, 1)
|
| 47 |
+
max_val = x_flat.max(dim=2, keepdim=True)[0].view(B, C, 1, 1)
|
| 48 |
+
return (x - min_val) / (max_val - min_val + 1e-8)
|
| 49 |
+
|
| 50 |
+
def _equalize(self, x: torch.Tensor) -> torch.Tensor:
|
| 51 |
+
B, C, H, W = x.shape
|
| 52 |
+
x_int = (x * 255).long().clamp(0, 255)
|
| 53 |
+
|
| 54 |
+
out = torch.zeros_like(x)
|
| 55 |
+
|
| 56 |
+
for b in range(B):
|
| 57 |
+
for c in range(C):
|
| 58 |
+
hist = torch.histc(x[b, c].float(), bins=256, min=0, max=1)
|
| 59 |
+
cdf = hist.cumsum(0)
|
| 60 |
+
cdf = cdf / cdf[-1] # 归一化
|
| 61 |
+
# 使用cdf作为查找表
|
| 62 |
+
out[b, c] = cdf[x_int[b, c]]
|
| 63 |
+
|
| 64 |
+
return out
|
| 65 |
+
|
| 66 |
+
def _solarize(self, x: torch.Tensor) -> torch.Tensor:
|
| 67 |
+
threshold = random.uniform(0.3, 0.7)
|
| 68 |
+
return torch.where(x < threshold, x, 1.0 - x)
|
| 69 |
+
|
| 70 |
+
def _color(self, x: torch.Tensor) -> torch.Tensor:
|
| 71 |
+
factor = 1.0 + (random.random() - 0.5) * 0.4
|
| 72 |
+
mean = x.mean(dim=1, keepdim=True)
|
| 73 |
+
return torch.clamp(mean + factor * (x - mean), 0, 1)
|
| 74 |
+
|
| 75 |
+
def _contrast(self, x: torch.Tensor) -> torch.Tensor:
|
| 76 |
+
factor = 1.0 + (random.random() - 0.5) * 0.4
|
| 77 |
+
# 计算整张图的均值,保留 Batch 维度
|
| 78 |
+
# view(B, -1) -> mean(1) -> view(B, 1, 1, 1)
|
| 79 |
+
mean = x.view(x.size(0), -1).mean(dim=1).view(-1, 1, 1, 1)
|
| 80 |
+
return torch.clamp(mean + factor * (x - mean), 0, 1)
|
| 81 |
+
|
| 82 |
+
def _brightness(self, x: torch.Tensor) -> torch.Tensor:
|
| 83 |
+
"""亮度"""
|
| 84 |
+
factor = 1.0 + (random.random() - 0.5) * 0.4
|
| 85 |
+
return torch.clamp(x * factor, 0, 1)
|
| 86 |
+
|
| 87 |
+
def _sharpness(self, x: torch.Tensor) -> torch.Tensor:
|
| 88 |
+
"""锐化: 通过混合原图和高斯模糊图实现"""
|
| 89 |
+
factor = 1.0 + (random.random() - 0.5) * 0.4
|
| 90 |
+
# 使用 AvgPool 模拟模糊
|
| 91 |
+
kernel_size = 3
|
| 92 |
+
pad = kernel_size // 2
|
| 93 |
+
blurred = F.avg_pool2d(x, kernel_size=kernel_size, stride=1, padding=pad)
|
| 94 |
+
return torch.clamp(x + (factor - 1.0) * (x - blurred), 0, 1)
|
| 95 |
+
|
| 96 |
+
class MixUp(nn.Module):
|
| 97 |
+
def __init__(self, alpha: float = 1.0, num_classes: Optional[int] = None):
|
| 98 |
+
super().__init__()
|
| 99 |
+
self.alpha = alpha
|
| 100 |
+
self.num_classes = num_classes
|
| 101 |
+
|
| 102 |
+
def forward(
|
| 103 |
+
self,
|
| 104 |
+
x: torch.Tensor,
|
| 105 |
+
y: Optional[torch.Tensor] = None
|
| 106 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], float]:
|
| 107 |
+
|
| 108 |
+
if self.alpha > 0:
|
| 109 |
+
lambda_ = random.betavariate(self.alpha, self.alpha)
|
| 110 |
+
else:
|
| 111 |
+
lambda_ = 1.0
|
| 112 |
+
|
| 113 |
+
batch_size = x.shape[0]
|
| 114 |
+
index = torch.randperm(batch_size, device=x.device)
|
| 115 |
+
|
| 116 |
+
mixed_x = lambda_ * x + (1 - lambda_) * x[index]
|
| 117 |
+
|
| 118 |
+
mixed_y = None
|
| 119 |
+
if y is not None:
|
| 120 |
+
# 处理标签混合
|
| 121 |
+
y_a = y
|
| 122 |
+
y_b = y[index]
|
| 123 |
+
|
| 124 |
+
if y.dtype == torch.long or y.ndim == 1:
|
| 125 |
+
if self.num_classes is None:
|
| 126 |
+
self.num_classes = int(y.max().item()) + 1
|
| 127 |
+
|
| 128 |
+
y_a = F.one_hot(y_a, num_classes=self.num_classes).float()
|
| 129 |
+
y_b = F.one_hot(y_b, num_classes=self.num_classes).float()
|
| 130 |
+
|
| 131 |
+
mixed_y = lambda_ * y_a + (1 - lambda_) * y_b
|
| 132 |
+
|
| 133 |
+
return mixed_x, mixed_y, lambda_
|
| 134 |
+
|
| 135 |
+
class CutMix(nn.Module):
|
| 136 |
+
def __init__(self, alpha: float = 1.0, num_classes: Optional[int] = None):
|
| 137 |
+
super().__init__()
|
| 138 |
+
self.alpha = alpha
|
| 139 |
+
self.num_classes = num_classes
|
| 140 |
+
|
| 141 |
+
def _rand_bbox(
|
| 142 |
+
self,
|
| 143 |
+
size: Tuple[int, ...],
|
| 144 |
+
lambda_: float
|
| 145 |
+
) -> Tuple[int, int, int, int]:
|
| 146 |
+
W = size[-1] # 兼容 [B, C, H, W]
|
| 147 |
+
H = size[-2]
|
| 148 |
+
cut_rat = math.sqrt(1.0 - lambda_)
|
| 149 |
+
cut_w = int(W * cut_rat)
|
| 150 |
+
cut_h = int(H * cut_rat)
|
| 151 |
+
|
| 152 |
+
cx = random.randint(0, W)
|
| 153 |
+
cy = random.randint(0, H)
|
| 154 |
+
|
| 155 |
+
bbx1 = torch.tensor(cx - cut_w // 2, device='cpu').clamp(0, W).item()
|
| 156 |
+
bby1 = torch.tensor(cy - cut_h // 2, device='cpu').clamp(0, H).item()
|
| 157 |
+
bbx2 = torch.tensor(cx + cut_w // 2, device='cpu').clamp(0, W).item()
|
| 158 |
+
bby2 = torch.tensor(cy + cut_h // 2, device='cpu').clamp(0, H).item()
|
| 159 |
+
|
| 160 |
+
return int(bbx1), int(bby1), int(bbx2), int(bby2)
|
| 161 |
+
|
| 162 |
+
def forward(
|
| 163 |
+
self,
|
| 164 |
+
x: torch.Tensor,
|
| 165 |
+
y: Optional[torch.Tensor] = None
|
| 166 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], float]:
|
| 167 |
+
|
| 168 |
+
if self.alpha > 0:
|
| 169 |
+
lambda_ = random.betavariate(self.alpha, self.alpha)
|
| 170 |
+
else:
|
| 171 |
+
lambda_ = 1.0
|
| 172 |
+
|
| 173 |
+
batch_size = x.shape[0]
|
| 174 |
+
index = torch.randperm(batch_size, device=x.device)
|
| 175 |
+
|
| 176 |
+
bbx1, bby1, bbx2, bby2 = self._rand_bbox(x.size(), lambda_)
|
| 177 |
+
|
| 178 |
+
x = x.clone()
|
| 179 |
+
x[:, :, bby1:bby2, bbx1:bbx2] = x[index, :, bby1:bby2, bbx1:bbx2]
|
| 180 |
+
|
| 181 |
+
H, W = x.size()[-2], x.size()[-1]
|
| 182 |
+
lambda_ = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (H * W))
|
| 183 |
+
|
| 184 |
+
mixed_y = None
|
| 185 |
+
if y is not None:
|
| 186 |
+
y_a = y
|
| 187 |
+
y_b = y[index]
|
| 188 |
+
|
| 189 |
+
if y.dtype == torch.long or y.ndim == 1:
|
| 190 |
+
if self.num_classes is None:
|
| 191 |
+
self.num_classes = int(y.max().item()) + 1
|
| 192 |
+
y_a = F.one_hot(y_a, num_classes=self.num_classes).float()
|
| 193 |
+
y_b = F.one_hot(y_b, num_classes=self.num_classes).float()
|
| 194 |
+
|
| 195 |
+
mixed_y = lambda_ * y_a + (1 - lambda_) * y_b
|
| 196 |
+
|
| 197 |
+
return x, mixed_y, lambda_
|
| 198 |
+
|
| 199 |
+
class SpecAugment(nn.Module):
|
| 200 |
+
def __init__(
|
| 201 |
+
self,
|
| 202 |
+
freq_mask_param: int = 27,
|
| 203 |
+
time_mask_param: int = 100,
|
| 204 |
+
num_freq_masks: int = 2,
|
| 205 |
+
num_time_masks: int = 2
|
| 206 |
+
):
|
| 207 |
+
super().__init__()
|
| 208 |
+
self.freq_mask_param = freq_mask_param
|
| 209 |
+
self.time_mask_param = time_mask_param
|
| 210 |
+
self.num_freq_masks = num_freq_masks
|
| 211 |
+
self.num_time_masks = num_time_masks
|
| 212 |
+
|
| 213 |
+
def forward(self, spec: torch.Tensor) -> torch.Tensor:
|
| 214 |
+
"""
|
| 215 |
+
Args:
|
| 216 |
+
spec: [B, F, T] or [B, C, F, T]
|
| 217 |
+
"""
|
| 218 |
+
input_ndim = spec.ndim
|
| 219 |
+
if input_ndim == 3:
|
| 220 |
+
spec = spec.unsqueeze(1) # [B, 1, F, T]
|
| 221 |
+
|
| 222 |
+
B, C, F, T = spec.shape
|
| 223 |
+
spec = spec.clone()
|
| 224 |
+
|
| 225 |
+
# 频率遮罩
|
| 226 |
+
for _ in range(self.num_freq_masks):
|
| 227 |
+
# 确保 mask 不超过 F
|
| 228 |
+
f_param = min(self.freq_mask_param, F)
|
| 229 |
+
f = random.randint(0, f_param)
|
| 230 |
+
f0 = random.randint(0, max(0, F - f))
|
| 231 |
+
spec[:, :, f0:f0+f, :] = 0
|
| 232 |
+
|
| 233 |
+
# 时间遮罩
|
| 234 |
+
for _ in range(self.num_time_masks):
|
| 235 |
+
# 确保 mask 不超过 T
|
| 236 |
+
t_param = min(self.time_mask_param, T)
|
| 237 |
+
t = random.randint(0, t_param)
|
| 238 |
+
t0 = random.randint(0, max(0, T - t))
|
| 239 |
+
spec[:, :, :, t0:t0+t] = 0
|
| 240 |
+
|
| 241 |
+
if input_ndim == 3:
|
| 242 |
+
return spec.squeeze(1)
|
| 243 |
+
return spec
|
| 244 |
+
|
| 245 |
+
class TemporalMasking(nn.Module):
|
| 246 |
+
"""视频的时序遮罩"""
|
| 247 |
+
def __init__(self, mask_ratio: float = 0.15):
|
| 248 |
+
super().__init__()
|
| 249 |
+
self.mask_ratio = mask_ratio
|
| 250 |
+
|
| 251 |
+
def forward(self, video: torch.Tensor) -> torch.Tensor:
|
| 252 |
+
"""
|
| 253 |
+
Args:
|
| 254 |
+
video: [B, T, C, H, W]
|
| 255 |
+
"""
|
| 256 |
+
B, T, C, H, W = video.shape
|
| 257 |
+
num_mask = int(T * self.mask_ratio)
|
| 258 |
+
if num_mask == 0:
|
| 259 |
+
return video
|
| 260 |
+
|
| 261 |
+
video = video.clone()
|
| 262 |
+
|
| 263 |
+
for b in range(B):
|
| 264 |
+
# 随机采样要遮罩的帧索引
|
| 265 |
+
mask_indices = torch.randperm(T)[:num_mask]
|
| 266 |
+
video[b, mask_indices] = 0
|
| 267 |
+
|
| 268 |
+
return video
|
| 269 |
+
|
| 270 |
+
class MultiModalAugmentation(nn.Module):
|
| 271 |
+
"""统一的多模态数据增强"""
|
| 272 |
+
def __init__(
|
| 273 |
+
self,
|
| 274 |
+
image_aug: bool = True,
|
| 275 |
+
audio_aug: bool = True,
|
| 276 |
+
video_aug: bool = True,
|
| 277 |
+
use_mixup: bool = True,
|
| 278 |
+
use_cutmix: bool = True,
|
| 279 |
+
num_classes: Optional[int] = None
|
| 280 |
+
):
|
| 281 |
+
super().__init__()
|
| 282 |
+
self.image_aug = RandAugment() if image_aug else None
|
| 283 |
+
self.audio_aug = SpecAugment() if audio_aug else None
|
| 284 |
+
self.video_aug = TemporalMasking() if video_aug else None
|
| 285 |
+
|
| 286 |
+
self.mixup = MixUp(num_classes=num_classes) if use_mixup else None
|
| 287 |
+
self.cutmix = CutMix(num_classes=num_classes) if use_cutmix else None
|
| 288 |
+
|
| 289 |
+
def forward(
|
| 290 |
+
self,
|
| 291 |
+
data: torch.Tensor,
|
| 292 |
+
modality: str,
|
| 293 |
+
labels: Optional[torch.Tensor] = None
|
| 294 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
|
| 295 |
+
"""
|
| 296 |
+
Args:
|
| 297 |
+
data: 输入数据
|
| 298 |
+
modality: 模态类型 ('image', 'audio', 'video')
|
| 299 |
+
labels: 标签(可选)
|
| 300 |
+
"""
|
| 301 |
+
if modality == 'image' and self.image_aug is not None:
|
| 302 |
+
data = self.image_aug(data)
|
| 303 |
+
elif modality == 'audio' and self.audio_aug is not None:
|
| 304 |
+
data = self.audio_aug(data)
|
| 305 |
+
elif modality == 'video' and self.video_aug is not None:
|
| 306 |
+
data = self.video_aug(data)
|
| 307 |
+
|
| 308 |
+
if self.training and labels is not None:
|
| 309 |
+
|
| 310 |
+
apply_mixup = False
|
| 311 |
+
apply_cutmix = False
|
| 312 |
+
|
| 313 |
+
p = random.random()
|
| 314 |
+
|
| 315 |
+
if self.cutmix is not None and modality == 'image':
|
| 316 |
+
if p < 0.5:
|
| 317 |
+
apply_cutmix = True
|
| 318 |
+
elif self.mixup is not None:
|
| 319 |
+
apply_mixup = True
|
| 320 |
+
elif self.mixup is not None:
|
| 321 |
+
if p < 0.5:
|
| 322 |
+
apply_mixup = True
|
| 323 |
+
|
| 324 |
+
if apply_cutmix:
|
| 325 |
+
data, labels, _ = self.cutmix(data, labels)
|
| 326 |
+
elif apply_mixup:
|
| 327 |
+
data, labels, _ = self.mixup(data, labels)
|
| 328 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
return data, labels
|