-
Notifications
You must be signed in to change notification settings - Fork 4
/
MLUDA_sh.py
269 lines (222 loc) · 11 KB
/
MLUDA_sh.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
# -*- coding:utf-8 -*-
# Author:Mingshuo Cai
# Create_time:2023-08-01
# Updata_time:2024-03-15
# Usage:Implementation of the MLUDA method on the SH2HZ cross-domain dataset
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import mmd
import numpy as np
from sklearn import neighbors
from sklearn import metrics
from net2 import DSANSS
import time
import utils
from torch.utils.data import TensorDataset, DataLoader
from contrastive_loss import SupConLoss
from config_SH2HZ import *
from net2 import DSANSS
from sklearn import svm
from UtilsCMS import *
##################################
file_path = './datasets/Shanghai-Hangzhou/DataCube.mat'
data_s, data_t, label_s, label_t = utils.cubeData(file_path)
data_s,data_t = ILDA(data_s,data_t,pca_n,radius)
# Loss Function
crossEntropy = nn.CrossEntropyLoss().cuda()
ContrastiveLoss_s = SupConLoss(temperature=0.1).cuda()
ContrastiveLoss_t = SupConLoss(temperature=0.1).cuda()
DSH_loss = utils.Domain_Occ_loss().cuda()
acc = np.zeros([nDataSet, 1])
A = np.zeros([nDataSet, CLASS_NUM])
k = np.zeros([nDataSet, 1])
best_predict_all = []
best_acc_all = 0.0
best_G,best_RandPerm,best_Row, best_Column,best_nTrain = None,None,None,None,None
for iDataSet in range(nDataSet):
print('#######################idataset######################## ', iDataSet)
utils.set_seed(seeds[iDataSet])
trainX, trainY = utils.get_sample_data(data_s, label_s, HalfWidth, 180)
testID, testX, testY, G, RandPerm, Row, Column = utils.get_all_data(data_t, label_t, HalfWidth)
train_dataset = TensorDataset(torch.tensor(trainX), torch.tensor(trainY))
test_dataset = TensorDataset(torch.tensor(testX), torch.tensor(testY))
train_loader_s = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=True)
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=True)
train_loader_t = DataLoader(test_dataset,batch_size=BATCH_SIZE,shuffle=True,drop_last=True)
test_loader = DataLoader(test_dataset,batch_size=BATCH_SIZE,shuffle=False,drop_last=True)
len_source_loader = len(train_loader_s)
len_target_loader = len(train_loader_t)
# model
feature_encoder = DSANSS(nBand, patch_size, CLASS_NUM).cuda()
print("Training...")
last_accuracy = 0.0
best_episdoe = 0
train_loss = []
test_acc = []
running_D_loss, running_F_loss = 0.0, 0.0
running_label_loss = 0
running_domain_loss = 0
total_hit, total_num = 0.0, 0.0
size = 0.0
test_acc_list = []
train_start = time.time()
#loss plot
loss1 = []
loss2 = []
loss3 = []
for epoch in range(1, epochs + 1):
LEARNING_RATE = lr #/ math.pow((1 + 10 * (epoch - 1) / epochs), 0.75)
print('learning rate{: .4f}'.format(LEARNING_RATE))
optimizer = torch.optim.SGD([
{'params': feature_encoder.feature_layers.parameters(),},
{'params': feature_encoder.fc1.parameters(), 'lr': LEARNING_RATE},
{'params': feature_encoder.fc2.parameters(), 'lr': LEARNING_RATE},
{'params': feature_encoder.head1.parameters(), 'lr': LEARNING_RATE},
{'params': feature_encoder.head2.parameters(), 'lr': LEARNING_RATE},
], lr=LEARNING_RATE , momentum=momentum, weight_decay=l2_decay)
feature_encoder.train()
iter_source = iter(train_loader_s)
iter_target = iter(train_loader_t)
num_iter = len_source_loader
for i in range(1,num_iter):
source_data, source_label = next(iter_source)
target_data, target_label = next(iter_target)
if i % len_target_loader == 0:
iter_target = iter(train_loader_t)
# 0
source_data0 = utils.radiation_noise(source_data)
source_data0 = source_data0.type(torch.FloatTensor)
# 1
source_data1 = utils.flip_augmentation(source_data)
# 2
target_data0 = utils.radiation_noise(target_data)
target_data0 = target_data0.type(torch.FloatTensor)
# 3
target_data1 = utils.flip_augmentation(target_data)
(source_features, source1, _, source_outputs, source_out,
target_features,_, target1, target_outputs, target_out) = feature_encoder(source_data.cuda(),target_data.cuda())
(_, source2, _, source_outputs2 ,_,
_, _, target2, t1, _) = feature_encoder(source_data0.cuda(),target_data0.cuda())
(_, source3, _, source_outputs3,_,
_, _, target3, t2, _) = feature_encoder(source_data1.cuda(),target_data1.cuda())
softmax_output_t = nn.Softmax(dim=1)(target_outputs).detach()
_, pseudo_label_t = torch.max(softmax_output_t, 1)
entropy_loss = mmd.EntropyLoss(softmax_output_t)
# Supervised Contrastive Loss
all_source_con_features = torch.cat([source2.unsqueeze(1), source3.unsqueeze(1)],dim=1)
all_target_con_features = torch.cat([target2.unsqueeze(1), target3.unsqueeze(1)], dim=1)
# Loss Cls
cls_loss = crossEntropy(source_outputs, source_label.cuda())
# Loss Lmmd
lmmd_loss = mmd.lmmd(source_features, target_features, source_label,
torch.nn.functional.softmax(target_outputs, dim=1), BATCH_SIZE=BATCH_SIZE,
CLASS_NUM=CLASS_NUM)
lambd = 2 / (1 + math.exp(-10 * (epoch) / epochs)) - 1
# Loss Con_s
contrastive_loss_s = ContrastiveLoss_s(all_source_con_features, source_label)
# Loss Con_t
contrastive_loss_t = ContrastiveLoss_t(all_target_con_features, pseudo_label_t)
loss = cls_loss + 0.01 * lambd * lmmd_loss + contrastive_loss_t + contrastive_loss_s
# Update parameters
optimizer.zero_grad()
loss.backward()
optimizer.step()
pred = source_outputs.data.max(1)[1]
total_hit += pred.eq(source_label.data.cuda()).sum()
size += source_label.data.size()[0]
test_accuracy = 100. * float(total_hit) / size
print('epoch {:>3d}: cls loss: {:6.4f},lmmd loss:{:6f},con_s loss:{:6f}, con_t loss:{:6f},acc {:6.4f}, total loss: {:6.4f}'
.format(epoch , cls_loss.item(),lmmd_loss.item(), contrastive_loss_s.item(),contrastive_loss_t.item(),
total_hit / size,loss.item()))
train_end = time.time()
if epoch % epochs == 0:
feature_encoder.eval()
total_rewards = 0
counter = 0
accuracies = []
predict = np.array([], dtype=np.int64)
labels = np.array([], dtype=np.int64)
with torch.no_grad():
for test_datas, test_labels in test_loader:
batch_size = test_labels.shape[0]
source_features, source1, _, source_outputs, source_out, test_features, _, _, test_outputs, _ = feature_encoder(
Variable(source_data).cuda(), Variable(test_datas).cuda())
pred = test_outputs.data.max(1)[1]
test_labels = test_labels.numpy()
rewards = [1 if pred[j] == test_labels[j] else 0 for j in range(batch_size)]
total_rewards += np.sum(rewards)
counter += batch_size
predict = np.append(predict, pred.cpu().numpy())
labels = np.append(labels, test_labels)
accuracy = total_rewards / 1.0 / counter #
accuracies.append(accuracy)
test_accuracy = 100. * total_rewards / len(test_loader.dataset)
acc[iDataSet] = 100. * total_rewards / len(test_loader.dataset)
OA = acc
C = metrics.confusion_matrix(labels, predict)
A[iDataSet, :] = np.diag(C) / np.sum(C, 1, dtype=np.float64)
k[iDataSet] = metrics.cohen_kappa_score(labels, predict)
print('\t\tAccuracy: {}/{} ({:.2f}%)\n'.format(total_rewards, len(test_loader.dataset),
100. * total_rewards / len(test_loader.dataset)))
test_end = time.time()
# Training mode
if test_accuracy > last_accuracy:
# save networks
# torch.save(feature_encoder.state_dict(),str("../checkpoints/DFSL_feature_encoder_" + "houston_cl_lmmd_dis_attention" +str(iDataSet) +".pkl"))
print("save networks for epoch:", epoch + 1)
last_accuracy = test_accuracy
best_episdoe = epoch
best_predict_all = predict
best_G, best_RandPerm, best_Row, best_Column = G, RandPerm, Row, Column
print('best epoch:[{}], best accuracy={}'.format(best_episdoe + 1, last_accuracy))
print('iter:{} best epoch:[{}], best accuracy={}'.format(iDataSet, best_episdoe + 1, last_accuracy))
print('***********************************************************************************')
AA = np.mean(A, 1)
AAMean = np.mean(AA,0)
AAStd = np.std(AA)
AMean = np.mean(A, 0)
AStd = np.std(A, 0)
OAMean = np.mean(acc)
OAStd = np.std(acc)
kMean = np.mean(k)
kStd = np.std(k)
print ("train time per DataSet(s): " + "{:.5f}".format(train_end-train_start))
print("test time per DataSet(s): " + "{:.5f}".format(test_end-train_end))
print ("average OA: " + "{:.2f}".format( OAMean) + " +- " + "{:.2f}".format( OAStd))
print ("average AA: " + "{:.2f}".format(100 * AAMean) + " +- " + "{:.2f}".format(100 * AAStd))
print ("average kappa: " + "{:.4f}".format(100 *kMean) + " +- " + "{:.4f}".format(100 *kStd))
print ("accuracy for each class: ")
for i in range(CLASS_NUM):
print ("Class " + str(i) + ": " + "{:.2f}".format(100 * AMean[i]) + " +- " + "{:.2f}".format(100 * AStd[i]))
best_iDataset = 0
for i in range(len(acc)):
print('{}:{}'.format(i, acc[i]))
if acc[i] > acc[best_iDataset]:
best_iDataset = i
print('best acc all={}'.format(acc[best_iDataset]))
#################classification map################################
for i in range(len(best_predict_all)): # predict ndarray <class 'tuple'>: (9729,)
best_G[best_Row[best_RandPerm[ i]]][best_Column[best_RandPerm[ i]]] = best_predict_all[i] + 1
hsi_pic = np.zeros((best_G.shape[0], best_G.shape[1], 3))
for i in range(best_G.shape[0]):
for j in range(best_G.shape[1]):
if best_G[i][j] == 0:
hsi_pic[i, j, :] = [0, 0, 0]
if best_G[i][j] == 1:
hsi_pic[i, j, :] = [0, 0, 1]
if best_G[i][j] == 2:
hsi_pic[i, j, :] = [0, 1, 0]
if best_G[i][j] == 3:
hsi_pic[i, j, :] = [0, 1, 1]
if best_G[i][j] == 4:
hsi_pic[i, j, :] = [1, 0, 0]
if best_G[i][j] == 5:
hsi_pic[i, j, :] = [1, 0, 1]
if best_G[i][j] == 6:
hsi_pic[i, j, :] = [1, 1, 0]
if best_G[i][j] == 7:
hsi_pic[i, j, :] = [0.5, 0.5, 1]
# utils.classification_map(hsi_pic[4:-4, 4:-4, :], best_G[4:-4, 4:-4], 24, "classificationMap/housotn18.png")