-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresnet.py
311 lines (261 loc) · 12.9 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
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
# Copyright (c) 2023-present, Royal Bank of Canada.
# Copyright (c) 2021-present, Yuzhe Yang
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
########################################################################################
# Code is based on the LDS and FDS (https://arxiv.org/pdf/2102.09554.pdf) implementation
# from https://github.com/YyzHarry/imbalanced-regression/tree/main/imdb-wiki-dir
# by Yuzhe Yang et al.
########################################################################################
import math
import numpy as np
from scipy.ndimage import gaussian_filter1d
from scipy.signal.windows import triang
import torch
import torch.nn as nn
import torch.nn.functional as F
from utils import calibrate_mean_var
import logging
print = logging.info
class FDS(nn.Module):
def __init__(self, feature_dim, bucket_num=100, bucket_start=0, start_update=0, start_smooth=1,
kernel='gaussian', ks=5, sigma=2, momentum=0.9):
super(FDS, self).__init__()
self.feature_dim = feature_dim
self.bucket_num = bucket_num
self.bucket_start = bucket_start
self.kernel_window = self._get_kernel_window(kernel, ks, sigma)
self.half_ks = (ks - 1) // 2
self.momentum = momentum
self.start_update = start_update
self.start_smooth = start_smooth
self.register_buffer('epoch', torch.zeros(1).fill_(start_update))
self.register_buffer('running_mean', torch.zeros(bucket_num - bucket_start, feature_dim))
self.register_buffer('running_var', torch.ones(bucket_num - bucket_start, feature_dim))
self.register_buffer('running_mean_last_epoch', torch.zeros(bucket_num - bucket_start, feature_dim))
self.register_buffer('running_var_last_epoch', torch.ones(bucket_num - bucket_start, feature_dim))
self.register_buffer('smoothed_mean_last_epoch', torch.zeros(bucket_num - bucket_start, feature_dim))
self.register_buffer('smoothed_var_last_epoch', torch.ones(bucket_num - bucket_start, feature_dim))
self.register_buffer('num_samples_tracked', torch.zeros(bucket_num - bucket_start))
@staticmethod
def _get_kernel_window(kernel, ks, sigma):
assert kernel in ['gaussian', 'triang', 'laplace']
half_ks = (ks - 1) // 2
if kernel == 'gaussian':
base_kernel = [0.] * half_ks + [1.] + [0.] * half_ks
base_kernel = np.array(base_kernel, dtype=np.float32)
kernel_window = gaussian_filter1d(base_kernel, sigma=sigma) / sum(gaussian_filter1d(base_kernel, sigma=sigma))
elif kernel == 'triang':
kernel_window = triang(ks) / sum(triang(ks))
else:
laplace = lambda x: np.exp(-abs(x) / sigma) / (2. * sigma)
kernel_window = list(map(laplace, np.arange(-half_ks, half_ks + 1))) / sum(map(laplace, np.arange(-half_ks, half_ks + 1)))
print(f'Using FDS: [{kernel.upper()}] ({ks}/{sigma})')
return torch.tensor(kernel_window, dtype=torch.float32).cuda()
def _update_last_epoch_stats(self):
self.running_mean_last_epoch = self.running_mean
self.running_var_last_epoch = self.running_var
self.smoothed_mean_last_epoch = F.conv1d(
input=F.pad(self.running_mean_last_epoch.unsqueeze(1).permute(2, 1, 0),
pad=(self.half_ks, self.half_ks), mode='reflect'),
weight=self.kernel_window.view(1, 1, -1), padding=0
).permute(2, 1, 0).squeeze(1)
self.smoothed_var_last_epoch = F.conv1d(
input=F.pad(self.running_var_last_epoch.unsqueeze(1).permute(2, 1, 0),
pad=(self.half_ks, self.half_ks), mode='reflect'),
weight=self.kernel_window.view(1, 1, -1), padding=0
).permute(2, 1, 0).squeeze(1)
def reset(self):
self.running_mean.zero_()
self.running_var.fill_(1)
self.running_mean_last_epoch.zero_()
self.running_var_last_epoch.fill_(1)
self.smoothed_mean_last_epoch.zero_()
self.smoothed_var_last_epoch.fill_(1)
self.num_samples_tracked.zero_()
def update_last_epoch_stats(self, epoch):
if epoch == self.epoch + 1:
self.epoch += 1
self._update_last_epoch_stats()
print(f"Updated smoothed statistics on Epoch [{epoch}]!")
def update_running_stats(self, features, labels, epoch):
if epoch < self.epoch:
return
assert self.feature_dim == features.size(1), "Input feature dimension is not aligned!"
assert features.size(0) == labels.size(0), "Dimensions of features and labels are not aligned!"
for label in torch.unique(labels):
if label > self.bucket_num - 1 or label < self.bucket_start:
continue
elif label == self.bucket_start:
curr_feats = features[labels <= label]
elif label == self.bucket_num - 1:
curr_feats = features[labels >= label]
else:
curr_feats = features[labels == label]
curr_num_sample = curr_feats.size(0)
curr_mean = torch.mean(curr_feats, 0)
curr_var = torch.var(curr_feats, 0, unbiased=True if curr_feats.size(0) != 1 else False)
self.num_samples_tracked[int(label - self.bucket_start)] += curr_num_sample
factor = self.momentum if self.momentum is not None else \
(1 - curr_num_sample / float(self.num_samples_tracked[int(label - self.bucket_start)]))
factor = 0 if epoch == self.start_update else factor
self.running_mean[int(label - self.bucket_start)] = \
(1 - factor) * curr_mean + factor * self.running_mean[int(label - self.bucket_start)]
self.running_var[int(label - self.bucket_start)] = \
(1 - factor) * curr_var + factor * self.running_var[int(label - self.bucket_start)]
print(f"Updated running statistics with Epoch [{epoch}] features!")
def smooth(self, features, labels, epoch):
if epoch < self.start_smooth:
return features
labels = labels.squeeze(1)
for label in torch.unique(labels):
if label > self.bucket_num - 1 or label < self.bucket_start:
continue
elif label == self.bucket_start:
features[labels <= label] = calibrate_mean_var(
features[labels <= label],
self.running_mean_last_epoch[int(label - self.bucket_start)],
self.running_var_last_epoch[int(label - self.bucket_start)],
self.smoothed_mean_last_epoch[int(label - self.bucket_start)],
self.smoothed_var_last_epoch[int(label - self.bucket_start)])
elif label == self.bucket_num - 1:
features[labels >= label] = calibrate_mean_var(
features[labels >= label],
self.running_mean_last_epoch[int(label - self.bucket_start)],
self.running_var_last_epoch[int(label - self.bucket_start)],
self.smoothed_mean_last_epoch[int(label - self.bucket_start)],
self.smoothed_var_last_epoch[int(label - self.bucket_start)])
else:
features[labels == label] = calibrate_mean_var(
features[labels == label],
self.running_mean_last_epoch[int(label - self.bucket_start)],
self.running_var_last_epoch[int(label - self.bucket_start)],
self.smoothed_mean_last_epoch[int(label - self.bucket_start)],
self.smoothed_var_last_epoch[int(label - self.bucket_start)])
return features
def conv3x3(in_planes, out_planes, stride=1):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, layers, fds, bucket_num, bucket_start, start_update, start_smooth,
kernel, ks, sigma, momentum, dropout=None, return_features=False):
self.inplanes = 64
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
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.avgpool = nn.AvgPool2d(7, stride=1)
self.linear = nn.Linear(512 * block.expansion, 1)
if fds:
self.FDS = FDS(
feature_dim=512 * block.expansion, bucket_num=bucket_num, bucket_start=bucket_start,
start_update=start_update, start_smooth=start_smooth, kernel=kernel, ks=ks, sigma=sigma, momentum=momentum
)
self.fds = fds
self.start_smooth = start_smooth
self.use_dropout = True if dropout else False
if self.use_dropout:
print(f'Using dropout: {dropout}')
self.dropout = nn.Dropout(p=dropout)
self.return_features = return_features
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x, targets=None, epoch=None,reg = True):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
encoding = x.view(x.size(0), -1)
encoding_s = encoding
if self.training and self.fds and reg:
if epoch >= self.start_smooth:
encoding_s = self.FDS.smooth(encoding_s, targets, epoch)
if self.use_dropout:
encoding_s = self.dropout(encoding_s)
x = self.linear(encoding_s)
return x, encoding
def resnet50(**kwargs):
return ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)