-
Notifications
You must be signed in to change notification settings - Fork 1
/
CNN_Auto_prompt_block.py
164 lines (115 loc) · 4.92 KB
/
CNN_Auto_prompt_block.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class DownsampleConv(nn.Module):
def __init__(self, in_channels, out_channels):
super(DownsampleConv, self).__init__()
self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
self.norm = nn.BatchNorm2d(num_features=out_channels)
self.gelu = nn.GELU()
def forward(self, x):
x = self.conv(x)
x = self.norm(x)
x = self.gelu(x)
return x
class ConvUpsample(nn.Module):
def __init__(self, in_channels, out_channels):
super(ConvUpsample, self).__init__()
self.upconv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2)
self.upconv1 = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=1, stride=1)
self.conv = nn.Sequential(
nn.Conv2d(out_channels * 2, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU()
)
def forward(self, x_enc, x_dec):
x = self.upconv(x_enc)
x = torch.cat((x, x_dec), dim=1)
return self.conv(x)
class ConvUpsample1(nn.Module):
def __init__(self, in_channels, out_channels):
super(ConvUpsample1, self).__init__()
self.upconv1 = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=1, stride=1)
self.conv = nn.Sequential(
nn.Conv2d(out_channels * 2, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU()
)
def forward(self, x_enc, x_dec):
x = self.upconv1(x_enc)
x = torch.cat((x, x_dec), dim=1)
return self.conv(x)
class LayerNorm2d(nn.Module):
def __init__(self, num_channels: int, eps: float = 1e-6) -> None:
super().__init__()
self.weight = nn.Parameter(torch.ones(num_channels))
self.bias = nn.Parameter(torch.zeros(num_channels))
self.eps = eps
def forward(self, x: torch.Tensor) -> torch.Tensor:
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
class CNN_APB(nn.Module):
def __init__(self):
super(CNN_APB, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=160, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(160),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=160, out_channels=320, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(320),
nn.ReLU(),
nn.MaxPool2d(kernel_size=1, stride=1)
)
self.conv4 = nn.Sequential(
nn.Conv2d(in_channels=320, out_channels=320, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(320),
nn.ReLU()
)
self.downsample1 = DownsampleConv(in_channels=128 * 2,
out_channels=128)
self.downsample2 = DownsampleConv(in_channels=320, out_channels=160)
self.downsample3 = DownsampleConv(in_channels=640, out_channels=320)
self.downsample4 = DownsampleConv(in_channels=640, out_channels=320)
#Auto_prompt_block
self.decoder4 = ConvUpsample1(320, 320)
self.decoder3 = ConvUpsample1(320, 160)
self.decoder2 = ConvUpsample(160, 128)
self.decoder1 = ConvUpsample(128, 3)
self.decoder0 = torch.nn.Conv2d(in_channels=3, out_channels=1, kernel_size=1)
def forward(self, x_c,x0,x1,x2,x3):
x_0 = self.conv1(x_c) # (1, 128, 128, 128)
x_0 = torch.cat((x_0,x0),dim=1)
x_0 = self.downsample1(x_0)
x_1 = self.conv2(x_0) # (1,160, 64, 64)
x_1 = torch.cat((x_1, x1), dim=1)
x_1 = self.downsample2(x_1)
x_2 = self.conv3(x_1) # (1, 320, 64, 64)
x_2 = torch.cat((x_2, x2), dim=1)
x_2 = self.downsample3(x_2)
x_3 = self.conv4(x_2) # (1, 320, 64, 64)
x_3 = torch.cat((x_3, x3), dim=1)
x_3 = self.downsample4(x_3)
x = self.decoder4(x_3, x_2)
x = self.decoder3(x, x_1)
x = self.decoder2(x, x_0)
x = self.decoder1(x, x_c)
x = self.decoder0(x)
return x