forked from stovecat/CGS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_m2m.py
349 lines (278 loc) · 10.1 KB
/
utils_m2m.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
'''Some helper functions for PyTorch, including:
- get_mean_and_std: calculate the mean and std value of dataset.
- msr_init: net parameter initialization.
- progress_bar: progress bar mimic xlua.progress.
'''
import os
import sys
import time
from datetime import datetime
import shutil
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
import numpy as np
import importlib
def source_import(file_path):
"""This function imports python module directly from source code using importlib"""
spec = importlib.util.spec_from_file_location('', file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def sum_t(tensor):
return tensor.float().sum().item()
class InputNormalize(nn.Module):
'''
A module (custom layer) for normalizing the input to have a fixed
mean and standard deviation (user-specified).
'''
def __init__(self, new_mean, new_std):
super(InputNormalize, self).__init__()
new_std = new_std[..., None, None].cuda()
new_mean = new_mean[..., None, None].cuda()
# To prevent the updates the mean, std
self.register_buffer("new_mean", new_mean)
self.register_buffer("new_std", new_std)
def forward(self, x):
x = torch.clamp(x, 0, 1)
x_normalized = (x - self.new_mean)/self.new_std
return x_normalized
class Logger(object):
"""Reference: https://gist.github.com/gyglim/1f8dfb1b5c82627ae3efcfbbadb9f514"""
def __init__(self, fn):
if not os.path.exists("./logs/"):
os.mkdir("./logs/")
logdir = 'logs/' + fn
if not os.path.exists(logdir):
os.mkdir(logdir)
if len(os.listdir(logdir)) != 0:
ans = input("log_dir is not empty. All data inside log_dir will be deleted. "
"Will you proceed [y/N]? ")
if ans in ['y', 'Y']:
shutil.rmtree(logdir)
else:
exit(1)
self.set_dir(logdir)
def set_dir(self, logdir, log_fn='log.txt'):
self.logdir = logdir
if not os.path.exists(logdir):
os.mkdir(logdir)
self.log_file = open(os.path.join(logdir, log_fn), 'a')
def log(self, string):
self.log_file.write('[%s] %s' % (datetime.now(), string) + '\n')
self.log_file.flush()
print('[%s] %s' % (datetime.now(), string))
sys.stdout.flush()
def log_dirname(self, string):
self.log_file.write('%s (%s)' % (string, self.logdir) + '\n')
self.log_file.flush()
print('%s (%s)' % (string, self.logdir))
sys.stdout.flush()
######## Loss ########
def soft_cross_entropy(input, labels, reduction='mean'):
xent = (-labels * F.log_softmax(input, dim=1)).sum(1)
if reduction == 'sum':
return xent.sum()
elif reduction == 'mean':
return xent.mean()
elif reduction == 'none':
return xent
else:
raise NotImplementedError()
def classwise_loss(outputs, targets):
out_1hot = torch.ones_like(outputs)
out_1hot.scatter_(1, targets.view(-1, 1), -1)
return (outputs * out_1hot).mean()
def focal_loss(input_values, gamma):
"""Computes the focal loss
Reference: https://github.com/kaidic/LDAM-DRW/blob/master/losses.py
"""
p = torch.exp(-input_values)
loss = (1 - p) ** gamma * input_values
return loss
class FocalLoss(nn.Module):
"""Reference: https://github.com/kaidic/LDAM-DRW/blob/master/losses.py"""
def __init__(self, weight=None, gamma=0., reduction='mean'):
super(FocalLoss, self).__init__()
assert gamma >= 0
self.gamma = gamma
self.weight = weight
self.reduction = reduction
def forward(self, input, target):
return focal_loss(F.cross_entropy(input, target, weight=self.weight, reduction=self.reduction), self.gamma)
class LDAMLoss(nn.Module):
"""Reference: https://github.com/kaidic/LDAM-DRW/blob/master/losses.py"""
def __init__(self, cls_num_list, max_m=0.5, weight=None, s=30, reduction='mean'):
super(LDAMLoss, self).__init__()
m_list = 1.0 / np.sqrt(np.sqrt(cls_num_list))
m_list = m_list * (max_m / np.max(m_list))
m_list = torch.cuda.FloatTensor(m_list)
self.m_list = m_list
self.scale = s
self.weight = weight
self.reduction = reduction
def forward(self, x, target):
index = torch.zeros_like(x, dtype=torch.uint8)
index.scatter_(1, target.data.view(-1, 1), 1)
index_float = index.type(torch.cuda.FloatTensor)
batch_m = torch.matmul(self.m_list[None, :], index_float.transpose(0, 1))
batch_m = batch_m.view((-1, 1))
x_m = x - batch_m
output = torch.where(index, x_m, x)
return F.cross_entropy(self.scale * output, target, weight=self.weight, reduction=self.reduction)
######## Generation ########
def project(inputs, orig_inputs, attack, eps):
diff = inputs - orig_inputs
if attack == 'l2':
diff = diff.renorm(p=2, dim=0, maxnorm=eps)
elif attack == 'inf':
diff = torch.clamp(diff, -eps, eps)
return orig_inputs + diff
def make_step(grad, attack, step_size):
if attack == 'l2':
#print('Grade Shape', grad.shape)
#grad_norm = torch.norm(grad.view(grad.shape[0], -1), dim=1).view(-1, 1, 1, 1)
grad_norm = torch.norm(grad.view(grad.shape[0], -1), dim=1).view(-1, 1)
#print('Grade Norm', grad_norm.shape)
scaled_grad = grad / (grad_norm + 1e-10)
#print('Scaled Grade', scaled_grad.shape)
step = step_size * scaled_grad
#print('Step size', step.shape)
elif attack == 'inf':
step = step_size * torch.sign(grad)
else:
step = step_size * grad
return step
def random_perturb(inputs, attack, eps):
if attack == 'inf':
r_inputs = 2 * (torch.rand_like(inputs) - 0.5) * eps
else:
r_inputs = (torch.rand_like(inputs) - 0.5).renorm(p=2, dim=1, maxnorm=eps)
return r_inputs
######## Data ########
def make_imb_data(max_num, min_num, class_num, gamma):
class_idx = torch.arange(1, class_num + 1).float()
ratio = max_num / min_num
b = (torch.pow(class_idx[-1], gamma) - ratio) / (ratio - 1)
a = max_num * (1 + b)
class_num_list = []
for i in range(class_num):
class_num_list.append(int(torch.round(a / (torch.pow(class_idx[i], gamma) + b))))
print(class_num_list)
return list(class_num_list)
def make_imb_data2(max_num, class_num, gamma):
mu = np.power(1/gamma, 1/(class_num - 1))
print(mu)
class_num_list = []
for i in range(class_num):
class_num_list.append(int(max_num * np.power(mu, i)))
return list(class_num_list)
def inf_data_gen(dataloader):
while True:
for images, targets in dataloader:
yield images, targets
def source_import(file_path):
"""This function imports python module directly from source code using importlib"""
spec = importlib.util.spec_from_file_location('', file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def get_mean_and_std(dataset):
'''Compute the mean and std value of dataset.'''
dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True, num_workers=2)
mean = torch.zeros(3)
std = torch.zeros(3)
print('==> Computing mean and std..')
for inputs, targets in dataloader:
for i in range(3):
mean[i] += inputs[:,i,:,:].mean()
std[i] += inputs[:,i,:,:].std()
mean.div_(len(dataset))
std.div_(len(dataset))
return mean, std
def init_params(net):
'''Init layer parameters.'''
for m in net.modules():
if isinstance(m, nn.Conv2d):
init.kaiming_normal(m.weight, mode='fan_out')
if m.bias:
init.constant(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant(m.weight, 1)
init.constant(m.bias, 0)
elif isinstance(m, nn.Linear):
init.normal(m.weight, std=1e-3)
if m.bias:
init.constant(m.bias, 0)
#_, term_width = os.popen('stty size', 'r').read().split()
term_width = int(145)
TOTAL_BAR_LENGTH = 50.
last_time = time.time()
begin_time = last_time
def progress_bar(current, total, msg=None):
global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.
cur_len = int(TOTAL_BAR_LENGTH*current/total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1
sys.stdout.write(' [')
for i in range(cur_len):
sys.stdout.write('=')
sys.stdout.write('>')
for i in range(rest_len):
sys.stdout.write('.')
sys.stdout.write(']')
cur_time = time.time()
step_time = cur_time - last_time
last_time = cur_time
tot_time = cur_time - begin_time
L = []
L.append(' Step: %s' % format_time(step_time))
L.append(' | Tot: %s' % format_time(tot_time))
if msg:
L.append(' | ' + msg)
msg = ''.join(L)
sys.stdout.write(msg)
for i in range(term_width-int(TOTAL_BAR_LENGTH)-len(msg)-3):
sys.stdout.write(' ')
# Go back to the center of the bar.
for i in range(term_width-int(TOTAL_BAR_LENGTH/2)):
sys.stdout.write('\b')
sys.stdout.write(' %d/%d ' % (current+1, total))
if current < total-1:
sys.stdout.write('\r')
else:
sys.stdout.write('\n')
sys.stdout.flush()
def format_time(seconds):
days = int(seconds / 3600/24)
seconds = seconds - days*3600*24
hours = int(seconds / 3600)
seconds = seconds - hours*3600
minutes = int(seconds / 60)
seconds = seconds - minutes*60
secondsf = int(seconds)
seconds = seconds - secondsf
millis = int(seconds*1000)
f = ''
i = 1
if days > 0:
f += str(days) + 'D'
i += 1
if hours > 0 and i <= 2:
f += str(hours) + 'h'
i += 1
if minutes > 0 and i <= 2:
f += str(minutes) + 'm'
i += 1
if secondsf > 0 and i <= 2:
f += str(secondsf) + 's'
i += 1
if millis > 0 and i <= 2:
f += str(millis) + 'ms'
i += 1
if f == '':
f = '0ms'
return f