-
Notifications
You must be signed in to change notification settings - Fork 9
/
Train_CS_OPINE_Net_plus.py
264 lines (181 loc) · 8.72 KB
/
Train_CS_OPINE_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
263
264
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='OPINE-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 OPINE-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=25, help='from {1, 4, 10, 25, 40, 50}')
parser.add_argument('--gpu_list', type=str, default='0', help='gpu index')
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')
parser.add_argument('--save_interval', type=int, default=10, help='interval of saving model')
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
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
Training_data_Name = 'Training_Data.mat'
Training_data = sio.loadmat('./%s/%s' % (args.data_dir, Training_data_Name))
Training_labels = Training_data['labels']
class MySign(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
output = input.new(input.size())
output[input >= 0] = 1
output[input < 0] = -1
return output
@staticmethod
def backward(ctx, grad_output):
grad_input = grad_output.clone()
return grad_input
MyBinarize = MySign.apply
# Define OPINE-Net 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.conv1_G = nn.Parameter(init.xavier_normal_(torch.Tensor(32, 32, 3, 3)))
self.conv2_G = nn.Parameter(init.xavier_normal_(torch.Tensor(32, 32, 3, 3)))
self.conv3_G = nn.Parameter(init.xavier_normal_(torch.Tensor(1, 32, 3, 3)))
def forward(self, x, PhiWeight, PhiTWeight, PhiTb):
x = x - self.lambda_step * PhiTPhi_fun(x, PhiWeight, PhiTWeight)
x = x + self.lambda_step * PhiTb
x_input = x
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 = F.conv2d(F.relu(x_backward), self.conv1_G, padding=1)
x = F.conv2d(F.relu(x), self.conv2_G, padding=1)
x_G = F.conv2d(x, self.conv3_G, padding=1)
x_pred = x_input + x_G
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 OPINE-Net-plus
class OPINENetplus(torch.nn.Module):
def __init__(self, LayerNo, n_input):
super(OPINENetplus, self).__init__()
self.Phi = nn.Parameter(init.xavier_normal_(torch.Tensor(n_input, 1089)))
self.Phi_scale = nn.Parameter(torch.Tensor([0.01]))
onelayer = []
self.LayerNo = LayerNo
for i in range(LayerNo):
onelayer.append(BasicBlock())
self.fcs = nn.ModuleList(onelayer)
def forward(self, x):
# Sampling-subnet
Phi_ = MyBinarize(self.Phi)
Phi = self.Phi_scale * Phi_
PhiWeight = Phi.contiguous().view(n_input, 1, 33, 33)
Phix = F.conv2d(x, PhiWeight, padding=0, stride=33, bias=None) # Get measurements
# Initialization-subnet
PhiTWeight = Phi.t().contiguous().view(n_output, n_input, 1, 1)
PhiTb = F.conv2d(Phix, PhiTWeight, padding=0, bias=None)
PhiTb = torch.nn.PixelShuffle(33)(PhiTb)
x = PhiTb # Conduct initialization
# Recovery-subnet
layers_sym = [] # for computing symmetric loss
for i in range(self.LayerNo):
[x, layer_sym] = self.fcs[i](x, PhiWeight, PhiTWeight, PhiTb)
layers_sym.append(layer_sym)
x_final = x
return [x_final, layers_sym, Phi]
def PhiTPhi_fun(x, PhiW, PhiTW):
temp = F.conv2d(x, PhiW, padding=0,stride=33, bias=None)
temp = F.conv2d(temp, PhiTW, padding=0, bias=None)
return torch.nn.PixelShuffle(33)(temp)
model = OPINENetplus(layer_num, n_input)
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_OPINE_Net_plus_layer_%d_group_%d_ratio_%d" % (args.model_dir, layer_num, group_num, cs_ratio)
log_file_name = "./%s/Log_CS_OPINE_Net_plus_layer_%d_group_%d_ratio_%d.txt" % (args.log_dir, layer_num, group_num, cs_ratio)
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)))
Eye_I = torch.eye(n_input).to(device)
# Training loop
for epoch_i in range(start_epoch+1, end_epoch+1):
for data in rand_loader:
batch_x = data.view(-1, 1, 33, 33)
batch_x = batch_x.to(device)
[x_output, loss_layers_sym, Phi] = model(batch_x)
# Compute and print loss
loss_discrepancy = torch.mean(torch.pow(x_output - batch_x, 2))
loss_symmetry = torch.mean(torch.pow(loss_layers_sym[0], 2))
for k in range(layer_num-1):
loss_symmetry += torch.mean(torch.pow(loss_layers_sym[k+1], 2))
loss_orth = torch.mean(torch.pow(torch.mm(Phi, torch.transpose(Phi, 0, 1))-Eye_I, 2))
gamma = torch.Tensor([0.01]).to(device)
mu = torch.Tensor([0.01]).to(device)
# loss_all = loss_discrepancy
loss_all = loss_discrepancy + torch.mul(gamma, loss_symmetry) + torch.mul(mu, loss_orth)
# 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, Symmetry Loss: %.4f, Orth Loss: %.4f\n" % (epoch_i, end_epoch, loss_all.item(), loss_discrepancy.item(), loss_symmetry.item(), loss_orth.item())
print(output_data)
output_file = open(log_file_name, 'a')
output_file.write(output_data)
output_file.close()
if epoch_i % args.save_interval == 0:
torch.save(model.state_dict(), "./%s/net_params_%d.pkl" % (model_dir, epoch_i)) # save only the parameters