-
Notifications
You must be signed in to change notification settings - Fork 36
/
Train_CS_ISTA_Net_plus.py
262 lines (178 loc) · 8.37 KB
/
Train_CS_ISTA_Net_plus.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
import torch
import torch.nn as nn
from torch.nn import init
import torch.nn.functional as F
import scipy.io as sio
import numpy as np
import os
from torch.utils.data import Dataset, DataLoader
import platform
from argparse import ArgumentParser
parser = ArgumentParser(description='ISTA-Net-plus')
parser.add_argument('--start_epoch', type=int, default=0, help='epoch number of start training')
parser.add_argument('--end_epoch', type=int, default=200, help='epoch number of end training')
parser.add_argument('--layer_num', type=int, default=9, help='phase number of ISTA-Net-plus')
parser.add_argument('--learning_rate', type=float, default=1e-4, help='learning rate')
parser.add_argument('--group_num', type=int, default=1, help='group number for training')
parser.add_argument('--cs_ratio', type=int, default=4, help='from {1, 4, 10, 25, 40, 50}')
parser.add_argument('--gpu_list', type=str, default='0', help='gpu index')
parser.add_argument('--matrix_dir', type=str, default='sampling_matrix', help='sampling matrix directory')
parser.add_argument('--model_dir', type=str, default='model', help='trained or pre-trained model directory')
parser.add_argument('--data_dir', type=str, default='data', help='training data directory')
parser.add_argument('--log_dir', type=str, default='log', help='log directory')
args = parser.parse_args()
start_epoch = args.start_epoch
end_epoch = args.end_epoch
learning_rate = args.learning_rate
layer_num = args.layer_num
group_num = args.group_num
cs_ratio = args.cs_ratio
gpu_list = args.gpu_list
try:
# The flag below controls whether to allow TF32 on matmul. This flag defaults to True.
torch.backends.cuda.matmul.allow_tf32 = False
# The flag below controls whether to allow TF32 on cuDNN. This flag defaults to True.
torch.backends.cudnn.allow_tf32 = False
except:
pass
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = gpu_list
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
ratio_dict = {1: 10, 4: 43, 10: 109, 25: 272, 30: 327, 40: 436, 50: 545}
n_input = ratio_dict[cs_ratio]
n_output = 1089
nrtrain = 88912 # number of training blocks
batch_size = 64
# Load CS Sampling Matrix: phi
Phi_data_Name = './%s/phi_0_%d_1089.mat' % (args.matrix_dir, cs_ratio)
Phi_data = sio.loadmat(Phi_data_Name)
Phi_input = Phi_data['phi']
Training_data_Name = 'Training_Data.mat'
Training_data = sio.loadmat('./%s/%s' % (args.data_dir, Training_data_Name))
Training_labels = Training_data['labels']
Qinit_Name = './%s/Initialization_Matrix_%d.mat' % (args.matrix_dir, cs_ratio)
# Computing Initialization Matrix:
if os.path.exists(Qinit_Name):
Qinit_data = sio.loadmat(Qinit_Name)
Qinit = Qinit_data['Qinit']
else:
X_data = Training_labels.transpose()
Y_data = np.dot(Phi_input, X_data)
Y_YT = np.dot(Y_data, Y_data.transpose())
X_YT = np.dot(X_data, Y_data.transpose())
Qinit = np.dot(X_YT, np.linalg.inv(Y_YT))
del X_data, Y_data, X_YT, Y_YT
sio.savemat(Qinit_Name, {'Qinit': Qinit})
# Define ISTA-Net-plus Block
class BasicBlock(torch.nn.Module):
def __init__(self):
super(BasicBlock, self).__init__()
self.lambda_step = nn.Parameter(torch.Tensor([0.5]))
self.soft_thr = nn.Parameter(torch.Tensor([0.01]))
self.conv_D = nn.Parameter(init.xavier_normal_(torch.Tensor(32, 1, 3, 3)))
self.conv1_forward = nn.Parameter(init.xavier_normal_(torch.Tensor(32, 32, 3, 3)))
self.conv2_forward = nn.Parameter(init.xavier_normal_(torch.Tensor(32, 32, 3, 3)))
self.conv1_backward = nn.Parameter(init.xavier_normal_(torch.Tensor(32, 32, 3, 3)))
self.conv2_backward = nn.Parameter(init.xavier_normal_(torch.Tensor(32, 32, 3, 3)))
self.conv_G = nn.Parameter(init.xavier_normal_(torch.Tensor(1, 32, 3, 3)))
def forward(self, x, PhiTPhi, PhiTb):
x = x - self.lambda_step * torch.mm(x, PhiTPhi)
x = x + self.lambda_step * PhiTb
x_input = x.view(-1, 1, 33, 33)
x_D = F.conv2d(x_input, self.conv_D, padding=1)
x = F.conv2d(x_D, self.conv1_forward, padding=1)
x = F.relu(x)
x_forward = F.conv2d(x, self.conv2_forward, padding=1)
x = torch.mul(torch.sign(x_forward), F.relu(torch.abs(x_forward) - self.soft_thr))
x = F.conv2d(x, self.conv1_backward, padding=1)
x = F.relu(x)
x_backward = F.conv2d(x, self.conv2_backward, padding=1)
x_G = F.conv2d(x_backward, self.conv_G, padding=1)
x_pred = x_input + x_G
x_pred = x_pred.view(-1, 1089)
x = F.conv2d(x_forward, self.conv1_backward, padding=1)
x = F.relu(x)
x_D_est = F.conv2d(x, self.conv2_backward, padding=1)
symloss = x_D_est - x_D
return [x_pred, symloss]
# Define ISTA-Net-plus
class ISTANetplus(torch.nn.Module):
def __init__(self, LayerNo):
super(ISTANetplus, self).__init__()
onelayer = []
self.LayerNo = LayerNo
for i in range(LayerNo):
onelayer.append(BasicBlock())
self.fcs = nn.ModuleList(onelayer)
def forward(self, Phix, Phi, Qinit):
PhiTPhi = torch.mm(torch.transpose(Phi, 0, 1), Phi)
PhiTb = torch.mm(Phix, Phi)
x = torch.mm(Phix, torch.transpose(Qinit, 0, 1))
layers_sym = [] # for computing symmetric loss
for i in range(self.LayerNo):
[x, layer_sym] = self.fcs[i](x, PhiTPhi, PhiTb)
layers_sym.append(layer_sym)
x_final = x
return [x_final, layers_sym]
model = ISTANetplus(layer_num)
model = nn.DataParallel(model)
model = model.to(device)
print_flag = 1 # print parameter number
if print_flag:
num_count = 0
for para in model.parameters():
num_count += 1
print('Layer %d' % num_count)
print(para.size())
class RandomDataset(Dataset):
def __init__(self, data, length):
self.data = data
self.len = length
def __getitem__(self, index):
return torch.Tensor(self.data[index, :]).float()
def __len__(self):
return self.len
if (platform.system() =="Windows"):
rand_loader = DataLoader(dataset=RandomDataset(Training_labels, nrtrain), batch_size=batch_size, num_workers=0,
shuffle=True)
else:
rand_loader = DataLoader(dataset=RandomDataset(Training_labels, nrtrain), batch_size=batch_size, num_workers=4,
shuffle=True)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
model_dir = "./%s/CS_ISTA_Net_plus_layer_%d_group_%d_ratio_%d_lr_%.4f" % (args.model_dir, layer_num, group_num, cs_ratio, learning_rate)
log_file_name = "./%s/Log_CS_ISTA_Net_plus_layer_%d_group_%d_ratio_%d_lr_%.4f.txt" % (args.log_dir, layer_num, group_num, cs_ratio, learning_rate)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
if start_epoch > 0:
pre_model_dir = model_dir
model.load_state_dict(torch.load('./%s/net_params_%d.pkl' % (pre_model_dir, start_epoch)))
Phi = torch.from_numpy(Phi_input).type(torch.FloatTensor)
Phi = Phi.to(device)
Qinit = torch.from_numpy(Qinit).type(torch.FloatTensor)
Qinit = Qinit.to(device)
# Training loop
for epoch_i in range(start_epoch+1, end_epoch+1):
for data in rand_loader:
batch_x = data
batch_x = batch_x.to(device)
Phix = torch.mm(batch_x, torch.transpose(Phi, 0, 1))
[x_output, loss_layers_sym] = model(Phix, Phi, Qinit)
# Compute and print loss
loss_discrepancy = torch.mean(torch.pow(x_output - batch_x, 2))
loss_constraint = torch.mean(torch.pow(loss_layers_sym[0], 2))
for k in range(layer_num-1):
loss_constraint += torch.mean(torch.pow(loss_layers_sym[k+1], 2))
gamma = torch.Tensor([0.01]).to(device)
# loss_all = loss_discrepancy
loss_all = loss_discrepancy + torch.mul(gamma, loss_constraint)
# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
loss_all.backward()
optimizer.step()
output_data = "[%02d/%02d] Total Loss: %.4f, Discrepancy Loss: %.4f, Constraint Loss: %.4f\n" % (epoch_i, end_epoch, loss_all.item(), loss_discrepancy.item(), loss_constraint)
print(output_data)
output_file = open(log_file_name, 'a')
output_file.write(output_data)
output_file.close()
if epoch_i % 5 == 0:
torch.save(model.state_dict(), "./%s/net_params_%d.pkl" % (model_dir, epoch_i)) # save only the parameters