-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
128 lines (103 loc) · 3.58 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
import torch
import torch.nn as nn
from pvtv2 import pvt_v2_b3
class Conv2D(nn.Module):
def __init__(self, in_c, out_c, kernel_size=3, padding=1, dilation=1, bias=True, act=True):
super().__init__()
self.act = act
self.conv = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel_size, padding=padding, dilation=dilation, bias=bias),
nn.BatchNorm2d(out_c)
)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
if self.act == True:
x = self.relu(x)
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 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+out_c, out_c)
def forward(self, x, s):
x = self.up(x)
x = torch.cat([x, s], axis=1)
x = self.r1(x)
return x
class UpBlock(nn.Module):
def __init__(self, in_c, out_c, scale):
super().__init__()
self.up = nn.Upsample(scale_factor=scale, mode="bilinear", align_corners=True)
self.r1 = ResidualBlock(in_c, out_c)
def forward(self, inputs):
x = self.up(inputs)
x = self.r1(x)
return x
class PVTFormer(nn.Module):
def __init__(self):
super().__init__()
""" Encoder """
self.backbone = pvt_v2_b3() ## [64, 128, 320, 512]
path = 'pvt_v2_b3.pth'
save_model = torch.load(path)
model_dict = self.backbone.state_dict()
state_dict = {k: v for k, v in save_model.items() if k in model_dict.keys()}
model_dict.update(state_dict)
self.backbone.load_state_dict(model_dict)
""" Channel Reduction """
self.c1 = Conv2D(64, 64, kernel_size=1, padding=0)
self.c2 = Conv2D(128, 64, kernel_size=1, padding=0)
self.c3 = Conv2D(320, 64, kernel_size=1, padding=0)
self.d1 = DecoderBlock(64, 64)
self.d2 = DecoderBlock(64, 64)
self.d3 = UpBlock(64, 64, 4)
self.u1 = UpBlock(64, 64, 4)
self.u2 = UpBlock(64, 64, 8)
self.u3 = UpBlock(64, 64, 16)
self.r1 = ResidualBlock(64*4, 64)
self.y = nn.Conv2d(64, 1, kernel_size=1, padding=0)
def forward(self, inputs):
""" Encoder """
pvt1 = self.backbone(inputs)
e1 = pvt1[0] ## [-1, 64, h/4, w/4]
e2 = pvt1[1] ## [-1, 128, h/8, w/8]
e3 = pvt1[2] ## [-1, 320, h/16, w/16]
c1 = self.c1(e1)
c2 = self.c2(e2)
c3 = self.c3(e3)
d1 = self.d1(c3, c2)
d2 = self.d2(d1, c1)
d3 = self.d3(d2)
u1 = self.u1(c1)
u2 = self.u2(c2)
u3 = self.u3(c3)
x = torch.cat([d3, u1, u2, u3], axis=1)
x = self.r1(x)
y = self.y(x)
return y
if __name__ == "__main__":
x = torch.randn((4, 3, 256, 256))
model = PVTFormer()
y = model(x)
print(y.shape)