-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
83 lines (64 loc) · 2.18 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
"""
Author: Rui Hu
All rights reserved.
"""
import random
import numpy as np
import torch
from torch.utils.data import Dataset
import torch.backends.cudnn as cudnn
EPS = 1e-6
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
cudnn.deterministic = True
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, bit=6):
self.reset()
self.bit = bit
def reset(self):
self.val = 0.0
self.avg = 0.0
self.sum = 0.0
self.count = 0.0
def update(self, correct_num, batch_size):
self.val = correct_num
self.sum += correct_num
self.count += batch_size
self.avg = self.sum / self.count
class IdxDataset(Dataset):
def __init__(self, dataset):
self.dataset = dataset
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
return idx, self.dataset[idx]
class EMAGPU:
def __init__(self, label, alpha=0.9):
self.label = label
self.alpha = alpha
self.parameter = torch.zeros(label.size(0)).cuda()
self.updated = torch.zeros(label.size(0)).cuda()
self.num_class = label.max().item() + 1
self.max_param_per_class = torch.zeros(self.num_class).cuda()
def update(self, data, index):
self.parameter[index] = (
self.alpha * self.parameter[index]
+ (1 - self.alpha * self.updated[index]) * data
)
self.updated[index] = 1
# update max_param_per_class
batch_size = len(index)
buffer = torch.zeros(batch_size, self.num_class).cuda()
buffer[range(batch_size), self.label[index]] = self.parameter[index]
cur_max = buffer.max(dim=0).values
global_max = torch.maximum(cur_max, self.max_param_per_class)
label_set_indices = self.label[index].unique()
self.max_param_per_class[label_set_indices] = global_max[
label_set_indices
]
def max_loss(self, label):
return self.max_param_per_class[label]