-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.py
More file actions
executable file
·171 lines (147 loc) · 5.16 KB
/
modules.py
File metadata and controls
executable file
·171 lines (147 loc) · 5.16 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
import torch
import torch.nn as nn
import torch.nn.functional as F
def Normalize(in_channels, num_groups=16):
"""GroupNorm模块,"""
# 如果不能整除
if in_channels % num_groups != 0:
num_groups = in_channels
return nn.GroupNorm(
num_groups=num_groups, num_channels=in_channels, eps=1e-6, affine=True
)
class ResnetBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.layer = nn.Sequential(
nn.Conv2d(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
),
nn.SiLU(),
Normalize(out_channels),
nn.Conv2d(
out_channels,
out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
),
nn.SiLU(),
Normalize(out_channels),
)
if self.in_channels != self.out_channels:
# 如果输入输出通道不等,加一个卷积使残差可以连接
self.conv_shortcut = torch.nn.Conv2d(
in_channels, out_channels, kernel_size=1, stride=1, padding=0
)
def forward(self, x):
x0 = self.layer(x)
if self.in_channels != self.out_channels:
x = self.conv_shortcut(x)
return x + x0
class DownSample(nn.Module):
"""平均池化下采样两倍或卷积缩小两倍"""
def __init__(self, in_chs):
super().__init__()
self.down = nn.Sequential(
nn.MaxPool2d(kernel_size=2, stride=2),
# Transformer(in_chs, in_chs)
)
def forward(self, x):
return self.down(x)
class UpSample(nn.Module):
"""最临近插值二倍上采样或上采样后再进行一次卷积"""
def __init__(self, in_channels, with_conv=False):
super().__init__()
self.with_conv = with_conv
if self.with_conv:
self.conv = torch.nn.Conv2d(
in_channels, in_channels, kernel_size=3, stride=1, padding=1
)
self.up = nn.Upsample(scale_factor=2)
def forward(self, x):
x = self.up(x)
if self.with_conv:
x = self.conv(x)
return x
class Transformer(nn.Module):
def __init__(
self, in_channels, out_channels, embed_dim=None, num_heads=2, dropout=0.2
):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
if embed_dim is None:
self.embed_dim = in_channels
else:
self.embed_dim = embed_dim
self.att = nn.MultiheadAttention(
self.embed_dim, num_heads, batch_first=True, dropout=dropout
)
self.conv = nn.Conv2d(
in_channels, out_channels, kernel_size=1, stride=1, padding=0
)
self.norm1 = nn.LayerNorm(self.embed_dim)
self.norm2 = nn.LayerNorm(self.embed_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
batch_size, channels, height, width = x.shape
x = x.permute(0, 2, 3, 1).reshape(batch_size, -1, channels)
x = self.norm1(x)
attn_output, _ = self.att(x, x, x)
x = x + self.dropout(attn_output) # 残差连接
x = self.norm2(x)
x = x.reshape(batch_size, height, width, channels).permute(0, 3, 1, 2)
x = self.conv(x)
return x
class AvgMaxAttention(nn.Module):
def __init__(self, in_chs, out_chs):
super().__init__()
self.avg = nn.Sequential(
nn.ReflectionPad2d(padding=1),
nn.AvgPool2d(kernel_size=3, stride=1, padding=0),
nn.Conv2d(in_chs, out_chs, kernel_size=1, stride=1, padding=0),
nn.SiLU(),
Normalize(out_chs),
)
self.max = nn.Sequential(
nn.ReflectionPad2d(padding=1),
nn.MaxPool2d(kernel_size=3, stride=1, padding=0),
nn.Conv2d(in_chs, out_chs, kernel_size=1, stride=1, padding=0),
nn.SiLU(),
Normalize(out_chs),
)
self.mix = nn.Sequential(
nn.Conv2d(2 * out_chs, in_chs, kernel_size=1, stride=1, padding=0),
)
def forward(self, x):
x_avg = self.avg(x)
x_max = self.max(x)
score = self.mix(torch.cat((x_avg, x_max), dim=1))
return x * F.sigmoid(score)
class ENetConv(nn.Module):
"""同UNet定义连续的俩次卷积"""
def __init__(
self,
in_channels,
out_channels,
att=True,
):
super(ENetConv, self).__init__()
self.key = att
self.conv = ResnetBlock(in_channels, out_channels)
if att:
self.att = AvgMaxAttention(out_channels, out_channels)
# self.att = DotAttention(out_channels, out_channels)
# self.att = Transformer(out_channels, out_channels)
def forward(self, x):
x = self.conv(x)
if self.key:
att = self.att(x)
return x + att
return x