-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhoc_utils.py
340 lines (265 loc) · 12.1 KB
/
hoc_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
import numpy as np
import torch.nn.functional as F
import time
import torch
import random
import math
import torch.nn as nn
import torchvision
# import torchvision.transforms as transforms
# numLocal = config.numLocal
# numNoisyGroup = config.numNoisyGroup
smp = torch.nn.Softmax(dim=0)
smt = torch.nn.Softmax(dim=1)
def distCosine(x, y):
"""
:param x: m x k array
:param y: n x k array
:return: m x n array
"""
xx = np.sum(x ** 2, axis=1) ** 0.5
x = x / xx[:, np.newaxis]
yy = np.sum(y ** 2, axis=1) ** 0.5
y = y / yy[:, np.newaxis]
dist = 1 - np.dot(x, y.transpose()) # 1 - cosine distance
return dist
def check_T_torch(KINDS, clean_label, noisy_label):
T_real = np.zeros((KINDS,KINDS))
for i in range(clean_label.shape[0]):
T_real[clean_label[i]][noisy_label[i]] += 1
P_real = [sum(T_real[i])*1.0 for i in range(KINDS)] # random selection
for i in range(KINDS):
if P_real[i]>0:
T_real[i] /= P_real[i]
P_real = np.array(P_real)/sum(P_real)
print(f'Check: P = {P_real},\n T = \n{np.round(T_real,3)}')
return T_real, P_real
def extract_sub_dataset_local_c100(origin_trans, center_idx = None, numLocal = 10000):
feat_cord0 = origin_trans[center_idx]
dist_all = torch.norm(feat_cord0.view(1,-1) - origin_trans, dim=1)
dist_s, idx = torch.sort(dist_all)
idx_sel = idx[:numLocal].detach().cpu().tolist()
return idx_sel
def extract_sub_dataset_local(origin_trans, center_idx = None, numLocal = 250):
feat_cord0 = origin_trans[center_idx]
dist_all = torch.norm(feat_cord0.view(1,-1) - origin_trans, dim=1)
dist_s, idx = torch.sort(dist_all)
idx_sel = idx[:numLocal].detach().cpu().tolist()
return idx_sel
def extract_sub_dataset(sub_cluster_each, origin, sub_clean_dataset_name, sub_noisy_dataset_name = None):
for i in range(len(sub_cluster_each)): #KINDS
random.shuffle(origin[i])
origin[i] = origin[i][:sub_cluster_each[i]]
for ori in origin[i]:
ori['label'] = i
total_len = sum([len(a) for a in origin])
origin_trans = torch.zeros(total_len,origin[0][0]['feature'].shape[0])
origin_label = torch.zeros(total_len).long()
origin_index = torch.zeros(total_len).long()
cnt = 0
for item in origin:
for i in item:
origin_trans[cnt] = i['feature']
origin_label[cnt] = i['label']
origin_index[cnt] = i['index']
cnt += 1
torch.save({'feature': origin_trans, 'clean_label': origin_label, 'index': origin_index},f'{sub_clean_dataset_name}')
origin_dataset = torch.load(f'{sub_clean_dataset_name}')
origin_dataset['noisy_label'] = origin_dataset['clean_label'].clone()
torch.save(origin_dataset, f'{sub_noisy_dataset_name}')
def add_noise_dataset(KINDS, sub_clean_dataset_name, sub_noisy_dataset_name, cluster_cnt, sub_cluster_each, label_list, T):
origin_dataset = torch.load(f'{sub_noisy_dataset_name}')
T_real = np.zeros((KINDS,KINDS))
for i in range(sum(sub_cluster_each)):
origin_dataset['noisy_label'][i] = torch.tensor(np.random.choice(label_list,1,p=T[origin_dataset['clean_label'][i]])).long()
T_real[origin_dataset['clean_label'][i]][origin_dataset['noisy_label'][i]] += 1
P_real = [sum(T_real[i])*1.0 for i in range(KINDS)] # random selection
for i in range(KINDS):
if P_real[i]>0:
T_real[i] /= P_real[i]
P_real = np.array(P_real)/sum(P_real)
torch.save(origin_dataset, f'{sub_noisy_dataset_name}')
def add_noise_dataset_local(KINDS, sub_noisy_dataset_name, cluster_cnt, sub_cluster_each, label_list, T, idx_sel):
origin_dataset = torch.load(f'{sub_noisy_dataset_name}')
T_real = np.zeros((KINDS,KINDS))
for i in range(len(idx_sel)):
origin_dataset['noisy_label'][idx_sel[i]] = torch.tensor(np.random.choice(label_list,1,p=T[origin_dataset['clean_label'][idx_sel[i]]])).long()
T_real[origin_dataset['clean_label'][idx_sel[i]]][origin_dataset['noisy_label'][idx_sel[i]]] += 1
P_real = [sum(T_real[i])*1.0 for i in range(KINDS)] # random selection
for i in range(KINDS):
if P_real[i]>0:
T_real[i] /= P_real[i]
P_real = np.array(P_real)/sum(P_real)
torch.save(origin_dataset, f'{sub_noisy_dataset_name}')
return P_real, T_real
def get_feat_clusters(origin, sample):
final_feat = origin['feature'][sample]
noisy_label = origin['noisy_label'][sample]
return final_feat, noisy_label
def get_feat_clusters_local(subdataset_name, sample):
origin = torch.load(f'{subdataset_name}', map_location=torch.device('cpu'))
final_feat = origin['feature'][sample]
noisy_label = origin['noisy_label'][sample]
return 0, final_feat, noisy_label
def count_real(KINDS, T, P, mode, _device = 'cpu'):
# time1 = time.time()
P = P.reshape((KINDS, 1))
p_real = [[] for _ in range(3)]
p_real[0] = torch.mm(T.transpose(0, 1), P).transpose(0, 1)
# p_real[2] = torch.zeros((KINDS, KINDS, KINDS)).to(_device)
p_real[2] = torch.zeros((KINDS, KINDS, KINDS))
temp33 = torch.tensor([])
for i in range(KINDS):
Ti = torch.cat((T[:, i:], T[:, :i]), 1)
temp2 = torch.mm((T * Ti).transpose(0, 1), P)
p_real[1] = torch.cat([p_real[1], temp2], 1) if i != 0 else temp2
for j in range(KINDS):
Tj = torch.cat((T[:, j:], T[:, :j]), 1)
temp3 = torch.mm((T * Ti * Tj).transpose(0, 1), P)
temp33 = torch.cat([temp33, temp3], 1) if j != 0 else temp3
# adjust the order of the output (N*N*N), keeping consistent with p_estimate
t3 = []
for p3 in range(KINDS):
t3 = torch.cat((temp33[p3, KINDS - p3:], temp33[p3, :KINDS - p3]))
temp33[p3] = t3
if mode == -1:
for r in range(KINDS):
p_real[2][r][(i+r+KINDS)%KINDS] = temp33[r]
else:
p_real[2][mode][(i + mode + KINDS) % KINDS] = temp33[mode]
temp = [] # adjust the order of the output (N*N), keeping consistent with p_estimate
for p1 in range(KINDS):
temp = torch.cat((p_real[1][p1, KINDS-p1:], p_real[1][p1, :KINDS-p1]))
p_real[1][p1] = temp
return p_real
def build_T(cluster):
T = [[0 for _ in range(cluster)] for _ in range(cluster)]
for i in range(cluster):
rand_sum = 0
for j in range(cluster):
if i != j:
rand = round(random.uniform(0.01, 0.07), 3)
rand_sum += rand
T[i][j] = rand
T[i][i] = 1 - rand_sum
return T
def build_T_local(cluster, center_class):
T = [[0 for _ in range(cluster)] for _ in range(cluster)]
zero_class = np.random.choice(range(cluster),int(np.sqrt(cluster)), replace = False)
for i in range(cluster):
rand_sum = 0
for j in range(cluster):
if i != j:
rand = round(random.uniform(0.15, 0.25), 3) * (j in zero_class) if i == center_class else round(random.uniform(0.01, 0.07), 3)
rand_sum += rand
T[i][j] = rand
T[i][i] = 1 - rand_sum
# print(torch.tensor(T))
# exit()
return T
def check_T(KINDS, noisy_label, point_each_cluster):
temp_error_matrix = [[0 for _ in range(KINDS)] for _ in range(KINDS)]
cnt_point = 0
for cnt in range(len(point_each_cluster)):
for label in noisy_label[cnt_point : point_each_cluster[cnt]+cnt_point]:
temp_error_matrix[cnt][label] = temp_error_matrix[cnt][label] + 1
cnt_point += point_each_cluster[cnt]
for i in range(KINDS):
temp_sum = sum(temp_error_matrix[i])
for j in range(KINDS):
temp_error_matrix[i][j] = round(temp_error_matrix[i][j]/temp_sum, 3)
print(f'Check_Error_Rate = \n{np.array(temp_error_matrix)}')
def select_next_idx(selected_idx, idx_sel):
# selected_idx[idx_sel[:int(numLocal*0.7)]] = -1
selected_idx[idx_sel] = -1
if selected_idx[selected_idx > -1].size(0) > 0:
next_select_idx = random.choice(selected_idx[selected_idx > 0]) # select one from the remaining part
return next_select_idx, selected_idx
else:
return random.randint(0, 49999), selected_idx
def adjust_learning_rate(optimizer, epoch,alpha_plan):
for param_group in optimizer.param_groups:
param_group['lr']=alpha_plan[epoch]
def accuracy(logit, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
output = F.softmax(logit, dim=1)
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].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def set_device():
if torch.cuda.is_available():
_device = torch.device("cuda")
else:
_device = torch.device("cpu")
print(f'Current device is {_device}', flush=True)
return _device
def set_model_pre(config):
# use resnet50 for ImageNet pretrain (PyTorch official pre-trained model)
if config.pre_type == 'image':
model = res_image.resnet50(pretrained=True)
else:
RuntimeError('Undefined pretrained model.')
for param in model.parameters():
param.requires_grad = False
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, config.num_classes)
model.to(config.device)
return model
def set_model_train(config):
model = res_cifar_new.ResNet34(num_classes = config.num_classes)
model.to(config.device)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9, weight_decay=0.0005)
return model, optimizer
def init_feature_set(config, model_pre, train_dataloader, rnd):
c1m_cluster_each = [0 for _ in range(config.num_classes)]
# save the 512-dim feature as a dataset
model_pre.eval()
record = [[] for _ in range(config.num_classes)]
for i_batch, (feature, label, index) in enumerate(train_dataloader):
feature = feature.to(config.device)
label = label.to(config.device)
extracted_feature, _ = model_pre(feature)
for i in range(extracted_feature.shape[0]):
record[label[i]].append({'feature': extracted_feature[i].detach().cpu(), 'index': index[i]})
path = f'./data/{config.pre_type}_{config.dataset}_{config.label_file_path[7:-3]}.pt'
return path, record, c1m_cluster_each
def build_dataset_informal(config, record, c1m_cluster_each):
# estimate T
original_clean_dataset_name = config.path
sub_clean_dataset_name = f'{config.path[:-3]}_clean.pt'
sub_noisy_dataset_name = f'{config.path[:-3]}_noisy.pt'
# if ~config.build_feat:
# return sub_clean_dataset_name, sub_noisy_dataset_name
# Build Dataset -----------------------------------------------
label_list = [i for i in range(config.num_classes)] # label-type: 0, 1, 2 ... 9
P = config.P
sub_cluster_each = [int(50000/config.num_classes)] * config.num_classes
extract_sub_dataset(sub_cluster_each, record, sub_clean_dataset_name,
sub_noisy_dataset_name)
if config.label_file_path is 'NA':
RuntimeError('Cannot load noisy labels.')
else:
# load noise file. re-format
origin_dataset = torch.load(f'{sub_noisy_dataset_name}')
noise_label = torch.load(config.label_file_path)
T_real = np.zeros((config.num_classes,config.num_classes))
for i in range(sum(sub_cluster_each)):
idx = origin_dataset['index'][i]
assert origin_dataset['clean_label'][i] == noise_label['clean_label_train'][idx]
origin_dataset['noisy_label'][i] = noise_label['noise_label_train'][idx].long()
T_real[origin_dataset['clean_label'][i]][origin_dataset['noisy_label'][i]] += 1
P_real = [sum(T_real[i])*1.0 for i in range(config.num_classes)] # random selection
for i in range(config.num_classes):
if P_real[i]>0:
T_real[i] /= P_real[i]
P_real = np.array(P_real)/sum(P_real)
config.T = T_real
torch.save(origin_dataset, f'{sub_noisy_dataset_name}')
return sub_clean_dataset_name, sub_noisy_dataset_name