-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
231 lines (187 loc) · 6.9 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
import torch
import torch.nn as nn
import os
import time
import numpy as np
import random
import torch.nn.functional as F
import re
from pdb import set_trace
def normalize_fn(tensor, mean, std):
"""Differentiable version of torchvision.functional.normalize"""
# here we assume the color channel is in at dim=1
mean = mean[None, :, None, None]
std = std[None, :, None, None]
return tensor.sub(mean).div(std)
class NormalizeByChannelMeanStd(nn.Module):
def __init__(self, mean, std):
super(NormalizeByChannelMeanStd, self).__init__()
if not isinstance(mean, torch.Tensor):
mean = torch.tensor(mean)
if not isinstance(std, torch.Tensor):
std = torch.tensor(std)
self.register_buffer("mean", mean)
self.register_buffer("std", std)
def forward(self, tensor):
return normalize_fn(tensor, self.mean, self.std)
def extra_repr(self):
return 'mean={}, std={}'.format(self.mean, self.std)
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.enabled = True
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
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].reshape(-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
class logger(object):
def __init__(self, path, log_name="log.txt", local_rank=0):
self.path = path
self.local_rank = local_rank
self.log_name = log_name
def info(self, msg):
if self.local_rank == 0:
print(msg)
with open(os.path.join(self.path, self.log_name), 'a') as f:
f.write(msg + "\n")
def fix_bn(model, fixto):
if fixto == 'nothing':
# fix none
# fix previous three layers
pass
elif fixto == 'layer1':
# fix the first layer
for name, m in model.named_modules():
if not ("layer2" in name or "layer3" in name or "layer4" in name or "fc" in name):
m.eval()
elif fixto == 'layer2':
# fix the previous two layers
for name, m in model.named_modules():
if not ("layer3" in name or "layer4" in name or "fc" in name):
m.eval()
elif fixto == 'layer3':
# fix every layer except fc
# fix previous four layers
for name, m in model.named_modules():
if not ("layer4" in name or "fc" in name):
m.eval()
elif fixto == 'layer4':
# fix every layer except fc
# fix previous four layers
for name, m in model.named_modules():
if not ("fc" in name):
m.eval()
else:
assert False
def gatherFeatures(features, local_rank, world_size):
features_list = [torch.zeros_like(features) for _ in range(world_size)]
torch.distributed.all_gather(features_list, features)
features_list[local_rank] = features
features = torch.cat(features_list)
return features
def get_negative_mask(batch_size):
negative_mask = torch.ones((batch_size, 2 * batch_size), dtype=bool)
for i in range(batch_size):
negative_mask[i, i] = 0
negative_mask[i, i + batch_size] = 0
negative_mask = torch.cat((negative_mask, negative_mask), 0)
return negative_mask
def nt_xent(x, t=0.5, features2=None):
if features2 is None:
out = F.normalize(x, dim=-1)
d = out.size()
batch_size = d[0] // 2
out = out.view(batch_size, 2, -1).contiguous()
out_1 = out[:, 0]
out_2 = out[:, 1]
else:
batch_size = x.shape[0]
out_1 = F.normalize(x, dim=-1)
out_2 = F.normalize(features2, dim=-1)
# neg score
out = torch.cat([out_1, out_2], dim=0)
# print("temperature is {}".format(t))
neg = torch.exp(torch.mm(out, out.t().contiguous()) / t)
mask = get_negative_mask(batch_size).cuda()
neg = neg.masked_select(mask).view(2 * batch_size, -1)
# pos score
pos = torch.exp(torch.sum(out_1 * out_2, dim=-1) / t)
pos = torch.cat([pos, pos], dim=0)
# estimator g()
Ng = neg.sum(dim=-1)
# contrastive loss
loss = (- torch.log(pos / (pos + Ng)))
return loss.mean()
def getStatisticsFromTxt(txtName, num_class=1000):
statistics = [0 for _ in range(num_class)]
with open(txtName, 'r') as f:
lines = f.readlines()
for line in lines:
s = re.search(r" ([0-9]+)$", line)
if s is not None:
statistics[int(s[1])] += 1
return statistics
def gather_tensor(tensor, local_rank, world_size):
# gather features
tensor_list = [torch.zeros_like(tensor) for _ in range(world_size)]
torch.distributed.all_gather(tensor_list, tensor)
tensor_list[local_rank] = tensor
tensors = torch.cat(tensor_list)
return tensors
def getImagenetRoot(root):
if os.path.isdir(root):
pass
elif os.path.isdir("/ssd1/bansa01/imagenet_final"):
root = "/ssd1/bansa01/imagenet_final"
elif os.path.isdir("/hdd3/ziyu/imagenet"):
root = "/hdd3/ziyu/imagenet"
elif os.path.isdir("/ssd2/invert/imagenet_final/"):
root = "/ssd2/invert/imagenet_final/"
elif os.path.isdir("/home/yucheng/imagenet"):
root = "/home/yucheng/imagenet"
elif os.path.isdir("/data/datasets/ImageNet_unzip"):
root = "/data/datasets/ImageNet_unzip"
elif os.path.isdir("/scratch/user/jiangziyu/imageNet"):
root = "/scratch/user/jiangziyu/imageNet"
elif os.path.isdir("/datadrive_d/imagenet"):
root = "/datadrive_d/imagenet"
elif os.path.isdir("/datadrive_c/imagenet"):
root = "/datadrive_c/imagenet"
elif os.path.isdir("/ssd2/tianlong/zzy/data/imagenet"):
root = "/ssd2/tianlong/zzy/data/imagenet"
elif os.path.isdir("/home/xinyu/dataset/imagenet2012"):
root = "/home/xinyu/dataset/imagenet2012"
elif os.path.isdir("/home/xueq13/scratch/ziyu/ImageNet/ILSVRC/Data/CLS-LOC"):
root = "/home/xueq13/scratch/ziyu/ImageNet/ILSVRC/Data/CLS-LOC"
elif os.path.isdir("/datadrive_c/shwang/philly/data/imagnetBP"):
root = "/datadrive_c/shwang/philly/data/imagnetBP"
else:
print("No dir for imagenet")
assert False
return root