-
Notifications
You must be signed in to change notification settings - Fork 9
/
TransUNet.py
178 lines (141 loc) · 5.02 KB
/
TransUNet.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import torch.nn as nn
from ViT import ViT
from einops import rearrange
import torch
class BottleNeckUnit(nn.Module):
def __init__(self, in_channels, out_channels, base_width=64, stride=1):
super(BottleNeckUnit, self).__init__()
if stride != 1 or in_channels != out_channels:
self.downsample = nn.Sequential(
nn.Conv2d(
in_channels, out_channels, kernel_size=1, stride=stride, bias=False
),
nn.BatchNorm2d(out_channels),
)
else:
self.downsample = nn.Identity()
gamma = (base_width // 64) * out_channels
self.layer = nn.Sequential(
nn.Conv2d(
in_channels,
gamma * out_channels,
kernel_size=1,
stride=stride,
bias=False,
),
nn.BatchNorm2d(gamma * out_channels),
nn.Conv2d(
gamma * out_channels,
gamma * out_channels,
kernel_size=3,
stride=stride,
padding=1,
groups=1,
bias=False,
dilation=1,
),
nn.BatchNorm2d(gamma * out_channels),
nn.Conv2d(
gamma * out_channels,
out_channels,
kernel_size=1,
stride=stride,
bias=False,
),
nn.BatchNorm2d(out_channels),
)
def forward(self, x):
x = self.layer(x)
identity = self.downsample(x)
x += identity
return nn.ReLU()(x)
class TransUNetEncoder(nn.Module):
def __init__(
self,
img_dim,
patch_dim,
in_channels,
classes,
layers=12,
heads=4,
linear_dim=1024,
):
super(TransUNetEncoder, self).__init__()
# inital conv 3 -> channels
# bottleneck channels -> channels*2
# bottleneck channels*2 -> channels*4
# bottleneck channels*4 -> channels*8
# vit
# conv -> channels*8 -> 512
self.channels = 128
self.img_dim = img_dim
self.patch_dim = patch_dim
self.layer1 = nn.Conv2d(
in_channels, self.channels, kernel_size=7, stride=2, padding=3, bias=False
)
self.layer2 = BottleNeckUnit(self.channels, self.channels * 2)
self.layer3 = BottleNeckUnit(self.channels * 2, self.channels * 4)
self.layer4 = BottleNeckUnit(self.channels * 4, self.channels * 8)
self.layer5 = ViT(img_dim, in_channels=self.channels * 8, classification=False)
self.layer6 = nn.Conv2d(self.channels * 8, 512, 3, stride=1, padding=1)
self.batchnorm = nn.BatchNorm2d(512)
def forward(self, x):
x1 = self.layer1(x)
x2 = self.layer2(x1)
x3 = self.layer3(x2)
x = self.layer4(x3)
x = self.layer5(x)
x = rearrange(
x,
"b (x y) c -> b c x y",
x=self.img_dim // self.patch_dim,
y=self.img_dim // self.patch_dim,
)
x = self.batchnorm(self.layer6(x))
return nn.ReLU()(x), x1, x2, x3
class TransUNetDecoderUnit(nn.Module):
def __init__(self, in_channels, out_channels):
super(TransUNetDecoderUnit, self).__init__()
self.upsample = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
self.layer = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x, x_skip=None):
x = self.upsample(x)
if x_skip is not None:
x = torch.cat([x_skip, x], dim=1)
out = self.layer(x)
return out
class TransUNet(nn.Module):
def __init__(
self,
img_dim,
patch_dim,
in_channels=3,
classes=1,
blocks=6,
heads=8,
linear_dim=1024,
):
super(TransUNet, self).__init__()
self.encoder = TransUNetEncoder(
img_dim, patch_dim, in_channels, classes, blocks, heads, linear_dim
)
self.decoder1 = TransUNetDecoderUnit(1024, 256)
self.decoder2 = TransUNetDecoderUnit(512, 128)
self.decoder3 = TransUNetDecoderUnit(256, 64)
self.decoder4 = TransUNetDecoderUnit(64, 16)
self.conv = nn.Conv2d(16, classes, kernel_size=1, bias=False)
def forward(self, x):
x, x1, x2, x3 = self.encoder(x)
out = self.decoder1(x, x3)
out = self.decoder2(out, x2)
out = self.decoder3(out, x1)
out = self.decoder4(out)
out = self.conv(out)
return out