-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
360 lines (309 loc) · 11.3 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
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
350
351
352
353
354
355
356
357
358
359
360
import os
import random
from copy import deepcopy
import numpy as np
import torch
from PIL import Image, ImageDraw
from pycocotools.coco import COCO
from torchvision import datasets as datasets
from config import cfg
from log import logger
def average_precision(output, target):
epsilon = 1e-8
# sort examples
indices = output.argsort()[::-1]
# Computes prec@i
total_count_ = np.cumsum(np.ones((len(output), 1)))
target_ = target[indices]
ind = target_ == 1
pos_count_ = np.cumsum(ind)
total = pos_count_[-1]
pos_count_[np.logical_not(ind)] = 0 # type: ignore
pp = pos_count_ / total_count_
precision_at_i_ = np.sum(pp)
precision_at_i = precision_at_i_ / (total + epsilon)
return precision_at_i
def mAP(targs, preds):
"""Returns the model's average precision for each class
Return:
ap (FloatTensor): 1xK tensor, with avg precision for each class k
"""
if np.size(preds) == 0:
return 0
ap = np.zeros((preds.shape[1]))
# compute average precision for each class
for k in range(preds.shape[1]):
# sort scores
scores = preds[:, k]
targets = targs[:, k]
# compute average precision
ap[k] = average_precision(scores, targets)
return 100 * ap.mean()
class AverageMeter(object):
def __init__(self):
self.val = None
self.sum = None
self.cnt = None
self.avg = None
self.ema = None
self.initialized = False
def update(self, val, n=1):
if not self.initialized:
self.initialize(val, n)
else:
self.add(val, n)
def initialize(self, val, n):
self.val = val
self.sum = val * n
self.cnt = n
self.avg = val
self.ema = val
self.initialized = True
def add(self, val, n):
self.val = val
self.sum += val * n
self.cnt += n
self.avg = self.sum / self.cnt
self.ema = self.ema * 0.99 + self.val * 0.01 # type: ignore
class CocoDetection(datasets.coco.CocoDetection):
def __init__(self, root, annFile, transform=None, target_transform=None):
self.root = root
self.coco = COCO(annFile)
self.ids = list(self.coco.imgToAnns.keys())
self.transform = transform
self.target_transform = target_transform
self.cat2cat = dict()
for cat in self.coco.cats.keys():
self.cat2cat[cat] = len(self.cat2cat)
def labels(self):
return [v["name"] for v in self.coco.cats.values()]
def __getitem__(self, index):
coco = self.coco
img_id = self.ids[index]
ann_ids = coco.getAnnIds(imgIds=img_id)
target = coco.loadAnns(ann_ids)
output = torch.zeros((3, 80), dtype=torch.long)
for obj in target: # type: ignore
if obj['area'] < 32 * 32:
output[0][self.cat2cat[obj['category_id']]] = 1
elif obj['area'] < 96 * 96:
output[1][self.cat2cat[obj['category_id']]] = 1
else:
output[2][self.cat2cat[obj['category_id']]] = 1
target = output
path = coco.loadImgs(img_id)[0]['file_name'] # type: ignore
img = Image.open(os.path.join(self.root, path)).convert('RGB')
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
target = target.max(dim=0)[0]
return img, target
class COCO_missing_dataset(torch.utils.data.Dataset): # type: ignore
def __init__(self,
root,
annFile,
transform=None,
target_transform=None,
class_num: int = -1):
self.root = root
with open(annFile, 'r') as f:
names = f.readlines()
# name = names.strip('\n').split(' ')
self.name = names
# self.label = name[:,1]
self.transform = transform
self.class_num = class_num
self.target_transform = target_transform
def __getitem__(self, index):
name = self.name[index]
path = name.strip('\n').split(',')[0]
num = name.strip('\n').split(',')[1]
num = num.strip(' ').split(' ')
num = np.array([int(i) for i in num])
label = np.zeros([self.class_num])
label[num] = 1
label = torch.tensor(label, dtype=torch.long)
if os.path.exists(os.path.join(self.root, path)) == False:
label = np.zeros([self.class_num])
label = torch.tensor(label, dtype=torch.long)
img = np.zeros((448, 448, 3))
img = Image.fromarray(np.uint8(img)) # type: ignore
exit(1)
else:
img = Image.open(os.path.join(self.root, path)).convert('RGB')
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target) # type: ignore # noqa
assert (self.target_transform is None)
return [index,img], label
def __len__(self):
return len(self.name)
def labels(self):
if "coco" in cfg.data:
assert (False)
elif "nuswide" in cfg.data:
with open('nuswide_labels.txt', 'r') as f:
text = f.read()
return text.split('\n')
elif "voc" in cfg.data:
with open('voc_labels.txt', 'r') as f:
text = f.read()
return text.split('\n')
elif "cub" in cfg.data:
with open('cub_labels.txt', 'r') as f:
text = f.read()
return text.split('\n')
else:
assert (False)
class COCO_missing_val_dataset(torch.utils.data.Dataset): # type: ignore
def __init__(self,
root,
annFile,
transform=None,
target_transform=None,
class_num: int = -1):
self.root = root
with open(annFile, 'r') as f:
names = f.readlines()
# name = names.strip('\n').split(' ')
self.name = names
# self.label = name[:,1]
self.transform = transform
self.class_num = class_num
self.target_transform = target_transform
def __getitem__(self, index):
name = self.name[index]
path = name.strip('\n').split(',')[0]
num = name.strip('\n').split(',')[1]
num = num.strip(' ').split(' ')
num = np.array([int(i) for i in num])
label = np.zeros([self.class_num])
label[num] = 1
label = torch.tensor(label, dtype=torch.long)
if os.path.exists(os.path.join(self.root, path)) == False:
label = np.zeros([self.class_num])
label = torch.tensor(label, dtype=torch.long)
img = np.zeros((448, 448, 3))
img = Image.fromarray(np.uint8(img)) # type: ignore
exit(1)
else:
img = Image.open(os.path.join(self.root, path)).convert('RGB')
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target) # type: ignore # noqa
assert (self.target_transform is None)
return img, label
def __len__(self):
return len(self.name)
def labels(self):
if "coco" in cfg.data:
assert (False)
elif "nuswide" in cfg.data:
with open('nuswide_labels.txt', 'r') as f:
text = f.read()
return text.split('\n')
elif "voc" in cfg.data:
with open('voc_labels.txt', 'r') as f:
text = f.read()
return text.split('\n')
elif "cub" in cfg.data:
with open('cub_labels.txt', 'r') as f:
text = f.read()
return text.split('\n')
else:
assert (False)
class ModelEma(torch.nn.Module):
def __init__(self, model, decay=0.9997, device=None):
super(ModelEma, self).__init__()
# make a copy of the model for accumulating moving average of weights
self.module = deepcopy(model)
self.module.eval()
self.decay = decay
self.device = device # perform ema on different device from model if set
if self.device is not None:
self.module.to(device=device)
def _update(self, model, update_fn):
with torch.no_grad():
for ema_v, model_v in zip(self.module.state_dict().values(),
model.state_dict().values()):
if self.device is not None:
model_v = model_v.to(device=self.device)
ema_v.copy_(update_fn(ema_v, model_v))
def update(self, model):
self._update(model,
update_fn=lambda e, m: self.decay * e +
(1. - self.decay) * m)
def set(self, model):
self._update(model, update_fn=lambda e, m: m)
class CutoutPIL(object):
def __init__(self, cutout_factor=0.5):
self.cutout_factor = cutout_factor
def __call__(self, x):
img_draw = ImageDraw.Draw(x)
h, w = x.size[0], x.size[1] # HWC
h_cutout = int(self.cutout_factor * h + 0.5)
w_cutout = int(self.cutout_factor * w + 0.5)
y_c = np.random.randint(h)
x_c = np.random.randint(w)
y1 = np.clip(y_c - h_cutout // 2, 0, h)
y2 = np.clip(y_c + h_cutout // 2, 0, h)
x1 = np.clip(x_c - w_cutout // 2, 0, w)
x2 = np.clip(x_c + w_cutout // 2, 0, w)
fill_color = (random.randint(0, 255), random.randint(0, 255),
random.randint(0, 255))
img_draw.rectangle([x1, y1, x2, y2], fill=fill_color) # type: ignore
return x
def add_weight_decay(model, weight_decay=1e-4, skip_list=()):
decay = []
no_decay = []
gcn = []
gcn_no_decay = []
prefix = "module." if torch.cuda.device_count() > 1 else ""
for name, param in model.named_parameters():
if not param.requires_grad:
continue # frozen weights
if name.startswith(f"{prefix}gc"):
if len(param.shape) == 1 or name.endswith(".bias") or name in skip_list:
gcn_no_decay.append(param)
else:
gcn.append(param)
assert("gcn" in cfg.model_name)
elif len(param.shape) == 1 or name.endswith(
".bias") or name in skip_list:
no_decay.append(param)
else:
decay.append(param)
return [{
'params': no_decay,
'weight_decay': 0.
}, {
'params': decay,
'weight_decay': weight_decay
}, {
'params': gcn_no_decay,
'weight_decay': 0.
}, {
'params': gcn,
'weight_decay': weight_decay
}]
def get_ema_co():
if "coco" in cfg.data:
ema_co = np.exp(np.log(0.82)/(641*cfg.ratio)) # type: ignore
# ema_co = 0.9997
elif "nus" in cfg.data:
ema_co = np.exp(np.log(0.82)/(931*cfg.ratio)) # type: ignore
# ema_co = 0.9998
elif "voc" in cfg.data:
ema_co = np.exp(np.log(0.82)/(45*cfg.ratio)) # type: ignore
# ema_co = 0.9956
elif "cub" in cfg.data:
if cfg.batch_size == 96:
ema_co = np.exp(np.log(0.82)/(63*cfg.ratio))
else:
ema_co = np.exp(np.log(0.82)/(47*cfg.ratio)) # type: ignore
else:
assert(False)
return ema_co