-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodel.py
195 lines (161 loc) · 5.39 KB
/
model.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import torch
import torch.nn as nn
from resnet import resnet50
import numpy as np
import cv2
def save_feats_mean(x):
b, c, h, w = x.shape
if h == 256:
with torch.no_grad():
x = x.detach().cpu().numpy()
x = np.transpose(x[0], (1, 2, 0))
x = np.mean(x, axis=-1)
x = x/np.max(x)
x = x * 255.0
x = x.astype(np.uint8)
x = cv2.applyColorMap(x, cv2.COLORMAP_JET)
x = np.array(x, dtype=np.uint8)
return x
class ResidualBlock(nn.Module):
def __init__(self, in_c, out_c):
super().__init__()
self.relu = nn.ReLU()
self.conv = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=3, padding=1),
nn.BatchNorm2d(out_c),
nn.ReLU(),
nn.Conv2d(out_c, out_c, kernel_size=3, padding=1),
nn.BatchNorm2d(out_c)
)
self.shortcut = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=1, padding=0),
nn.BatchNorm2d(out_c)
)
def forward(self, inputs):
x1 = self.conv(inputs)
x2 = self.shortcut(inputs)
x = self.relu(x1 + x2)
return x
class EncoderBlock(nn.Module):
def __init__(self, in_c, out_c):
super().__init__()
self.r1 = ResidualBlock(in_c, out_c)
self.pool = nn.MaxPool2d((2, 2))
def forward(self, inputs):
x = self.r1(inputs)
p = self.pool(x)
return x, p
class Bottleneck(nn.Module):
def __init__(self, in_c, out_c, dim, num_layers=2):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=1, padding=0),
nn.BatchNorm2d(out_c),
nn.ReLU()
)
encoder_layer = nn.TransformerEncoderLayer(d_model=dim, nhead=8)
self.tblock = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
self.conv2 = nn.Sequential(
nn.Conv2d(out_c, out_c, kernel_size=3, padding=1),
nn.BatchNorm2d(out_c),
nn.ReLU()
)
def forward(self, x):
x = self.conv1(x)
b, c, h, w = x.shape
x = x.reshape((b, c, h*w))
x = self.tblock(x)
x = x.reshape((b, c, h, w))
x = self.conv2(x)
return x
class DilatedConv(nn.Module):
def __init__(self, in_c, out_c):
super().__init__()
self.c1 = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=3, padding=1, dilation=1),
nn.BatchNorm2d(out_c),
nn.ReLU()
)
self.c2 = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=3, padding=3, dilation=3),
nn.BatchNorm2d(out_c),
nn.ReLU()
)
self.c3 = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=3, padding=6, dilation=6),
nn.BatchNorm2d(out_c),
nn.ReLU()
)
self.c4 = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size=3, padding=9, dilation=9),
nn.BatchNorm2d(out_c),
nn.ReLU()
)
self.c5 = nn.Sequential(
nn.Conv2d(out_c*4, out_c, kernel_size=1, padding=0),
nn.BatchNorm2d(out_c),
nn.ReLU()
)
def forward(self, inputs):
x1 = self.c1(inputs)
x2 = self.c2(inputs)
x3 = self.c3(inputs)
x4 = self.c4(inputs)
x = torch.cat([x1, x2, x3, x4], axis=1)
x = self.c5(x)
return x
class DecoderBlock(nn.Module):
def __init__(self, in_c, out_c):
super().__init__()
self.up = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
self.r1 = ResidualBlock(in_c[0]+in_c[1], out_c)
self.r2 = ResidualBlock(out_c, out_c)
def forward(self, inputs, skip):
x = self.up(inputs)
x = torch.cat([x, skip], axis=1)
x = self.r1(x)
x = self.r2(x)
return x
class TResUnet(nn.Module):
def __init__(self):
super().__init__()
""" ResNet50 """
backbone = resnet50()
self.layer0 = nn.Sequential(backbone.conv1, backbone.bn1, backbone.relu)
self.layer1 = nn.Sequential(backbone.maxpool, backbone.layer1)
self.layer2 = backbone.layer2
self.layer3 = backbone.layer3
""" Bridge blocks """
self.b1 = Bottleneck(1024, 256, 256, num_layers=2)
self.b2 = DilatedConv(1024, 256)
""" Decoder """
self.d1 = DecoderBlock([512, 512], 256)
self.d2 = DecoderBlock([256, 256], 128)
self.d3 = DecoderBlock([128, 64], 64)
self.d4 = DecoderBlock([64, 3], 32)
self.output = nn.Conv2d(32, 1, kernel_size=1)
def forward(self, x, heatmap=None):
s0 = x
s1 = self.layer0(s0) ## [-1, 64, h/2, w/2]
s2 = self.layer1(s1) ## [-1, 256, h/4, w/4]
s3 = self.layer2(s2) ## [-1, 512, h/8, w/8]
s4 = self.layer3(s3) ## [-1, 1024, h/16, w/16]
b1 = self.b1(s4)
b2 = self.b2(s4)
b3 = torch.cat([b1, b2], axis=1)
# print(b3.shape)
d1 = self.d1(b3, s3)
d2 = self.d2(d1, s2)
d3 = self.d3(d2, s1)
d4 = self.d4(d3, s0)
y = self.output(d4)
if heatmap != None:
hmap = save_feats_mean(d4)
return hmap, y
else:
return y
if __name__ == "__main__":
x = torch.randn((8, 3, 256, 256))
model = TResUnet()
y = model(x)
print(y.shape)