-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
285 lines (209 loc) · 8.49 KB
/
utils.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
from PIL import Image
from torch.utils.data import Dataset
import numpy as np
import torch
import torch.utils
from torchvision.datasets.folder import default_loader
import torch.nn as nn
import torch.distributed as dist
import torch.nn.functional as F
import sys
def reduce_mean(tensor, nprocs):
rt = tensor.clone()
dist.all_reduce(rt, op=dist.ReduceOp.SUM)
rt /= nprocs
return rt
def accuracy(output, target, topk=(1, )):
"""Computes the precision@k for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].contiguous().view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def validate(val_loader, model, criterion, local_rank=None, nprocs=None):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
with torch.no_grad():
for i, (input, target) in enumerate(val_loader):
if local_rank==None:
target = target.cuda()
input = input.cuda()
else:
target = target.cuda(local_rank)
input = input.cuda(local_rank)
# compute output
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
if nprocs!=None:
reduced_loss = reduce_mean(loss, nprocs)
reduced_prec1 = reduce_mean(prec1, nprocs)
reduced_prec5 = reduce_mean(prec5, nprocs)
losses.update(reduced_loss.item(), input.size(0))
top1.update(reduced_prec1.item(), input.size(0))
top5.update(reduced_prec5.item(), input.size(0))
else:
losses.update(loss.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
top5.update(prec5.item(), input.size(0))
# if local_rank == None or local_rank == 0:
# print(
# ' **Test** Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f} Error@1 {error1:.3f}'
# .format(top1=top1, top5=top5, error1=100 - top1.avg))
return top1.avg, top5.avg, losses.avg
def validate_tro(val_loader, grid, noise, target_class, model, criterion, local_rank=None, nprocs=None):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
with torch.no_grad():
for i, (input, target) in enumerate(val_loader):
if local_rank==None:
target = target.cuda() * 0 + target_class
input = input.cuda()
else:
target = target.cuda(local_rank) * 0 + target_class
input = input.cuda(local_rank)
# compute output
output = model(F.grid_sample(torch.clamp(input+noise, min=0.0, max=1.0),
grid.repeat(input.shape[0], 1, 1, 1)))
loss = criterion(output, target)
# measure accuracy and record loss
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
if nprocs!=None:
reduced_loss = reduce_mean(loss, nprocs)
reduced_prec1 = reduce_mean(prec1, nprocs)
reduced_prec5 = reduce_mean(prec5, nprocs)
losses.update(reduced_loss.item(), input.size(0))
top1.update(reduced_prec1.item(), input.size(0))
top5.update(reduced_prec5.item(), input.size(0))
else:
losses.update(loss.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
top5.update(prec5.item(), input.size(0))
return top1.avg, top5.avg, losses.avg
def validate_warp_untarget(val_loader, grid, model, criterion, local_rank=None, nprocs=None):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
with torch.no_grad():
for i, (input, target) in enumerate(val_loader):
if local_rank==None:
target = target.cuda()
input = input.cuda()
else:
target = target.cuda(local_rank)
input = input.cuda(local_rank)
# compute output
output = model(F.grid_sample(input, grid.repeat(input.shape[0], 1, 1, 1)))
loss = criterion(output, target)
# measure accuracy and record loss
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
if nprocs!=None:
reduced_loss = reduce_mean(loss, nprocs)
reduced_prec1 = reduce_mean(prec1, nprocs)
reduced_prec5 = reduce_mean(prec5, nprocs)
losses.update(reduced_loss.item(), input.size(0))
top1.update(reduced_prec1.item(), input.size(0))
top5.update(reduced_prec5.item(), input.size(0))
else:
losses.update(loss.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
top5.update(prec5.item(), input.size(0))
return top1.avg, top5.avg, losses.avg
class ImageFolder_cifar10(Dataset):
def __init__(self, samples, targets, transform=None):
self.samples = samples
self.transform = transform
self.target = targets
def __getitem__(self, index):
sample = self.samples[index]
if self.transform is not None:
sample = self.transform(Image.fromarray(sample))
return sample, self.target[index]
def __len__(self):
return len(self.samples)
class ImageFolder_svhn(Dataset):
def __init__(self, samples, targets, transform=None):
self.samples = samples
self.transform = transform
self.target = targets
def __getitem__(self, index):
sample = self.samples[index].transpose((1, 2, 0))
if self.transform is not None:
sample = self.transform(Image.fromarray(sample))
return sample, self.target[index]
def __len__(self):
return len(self.samples)
class ImageFolder_imagenet(Dataset):
def __init__(self, paths, targets, transform=None):
self.paths = paths
self.transform = transform
self.target = targets
def __getitem__(self, index):
sample = default_loader(self.paths[index])
if self.transform is not None:
sample = self.transform(sample)
return sample, self.target[index]
def __len__(self):
return len(self.paths)
def project_box(x):
xp = x
xp[x>1]=1
xp[x<0]=0
return xp
def project_shifted_Lp_ball(x, p=2):
shift_vec = 1/2*np.ones(x.size)
shift_x = x-shift_vec
normp_shift = np.linalg.norm(shift_x, p)
n = x.size
xp = (n**(1/p)) * shift_x / (2*normp_shift) + shift_vec
return xp
def project_positive(x):
xp = np.clip(x, 0, None)
return xp
# simple Module to normalize an image
class Normalize(nn.Module):
def __init__(self, mean, std):
super(Normalize, self).__init__()
self.mean = torch.tensor(mean)
self.std = torch.tensor(std)
def forward(self, x):
return (x - self.mean.type_as(x)[None, :, None, None]) / self.std.type_as(x)[None, :, None, None]
class Logger(object):
def __init__(self, filename="log.txt"):
self.terminal = sys.stdout
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
self.flush()
def flush(self):
self.log.flush()