-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
599 lines (503 loc) · 22.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.sampler import Sampler
import numpy as np
import scipy as sp
import scipy.stats
import random
import scipy.io as sio
from sklearn import preprocessing
import matplotlib.pyplot as plt
import os
import logging
import shutil
import imp
import math
from OT_torch_ import cost_matrix_batch_torch, GW_distance_uniform, IPOT_distance_torch_batch_uniform
def same_seeds(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
np.random.seed(seed) # Numpy module.
random.seed(seed) # Python random module.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def mean_confidence_interval(data, confidence=0.95):
a = 1.0*np.array(data)
n = len(a)
m, se = np.mean(a), scipy.stats.sem(a)
h = se * sp.stats.t._ppf((1+confidence)/2., n-1)
return m,h
from operator import truediv
def AA_andEachClassAccuracy(confusion_matrix):
counter = confusion_matrix.shape[0]
list_diag = np.diag(confusion_matrix)
list_raw_sum = np.sum(confusion_matrix, axis=1)
each_acc = np.nan_to_num(truediv(list_diag, list_raw_sum))
average_acc = np.mean(each_acc)
return each_acc, average_acc
import torch.utils.data as data
class matcifar(data.Dataset):
"""`CIFAR10 <https://www.cs.toronto.edu/~kriz/cifar.html>`_ Dataset.
Args:
root (string): Root directory of dataset where directory
``cifar-10-batches-py`` exists.
train (bool, optional): If True, creates dataset from training set, otherwise
creates from test set.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
download (bool, optional): If true, downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, it is not
downloaded again.
"""
def __init__(self, imdb, train, d, medicinal):
self.train = train # training set or test set
self.imdb = imdb
self.d = d
self.x1 = np.argwhere(self.imdb['set'] == 1)
self.x2 = np.argwhere(self.imdb['set'] == 3)
self.x1 = self.x1.flatten()
self.x2 = self.x2.flatten()
# if medicinal==4 and d==2:
# self.train_data=self.imdb['data'][self.x1,:]
# self.train_labels=self.imdb['Labels'][self.x1]
# self.test_data=self.imdb['data'][self.x2,:]
# self.test_labels=self.imdb['Labels'][self.x2]
if medicinal == 1:
self.train_data = self.imdb['data'][self.x1, :, :, :]
self.train_labels = self.imdb['Labels'][self.x1]
self.test_data = self.imdb['data'][self.x2, :, :, :]
self.test_labels = self.imdb['Labels'][self.x2]
else:
self.train_data = self.imdb['data'][:, :, :, self.x1]
self.train_labels = self.imdb['Labels'][self.x1]
self.test_data = self.imdb['data'][:, :, :, self.x2]
self.test_labels = self.imdb['Labels'][self.x2]
if self.d == 3:
self.train_data = self.train_data.transpose((3, 2, 0, 1)) ##(17, 17, 200, 10249)
self.test_data = self.test_data.transpose((3, 2, 0, 1))
else:
self.train_data = self.train_data.transpose((3, 0, 2, 1))
self.test_data = self.test_data.transpose((3, 0, 2, 1))
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
if self.train:
img, target = self.train_data[index], self.train_labels[index]
else:
img, target = self.test_data[index], self.test_labels[index]
return img, target
def __len__(self):
if self.train:
return len(self.train_data)
else:
return len(self.test_data)
def sanity_check(all_set):
nclass = 0
nsamples = 0
all_good = {}
for class_ in all_set:
if len(all_set[class_]) >= 200:
# all_good[class_] = all_set[class_][:200]
all_good[class_] = all_set[class_][len(all_set[class_])-200:]
nclass += 1
nsamples += len(all_good[class_])
print('the number of class:', nclass)
print('the number of sample:', nsamples)
return all_good
def sanity_check_unlabel(all_set, num_unlabel):
nclass = 0
nsamples = 0
all_good = {}
for class_ in all_set:
# all_good[class_] = all_set[class_][:200]
all_good[class_] = all_set[class_][len(all_set[class_])-num_unlabel:]
nclass += 1
nsamples += len(all_good[class_])
print('the number of class:', nclass)
print('the number of sample:', nsamples)
return all_good
def flip(data):
y_4 = np.zeros_like(data)
y_1 = y_4
y_2 = y_4
first = np.concatenate((y_1, y_2, y_1), axis=1)
second = np.concatenate((y_4, data, y_4), axis=1)
third = first
Data = np.concatenate((first, second, third), axis=0)
return Data
def load_data(image_file, label_file):
image_data = sio.loadmat(image_file)
label_data = sio.loadmat(label_file)
data_key = image_file.split('/')[-1].split('.')[0]
label_key = label_file.split('/')[-1].split('.')[0]
data_all = image_data[data_key]
GroundTruth = label_data[label_key]
[nRow, nColumn, nBand] = data_all.shape
print(data_key, nRow, nColumn, nBand)
data = data_all.reshape(np.prod(data_all.shape[:2]), np.prod(data_all.shape[2:]))
data_scaler = preprocessing.scale(data.astype(float)) # (X-X_mean)/X_std,
Data_Band_Scaler = data_scaler.reshape(data_all.shape[0], data_all.shape[1],data_all.shape[2])
return Data_Band_Scaler, GroundTruth
def radiation_noise(data, alpha_range=(0.9, 1.1), beta=1/25):
alpha = np.random.uniform(*alpha_range)
noise = np.random.normal(loc=0., scale=1.0, size=data.shape)
return alpha * data + beta * noise
def flip_augmentation(data): # arrays tuple 0:(7, 7, 103) 1=(7, 7)
horizontal = np.random.random() > 0.5 # True
vertical = np.random.random() > 0.5 # False
if horizontal:
data = np.fliplr(data)
if vertical:
data = np.flipud(data)
return data
class Task(object):
def __init__(self, data, num_classes, shot_num, query_num):
self.data = data
self.num_classes = num_classes
self.support_num = shot_num
self.query_num = query_num
class_folders = sorted(list(data))
class_list = random.sample(class_folders, self.num_classes)
labels = np.array(range(len(class_list)))
labels = dict(zip(class_list, labels))
samples = dict()
self.support_datas = []
self.query_datas = []
self.support_labels = []
self.query_labels = []
for c in class_list:
temp = self.data[c] # list
samples[c] = random.sample(temp, len(temp))
random.shuffle(samples[c])
self.support_datas += samples[c][:shot_num]
self.query_datas += samples[c][shot_num:shot_num + query_num]
self.support_labels += [labels[c] for i in range(shot_num)]
self.query_labels += [labels[c] for i in range(query_num)]
# print(self.support_labels)
# print(self.query_labels)
class FewShotDataset(Dataset):
def __init__(self, task, split='train'):
self.task = task
self.split = split
self.image_datas = self.task.support_datas if self.split == 'train' else self.task.query_datas
self.labels = self.task.support_labels if self.split == 'train' else self.task.query_labels
def __len__(self):
return len(self.image_datas)
def __getitem__(self, idx):
raise NotImplementedError("This is an abstract class. Subclass this class for your particular dataset.")
class HBKC_dataset(FewShotDataset):
def __init__(self, *args, **kwargs):
super(HBKC_dataset, self).__init__(*args, **kwargs)
def __getitem__(self, idx):
image = self.image_datas[idx]
label = self.labels[idx]
return image, label
# Sampler
class ClassBalancedSampler(Sampler):
''' Samples 'num_inst' examples each from 'num_cl' pool of examples of size 'num_per_class' '''
# 参数:
# num_per_class: 每个类的样本数量
# num_cl: 类别数量
# num_inst:support set或query set中的样本数量
# shuffle:样本是否乱序
def __init__(self, num_per_class, num_cl, num_inst,shuffle=True):
self.num_per_class = num_per_class
self.num_cl = num_cl
self.num_inst = num_inst
self.shuffle = shuffle
def __iter__(self):
# return a single list of indices, assuming that items will be grouped by class
if self.shuffle:
batch = [[i+j*self.num_inst for i in torch.randperm(self.num_inst)[:self.num_per_class]] for j in range(self.num_cl)]
else:
batch = [[i+j*self.num_inst for i in range(self.num_inst)[:self.num_per_class]] for j in range(self.num_cl)]
batch = [item for sublist in batch for item in sublist]
if self.shuffle:
random.shuffle(batch)
return iter(batch)
def __len__(self):
return 1
# dataloader
def get_HBKC_data_loader(task, num_per_class=1, split='train',shuffle = False):
# 参数:
# task: 当前任务
# num_per_class:每个类别的样本数量,与split有关
# split:‘train'或‘test'代表support和querya
# shuffle:样本是否乱序
# 输出:
# loader
dataset = HBKC_dataset(task,split=split)
if split == 'train':
sampler = ClassBalancedSampler(num_per_class, task.num_classes, task.support_num, shuffle=shuffle) # support set
else:
sampler = ClassBalancedSampler(num_per_class, task.num_classes, task.query_num, shuffle=shuffle) # query set
loader = DataLoader(dataset, batch_size=num_per_class*task.num_classes, sampler=sampler)
return loader
def classification_map(map, groundTruth, dpi, savePath):
fig = plt.figure(frameon=False)
fig.set_size_inches(groundTruth.shape[1]*2.0/dpi, groundTruth.shape[0]*2.0/dpi)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
fig.add_axes(ax)
ax.imshow(map)
fig.savefig(savePath, dpi = dpi)
return 0
def allocate_tensors():
"""
init data tensors
:return: data tensors
"""
tensors = dict()
tensors['support_data'] = torch.FloatTensor()
tensors['support_label'] = torch.LongTensor()
tensors['query_data'] = torch.FloatTensor()
tensors['query_label'] = torch.LongTensor()
return tensors
def allocate_tensors_unlabel():
"""
init data tensors
:return: data tensors
"""
tensors = dict()
tensors['support_data'] = torch.FloatTensor()
tensors['support_label'] = torch.LongTensor()
tensors['query_data'] = torch.FloatTensor()
# tensors['query_label'] = torch.LongTensor()
return tensors
def set_tensors(tensors, batch):
"""
set data to initialized tensors
:param tensors: initialized data tensors
:param batch: current batch of data
:return: None
"""
support_data, support_label, query_data, query_label = batch
tensors['support_data'].resize_(support_data.size()).copy_(support_data)
tensors['support_label'].resize_(support_label.size()).copy_(support_label)
tensors['query_data'].resize_(query_data.size()).copy_(query_data)
tensors['query_label'].resize_(query_label.size()).copy_(query_label)
def set_tensors_unlabel(tensors, batch):
support_data, support_label, query_data = batch
tensors['support_data'].resize_(support_data.size()).copy_(support_data)
tensors['support_label'].resize_(support_label.size()).copy_(support_label)
tensors['query_data'].resize_(query_data.size()).copy_(query_data)
# tensors['query_label'].resize_(query_label.size()).copy_(query_label)
def set_logging_config(logdir):
"""
set logging configuration
:param logdir: directory put logs
:return: None
"""
if not os.path.exists(logdir):
os.makedirs(logdir)
logging.basicConfig(format="[%(asctime)s] [%(name)s] %(message)s",
level=logging.INFO,
handlers=[logging.FileHandler(os.path.join(logdir, 'log.txt')),
logging.StreamHandler(os.sys.stdout)])
def save_checkpoint(state, is_best, exp_name):
"""
save the checkpoint during training stage
:param state: content to be saved
:param is_best: if DPGN model's performance is the best at current step
:param exp_name: experiment name
:return: None
"""
torch.save(state, os.path.join('{}'.format(exp_name), 'checkpoint.pth.tar'))
if is_best:
shutil.copyfile(os.path.join('{}'.format(exp_name), 'checkpoint.pth.tar'),
os.path.join('{}'.format(exp_name), 'model_best.pth.tar'))
def adjust_learning_rate(optimizers, lr, iteration, dec_lr_step, lr_adj_base):
"""
adjust learning rate after some iterations
:param optimizers: the optimizers
:param lr: learning rate
:param iteration: current iteration
:param dec_lr_step: decrease learning rate in how many step
:return: None
"""
# new_lr = lr * (lr_adj_base ** (int(iteration / dec_lr_step)))
new_lr = lr / math.pow((1 + 10 * (iteration - 1) / dec_lr_step), 0.75)
for optimizer in optimizers:
for param_group in optimizer.param_groups:
param_group['lr'] = new_lr
def label2edge(label, device):
"""
convert ground truth labels into ground truth edges
:param label: ground truth labels
:param device: the gpu device that holds the ground truth edges
:return: ground truth edges
"""
# get size
num_samples = label.size(1)
# reshape
label_i = label.unsqueeze(-1).repeat(1, 1, num_samples)
label_j = label_i.transpose(1, 2)
# compute edge
edge = torch.eq(label_i, label_j).float().to(device)
return edge
def one_hot_encode(num_classes, class_idx, device):
"""
one-hot encode the ground truth
:param num_classes: number of total class
:param class_idx: belonging class's index
:param device: the gpu device that holds the one-hot encoded ground truth label
:return: one-hot encoded ground truth label
"""
return torch.eye(num_classes)[class_idx].to(device)
def preprocess(num_ways, num_shots, num_queries, batch_size, device):
"""
prepare for train and evaluation
:param num_ways: number of classes for each few-shot task
:param num_shots: number of samples for each class in few-shot task
:param num_queries: number of queries for each class in few-shot task
:param batch_size: how many tasks per batch
:param device: the gpu device that holds all data
:return: number of samples in support set
number of total samples (support and query set)
mask for edges connect query nodes
mask for unlabeled data (for semi-supervised setting)
"""
# set size of support set, query set and total number of data in single task
num_supports = num_ways * num_shots
num_samples = num_supports + num_queries * num_ways
# set edge mask (to distinguish support and query edges)
support_edge_mask = torch.zeros(batch_size, num_samples, num_samples).to(device)
support_edge_mask[:, :num_supports, :num_supports] = 1
query_edge_mask = 1 - support_edge_mask
evaluation_mask = torch.ones(batch_size, num_samples, num_samples).to(device)
return num_supports, num_samples, query_edge_mask, evaluation_mask
def preprocess_one(num_supports, num_samples, batch_size, device):
"""
prepare for train and evaluation
:param num_ways: number of classes for each few-shot task
:param num_shots: number of samples for each class in few-shot task
:param num_queries: number of queries for each class in few-shot task
:param batch_size: how many tasks per batch
:param device: the gpu device that holds all data
:return: number of samples in support set
number of total samples (support and query set)
mask for edges connect query nodes
mask for unlabeled data (for semi-supervised setting)
"""
# set size of support set, query set and total number of data in single task
# set edge mask (to distinguish support and query edges)
support_edge_mask = torch.zeros(batch_size, num_samples, num_samples).to(device)
support_edge_mask[:, :num_supports, :num_supports] = 1
query_edge_mask = 1 - support_edge_mask
evaluation_mask = torch.ones(batch_size, num_samples, num_samples).to(device)
return num_supports, query_edge_mask, evaluation_mask
def initialize_nodes_edges(batch, num_supports, tensors, batch_size, num_queries, num_ways, device):
"""
:param batch: data batch
:param num_supports: number of samples in support set
:param tensors: initialized tensors for holding data
:param batch_size: how many tasks per batch
:param num_queries: number of samples in query set
:param num_ways: number of classes for each few-shot task
:param device: the gpu device that holds all data
:return: data of support set,
label of support set,
data of query set,
label of query set,
data of support and query set,
label of support and query set,
initialized node features of distribution graph (Vd_(0)),
initialized edge features of point graph (Ep_(0)),
initialized edge_features_of distribution graph (Ed_(0))
"""
# allocate data in this batch to specific variables
set_tensors(tensors, batch)
support_data = tensors['support_data'].squeeze(0)
support_label = tensors['support_label'].squeeze(0)
query_data = tensors['query_data'].squeeze(0)
query_label = tensors['query_label'].squeeze(0)
# initialize nodes of distribution graph
node_gd_init_support = label2edge(support_label, device)
node_gd_init_query = (torch.ones([batch_size, num_queries, num_supports])
* torch.tensor(1. / num_supports)).to(device)
node_feature_gd = torch.cat([node_gd_init_support, node_gd_init_query], dim=1)
# initialize edges of point graph
all_data = torch.cat([support_data, query_data], 1)
all_label = torch.cat([support_label, query_label], 1)
all_label_in_edge = label2edge(all_label, device)
edge_feature_gp = all_label_in_edge.clone()
# uniform initialization for point graph's edges
edge_feature_gp[:, num_supports:, :num_supports] = 1. / num_supports
edge_feature_gp[:, :num_supports, num_supports:] = 1. / num_supports
edge_feature_gp[:, num_supports:, num_supports:] = 0
for i in range(num_queries):
edge_feature_gp[:, num_supports + i, num_supports + i] = 1
# initialize edges of distribution graph (same as point graph)
edge_feature_gd = edge_feature_gp.clone()
return support_data, support_label, query_data, query_label, all_data, all_label_in_edge, \
node_feature_gd, edge_feature_gp, edge_feature_gd
def unlabel2edge(data, device):
"""
convert ground truth labels into ground truth edges
:param label: ground truth labels
:param device: the gpu device that holds the ground truth edges
:return: ground truth edges
"""
# get size
num_samples = data.size(1)
# reshape
scores = torch.einsum('bhm,bmn->bhn', data, data.transpose(2,1))
edge = torch.nn.functional.softmax(scores, dim=-1)
return edge
def initialize_nodes_edges_unlabel(batch, num_supports, tensors, batch_size, num_queries, num_ways, device):
# allocate data in this batch to specific variables
set_tensors_unlabel(tensors, batch)
support_data = tensors['support_data'].squeeze(0)
support_label = tensors['support_label'].squeeze(0)
query_data = tensors['query_data'].squeeze(0)
# query_label = tensors['query_label'].squeeze(0)
# initialize nodes of distribution graph
node_gd_init_support = label2edge(support_label, device)
node_gd_init_query = (torch.ones([batch_size, num_queries, num_supports])
* torch.tensor(1. / num_supports)).to(device)
node_feature_gd = torch.cat([node_gd_init_support, node_gd_init_query], dim=1)
# initialize edges of point graph
all_data = torch.cat([support_data, query_data], 1)
all_label_in_edge = unlabel2edge(all_data, device)
edge_feature_gp = all_label_in_edge.clone()
# uniform initialization for point graph's edges
edge_feature_gp[:, num_supports:, :num_supports] = 1. / num_supports
edge_feature_gp[:, :num_supports, num_supports:] = 1. / num_supports
edge_feature_gp[:, num_supports:, num_supports:] = 0
for i in range(num_queries):
edge_feature_gp[:, num_supports + i, num_supports + i] = 1
# initialize edges of distribution graph (same as point graph)
edge_feature_gd = edge_feature_gp.clone()
return support_data, query_data, all_data, all_label_in_edge, \
node_feature_gd, edge_feature_gp, edge_feature_gd
def OT(src, tar, ori=False, sub=False, **kwargs):
wd, gwd = [], []
for i in range(len(src)):
source_share, target_share = src[i], tar[i]
cos_distance = cost_matrix_batch_torch(source_share, target_share)
cos_distance = cos_distance.transpose(1,2)
# TODO: GW as graph matching loss
beta = 0.1
if sub:
cos_distance = kwargs['w_st']*cos_distance
min_score = cos_distance.min()
max_score = cos_distance.max()
threshold = min_score + beta * (max_score - min_score)
cos_dist = torch.nn.functional.relu(cos_distance - threshold)
wd_val = - IPOT_distance_torch_batch_uniform(cos_dist, source_share.size(0), source_share.size(2), target_share.size(2), iteration=30)
gwd_val = GW_distance_uniform(source_share, target_share, sub,**kwargs)
wd.append(abs(wd_val))
gwd.append(abs(gwd_val))
ot = sum(wd)/len(wd) + sum(gwd)/len(gwd)
return ot, sum(wd)/len(wd), sum(gwd)/len(gwd)