-
Notifications
You must be signed in to change notification settings - Fork 4
/
RDPNet.py
110 lines (81 loc) · 2.74 KB
/
RDPNet.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
import torch.nn as nn
import torch
from switchable_norm import SwitchNorm2d
class mixer(nn.Module):
def __init__(self, dim):
super(mixer, self).__init__()
self.depthconv = nn.Conv2d(dim, dim, kernel_size=9, padding=4, groups=dim)
self.gn1 = SwitchNorm2d(dim)
self.pointconv = nn.Conv2d(dim, dim, kernel_size=1)
self.gn2 = SwitchNorm2d(dim)
self.gelu = nn.GELU()
def forward(self, x):
shortcut = x
x = self.depthconv(x)
x = self.gn1(x)
x = self.gelu(x)
x = x + shortcut
x = self.pointconv(x)
x = self.gn2(x)
x = self.gelu(x)
return x
class up_sampling(nn.Module):
def __init__(self, in_ch, out_ch, stride=8):
super(up_sampling, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(in_ch, out_ch, kernel_size=1),
SwitchNorm2d(out_ch),
nn.GELU(),
)
dim = out_ch
self.patchup = nn.ConvTranspose2d(dim, dim, kernel_size=stride, stride=stride)
self.bn2 = SwitchNorm2d(dim)
self.gelu = nn.GELU()
def forward(self, x):
x = self.layer1(x)
x = self.patchup(x)
x = self.bn2(x)
output = self.gelu(x)
return output
class RDPNet(nn.Module):
def __init__(self, in_ch, out_ch, hid_ch=384):
super(RDPNet, self).__init__()
depth = 32
self.patchEmb = nn.Conv2d(in_ch * 2, hid_ch, kernel_size=8, stride=8)
self.gn1 = SwitchNorm2d(hid_ch)
self.mixer1 = mixer(hid_ch)
self.ch1 = up_sampling(hid_ch, depth)
self.mixer2 = mixer(hid_ch)
self.ch2 = up_sampling(hid_ch, depth)
self.mixer3 = mixer(hid_ch)
self.ch3 = up_sampling(hid_ch, depth)
self.mixer4 = mixer(hid_ch)
self.ch4 = up_sampling(hid_ch, depth)
self.mixer5 = mixer(hid_ch)
self.ch5 = up_sampling(hid_ch, depth)
self.mixer6 = mixer(hid_ch)
self.ch6 = up_sampling(hid_ch, depth)
self.weight = nn.Parameter(torch.randn(1, depth * 6, 1, 1))
self.final = nn.Conv2d(depth * 6, out_ch, kernel_size=1)
self.gelu = nn.GELU()
def forward(self, a, b):
x = torch.cat([a, b], 1)
x = self.patchEmb(x)
x = self.gn1(x)
x = self.gelu(x)
x = self.mixer1(x)
ch1 = self.ch1(x)
x = self.mixer2(x)
ch2 = self.ch2(x)
x = self.mixer3(x)
ch3 = self.ch3(x)
x = self.mixer4(x)
ch4 = self.ch4(x)
x = self.mixer5(x)
ch5 = self.ch5(x)
x = self.mixer6(x)
ch6 = self.ch6(x)
out = torch.cat([ch1, ch2, ch3, ch4, ch5, ch6], 1)
out = out * self.weight
out = self.final(out)
return out