forked from yanwenheng/studies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixup.py
139 lines (109 loc) · 3.71 KB
/
mixup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
98
99
100
101
102
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Time: 2022-03-09 5:16 下午
Author: huayang
Subject:
"""
import os # noqa
import doctest # noqa
# from collections import defaultdict
# from itertools import islice
# from pathlib import Path
from typing import *
# from tqdm import tqdm
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F # noqa
__all__ = [
'mixup',
'mixup_loss',
'Mixup'
]
def mixup(x, y, alpha=1.0, mixup_y=True):
""""""
lam = np.random.beta(alpha, alpha)
batch_size = x.shape[0]
idx = np.random.permutation(batch_size)
x_shuffle, y_shuffle = x[idx], y[idx]
x_mixup = lam * x + (1 - lam) * x_shuffle
if mixup_y:
y_mixup = lam * y + (1 - lam) * y[idx]
return x_mixup, y_mixup
else:
# for mixup_loss
return x_mixup, y, y_shuffle, lam
def mixup_loss(loss_fn, x_mixup, y, y_shuffle, lam):
""""""
return lam * loss_fn(x_mixup, y) + (1 - lam) * loss_fn(x_mixup, y_shuffle)
class Mixup(nn.Module):
"""@Pytorch Utils
mixup 数据增强策略
Examples:
# 示例1: 在数据中混合 y(论文中的用法)
```python
# train in one step
mixup = Mixup(manifold_mixup=True)
for x, y in data_loader:
x, y = mixup(x, y)
x = model(x)
loss = loss_fn(x, y) # 法1)推荐用法
# loss = mixup.compute_loss(loss_fn, x, y) # 法2)when `manifold_mixup` is False
# 法1 是论文中提出的方法,法2 是论文代码中的实现方式;
# 以上两种计算 loss 的方法在使用 交叉熵 损失时是等价的;
# > https://github.com/facebookresearch/mixup-cifar10/issues/18
...
```
# 示例:Manifold Mixup,用于中间层混合
```
class ExampleModel(nn.Module):
def __init__(self, n_layers):
super().__init__()
self.n_layers = n_layers
self.layers = nn.ModuleList([nn.Linear(3, 5) for _ in range(self.n_layers)])
self.mixup = Mixup(manifold_mixup=True)
self.loss_fn = nn.CrossEntropyLoss()
def forward(self, x, y):
mixup_layer = np.random.randint(self.n_layers)
for idx, layer in enumerate(self.layers):
# mixup once
if idx == mixup_layer:
x, y = self.mixup(x, y)
x = layer(x)
return self.loss_fn(x, y)
```
References:
- https://github.com/facebookresearch/mixup-cifar10/blob/main/train.py
- https://github.com/vikasverma1077/manifold_mixup/blob/master/supervised/utils.py
"""
lam: Optional[float] = None
y_shuffle: Optional[torch.Tensor] = None
def __init__(self, manifold_mixup=True, alpha=1.0):
super().__init__()
assert alpha > 0., '`alpha` should be > 0.'
self.alpha = alpha
self.manifold_mixup = manifold_mixup
def forward(self, x, y):
""""""
if not self.training:
return x, y
if self.manifold_mixup:
x_mixup, y_mixup = mixup(x, y, self.alpha, mixup_y=True)
return x_mixup, y_mixup
else:
x_mixup, y, y_shuffle, lam = mixup(x, y, self.alpha, mixup_y=False)
self.y_shuffle, self.lam = y_shuffle, lam
return x_mixup, y
def compute_loss(self, loss_fn, x, y):
""""""
if self.manifold_mixup:
return loss_fn(x, y)
else:
return mixup_loss(loss_fn, x, y, self.y_shuffle, self.lam)
def _test():
""""""
doctest.testmod()
if __name__ == '__main__':
""""""
_test()