-
Notifications
You must be signed in to change notification settings - Fork 236
/
FMB.py
105 lines (82 loc) · 3.48 KB
/
FMB.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
import torch
import torch.nn as nn
import torch.nn.functional as F
#论文:SMFANet: A Lightweight Self-Modulation Feature Aggregation Network for Efficient Image Super-Resolution( ECCV 2024 )
#论文地址:https://openaccess.thecvf.com/content/CVPR2024W/NTIRE/papers/Ren_The_Ninth_NTIRE_2024_Efficient_Super-Resolution_Challenge_Report_CVPRW_2024_paper.pdf
class DMlp(nn.Module):
def __init__(self, dim, growth_rate=2.0):
super().__init__()
hidden_dim = int(dim * growth_rate)
self.conv_0 = nn.Sequential(
nn.Conv2d(dim, hidden_dim, 3, 1, 1, groups=dim),
nn.Conv2d(hidden_dim, hidden_dim, 1, 1, 0)
)
self.act = nn.GELU()
self.conv_1 = nn.Conv2d(hidden_dim, dim, 1, 1, 0)
def forward(self, x):
x = self.conv_0(x)
x = self.act(x)
x = self.conv_1(x)
return x
# partial convolution-based feed-forward network
class PCFN(nn.Module):
def __init__(self, dim, growth_rate=2.0, p_rate=0.25):
super().__init__()
hidden_dim = int(dim * growth_rate)
p_dim = int(hidden_dim * p_rate)
self.conv_0 = nn.Conv2d(dim, hidden_dim, 1, 1, 0)
self.conv_1 = nn.Conv2d(p_dim, p_dim, 3, 1, 1)
self.act = nn.GELU()
self.conv_2 = nn.Conv2d(hidden_dim, dim, 1, 1, 0)
self.p_dim = p_dim
self.hidden_dim = hidden_dim
def forward(self, x):
if self.training:
x = self.act(self.conv_0(x))
x1, x2 = torch.split(x, [self.p_dim, self.hidden_dim - self.p_dim], dim=1)
x1 = self.act(self.conv_1(x1))
x = self.conv_2(torch.cat([x1, x2], dim=1))
else:
x = self.act(self.conv_0(x))
x[:, :self.p_dim, :, :] = self.act(self.conv_1(x[:, :self.p_dim, :, :]))
x = self.conv_2(x)
return x
#self-modulation feature aggregation (SMFA) module
class SMFA(nn.Module):
def __init__(self, dim=36):
super(SMFA, self).__init__()
self.linear_0 = nn.Conv2d(dim, dim * 2, 1, 1, 0)
self.linear_1 = nn.Conv2d(dim, dim, 1, 1, 0)
self.linear_2 = nn.Conv2d(dim, dim, 1, 1, 0)
self.lde = DMlp(dim, 2)
self.dw_conv = nn.Conv2d(dim, dim, 3, 1, 1, groups=dim)
self.gelu = nn.GELU()
self.down_scale = 8
self.alpha = nn.Parameter(torch.ones((1, dim, 1, 1)))
self.belt = nn.Parameter(torch.zeros((1, dim, 1, 1)))
def forward(self, f):
_, _, h, w = f.shape
y, x = self.linear_0(f).chunk(2, dim=1)
x_s = self.dw_conv(F.adaptive_max_pool2d(x, (h // self.down_scale, w // self.down_scale)))
x_v = torch.var(x, dim=(-2, -1), keepdim=True)
x_l = x * F.interpolate(self.gelu(self.linear_1(x_s * self.alpha + x_v * self.belt)), size=(h, w),
mode='nearest')
y_d = self.lde(y)
return self.linear_2(x_l + y_d)
#Feature modulation block(FMB)
class FMB(nn.Module):
def __init__(self, dim, ffn_scale=2.0):
super().__init__()
self.smfa = SMFA(dim)
self.pcfn = PCFN(dim, ffn_scale)
def forward(self, x):
x = self.smfa(F.normalize(x)) + x
x = self.pcfn(F.normalize(x)) + x
return x
if __name__ == '__main__':
input_shape = (1, 36, 64, 64)# B C H W
input = torch.randn(input_shape)
block = FMB(dim=36)
output = block(input)
print(input.shape)
print(output.shape)