-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
271 lines (206 loc) · 8.67 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 7 03:22:06 2023
@author: Rojan Basnet
"""
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 set_random_seeds(seed):
# Set the seed for random number generators for reproducibility
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def calculate_mean_confidence_interval(data, confidence=0.95):
# Calculate the mean and confidence interval of the data
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
def calculate_accuracy(confusion_matrix):
# Calculate the average accuracy and accuracy for each class from the 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(np.divide(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>`"""
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 == 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):
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 filter_valid_classes(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 classes:', nclass)
print('The number of samples:', nsamples)
return all_good
def apply_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_image_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] # Dictionary -> ndarray , KSC:ndarray(512,217,204)
GroundTruth = label_data[label_key]
[nRow, nColumn, nBand] = data_all.shape
print('Data key:', data_key)
print('Number of rows:', nRow)
print('Number of columns:', nColumn)
print('Number of bands:', 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 add_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 apply_flip_augmentation(data):
# Apply flip augmentation to data
horizontal = np.random.random() > 0.5
vertical = np.random.random() > 0.5
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]
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)]
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
class ClassBalancedSampler(Sampler):
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
def get_HBKC_data_loader(task, num_per_class=1, split='train', shuffle=False):
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