-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalization.py
65 lines (53 loc) · 1.91 KB
/
normalization.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
"""Implement AdvProp Two BNs scheme."""
from functools import partial
import torch
import torch.nn as nn
def to_status(m: nn.Module, status: str) -> None:
"""Check `MixBatchNorm2d` for details."""
if isinstance(m, MixBatchNorm2d):
m.set_batch_type(status)
to_clean_status = partial(to_status, status="clean")
to_adv_status = partial(to_status, status="adv")
to_mix_status = partial(to_status, status="mix")
class MixBatchNorm2d(nn.BatchNorm2d):
"""BatchNorm2d with original & auxiliary branches.
`batch_type` must match the input type, and could be the following values:
- `clean`: use the original BNs
- `adv`: use the auxiliary BNs
- `mix`: the first half of the inputs use the original BNs, while the others use the auxiliary BNs.
"""
def __init__(
self,
num_features,
eps=1e-5,
momentum=0.1,
affine=True,
track_running_stats=True,
):
super().__init__(
num_features, eps, momentum, affine, track_running_stats
)
self.aux_bn = nn.BatchNorm2d(
num_features,
eps=eps,
momentum=momentum,
affine=affine,
track_running_stats=track_running_stats,
)
self.set_batch_type("clean")
def set_batch_type(self, batch_type: str):
if batch_type not in ["adv", "clean", "mix"]:
raise ValueError()
self.batch_type = batch_type
def forward(self, input):
if self.batch_type == "adv":
input = self.aux_bn(input)
elif self.batch_type == "clean":
input = super().forward(input)
else:
assert self.batch_type == "mix"
batch_size = input.shape[0]
input0 = super().forward(input[: batch_size // 2])
input1 = self.aux_bn(input[batch_size // 2 :])
input = torch.cat((input0, input1), 0)
return input