-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
332 lines (272 loc) · 11.8 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
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
def NormalizationEachBand(raw_data, unit=True):
'''
normalize the whole data to [0,1]
'''
new_data=np.zeros([raw_data.shape[0],raw_data.shape[1],raw_data.shape[2]])
for i in range(raw_data.shape[2]):
temp = raw_data[:,:,i]
MAX = np.max(temp.ravel()).astype('float32')
MIN = np.min(temp.ravel())
new_data[:,:,i] = (temp - MIN)/(MAX - MIN)
if unit:
new_data = new_data.reshape(np.prod(new_data.shape[:2]),np.prod(new_data.shape[2:]))
new_data=zscores(new_data)
new_data=new_data.reshape(raw_data.shape[0],raw_data.shape[1],raw_data.shape[2])
return new_data
def zscores(data):
'''
For matrix data, z-scores are computed using the mean and standard deviation along each row of data.
returns a centered, scaled version of each sample, (X-MEAN(X)) ./ STD(X)
input: data with the shape of [samples_number,feature]
This function performs well in ELM algorithm, but not well in 1D-CNN
'''
new_data=np.zeros([data.shape[0],data.shape[1]])
for j in range(data.shape[0]):
new_data[j,:] = (data[j,:]-np.mean(data[j,:]))/np.std(data[j,:],ddof=1)
return new_data
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
def split_first_dim_linear(x, first_two_dims):
"""
Undo the stacking operation
"""
x_shape = x.size()
new_shape = first_two_dims
if len(x_shape) > 1:
new_shape += [x_shape[-1]]
return x.view(new_shape)
def mean_pooling(x):
return torch.mean(x, dim=0, keepdim=True)