-
Notifications
You must be signed in to change notification settings - Fork 32
/
resnet.py
191 lines (148 loc) · 7.21 KB
/
resnet.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
import os
import random
import numpy as np
## Adapted from https://github.com/joaomonteirof/e2e_antispoofing
class SelfAttention(nn.Module):
def __init__(self, hidden_size, mean_only=False):
super(SelfAttention, self).__init__()
#self.output_size = output_size
self.hidden_size = hidden_size
self.att_weights = nn.Parameter(torch.Tensor(1, hidden_size),requires_grad=True)
self.mean_only = mean_only
init.kaiming_uniform_(self.att_weights)
def forward(self, inputs):
batch_size = inputs.size(0)
weights = torch.bmm(inputs, self.att_weights.permute(1, 0).unsqueeze(0).repeat(batch_size, 1, 1))
if inputs.size(0)==1:
attentions = F.softmax(torch.tanh(weights),dim=1)
weighted = torch.mul(inputs, attentions.expand_as(inputs))
else:
attentions = F.softmax(torch.tanh(weights.squeeze()),dim=1)
weighted = torch.mul(inputs, attentions.unsqueeze(2).expand_as(inputs))
if self.mean_only:
return weighted.sum(1)
else:
noise = 1e-5*torch.randn(weighted.size())
if inputs.is_cuda:
noise = noise.to(inputs.device)
avg_repr, std_repr = weighted.sum(1), (weighted+noise).std(1)
representations = torch.cat((avg_repr,std_repr),1)
return representations
class PreActBlock(nn.Module):
'''Pre-activation version of the BasicBlock.'''
expansion = 1
def __init__(self, in_planes, planes, stride, *args, **kwargs):
super(PreActBlock, self).__init__()
self.bn1 = nn.BatchNorm2d(in_planes)
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False))
def forward(self, x):
out = F.relu(self.bn1(x))
shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x
out = self.conv1(out)
out = self.conv2(F.relu(self.bn2(out)))
out += shortcut
return out
class PreActBottleneck(nn.Module):
'''Pre-activation version of the original Bottleneck module.'''
expansion = 4
def __init__(self, in_planes, planes, stride, *args, **kwargs):
super(PreActBottleneck, self).__init__()
self.bn1 = nn.BatchNorm2d(in_planes)
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False))
def forward(self, x):
out = F.relu(self.bn1(x))
shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x
out = self.conv1(out)
out = self.conv2(F.relu(self.bn2(out)))
out = self.conv3(F.relu(self.bn3(out)))
out += shortcut
return out
def conv3x3(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
def conv1x1(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
RESNET_CONFIGS = {'18': [[2, 2, 2, 2], PreActBlock],
'28': [[3, 4, 6, 3], PreActBlock],
'34': [[3, 4, 6, 3], PreActBlock],
'50': [[3, 4, 6, 3], PreActBottleneck],
'101': [[3, 4, 23, 3], PreActBottleneck]
}
def setup_seed(random_seed, cudnn_deterministic=True):
# initialization
torch.manual_seed(random_seed)
random.seed(random_seed)
np.random.seed(random_seed)
os.environ['PYTHONHASHSEED'] = str(random_seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(random_seed)
torch.backends.cudnn.deterministic = cudnn_deterministic
torch.backends.cudnn.benchmark = False
class ResNet(nn.Module):
def __init__(self, num_nodes, enc_dim, resnet_type='18', nclasses=2):
self.in_planes = 16
super(ResNet, self).__init__()
layers, block = RESNET_CONFIGS[resnet_type]
self._norm_layer = nn.BatchNorm2d
self.conv1 = nn.Conv2d(1, 16, kernel_size=(9, 3), stride=(3, 1), padding=(1, 1), bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.activation = nn.ReLU()
self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.conv5 = nn.Conv2d(512 * block.expansion, 256, kernel_size=(num_nodes, 3), stride=(1, 1), padding=(0, 1),
bias=False)
self.bn5 = nn.BatchNorm2d(256)
self.fc = nn.Linear(256 * 2, enc_dim)
self.fc_mu = nn.Linear(enc_dim, nclasses) if nclasses >= 2 else nn.Linear(enc_dim, 1)
self.initialize_params()
self.attention = SelfAttention(256)
def initialize_params(self):
for layer in self.modules():
if isinstance(layer, torch.nn.Conv2d):
init.kaiming_normal_(layer.weight, a=0, mode='fan_out')
elif isinstance(layer, torch.nn.Linear):
init.kaiming_uniform_(layer.weight)
elif isinstance(layer, torch.nn.BatchNorm2d) or isinstance(layer, torch.nn.BatchNorm1d):
layer.weight.data.fill_(1)
layer.bias.data.zero_()
def _make_layer(self, block, planes, num_blocks, stride=1):
norm_layer = self._norm_layer
downsample = None
if stride != 1 or self.in_planes != planes * block.expansion:
downsample = nn.Sequential(conv1x1(self.in_planes, planes * block.expansion, stride),
norm_layer(planes * block.expansion))
layers = []
layers.append(block(self.in_planes, planes, stride, downsample, 1, 64, 1, norm_layer))
self.in_planes = planes * block.expansion
for _ in range(1, num_blocks):
layers.append(
block(self.in_planes, planes, 1, groups=1, base_width=64, dilation=False, norm_layer=norm_layer))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.activation(self.bn1(x))
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.conv5(x)
x = self.activation(self.bn5(x)).squeeze(2)
stats = self.attention(x.permute(0, 2, 1).contiguous())
feat = self.fc(stats)
mu = self.fc_mu(feat)
return feat, mu