-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCNet_model.py
More file actions
147 lines (103 loc) · 4.5 KB
/
Copy pathLCNet_model.py
File metadata and controls
147 lines (103 loc) · 4.5 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
import torch.nn as nn
import torch
import torch.nn.functional as F
from torch.nn import init
class ResBlock(nn.Module):
def __init__(self, dim):
super(ResBlock, self).__init__()
self.conv1 = nn.Conv2d(dim, dim, kernel_size=3, padding=1, bias=True)
self.relu = nn.ReLU()
self.conv2 = nn.Conv2d(dim, dim, kernel_size=3, padding=1, bias=True)
def forward(self, x):
return x + self.conv2(self.relu(self.conv1(x)))
class FeaMod1(nn.Module):
def __init__(self, dim):
super(FeaMod1, self).__init__()
self.res1 = ResBlock(dim)
def forward(self, x):
x = self.res1(x)
return x
class FeaMod2(nn.Module):
def __init__(self, dim):
super(FeaMod2, self).__init__()
self.dowm = nn.Conv2d(dim, dim * 2, kernel_size=3, padding=1, stride=2, bias=True)
self.res1 = ResBlock(dim * 2)
def forward(self, x):
x = self.res1(self.dowm(x))
x = F.interpolate(x, scale_factor=2, mode='bilinear')
return x
class FeaMod(nn.Module):
def __init__(self, dim):
super(FeaMod, self).__init__()
self.fea1 = FeaMod1(dim)
self.fea2 = FeaMod2(dim)
self.conv1 = nn.Conv2d(dim * 3, dim, kernel_size=1, padding=0, stride=1, bias=True)
def forward(self, x):
x1 = self.fea1(x)
x2 = self.fea2(x)
x = torch.cat((x1, x2), dim=1)
x = self.conv1(x)
return x
class IgMod(nn.Module):
def __init__(self, dim):
super(IgMod, self).__init__()
self.conv1 = nn.Conv2d(dim, 1, kernel_size=3, padding=1, bias=True)
self.conv2 = nn.Conv2d(1, dim, kernel_size=3, padding=1, bias=True)
self.res1 = ResBlock(dim)
def forward(self, x, y, Phi, PhiT):
x_pixel = self.conv1(x)
Phix = F.conv2d(x_pixel, Phi, padding=0, stride=32, bias=None)
delta = y - Phix
x_pixel = nn.PixelShuffle(32)(F.conv2d(delta, PhiT, padding=0, bias=None))
x_delta = self.conv2(x_pixel)
x = self.res1(x_delta) + x
return x
class AttBlock(nn.Module):
def __init__(self, dim):
super(AttBlock, self).__init__()
self.conv1 = nn.Conv2d(dim, dim, kernel_size=7, padding=3, stride=1, bias=True, groups=dim)
self.conv2 = nn.Conv2d(dim, dim, kernel_size=1, padding=0, stride=1, bias=True)
self.conv3 = nn.Conv2d(dim, dim, kernel_size=1, padding=0, stride=1, bias=True)
self.scale = dim ** -0.5
self.conv4 = nn.Conv2d(dim, dim, kernel_size=1, padding=0, stride=1, bias=True)
def forward(self, x):
B, C, H, W = x.shape
x = self.conv1(x)
x = x.reshape(B, C, H // 32, 32, W // 32, 32).permute(0, 2, 4, 1, 3, 5).reshape(-1, C, 32, 32)
x1 = self.conv2(x).reshape(-1, C, 32 * 32)
x2 = self.conv3(x).reshape(-1, C, 32 * 32).transpose(1, 2)
att = (x2 @ x1) * self.scale
att = att.softmax(dim=1)
x = (x.reshape(-1, C, 32 * 32) @ att).reshape(-1, C, 32, 32)
x = self.conv4(x)
x = x.reshape(B, H // 32, W // 32, C, 32, 32).permute(0, 3, 1, 4, 2, 5).reshape(B, C, H, W)
return x
class LCNet(nn.Module):
def __init__(self, sensing_rate, LayerNo):
super(LCNet, self).__init__()
self.measurement = int(sensing_rate * 1024)
self.base = 16
self.Phi = nn.Parameter(init.xavier_normal_(torch.Tensor(self.measurement, 1024)))
self.conv1 = nn.Conv2d(1, self.base, kernel_size=3, padding=1, bias=True)
self.conv2 = nn.Conv2d(self.base, 2, kernel_size=1, padding=0, bias=True)
layer1 = []
layer2 = []
self.LayerNo = LayerNo
for i in range(LayerNo):
layer1.append(FeaMod(self.base))
layer2.append(IgMod(self.base))
self.fcs1 = nn.ModuleList(layer1)
self.fcs2 = nn.ModuleList(layer2)
def forward(self, x):
Phi = self.Phi.contiguous().view(self.measurement, 1, 32, 32)#######102,1,32,32
PhiT = self.Phi.t().contiguous().view(1024, self.measurement, 1, 1)
y = F.conv2d(x, Phi, padding=0, stride=32, bias=None)##########1,102,8,8
x = F.conv2d(y, PhiT, padding=0, bias=None)############1,1024,8,8
x = nn.PixelShuffle(32)(x)######1,1,256,256
x = self.conv1(x)##########1,16,256,256
for i in range(self.LayerNo):
x = self.fcs2[i](x, y, Phi, PhiT)
x = self.fcs1[i](x)#############1,16,256,256
x = self.conv2(x)############,1,2,256,256
phi_cons = torch.mm(self.Phi, self.Phi.t())
return x, phi_cons,y,Phi