-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
366 lines (293 loc) · 13.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
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
import os
import torch
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 torch.nn as nn
from scipy.spatial.distance import cdist
import torch.nn.functional as F
from operator import truediv
import torch.utils.data as data
import hdf5storage
def cdd(output_t1,output_t2):
mul = output_t1.transpose(0, 1).mm(output_t2)
cdd_loss = torch.sum(mul) - torch.trace(mul)
return cdd_loss
class Domain_Occ_loss(nn.Module):
def __init__(self):
super(Domain_Occ_loss,self).__init__()
def forward(self,p1,p2):#(64,1) (64,1)
loss = - torch.mean(torch.log(p1 + 1e-6))
loss -= torch.mean(torch.log(p2 + 1e-6))
return loss
#load data methods
def cubeData(file_path):
total = sio.loadmat(file_path)
data1 = total['DataCube1'] #up
data2 = total['DataCube2'] #pc
gt1 = total['gt1']
gt2 = total['gt2']
# Data_Band_Scaler_s = data1
# Data_Band_Scaler_t = data2
# print('max and min ')
# 归一化 [-0.5,0.5]
# data1 = data1.astype(np.float32) # 半精度浮点:1位符号,5位指数,10位尾数
# Data_Band_Scaler_s = (data1 - np.min(data1)) / (np.max(data1) - np.min(data1))# - 0.5
#
# data2 = data2.astype(np.float32) # 半精度浮点:1位符号,5位指数,10位尾数
# Data_Band_Scaler_t = (data2 - np.min(data2)) / (np.max(data2) - np.min(data2)) #- 0.5
# # # 标准化
data_s = data1.reshape(np.prod(data1.shape[:2]), np.prod(data1.shape[2:])) # (111104,204)
data_scaler_s = preprocessing.scale(data_s) #标准化 (X-X_mean)/X_std,
Data_Band_Scaler_s = data_scaler_s.reshape(data1.shape[0], data1.shape[1],data1.shape[2])
data_t = data2.reshape(np.prod(data2.shape[:2]), np.prod(data2.shape[2:])) # (111104,204)
data_scaler_t = preprocessing.scale(data_t) #标准化 (X-X_mean)/X_std,
Data_Band_Scaler_t = data_scaler_t.reshape(data2.shape[0], data2.shape[1],data2.shape[2])
print(np.max(Data_Band_Scaler_s),np.min(Data_Band_Scaler_s))
print(np.max(Data_Band_Scaler_t),np.min(Data_Band_Scaler_t))
return Data_Band_Scaler_s,Data_Band_Scaler_t, gt1,gt2 # image:(512,217,3),label:(512,217)
def load_data_houston(image_file, label_file):
image_data = hdf5storage.loadmat(image_file)
label_data = hdf5storage.loadmat(label_file)
# print(image_data.keys()) #mine
# print(label_data.keys())
data_all = image_data['ori_data']
GroundTruth = label_data['map']
Data_Band_Scaler = data_all
# # 归一化
# data = data.astype(np.float32) # 半精度浮点:1位符号,5位指数,10位尾数
# data_all = 1 * ((data_all - np.min(data_all)) / (np.max(data_all) - np.min(data_all)) - 0.5)
# 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])
print(np.max(Data_Band_Scaler), np.min(Data_Band_Scaler))
return Data_Band_Scaler, GroundTruth # image:(512,217,3),label:(512,217)
def load_data_hyrank(image_file, label_file):
image_data = sio.loadmat(image_file)
label_data = sio.loadmat(label_file)
# print(image_data.keys()) #mine
# print(label_data.keys())
data_all = image_data['ori_data']
GroundTruth = label_data['map']
# Data_Band_Scaler = data_all
# # 归一化
# data_all = data_all.astype(np.float32) # 半精度浮点:1位符号,5位指数,10位尾数
# Data_Band_Scaler = 1 * ((data_all - np.min(data_all)) / (np.max(data_all) - np.min(data_all)) - 0.5)
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])
print(np.max(Data_Band_Scaler), np.min(Data_Band_Scaler))
return Data_Band_Scaler, GroundTruth # image:(512,217,3),label:(512,217)
def load_data_pavia(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])
# data_all = data_all.astype(np.float32) # 半精度浮点:1位符号,5位指数,10位尾数
# Data_Band_Scaler = (data_all - np.min(data_all)) / (np.max(data_all) - np.min(data_all))
# Data_Band_Scaler = data_all
print(np.max(Data_Band_Scaler),np.min(Data_Band_Scaler))
return Data_Band_Scaler, GroundTruth # image:(512,217,3),label:(512,217)
def get_sample_data(Sample_data, Sample_label, HalfWidth, num_per_class):
print('get_sample_data() run...')
print('The original sample data shape:',Sample_data.shape)
nBand = Sample_data.shape[2]
data = np.pad(Sample_data, ((HalfWidth, HalfWidth), (HalfWidth, HalfWidth), (0, 0)), mode='constant')
label = np.pad(Sample_label, HalfWidth, mode='constant')
train = {}
train_indices = []
[Row, Column] = np.nonzero(label)
m = int(np.max(label))
print(f'num_class : {m}')
val = {}
val_indices = []
for i in range(m):
indices = [j for j, x in enumerate(Row.ravel().tolist()) if label[Row[j], Column[j]] == i + 1]
np.random.shuffle(indices)
train[i] = indices[:num_per_class]
val[i] = indices[num_per_class:]
for i in range(m):
train_indices += train[i]
val_indices += val[i]
np.random.shuffle(train_indices)
np.random.shuffle(val_indices)
#val
print('the number of val data:', len(val_indices))
nVAL = len(val_indices)
val_data = np.zeros([nVAL, nBand, 2 * HalfWidth + 1, 2 * HalfWidth + 1], dtype=np.float32)
val_label = np.zeros([nVAL], dtype=np.int64)
RandPerm = val_indices
RandPerm = np.array(RandPerm)
for i in range(nVAL):
val_data[i, :, :, :] = np.transpose(data[Row[RandPerm[i]] - HalfWidth: Row[RandPerm[i]] + HalfWidth + 1, \
Column[RandPerm[i]] - HalfWidth: Column[RandPerm[i]] + HalfWidth + 1,
:],
(2, 0, 1))
val_label[i] = label[Row[RandPerm[i]], Column[RandPerm[i]]].astype(np.int64)
val_label = val_label - 1
#train
print('the number of processed data:', len(train_indices))
nTrain = len(train_indices)
index = np.zeros([nTrain], dtype=np.int64)
processed_data = np.zeros([nTrain, nBand, 2 * HalfWidth + 1, 2 * HalfWidth + 1], dtype=np.float32)
processed_label = np.zeros([nTrain], dtype=np.int64)
RandPerm = train_indices
RandPerm = np.array(RandPerm)
for i in range(nTrain):
index[i] = i
processed_data[i, :, :, :] = np.transpose(data[Row[RandPerm[i]] - HalfWidth: Row[RandPerm[i]] + HalfWidth + 1, \
Column[RandPerm[i]] - HalfWidth: Column[RandPerm[i]] + HalfWidth + 1, :],
(2, 0, 1))
processed_label[i] = label[Row[RandPerm[i]], Column[RandPerm[i]]].astype(np.int64)
processed_label = processed_label - 1
print('sample data shape', processed_data.shape)
print('sample label shape', processed_label.shape)
print('get_sample_data() end...')
return processed_data, processed_label#, val_data, val_label
def get_all_data(All_data, All_label, HalfWidth):
print('get_all_data() run...')
print('The original data shape:', All_data.shape)
nBand = All_data.shape[2]
data = np.pad(All_data, ((HalfWidth, HalfWidth), (HalfWidth, HalfWidth), (0, 0)), mode='constant')
label = np.pad(All_label, HalfWidth, mode='constant')
train = {}
train_indices = []
[Row, Column] = np.nonzero(label)
num_class = int(np.max(label))
print(f'num_class : {num_class}')
for i in range(num_class):
indices = [j for j, x in enumerate(Row.ravel().tolist()) if
label[Row[j], Column[j]] == i + 1]
np.random.shuffle(indices)
train[i] = indices
for i in range(num_class):
train_indices += train[i]
np.random.shuffle(train_indices)
print('the number of all data:', len(train_indices))
nTest = len(train_indices)
index = np.zeros([nTest], dtype=np.int64)
processed_data = np.zeros([nTest, nBand, 2 * HalfWidth + 1, 2 * HalfWidth + 1], dtype=np.float32)
processed_label = np.zeros([nTest], dtype=np.int64)
RandPerm = train_indices
RandPerm = np.array(RandPerm)
for i in range(nTest):
index[i] = i
processed_data[i, :, :, :] = np.transpose(data[Row[RandPerm[i]] - HalfWidth: Row[RandPerm[i]] + HalfWidth + 1, \
Column[RandPerm[i]] - HalfWidth: Column[RandPerm[i]] + HalfWidth + 1, :],
(2, 0, 1))
processed_label[i] = label[Row[RandPerm[i]], Column[RandPerm[i]]].astype(np.int64)
processed_label = processed_label - 1
print('processed all data shape:', processed_data.shape)
print('processed all label shape:', processed_label.shape)
print('get_all_data() end...')
return index, processed_data, processed_label, label, RandPerm, Row, Column
def obtain_label(loader, net):
start_test = True
net.eval()
predict = np.array([], dtype=np.int64)
with torch.no_grad():
iter_test = iter(loader)
for i in range(len(loader)):
data = iter_test.next()
inputs = data[0]
labels = data[1]
inputs = inputs.cuda()
feas, _, _, outputs, _ = net(inputs)
if start_test:
all_fea = feas.float().cpu()
all_output = outputs.float().cpu()
all_label = labels.float()
start_test = False
else:
all_fea = torch.cat((all_fea, feas.float().cpu()), 0) # (53200,128)
all_output = torch.cat((all_output, outputs.float().cpu()), 0) # (53200,7)
all_label = torch.cat((all_label, labels.float()), 0) # 53200
all_output = nn.Softmax(dim=1)(all_output)
output, pred_label = torch.max(all_output, 1)
predict = np.append(predict, pred_label.cpu().numpy())
return predict, output
def Weighted_CrossEntropy(input_,labels):
input_s = F.softmax(input_)
entropy = -input_s * torch.log(input_s + 1e-5)
entropy = torch.sum(entropy, dim=1)
weight = 1.0 + torch.exp(-entropy)
weight = weight / torch.sum(weight).detach().item()
#print("cross:",nn.CrossEntropyLoss(reduction='none')(input_, labels))
return torch.mean(weight * nn.CrossEntropyLoss(reduction='none')(input_, labels))
def twist_loss(p1,p2,alpha=1,beta=1):
eps=1e-7 #ensure calculate
#eps=0
kl_div=((p2*p2.log()).sum(dim=1)-(p2*p1.log()).sum(dim=1)).mean()
mean_entropy=-(p1*(p1.log()+eps)).sum(dim=1).mean()
mean_prob=p1.mean(dim=0)
entropy_mean=-(mean_prob*(mean_prob.log()+eps)).sum()
return kl_div + alpha * mean_entropy - beta * entropy_mean
def extract_embeddings(model, dataloader):
model.eval()
n_samples = dataloader.batch_size * len(dataloader)
embeddings = np.zeros((n_samples, model.n_outputs))
labels = np.zeros(n_samples)
k = 0
for images, target in dataloader:
with torch.no_grad():
images = images.cuda()
embeddings[k:k+len(images)] = model.get_embedding(images).data.cpu().numpy()
labels[k:k+len(images)] = target.numpy()
k += len(images)
return embeddings[0:k], labels[0:k]
#data augmentation
def radiation_noise(data, alpha_range=(0.9, 1.1), beta=0.04): #pavia/houston = 0.04
alpha = np.random.uniform(*alpha_range)
noise = np.random.normal(loc=0., scale=1.0, size=data.shape)
x = alpha * data + beta * noise
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)
data = torch.from_numpy(data.copy())
if vertical:
data = np.flipud(data)
data = torch.from_numpy(data.copy())
return data
#set seed
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# torch.backends.cudnn.deterministic = True
# torch.backends.cudnn.benchmark = False
def Weighted_CrossEntropy(input_,labels):
input_s = F.softmax(input_, dim=1)
entropy = -input_s * torch.log(input_s + 1e-5)
entropy = torch.sum(entropy, dim=1)
weight = 1.0 + torch.exp(-entropy)
weight = weight / torch.sum(weight).detach().item()
#print("cross:",nn.CrossEntropyLoss(reduction='none')(input_, labels))
return torch.mean(weight * nn.CrossEntropyLoss(reduction='none')(input_, labels))
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