-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
723 lines (584 loc) · 24.2 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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
import torch
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 pickle
import torch.nn.functional as F
from typing import List
import yaml
from ast import literal_eval
import logging
import copy
from tqdm import tqdm
import logging
import shutil
import torch.utils.data as data
def get_one_hot(y_s):
num_classes = torch.unique(y_s).size(0)
eye = torch.eye(num_classes).to(y_s.device)
one_hot = []
for y_task in y_s:
one_hot.append(eye[y_task].unsqueeze(0))
one_hot = torch.cat(one_hot, 0)
return one_hot
class hsidataset_target(data.Dataset):
def __init__(self, data,label):
self.data=data
self.label=label
# self.trans=RandomErasing.RandomErasing()
def __getitem__(self, index):
img=self.data[index]
img1=img
return img,img1
def __len__(self):
return len(self.data)
def get_logs_path(model_path, method, shot):
exp_path = '_'.join(model_path.split('/')[1:])
file_path = os.path.join('tmp', exp_path, method)
os.makedirs(file_path, exist_ok=True)
return os.path.join(file_path, f'{shot}.txt')
def get_features(model, samples):
features, _ = model(samples, True)
features = F.normalize(features.view(features.size(0), -1), dim=1)
return features
def get_loss(logits_s, logits_q, labels_s, lambdaa):
Q = logits_q.softmax(2)
y_s_one_hot = get_one_hot(labels_s)
ce_sup = - (y_s_one_hot * torch.log(logits_s.softmax(2) + 1e-12)).sum(2).mean(1) # Taking the mean over samples within a task, and summing over all samples
ent_q = get_entropy(Q)
cond_ent_q = get_cond_entropy(Q)
loss = - (ent_q - cond_ent_q) + lambdaa * ce_sup
return loss
def get_mi(probs):
q_cond_ent = get_cond_entropy(probs)
q_ent = get_entropy(probs)
return q_ent - q_cond_ent
def get_entropy(probs):
q_ent = - (probs.mean(1) * torch.log(probs.mean(1) + 1e-12)).sum(1, keepdim=True)
return q_ent
def get_cond_entropy(probs):
q_cond_ent = - (probs * torch.log(probs + 1e-12)).sum(2).mean(1, keepdim=True)
return q_cond_ent
def get_metric(metric_type):
METRICS = {
'cosine': lambda gallery, query: 1. - F.cosine_similarity(query[:, None, :], gallery[None, :, :], dim=2),
'euclidean': lambda gallery, query: ((query[:, None, :] - gallery[None, :, :]) ** 2).sum(2),
'l1': lambda gallery, query: torch.norm((query[:, None, :] - gallery[None, :, :]), p=1, dim=2),
'l2': lambda gallery, query: torch.norm((query[:, None, :] - gallery[None, :, :]), p=2, dim=2),
}
return METRICS[metric_type]
class AverageMeter(object):
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
def setup_logger(filepath):
file_formatter = logging.Formatter(
"[%(asctime)s %(filename)s:%(lineno)s] %(levelname)-8s %(message)s",
datefmt='%Y-%m-%d %H:%M:%S',
)
logger = logging.getLogger('example')
# handler = logging.StreamHandler()
# handler.setFormatter(file_formatter)
# logger.addHandler(handler)
file_handle_name = "file"
if file_handle_name in [h.name for h in logger.handlers]:
return
if os.path.dirname(filepath) != '':
if not os.path.isdir(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
file_handle = logging.FileHandler(filename=filepath, mode="a")
file_handle.set_name(file_handle_name)
file_handle.setFormatter(file_formatter)
logger.addHandler(file_handle)
logger.setLevel(logging.DEBUG)
return logger
def warp_tqdm(data_loader, disable_tqdm):
if disable_tqdm:
tqdm_loader = data_loader
else:
tqdm_loader = tqdm(data_loader, total=len(data_loader))
return tqdm_loader
def save_pickle(file, data):
with open(file, 'wb') as f:
pickle.dump(data, f)
def load_pickle(file):
with open(file, 'rb') as f:
return pickle.load(f)
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar', folder='result/default'):
os.makedirs(folder, exist_ok=True)
torch.save(state, os.path.join(folder, filename))
if is_best:
shutil.copyfile(folder + '/' + filename, folder + '/model_best.pth.tar')
def load_checkpoint(model, model_path, type='best'):
if type == 'best':
checkpoint = torch.load('{}/model_best.pth.tar'.format(model_path), map_location=torch.device('cpu'))
elif type == 'last':
checkpoint = torch.load('{}/checkpoint.pth.tar'.format(model_path), map_location=torch.device('cpu'))
else:
assert False, 'type should be in [best, or last], but got {}'.format(type)
state_dict = checkpoint['state_dict']
names = []
for k, v in state_dict.items():
names.append(k)
model.load_state_dict(state_dict)
def compute_confidence_interval(data, axis=0):
"""
Compute 95% confidence interval
:param data: An array of mean accuracy (or mAP) across a number of sampled episodes.
:return: the 95% confidence interval for this data.
"""
a = 1.0 * np.array(data)
m = np.mean(a, axis=axis)
std = np.std(a, axis=axis)
pm = 1.96 * (std / np.sqrt(a.shape[axis]))
return m, pm
class CfgNode(dict):
"""
CfgNode represents an internal node in the configuration tree. It's a simple
dict-like container that allows for attribute-based access to keys.
"""
def __init__(self, init_dict=None, key_list=None, new_allowed=False):
# Recursively convert nested dictionaries in init_dict into CfgNodes
init_dict = {} if init_dict is None else init_dict
key_list = [] if key_list is None else key_list
for k, v in init_dict.items():
if type(v) is dict:
# Convert dict to CfgNode
init_dict[k] = CfgNode(v, key_list=key_list + [k])
super(CfgNode, self).__init__(init_dict)
def __getattr__(self, name):
if name in self:
return self[name]
else:
raise AttributeError(name)
def __setattr__(self, name, value):
self[name] = value
def __str__(self):
def _indent(s_, num_spaces):
s = s_.split("\n")
if len(s) == 1:
return s_
first = s.pop(0)
s = [(num_spaces * " ") + line for line in s]
s = "\n".join(s)
s = first + "\n" + s
return s
r = ""
s = []
for k, v in sorted(self.items()):
seperator = "\n" if isinstance(v, CfgNode) else " "
attr_str = "{}:{}{}".format(str(k), seperator, str(v))
attr_str = _indent(attr_str, 2)
s.append(attr_str)
r += "\n".join(s)
return r
def __repr__(self):
return "{}({})".format(self.__class__.__name__, super(CfgNode, self).__repr__())
def _decode_cfg_value(v):
if not isinstance(v, str):
return v
try:
v = literal_eval(v)
except ValueError:
pass
except SyntaxError:
pass
return v
def _check_and_coerce_cfg_value_type(replacement, original, key, full_key):
original_type = type(original)
replacement_type = type(replacement)
# The types must match (with some exceptions)
if replacement_type == original_type:
return replacement
def conditional_cast(from_type, to_type):
if replacement_type == from_type and original_type == to_type:
return True, to_type(replacement)
else:
return False, None
casts = [(tuple, list), (list, tuple)]
try:
casts.append((str, unicode)) # noqa: F821
except Exception:
pass
for (from_type, to_type) in casts:
converted, converted_value = conditional_cast(from_type, to_type)
if converted:
return converted_value
raise ValueError(
"Type mismatch ({} vs. {}) with values ({} vs. {}) for config "
"key: {}".format(
original_type, replacement_type, original, replacement, full_key
)
)
def load_cfg_from_cfg_file(file: str):
cfg = {}
assert os.path.isfile(file) and file.endswith('.yaml'), \
'{} is not a yaml file'.format(file)
with open(file, 'r') as f:
cfg_from_file = yaml.safe_load(f)
for key in cfg_from_file:
for k, v in cfg_from_file[key].items():
cfg[k] = v
cfg = CfgNode(cfg)
return cfg
def merge_cfg_from_list(cfg: CfgNode,
cfg_list: List[str]):
new_cfg = copy.deepcopy(cfg)
assert len(cfg_list) % 2 == 0, cfg_list
for full_key, v in zip(cfg_list[0::2], cfg_list[1::2]):
subkey = full_key.split('.')[-1]
assert subkey in cfg, 'Non-existent key: {}'.format(full_key)
value = _decode_cfg_value(v)
value = _check_and_coerce_cfg_value_type(
value, cfg[subkey], subkey, full_key
)
setattr(new_cfg, subkey, value)
return new_cfg
class Logger:
def __init__(self, module_name, filename):
self.module_name = module_name
self.filename = filename
self.formatter = self.get_formatter()
self.file_handler = self.get_file_handler()
self.stream_handler = self.get_stream_handler()
self.logger = self.get_logger()
def get_formatter(self):
log_format = '[%(name)s]: [%(levelname)s]: %(message)s'
formatter = logging.Formatter(log_format)
return formatter
def get_file_handler(self):
file_handler = logging.FileHandler(self.filename)
file_handler.setFormatter(self.formatter)
return file_handler
def get_stream_handler(self):
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(self.formatter)
return stream_handler
def get_logger(self):
logger = logging.getLogger(self.module_name)
logger.setLevel(logging.INFO)
logger.addHandler(self.file_handler)
logger.addHandler(self.stream_handler)
return logger
def del_logger(self):
handlers = self.logger.handlers[:]
for handler in handlers:
handler.close()
self.logger.removeHandler(handler)
def info(self, msg):
self.logger.info(msg)
def debug(self, msg):
self.logger.debug(msg)
def warning(self, msg):
self.logger.warning(msg)
def critical(self, msg):
self.logger.critical(msg)
def exception(self, msg):
self.logger.exception(msg)
def make_log_dir(log_path, dataset, backbone, method):
log_dir = os.path.join(log_path, dataset, backbone, method)
if not os.path.exists(log_dir):
os.makedirs(log_dir, exist_ok=True)
return log_dir
def get_log_file(log_path, dataset, backbone, method):
log_dir = make_log_dir(log_path=log_path, dataset=dataset, backbone=backbone, method=method)
i = 0
filename = os.path.join(log_dir, '{}_run_{}.log'.format(method, i))
while os.path.exists(os.path.join(log_dir, '{}_run_%s.log'.format(method)) % i):
i += 1
filename = os.path.join(log_dir, '{}_run_{}.log'.format(method, i))
return filename
def extract_features(model, support, query):
model.eval()
with torch.no_grad():
# Extracting features
outputs_s = []
outputs_q = []
for i in range(len(support)):
output_s, _ = model(support[i], feature=True)
output_q, _ = model(query[i], feature=True)
outputs_s.append(output_s)
outputs_q.append(output_q)
support = torch.stack(outputs_s)
query = torch.stack(outputs_q)
return support, query
def extract_mean_features(model, train_loader, args, logger, device):
"""
inputs:
model : The loaded model containing the feature extractor
train_loader : Train data loader
args : arguments
logger : logger object
device : GPU device
returns :
out_mean : Training data features mean
"""
# Load features from memory if previously saved ...
save_dir = os.path.join(args.ckpt_path, args.model_tag, args.used_set)
filepath_mean = os.path.join(save_dir, 'output_mean.plk')
# get training mean
if not os.path.isfile(filepath_mean):
logger.info(" ==> Beginning feature extraction to compute training mean")
os.makedirs(save_dir, exist_ok=True)
model.eval()
with torch.no_grad():
out_mean, fc_out_mean = [], []
for i, (inputs, labels, _) in enumerate(warp_tqdm(train_loader, False)):
inputs = inputs.to(device)
outputs, fc_outputs = model(inputs, True)
out_mean.append(outputs.cpu().data.numpy())
if fc_outputs is not None:
fc_out_mean.append(fc_outputs.cpu().data.numpy())
out_mean = np.concatenate(out_mean, axis=0).mean(0)
if len(fc_out_mean) > 0:
fc_out_mean = np.concatenate(fc_out_mean, axis=0).mean(0)
else:
fc_out_mean = -1
logger.info(" ==> Saving features to {}".format(filepath_mean))
save_pickle(save_dir + '/output_mean.plk', [out_mean, fc_out_mean])
return torch.from_numpy(out_mean), torch.from_numpy(fc_out_mean)
else:
out_mean, fc_out_mean = load_pickle(save_dir + '/output_mean.plk')
logger.info(" ==> Features loaded from {}".format(filepath_mean))
return torch.from_numpy(out_mean), torch.from_numpy(fc_out_mean)
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]
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] # dic-> narray , KSC:ndarray(512,217,204)
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:])) # (111104,204)
data_scaler = preprocessing.scale(data) # (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 # image:(512,217,3),label:(512,217)
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