-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAE.py
637 lines (526 loc) · 24.8 KB
/
AE.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
from common import *
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import torch.optim as optim
# from icvl_data import LoadData
# from utils import SAM_torch, PSNR_GPU, get_paths, TrainsetFromFolder, SAM
import sewar
# import MCNet
from pathlib import Path
from torch.nn.functional import interpolate
import torchvision.models as models
import numpy as np
# from SSPSR import HybridLoss
from GELIN import HLoss
from unet import UNet
import torch.nn.functional as F
import torch.nn as nn
from HStest import HSTestData
from HStrain import HSTrainingData
class Swish(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x)
class Codebook(nn.Module):
def __init__(self, num_codebook_vectors, latent_dim, beta=0.25):
super(Codebook, self).__init__()
self.num_codebook_vectors = num_codebook_vectors
self.latent_dim = latent_dim
self.beta = beta
self.embedding = nn.Embedding(self.num_codebook_vectors, self.latent_dim)
self.embedding.weight.data.uniform_(-1.0 / self.num_codebook_vectors, 1.0 / self.num_codebook_vectors)
def forward(self, z):
z = z.permute(0, 2, 3, 1).contiguous()
z_flattened = z.view(-1, self.latent_dim)
d = torch.sum(z_flattened**2, dim=1, keepdim=True) + \
torch.sum(self.embedding.weight**2, dim=1) - \
2*(torch.matmul(z_flattened, self.embedding.weight.t()))
min_encoding_indices = torch.argmin(d, dim=1)
z_q = self.embedding(min_encoding_indices).view(z.shape)
loss = torch.mean((z_q.detach() - z)**2) + self.beta * torch.mean((z_q - z.detach())**2)
z_q = z + (z_q - z).detach()
z_q = z_q.permute(0, 3, 1, 2)
return z_q, min_encoding_indices, loss
class Autoencoder(nn.Module):
def __init__(self):
super(Autoencoder, self).__init__()
# 编码器
self.encoder = nn.Sequential(
nn.Conv2d(33, 16, kernel_size=3, stride=1, padding=1),
nn.ReLU(True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(16, 8, kernel_size=3, stride=1, padding=1),
nn.ReLU(True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(8, 3, kernel_size=3, stride=1, padding=1),
nn.ReLU(True),
)
# 解码器
self.decoder = nn.Sequential(
nn.ConvTranspose2d(3, 8, kernel_size=3, stride=1, padding=1),
nn.ReLU(True),
nn.ConvTranspose2d(8, 16, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.ReLU(True),
nn.ConvTranspose2d(16, 33, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.ReLU(True),
)
def forward(self, x):
# 编码
x = self.encoder(x)
# 改变 z 的大小
z = x[:, :3, :, :]
# 解码
x = self.decoder(z)
return x,z
def random_mask(data, p=0.2):
"""\n 随机将数据中一定比例的像素置为0\n :param data: 输入数据,尺寸为(bs, 31, 128, 128)\n :param p: 置0的比例\n :return: mask后的数据\n """
mask = torch.rand(data.size()) > p
mask = mask.to(data.device)
return data * mask.float()
class SSB(nn.Module):
def __init__(self, n_feats, kernel_size, act, res_scale, conv=default_conv):
super(SSB, self).__init__()
self.spa = ResBlock(conv, n_feats, kernel_size, act=act, res_scale=res_scale)
self.spc = ResAttentionBlock(conv, n_feats, 1, act=act, res_scale=res_scale)
def forward(self, x):
return self.spc(self.spa(x))
class SSB_DAQ(nn.Module):
def __init__(self, n_feats, kernel_size, act, res_scale, conv=default_conv):
super(SSB_DAQ, self).__init__()
self.spa = ResBlock_DAQ(conv, n_feats, kernel_size, act=act, res_scale=res_scale)
self.spc = ResAttentionBlock_DAQ(conv, n_feats, 1, act=act, res_scale=res_scale)
def forward(self, x):
return self.spc(self.spa(x))
class SSPN(nn.Module):
def __init__(self, n_feats, n_blocks, act, res_scale):
super(SSPN, self).__init__()
kernel_size = 3
m = []
for i in range(n_blocks):
# ---------------------------------------------------------------------------------------------
# 是否使用DAQ策略的设置!!!!
# ---------------------------------------------------------------------------------------------
m.append(SSB(n_feats, kernel_size, act=act, res_scale=res_scale))
# m.append(SSB_DAQ(n_feats, kernel_size, act=act, res_scale=res_scale))
self.net = nn.Sequential(*m)
def forward(self, x):
res = self.net(x)
# res += x
res = res + x
return res
# a single branch of proposed SSPSR
class BranchUnit(nn.Module):
def __init__(self, n_colors, n_feats, n_blocks, act, res_scale, up_scale, use_tail=True, conv=default_conv):
super(BranchUnit, self).__init__()
kernel_size = 3
# self.head = conv(n_colors, n_feats, kernel_size)
self.head = nn.Conv2d(n_colors, n_feats, kernel_size=3, padding=1)
self.body = SSPN(n_feats, n_blocks, act, res_scale)
self.upsample = Upsampler(conv, up_scale, n_feats)
self.tail = None
if use_tail:
self.tail = conv(n_feats, n_colors, kernel_size)
def forward(self, x):
y = self.head(x)
y = self.body(y)
y = self.upsample(y)
if self.tail is not None:
y = self.tail(y)
return y
class Encoder(nn.Module):
def __init__(self, input_channel, out_channel,n_feats=128):
super(Encoder, self).__init__()
self.input_channel = input_channel
self.out_channel = out_channel
# self.model = nn.Sequential(
# SSPN(n_feats=self.input_channel, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel, self.input_channel // 3, 1),
# # SSB(n_feats=self.input_channel//3,kernel_size=1,act=nn.LeakyReLU(),res_scale=0.1),
# SSPN(n_feats=self.input_channel // 3, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel // 3, self.input_channel // 6, 1),
# SSPN(n_feats=self.input_channel // 6, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel // 6, self.out_channel, 1),
# )
# self.model2 = nn.Sequential(
# SSPN(n_feats=self.input_channel, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel, self.input_channel // 2, 1),
# # SSB(n_feats=self.input_channel//3,kernel_size=1,act=nn.LeakyReLU(),res_scale=0.1),
# SSPN(n_feats=self.input_channel // 2, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel // 2, self.out_channel, 1),
# )
self.branch = BranchUnit(input_channel, n_feats=n_feats, n_blocks=3, act=nn.LeakyReLU(), res_scale=0.1,use_tail=False, up_scale=1,conv=default_conv)
self.final = nn.Conv2d(n_feats, out_channel, kernel_size=3, padding=1)
def forward(self, x):
x = self.branch(x)
x = self.final(x)
# x = self.model2(x)
return x
class Decoder(nn.Module):
def __init__(self, input_channel, out_channel, n_feats=128):
super(Decoder, self).__init__()
self.input_channel = input_channel
self.out_channel = out_channel
# self.model = nn.Sequential(
# SSPN(n_feats=self.input_channel, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel, self.input_channel * 3, 1),
# # SSB(n_feats=self.input_channel//3,kernel_size=1,act=nn.LeakyReLU(),res_scale=0.1),
# SSPN(n_feats=self.input_channel * 3, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel * 3, self.input_channel * 9, 1),
# SSPN(n_feats=self.input_channel * 9, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel * 9, self.out_channel, 1),
# )
# self.model2 = nn.Sequential(
# SSPN(n_feats=self.input_channel, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel, self.input_channel * 2, 1),
# # SSB(n_feats=self.input_channel//3,kernel_size=1,act=nn.LeakyReLU(),res_scale=0.1),
# SSPN(n_feats=self.input_channel * 2, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1),
# nn.Conv2d(self.input_channel * 2, self.out_channel, 1),
# )
self.branch = BranchUnit(input_channel, n_feats=n_feats, n_blocks=3, act=nn.LeakyReLU(), res_scale=0.1, up_scale=1,conv=default_conv,use_tail=False)
self.final = nn.Conv2d(n_feats, out_channel, kernel_size=3, padding=1)
# self.codebook = Codebook(4096,512)
# self.quant_conv = nn.Conv2d(3, 3, 1)
# self.post_quant_conv = nn.Conv2d(3, 3, 1)
def forward(self, x):
# x = self.quant_conv(x)
# x,_,q_loss = self.codebook(x)
# x = self.post_quant_conv(x)
x = self.branch(x)
x = self.final(x)
# x = self.model2(x)
return x
class post_GAE(nn.Module):
def __init__(self, n_colors):
self.trunk = BranchUnit(n_colors, n_feats=256, n_blocks=3, act=nn.LeakyReLU(), res_scale=0.1, up_scale=1,conv=default_conv,use_tail=False)
self.final = nn.Conv2d(256, n_colors, kernel_size=3, padding=1)
def forward(self, x):
x = self.trunk(x)
x = self.final(x)
return x
class GAE(nn.Module):
def __init__(self, Encoder, Decoder, n_subs=8, n_ovls=2, n_colors=31, n_feats=128):
super(GAE, self).__init__()
self.Encoder = Encoder(n_subs,3,n_feats)
self.Decoder = Decoder(3,n_subs,n_feats)
self.device = 'cuda:0'
# calculate the group number (the number of branch networks)
self.G = math.ceil((n_colors - n_ovls) / (n_subs - n_ovls))
# calculate group indices
self.start_idx = []
self.end_idx = []
self.trunk = BranchUnit(n_colors, n_feats=32, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1, up_scale=1,conv=default_conv,use_tail=False)
self.final = nn.Conv2d(32, n_colors, kernel_size=3, padding=1)
# self.codebook = Codebook(4096,512)
for g in range(self.G):
sta_ind = (n_subs - n_ovls) * g
end_ind = sta_ind + n_subs
if end_ind > n_colors:
end_ind = n_colors
sta_ind = n_colors - n_subs
self.start_idx.append(sta_ind)
self.end_idx.append(end_ind)
def decode(self,x,z_list):
b, c, h, w = x.shape
self.device = 'cuda:0'
channel_counter = torch.zeros(c).to(self.device)
y = torch.zeros(b, c, h, w).to(self.device)
for g in range(self.G):
sta_ind = self.start_idx[g]
end_ind = self.end_idx[g]
output_i = self.Decoder(z_list[g])
y[:, sta_ind:end_ind, :, :] += output_i
channel_counter[sta_ind:end_ind] = channel_counter[sta_ind:end_ind] + 1
# intermediate “result” is averaged according to their spectral indices
y = y / channel_counter.unsqueeze(1).unsqueeze(2)
# x = self.quant_conv(y)
# x,_,__ = self.codebook(x)
# x = self.post_quant_conv(x)
# ---------------------------------------------------------------------------------------------
# 是否进行后处理以及不同方式的设置!!!!
# ---------------------------------------------------------------------------------------------
y1 = self.trunk(y)
y1 = self.final(y1)
# y1 = self.post_unet(y)
# y1 = self.unet(y1, time=None)
# y1 = self.after_unet(y1)
y = y1 +y
return y
def encode(self,x):
b, c, h, w = x.shape
# channel_counter = torch.zeros(c).cuda()
self.device='cuda:0'
y = torch.zeros(b, c, h, w).to(self.device)
z_list = []
for g in range(self.G):
sta_ind = self.start_idx[g]
end_ind = self.end_idx[g]
xi = x[:, sta_ind:end_ind, :, :]
# 编码与解码,保存中间的隐藏层变量。
z = self.Encoder(xi)
z_list.append(z)
return z_list
def forward(self,x):
b, c, h, w = x.shape
self.device='cuda:0'
channel_counter = torch.zeros(c).to(self.device)
y = torch.zeros(b, c, h, w).to(self.device)
z_list = []
q_total = 0.0
for g in range(self.G):
sta_ind = self.start_idx[g]
end_ind = self.end_idx[g]
xi = x[:, sta_ind:end_ind, :, :]
# 编码与解码,保存中间的隐藏层变量。
z = self.Encoder(xi)
# print(z.shape)
z_list.append(z)
output_i = self.Decoder(z)
# q_total += q_loss
y[:, sta_ind:end_ind, :, :] += output_i
channel_counter[sta_ind:end_ind] = channel_counter[sta_ind:end_ind] + 1
# intermediate “result” is averaged according to their spectral indices
y = y / channel_counter.unsqueeze(1).unsqueeze(2)
# x = self.quant_conv(y)
# x,_,__ = self.codebook(x)
# x = self.post_quant_conv(x)
# ---------------------------------------------------------------------------------------------
# 是否进行后处理以及不同方式的设置!!!!
# ---------------------------------------------------------------------------------------------
y1 = self.trunk(y)
y1 = self.final(y1)
# y1 = self.post_unet(y)
# y1 = self.unet(y1, time=None)
# y1 = self.after_unet(y1)
y = y1 +y
return y, z_list
# return y, z_list, q_total/self.G
class SR_encoder(nn.Module):
def __init__(self, Encoder, Decoder, n_subs=8, n_ovls=2, n_colors=31, n_feats=128, device='cuda:0'):
super(SR_encoder, self).__init__()
self.Encoder = Encoder(n_subs,3,n_feats)
self.device = device
# calculate the group number (the number of branch networks)
self.G = math.ceil((n_colors - n_ovls) / (n_subs - n_ovls))
# calculate group indices
self.start_idx = []
self.end_idx = []
for g in range(self.G):
sta_ind = (n_subs - n_ovls) * g
end_ind = sta_ind + n_subs
if end_ind > n_colors:
end_ind = n_colors
sta_ind = n_colors - n_subs
self.start_idx.append(sta_ind)
self.end_idx.append(end_ind)
def forward(self,x):
b, c, h, w = x.shape
# device='cuda:0'
channel_counter = torch.zeros(c).to(self.device)
y = torch.zeros(b, c, h, w).to(self.device)
z_list = []
q_total = 0.0
for g in range(self.G):
sta_ind = self.start_idx[g]
end_ind = self.end_idx[g]
xi = x[:, sta_ind:end_ind, :, :]
z = self.Encoder(xi)
# print(z.shape)
z_list.append(z)
return z_list
class AE(nn.Module):
def __init__(self, Encoder, Decoder, in_channels=102, n_feats=128):
super(AE, self).__init__()
self.Encoder = Encoder(in_channels,3,n_feats)
self.Decoder = Decoder(3,in_channels,n_feats)
self.device = 'cuda:1'
self.trunk = BranchUnit(in_channels, n_feats=32, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1, up_scale=1,conv=default_conv,use_tail=False)
self.final = nn.Conv2d(32, in_channels, kernel_size=3, padding=1)
def decode(self,x):
y = self.Decoder(x)
y1 = self.trunk(y)
y1 = self.final(y1)
y = y1 + y
return y
def encode(self,x):
x = self.Encoder(x)
return x
def forward(self,x):
x = self.Encoder(x)
y = self.Decoder(x)
y1 = self.trunk(y)
y1 = self.final(y1)
y = y1 + y
return y
class AE_duichen(nn.Module):
def __init__(self, Encoder, Decoder, n_subs=8, n_ovls=2, n_colors=31, n_feats=128):
super(AE_duichen, self).__init__()
self.Encoder = Encoder(n_subs,3,n_feats)
self.Decoder = Decoder(3,n_subs,n_feats)
self.device = 'cuda:0'
# calculate the group number (the number of branch networks)
self.G = math.ceil((n_colors - n_ovls) / (n_subs - n_ovls))
# calculate group indices
self.start_idx = []
self.end_idx = []
self.trunk = BranchUnit(n_colors, n_feats=32, n_blocks=2, act=nn.LeakyReLU(), res_scale=0.1, up_scale=1,conv=default_conv,use_tail=False)
self.final = nn.Conv2d(32, n_colors, kernel_size=3, padding=1)
for g in range(self.G):
sta_ind = (n_subs - n_ovls) * g
end_ind = sta_ind + n_subs
if end_ind > n_colors:
end_ind = n_colors
sta_ind = n_colors - n_subs
self.start_idx.append(sta_ind)
self.end_idx.append(end_ind)
def decode(self,x,z_list):
b, c, h, w = x.shape
channel_counter = torch.zeros(c).to(self.device)
y = torch.zeros(b, c, h, w).to(self.device)
for g in range(self.G):
sta_ind = self.start_idx[g]
end_ind = self.end_idx[g]
output_i = self.Decoder(z_list[g])
y[:, sta_ind:end_ind, :, :] += output_i
channel_counter[sta_ind:end_ind] = channel_counter[sta_ind:end_ind] + 1
# intermediate “result” is averaged according to their spectral indices
y = y / channel_counter.unsqueeze(1).unsqueeze(2)
return y
def encode(self,x):
b, c, h, w = x.shape
# channel_counter = torch.zeros(c).cuda()
self.device='cuda:0'
y = torch.zeros(b, c, h, w).to(self.device)
z_list = []
for g in range(self.G):
sta_ind = self.start_idx[g]
end_ind = self.end_idx[g]
xi = x[:, sta_ind:end_ind, :, :]
# 编码与解码,保存中间的隐藏层变量。
z = self.Encoder(xi)
z_list.append(z)
return z_list
def forward(self,x):
b, c, h, w = x.shape
# ---------------------------------------------------------------------------------------------
# cuda的设置!!!!
# ---------------------------------------------------------------------------------------------
self.device='cuda:0'
channel_counter = torch.zeros(c).to(self.device)
y = torch.zeros(b, c, h, w).to(self.device)
z_list = []
q_total = 0.0
for g in range(self.G):
sta_ind = self.start_idx[g]
end_ind = self.end_idx[g]
xi = x[:, sta_ind:end_ind, :, :]
# 编码与解码,保存中间的隐藏层变量。
z = self.Encoder(xi)
# print(z.shape)
z_list.append(z)
output_i = self.Decoder(z)
# q_total += q_loss
y[:, sta_ind:end_ind, :, :] += output_i
channel_counter[sta_ind:end_ind] = channel_counter[sta_ind:end_ind] + 1
# intermediate “result” is averaged according to their spectral indices
y = y / channel_counter.unsqueeze(1).unsqueeze(2)
return y, z_list
if __name__ == "__main__":
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# device = torch.device('cpu')
print('device is {}'.format(device))
torch.manual_seed(0)
torch.cuda.manual_seed(0)
# Buliding model
print('===> Building model')
load = False
# if load:
# model_E = torch.load('./weight/E_VGGSAM_4_Foster.pth',map_location='cuda:0')
# model_D = torch.load('./weight/D_VGGSAM_4_Foster.pth',map_location='cuda:0')
# # model_E = torch.load('./weight/E_VGGSAM1_4_Harvard.pth')
# # model_D = torch.load('./weight/D_VGGSAM1_4_Harvard.pth')
# model_E = model_E.to(device)
# model_D = model_D.to(device)
# print("模型读取成功, 进行fine tune 训练!!!")
# else:
# # Foster数据集是33通道的,注意更改,Chikusei 128, PaviaC 102
# model_E = Encoder(128,3)
# model_D = Decoder(3,128)
# model_E = model_E.to(device)
# model_D = model_D.to(device)
if load:
AE_model = torch.load('./weight/GAE_4_Pav.pth')
AE_model =AE_model.to(device)
print("模型读取成功, 进行fine tune 训练!!!")
else:
AE_model = GAE(Encoder=Encoder, Decoder=Decoder, n_subs=16, n_ovls=4, n_colors=102).to(device)
# AE_model = nn.DataParallel(AE_model,device_ids=[4,5,6,7])
vgg_model = models.vgg19(pretrained=True)
vgg_model = vgg_model.to(device)
# vgg_model = nn.DataParallel(vgg_model,device_ids=[0,1,2,3])
# x = torch.randn((1, 3,128,128))
# y = vgg_model(x)
# print(y.shape)
# model_E = nn.DataParallel(model_E,device_ids=[0,1,2,3,4,5,6,7])
# model_D = nn.DataParallel(model_D,device_ids=[0,1,2,3,4,5,6,7])
criterion = nn.L1Loss().to(device)
# optimizer_E = optim.Adam(model_E.parameters(), lr=0.00001, betas=(0.9, 0.999), eps=1e-08)
# optimizer_D = optim.Adam(model_D.parameters(), lr=0.00001, betas=(0.9, 0.999), eps=1e-08)
optimizer_AE = optim.Adam(AE_model.parameters(), lr=1e-4, betas=(0.9, 0.999), eps=1e-08)
# optimizer_E = optim.SGD(model_E.parameters(), lr=0.0001)
# optimizer_D = optim.SGD(model_D.parameters(), lr=0.0001)
# 加载数据集
# 重新处理了数据集,重新读取
# train_set = TrainsetFromFolder('../Harvard_4_train/') # 数据集有两个,第一个是input,人为制造的LR样本,第二个是label,HR样本,注意顺序
# train_set = TrainsetFromFolder('../train/Cave/4/') # 注意注意,不同数据集,记得切换通道数量,31,31,33,128,102
# train_set = TrainsetFromFolder('../train/Foster/4/')
# train_set = TrainsetFromFolder('../train/Chikusei/4/')
train_set = TrainsetFromFolder('../train/PaviaC/4/')
# train_set = HSTrainingData(image_dir= '../PaviaC_mat/train/', n_scale = 4, augment=True, ch3=False, num_ch=0)
print(len(train_set))
train_loader = DataLoader(dataset=train_set, batch_size=8, shuffle=True,num_workers=4)
for epoch in range(5):
count = 0
for data,hr in train_loader:
# bs 31 36 36 / bs 31 144 144
# lr = lr.reshape((lr.shape[0], 1, lr.shape[1], lr.shape[2], lr.shape[3]))
# lr = data['LR'].to(device)
# hr = hr.reshape((hr.shape[0], 1, hr.shape[1], hr.shape[2], hr.shape[3]))
# hr = data['HR'].to(device)
hr = hr.to(device)
# print(hr.shape,lr.shape)
# 进行mask操作,让AE学会复原。
# hr_mask = random_mask(hr,p=0.6)
hr_rcon, z_list = AE_model(hr)
z_list = AE_model.encode(hr)
# print(hr_rcon.shape,len(z_list),z_list[0].shape)
# 自己设计的损失函数
sam_loss = SAM_torch(hr_rcon.clone(),hr.clone())
random_list = torch.randint(0,102,(3,))
p_loss = criterion(vgg_model(hr_rcon[:,random_list,:,:]), vgg_model(hr[:,random_list,:,:]))
l1_loss = criterion(hr_rcon,hr)
# SSPSR loss 实验发现,生成的图片有明显的颜色偏移,使用颜色校正,效果也不好。有可能是训练轮次少了。目前实验结果不好
# h_loss = HybridLoss(spatial_tv=True, spectral_tv=True).to(device)
# loss = h_loss(hr_rcon, hr)
# VGGSAM,目前VGGSAM2版本最好。
# loss = l1_loss + 1e-3 * p_loss
loss_func = HLoss(0.3, 0.1)
loss = loss_func(hr_rcon, hr)
# loss = loss_func(hr_rcon, hr)+ 1e-3 * p_loss
# loss = l1_loss + 1e-3 * p_loss + 3e-3 * sam_loss + q_loss
# print(loss)
optimizer_AE.zero_grad()
# optimizer_D.zero_grad()
# optimizer_E.zero_grad()
loss.backward(retain_graph=True)
# print("每个损失对应的梯度 l1_loss.grad = {} , p_loss.grad = {}, sam_loss.grad = {} ".format(l1_loss.grad, p_loss.grad, sam_loss.grad))
optimizer_AE.step()
# optimizer_D.step()
# optimizer_E.step()
count = count + 1
# print("wow!!! 第{}个Epoch的第{}轮 total_loss = {} , p_loss = {} , l1_loss = {} , sam_loss = {}, q_loss = {} ."
# .format(epoch, count, loss, 1e-3 * p_loss, l1_loss , 1e-2 * sam_loss , q_loss))
print("wow!!! 第{}个Epoch的第{}轮 total_loss = {}" . format(epoch,count,loss))
OUT_DIR = Path('./weight')
# torch.save(model_E, OUT_DIR.joinpath('E_VGGSAM_4_Cks.pth'))
# torch.save(model_D, OUT_DIR.joinpath('D_VGGSAM_4_Cks.pth'))
torch.save(AE_model, OUT_DIR.joinpath('GAE_8_Pav.pth'))