forked from ZhonghaoZ/AMP-Net_TIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_AMP_Net.py
More file actions
297 lines (244 loc) · 10.4 KB
/
train_AMP_Net.py
File metadata and controls
297 lines (244 loc) · 10.4 KB
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
from dataset import dataset_full,dataset
import os
import numpy as np
import glob
from utils import *
from scipy import io
import torch
from torch.nn import Module
from torch import nn
from torch.autograd import Variable
"""
No mask training, no deblocking
AMP-Net-K
"""
class Denoiser(Module):
def __init__(self):
super().__init__()
self.D = nn.Sequential(nn.Conv2d(1, 32, 3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 32, 3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 32, 3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 1, 3, padding=1,bias=False))
def forward(self, inputs):
inputs = torch.unsqueeze(torch.reshape(torch.transpose(inputs,0,1),[-1,33,33]),dim=1)
output = self.D(inputs)
# output=inputs-output
output = torch.transpose(torch.reshape(torch.squeeze(output),[-1,33*33]),0,1)
return output
class Deblocker(Module):
def __init__(self):
super().__init__()
self.D = nn.Sequential(nn.Conv2d(1, 32, 3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 32, 3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 32, 3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 1, 3, padding=1,bias=False))
def forward(self, inputs):
inputs = torch.unsqueeze(inputs,dim=1)
output = self.D(inputs)
output = torch.squeeze(output,dim=1)
return output
class AMP_net_Deblock(Module):
def __init__(self,layer_num, A):
super().__init__()
self.layer_num = layer_num
self.denoisers = []
self.steps = []
self.register_parameter("A",nn.Parameter(torch.from_numpy(A).float(),requires_grad=False))
self.register_parameter("Q", nn.Parameter(torch.from_numpy(np.transpose(A)).float(), requires_grad=True))
for n in range(layer_num):
self.denoisers.append(Denoiser())
self.register_parameter("step_" + str(n + 1), nn.Parameter(torch.tensor(1.0),requires_grad=False))
self.steps.append(eval("self.step_" + str(n + 1)))
for n,denoiser in enumerate(self.denoisers):
self.add_module("denoiser_"+str(n+1),denoiser)
def forward(self, inputs, output_layers):
H = int(inputs.shape[2]/33)
L = int(inputs.shape[3]/33)
S = inputs.shape[0]
y = self.sampling(inputs)
X = torch.matmul(self.Q,y)
for n in range(output_layers):
step = self.steps[n]
denoiser = self.denoisers[n]
z = self.block1(X, y,step)
noise = denoiser(X)
X = z - torch.matmul(
(step * torch.matmul(torch.transpose(self.A,0,1), self.A)) - torch.eye(33 * 33).float().cuda(), noise)
X = self.together(X,S,H,L)
X = torch.cat(torch.split(X, split_size_or_sections=33, dim=1), dim=0)
X = torch.cat(torch.split(X, split_size_or_sections=33, dim=2), dim=0)
X = torch.transpose(torch.reshape(X, [-1, 33 * 33]), 0, 1)
X = self.together(X, S, H, L)
return torch.unsqueeze(X, dim=1)
def sampling(self,inputs):
# inputs = torch.squeeze(inputs)
# inputs = torch.reshape(inputs,[-1,33*33])
inputs = torch.squeeze(inputs,dim=1)
inputs = torch.cat(torch.split(inputs, split_size_or_sections=33, dim=1), dim=0)
inputs = torch.cat(torch.split(inputs, split_size_or_sections=33, dim=2), dim=0)
inputs = torch.transpose(torch.reshape(inputs, [-1, 33*33]),0,1)
outputs = torch.matmul(self.A, inputs)
return outputs
def block1(self,X,y,step):
# X = torch.squeeze(X)
# X = torch.transpose(torch.reshape(X, [-1, 33 * 33]),0,1)
outputs = torch.matmul(torch.transpose(self.A,0,1),y-torch.matmul(self.A,X))
outputs = step * outputs + X
# outputs = torch.unsqueeze(torch.reshape(torch.transpose(outputs,0,1),[-1,33,33]),dim=1)
return outputs
def together(self,inputs,S,H,L):
inputs = torch.reshape(torch.transpose(inputs,0,1),[-1,33,33])
inputs = torch.cat(torch.split(inputs, split_size_or_sections=H*S, dim=0), dim=2)
inputs = torch.cat(torch.split(inputs, split_size_or_sections=S, dim=0), dim=1)
return inputs
def compute_loss(outputs, target):
loss = []
for output in outputs:
loss.append(torch.mean((output - target) ** 2))
return loss
def get_final_loss(loss_all):
output = 0
for loss in loss_all:
output += loss
return output
def get_loss(outputs, noise_all, Xs, H, target, sigma=0.01):
loss1 = torch.mean((outputs[-1] - target) ** 2)
loss2 = torch.mean(torch.abs(outputs[-1] - target))
num = 0
for n in range(len(noise_all)):
num += 1
X = Xs[n]
noise = noise_all[n]
loss2 += torch.mean((noise - torch.matmul(H, target - X)) ** 2)
return loss1, loss2
def train(model, opt, train_loader, epoch, batch_size, CS_ratio,PhaseNum):
model.train()
n = 0
for data,_ in train_loader:
n = n + 1
opt.zero_grad()
data = torch.unsqueeze(data,dim=1)
data = Variable(data.float().cuda())
outputs= model(data,PhaseNum)
# loss_all = compute_loss(outputs,data)
# loss = get_final_loss(loss_all)
# loss = torch.mean((outputs[-1]-target)**2)
loss = torch.mean((outputs-data)**2)
loss.backward()
opt.step()
if n % 25 == 0:
output = "CS_ratio: %d [%02d/%02d] loss: %.4f " % (
CS_ratio, epoch, batch_size * n, loss.data.item())
print(output)
def get_val_result(model,PhaseNum, is_cuda=True):
model.eval()
with torch.no_grad():
test_set_path = "dataset/bsds500/val"
test_set_path = glob.glob(test_set_path + '/*.tif')
ImgNum = len(test_set_path)
PSNR_All = np.zeros([1, ImgNum], dtype=np.float32)
model.eval()
for img_no in range(ImgNum):
imgName = test_set_path[img_no]
[Iorg, row, col, Ipad, row_new, col_new] = imread_CS_py(imgName)
Icol = img2col_py(Ipad, 33) / 255.0
Ipad /= 255.0
# Img_input = np.dot(Icol, Phi_input)
# Img_output = Icol
if is_cuda:
inputs = Variable(torch.from_numpy(Ipad.astype('float32')).cuda())
else:
inputs = Variable(torch.from_numpy(Ipad.astype('float32')))
inputs = torch.unsqueeze(torch.unsqueeze(inputs,dim=0),dim=0)
outputs = model(inputs, PhaseNumber)
outputs = torch.squeeze(outputs)
if is_cuda:
outputs = outputs.cpu().data.numpy()
else:
outputs = outputs.data.numpy()
images_recovered = outputs[0:row,0:col]
# images_recovered = col2im_CS_py(output, row, col, row_new, col_new)
rec_PSNR = psnr(images_recovered * 255, Iorg)
PSNR_All[0, img_no] = rec_PSNR
out = np.mean(PSNR_All)
return out
def load_sampling_matrix(CS_ratio):
path = "dataset/sampling_matrix"
data = io.loadmat(os.path.join(path, str(CS_ratio) + '.mat'))['sampling_matrix']
return data
def get_Q(data_set,A):
A = torch.from_numpy(A)
n = 0
data_loader = torch.utils.data.DataLoader(data_set, batch_size=len(data_set),
shuffle=True, num_workers=2)
for data, target in data_loader:
data = torch.transpose(torch.reshape(data, [-1, 33 * 33]), 0, 1)
target = torch.transpose(torch.reshape(target, [-1, 33 * 33]), 0, 1)
y = torch.matmul(A.float(),data.float())
x = target.float()
if n==0:
ys = y
Xs = x
n = 1
else:
ys = torch.cat([ys,y],dim=1)
Xs = torch.cat([Xs,x],dim=1)
Q = torch.matmul(torch.matmul(Xs,torch.transpose(ys,0,1)),
torch.inverse(torch.matmul(ys, torch.transpose(ys, 0, 1))))
return Q.numpy()
if __name__ == "__main__":
is_cuda = True
CS_ratio = 25 # 4, 10, 25, 30, 40, 50
CS_ratios = [10]
# n_output = 1089
PhaseNumbers = [2, 4, 6, 9]
# PhaseNumber = 9
# nrtrain = 88912
learning_rate = 0.0001
EpochNum = 100
batch_size = 32
results_saving_path = "results"
net_name = "AMP_Net_K"
if not os.path.exists(results_saving_path):
os.mkdir(results_saving_path)
results_saving_path = os.path.join(results_saving_path, net_name)
if not os.path.exists(results_saving_path):
os.mkdir(results_saving_path)
print('Load Data...')
train_dataset = dataset(train=True, transform=None,
target_transform=None)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,
shuffle=True, num_workers=2)
for CS_ratio in CS_ratios:
for PhaseNumber in PhaseNumbers:
A = load_sampling_matrix(CS_ratio)
model = AMP_net_Deblock(PhaseNumber,A)
opt = torch.optim.Adam(model.parameters(), lr=learning_rate)
model.cuda()
sub_path = os.path.join(results_saving_path, str(CS_ratio))
if not os.path.exists(sub_path):
os.mkdir(sub_path)
sub_path = os.path.join(sub_path, str(PhaseNumber))
if not os.path.exists(sub_path):
os.mkdir(sub_path)
best_psnr = 0
for epoch in range(1, EpochNum + 1):
train(model, opt, train_loader, epoch, batch_size, CS_ratio,PhaseNumber)
one_psnr = get_val_result(model, PhaseNumber)
print_str = "CS_ratio: %d Phase: %d epoch: %d psnr: %.4f" % (CS_ratio, PhaseNumber, epoch, one_psnr)
print(print_str)
output_file = open(sub_path + "/log_PSNR.txt", 'a')
output_file.write("PSNR: %.4f\n" % (one_psnr))
output_file.close()
if one_psnr > best_psnr:
best_psnr = one_psnr
output_file = open(sub_path + "/log_PSNR_best.txt", 'a')
output_file.write("PSNR: %.4f\n" % (best_psnr))
output_file.close()
torch.save(model.state_dict(), sub_path + "/best_model.pkl")