-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathM5FISTANetPlus.py
172 lines (140 loc) · 6.03 KB
/
M5FISTANetPlus.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
# -*- coding: utf-8 -*-
"""
Created on Nov. 3, 2020
enhanced version of FISTA-Net
(1) with learned gradient matrix
(2)
@author: XIANG
"""
import torch
import torch.nn as nn
from torch.nn import init
import torch.nn.functional as F
import numpy as np
import os
def initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
init.xavier_normal_(m.weight)
if m.bias is not None:
init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant_(m.weight, 1)
init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
init.normal_(m.weight, 0, 0.01)
init.constant_(m.bias, 0)
# define basic block of FISTA-Net
class BasicBlock(nn.Module):
"""docstring for BasicBlock"""
def __init__(self, features=32):
super(BasicBlock, self).__init__()
#self.lambda_step = nn.Parameter(torch.Tensor([0.2]))
#self.soft_thr = nn.Parameter(torch.Tensor([0.05]))
self.Sp = nn.Softplus()
self.conv_D = nn.Conv2d(1, features, (3,3), stride=1, padding=1)
self.conv1_forward = nn.Conv2d(features, features, (3,3), stride=1, padding=1)
self.conv2_forward = nn.Conv2d(features, features, (3,3), stride=1, padding=1)
self.conv3_forward = nn.Conv2d(features, features, (3,3), stride=1, padding=1)
self.conv4_forward = nn.Conv2d(features, features, (3,3), stride=1, padding=1)
self.conv1_backward = nn.Conv2d(features, features, (3,3), stride=1, padding=1)
self.conv2_backward = nn.Conv2d(features, features, (3,3), stride=1, padding=1)
self.conv3_backward = nn.Conv2d(features, features, (3,3), stride=1, padding=1)
self.conv4_backward = nn.Conv2d(features, features, (3,3), stride=1, padding=1)
self.conv_G = nn.Conv2d(features, 1, (3,3), stride=1, padding=1)
def forward(self, x, PhiTPhi, PhiTb, mask, lambda_step, soft_thr):
# convert data format from (batch_size, channel, pnum, pnum) to (circle_num, batch_size)
pnum = x.size()[2]
x = x.view(x.size()[0], x.size()[1], pnum*pnum, -1) # (batch_size, channel, pnum*pnum, 1)
x = torch.squeeze(x, 1)
x = torch.squeeze(x, 2).t()
x = mask.mm(x)
x = x - self.Sp(lambda_step) * PhiTPhi.mm(x) + self.Sp(lambda_step) * PhiTb
# convert (circle_num, batch_size) to (batch_size, channel, pnum, pnum)
x = torch.mm(mask.t(), x)
x = x.view(pnum, pnum, -1)
x = x.unsqueeze(0)
x_input = x.permute(3, 0, 1, 2)
x_D = self.conv_D(x_input)
x = self.conv1_forward(x_D)
x = F.relu(x)
x = self.conv2_forward(x)
x = F.relu(x)
x = self.conv3_forward(x)
x = F.relu(x)
x_forward = self.conv4_forward(x)
# soft-thresholding block
x_st = torch.mul(torch.sign(x_forward), F.relu(torch.abs(x_forward) - self.Sp(soft_thr)))
x = self.conv1_backward(x_st)
x = F.relu(x)
x = self.conv2_backward(x)
x = F.relu(x)
x = self.conv3_backward(x)
x = F.relu(x)
x_backward = self.conv4_backward(x)
x_G = self.conv_G(x_backward)
# prediction output (skip connection); non-negative output
x_pred = F.relu(x_input + x_G)
# compute symmetry loss
x = self.conv1_backward(x_forward)
x = F.relu(x)
x = self.conv2_backward(x)
x = F.relu(x)
x = self.conv3_backward(x)
x = F.relu(x)
x_D_est = self.conv4_backward(x)
symloss = x_D_est - x_D
return [x_pred, symloss, x_st]
class FISTANetPlus(nn.Module):
def __init__(self, LayerNo, Phi, Wt, mask):
super(FISTANetPlus, self).__init__()
self.LayerNo = LayerNo
self.Phi = Phi
self.Wt = Wt # learned weight
self.mask =mask
onelayer = []
self.bb = BasicBlock()
for i in range(LayerNo):
onelayer.append(self.bb)
self.fcs = nn.ModuleList(onelayer)
self.fcs.apply(initialize_weights)
# thresholding value
self.w_theta = nn.Parameter(torch.Tensor([-0.5]))
self.b_theta = nn.Parameter(torch.Tensor([-2]))
# gradient step
self.w_mu = nn.Parameter(torch.Tensor([-0.2]))
self.b_mu = nn.Parameter(torch.Tensor([0.1]))
# two-step update weight
self.w_rho = nn.Parameter(torch.Tensor([0.5]))
self.b_rho = nn.Parameter(torch.Tensor([0]))
self.Sp = nn.Softplus()
def forward(self, x0, b):
"""
Phi : system matrix; default dim 104 * 3228;
mask : mask matrix, dim 3228 * 4096
b : measured signal vector;
x0 : initialized x with Laplacian Reg.
"""
# convert data format from (batch_size, channel, vector_row, vector_col) to (vector_row, batch_size)
b = torch.squeeze(b, 1)
b = torch.squeeze(b, 2)
b = b.t()
PhiTPhi = self.Wt.t().mm(self.Phi)
PhiTb = self.Wt.t().mm(b)
# initialize the result
xold = x0
y = xold
layers_sym = [] # for computing symmetric loss
layers_st = [] # for computing sparsity constraint
# xnews = [] # iteration result
for i in range(self.LayerNo):
theta_ = self.w_theta * i + self.b_theta
mu_ = self.w_mu * i + self.b_mu
[xnew, layer_sym, layer_st] = self.fcs[i](y, PhiTPhi, PhiTb, self.mask, mu_, theta_)
rho_ = (self.Sp(self.w_rho * i + self.b_rho) - self.Sp(self.b_rho)) / self.Sp(self.w_rho * i + self.b_rho)
y = xnew + rho_ * (xnew - xold) # two-step update
xold = xnew
# xnews.append(xnew) # iteration result
layers_st.append(layer_st)
layers_sym.append(layer_sym)
return [xnew, layers_sym, layers_st]