-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSSPSR.py
269 lines (212 loc) · 9.74 KB
/
SSPSR.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
from common import *
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import torch.optim as optim
from pathlib import Path
from torch.nn.functional import interpolate
import torch.distributed as dist
import os
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 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):
m.append(SSB(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
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.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 SSPSR(nn.Module):
def __init__(self, n_subs, n_ovls, n_colors, n_blocks, n_feats, n_scale, res_scale, use_share=True,
conv=default_conv):
super(SSPSR, self).__init__()
kernel_size = 3
self.shared = use_share
act = nn.ReLU(True)
# 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)
if self.shared:
self.branch = BranchUnit(n_subs, n_feats, n_blocks, act, res_scale, up_scale=n_scale // 2,
conv=default_conv)
# up_scale=n_scale//2 means that we upsample the LR input n_scale//2 at the branch network, and then conduct 2 times upsampleing at the global network
else:
self.branch = nn.ModuleList()
for i in range(self.G):
self.branch.append(BranchUnit(n_subs, n_feats, n_blocks, act, res_scale, up_scale=2, conv=default_conv))
self.trunk = BranchUnit(n_colors, n_feats, n_blocks, act, res_scale, up_scale=2, use_tail=False,
conv=default_conv)
self.skip_conv = conv(n_colors, n_feats, kernel_size)
self.final = conv(n_feats, n_colors, kernel_size)
self.sca = n_scale // 2
def forward(self, x, lms):
b, c, h, w = x.shape
# Initialize intermediate “result”, which is upsampled with n_scale//2 times
y = torch.zeros(b, c, self.sca * h, self.sca * w).cuda()
channel_counter = torch.zeros(c).cuda()
for g in range(self.G):
sta_ind = self.start_idx[g]
end_ind = self.end_idx[g]
xi = x[:, sta_ind:end_ind, :, :]
if self.shared:
xi = self.branch(xi)
else:
xi = self.branch[g](xi)
y[:, sta_ind:end_ind, :, :] += xi
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)
y = self.trunk(y)
lms = interpolate(
lms,
scale_factor=4, # 这个与具体的超分辨比例有关,这个是全局skip时候,对初始图像进行上采样,一般设置为2 3 4
mode='bicubic',
align_corners=True
)
# print(lms.shape)
# print(y.shape, self.skip_conv(lms).shape)
y = y + self.skip_conv(lms)
y = self.final(y)
return y
class HybridLoss(torch.nn.Module):
def __init__(self, lamd=1e-1, spatial_tv=False, spectral_tv=False):
super(HybridLoss, self).__init__()
self.lamd = lamd
self.use_spatial_TV = spatial_tv
self.use_spectral_TV = spectral_tv
self.fidelity = torch.nn.L1Loss()
self.spatial = TVLoss(weight=1e-3)
self.spectral = TVLossSpectral(weight=1e-3)
def forward(self, y, gt):
loss = self.fidelity(y, gt)
spatial_TV = 0.0
spectral_TV = 0.0
if self.use_spatial_TV:
spatial_TV = self.spatial(y)
if self.use_spectral_TV:
spectral_TV = self.spectral(y)
total_loss = loss + spatial_TV + spectral_TV
return total_loss
# from https://github.com/jxgu1016/Total_Variation_Loss.pytorch with slight modifications
class TVLoss(torch.nn.Module):
def __init__(self, weight=1.0):
super(TVLoss, self).__init__()
self.TVLoss_weight = weight
def forward(self, x):
batch_size = x.size()[0]
h_x = x.size()[2]
w_x = x.size()[3]
count_h = self._tensor_size(x[:, :, 1:, :])
count_w = self._tensor_size(x[:, :, :, 1:])
# h_tv = torch.abs(x[:, :, 1:, :] - x[:, :, :h_x - 1, :]).sum()
# w_tv = torch.abs(x[:, :, :, 1:] - x[:, :, :, :w_x - 1]).sum()
h_tv = torch.pow((x[:, :, 1:, :] - x[:, :, :h_x - 1, :]), 2).sum()
w_tv = torch.pow((x[:, :, :, 1:] - x[:, :, :, :w_x - 1]), 2).sum()
return self.TVLoss_weight * (h_tv / count_h + w_tv / count_w) / batch_size
def _tensor_size(self, t):
return t.size()[1] * t.size()[2] * t.size()[3]
class TVLossSpectral(torch.nn.Module):
def __init__(self, weight=1.0):
super(TVLossSpectral, self).__init__()
self.TVLoss_weight = weight
def forward(self, x):
batch_size = x.size()[0]
c_x = x.size()[1]
count_c = self._tensor_size(x[:, 1:, :, :])
# c_tv = torch.abs((x[:, 1:, :, :] - x[:, :c_x - 1, :, :])).sum()
c_tv = torch.pow((x[:, 1:, :, :] - x[:, :c_x - 1, :, :]), 2).sum()
return self.TVLoss_weight * 2 * (c_tv / count_c) / batch_size
def _tensor_size(self, t):
return t.size()[1] * t.size()[2] * t.size()[3]
EPOCHS = 40
BATCH_SIZE = 16
LR = 1e-4
high_sr = 128
low_sr = high_sr / 4
if __name__ == "__main__":
# 初始化
dist.init_process_group(backend='nccl')
local_rank = int(os.environ["LOCAL_RANK"])
print(local_rank)
# 指定具体的GPU
torch.cuda.set_device(local_rank)
os.environ['CUDA_VISIBLE_DEVICES'] = "0,1,2,3" # 根据gpu的数量来设定,初始gpu为0,这里我的gpu数量为4
device = torch.device("cuda", local_rank)
# 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')
model = SSPSR(n_subs=8, n_ovls=2, n_colors=31, n_blocks=3, n_feats=16,
n_scale=4, res_scale=0.1, use_share=True, conv=default_conv).to(device)
criterion = nn.L1Loss().to(device)
optimizer = optim.Adam(model.parameters(), lr=LR, betas=(0.9, 0.999), eps=1e-08)
# 初始化ddp模型,用于单机多卡并行运算
model = nn.parallel.DistributedDataParallel(model, device_ids=[local_rank])
# 设定对应的损失函数,由两部分组成,空间和光谱损失函数,都是TVloss,total variation损失
h_loss = HybridLoss(spatial_tv=True, spectral_tv=True)
# 重新处理了数据集,重新读取
# 分布式,需要划分数据集,增加sampler模块
train_set = TrainsetFromFolder('../Harvard_4_train/') # 数据集有两个,第一个是input,人为制造的LR样本,第二个是label,HR样本,注意顺序
print(len(train_set))
train_sampler = torch.utils.data.distributed.DistributedSampler(train_set) # 切分训练数据集
train_loader = DataLoader(dataset=train_set,sampler=train_sampler, batch_size=16, shuffle=False) # 分布式不能进行shuffle
for epoch in range(EPOCHS):
count = 0
for lr, 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 = lr.to(device)
# hr = hr.reshape((hr.shape[0], 1, hr.shape[1], hr.shape[2], hr.shape[3]))
hr = hr.to(device)
# print(lr.shape, hr.shape)
SR = model(lr, lr)
# print(SR.shape)
loss = h_loss(SR, hr)
optimizer.zero_grad()
loss.backward()
optimizer.step()
count = count + 1
print("天哪,这轮训练完成了!第{}个Epoch的第{}轮的损失为:{}".format(epoch, count, loss))
OUT_DIR = Path('./weight')
torch.save(model, OUT_DIR.joinpath('SSPSR_4_Harvard.pth'))