-
Notifications
You must be signed in to change notification settings - Fork 30
/
model.py
133 lines (120 loc) · 6.31 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
import torch
import torch.nn.functional as F
from torch import nn
from resnext import ResNeXt101
class R3Net(nn.Module):
def __init__(self):
super(R3Net, self).__init__()
resnext = ResNeXt101()
self.layer0 = resnext.layer0
self.layer1 = resnext.layer1
self.layer2 = resnext.layer2
self.layer3 = resnext.layer3
self.layer4 = resnext.layer4
self.reduce_low = nn.Sequential(
nn.Conv2d(64 + 256 + 512, 256, kernel_size=3, padding=1), nn.BatchNorm2d(256), nn.PReLU(),
nn.Conv2d(256, 256, kernel_size=3, padding=1), nn.BatchNorm2d(256), nn.PReLU(),
nn.Conv2d(256, 256, kernel_size=1), nn.BatchNorm2d(256), nn.PReLU()
)
self.reduce_high = nn.Sequential(
nn.Conv2d(1024 + 2048, 256, kernel_size=3, padding=1), nn.BatchNorm2d(256), nn.PReLU(),
nn.Conv2d(256, 256, kernel_size=3, padding=1), nn.BatchNorm2d(256), nn.PReLU(),
_ASPP(256)
)
self.predict0 = nn.Conv2d(256, 1, kernel_size=1)
self.predict1 = nn.Sequential(
nn.Conv2d(257, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 1, kernel_size=1)
)
self.predict2 = nn.Sequential(
nn.Conv2d(257, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 1, kernel_size=1)
)
self.predict3 = nn.Sequential(
nn.Conv2d(257, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 1, kernel_size=1)
)
self.predict4 = nn.Sequential(
nn.Conv2d(257, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 1, kernel_size=1)
)
self.predict5 = nn.Sequential(
nn.Conv2d(257, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 1, kernel_size=1)
)
self.predict6 = nn.Sequential(
nn.Conv2d(257, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.PReLU(),
nn.Conv2d(128, 1, kernel_size=1)
)
for m in self.modules():
if isinstance(m, nn.ReLU) or isinstance(m, nn.Dropout):
m.inplace = True
def forward(self, x):
layer0 = self.layer0(x)
layer1 = self.layer1(layer0)
layer2 = self.layer2(layer1)
layer3 = self.layer3(layer2)
layer4 = self.layer4(layer3)
l0_size = layer0.size()[2:]
reduce_low = self.reduce_low(torch.cat((
layer0,
F.upsample(layer1, size=l0_size, mode='bilinear', align_corners=True),
F.upsample(layer2, size=l0_size, mode='bilinear', align_corners=True)), 1))
reduce_high = self.reduce_high(torch.cat((
layer3,
F.upsample(layer4, size=layer3.size()[2:], mode='bilinear', align_corners=True)), 1))
reduce_high = F.upsample(reduce_high, size=l0_size, mode='bilinear', align_corners=True)
predict0 = self.predict0(reduce_high)
predict1 = self.predict1(torch.cat((predict0, reduce_low), 1)) + predict0
predict2 = self.predict2(torch.cat((predict1, reduce_high), 1)) + predict1
predict3 = self.predict3(torch.cat((predict2, reduce_low), 1)) + predict2
predict4 = self.predict4(torch.cat((predict3, reduce_high), 1)) + predict3
predict5 = self.predict5(torch.cat((predict4, reduce_low), 1)) + predict4
predict6 = self.predict6(torch.cat((predict5, reduce_high), 1)) + predict5
predict0 = F.upsample(predict0, size=x.size()[2:], mode='bilinear', align_corners=True)
predict1 = F.upsample(predict1, size=x.size()[2:], mode='bilinear', align_corners=True)
predict2 = F.upsample(predict2, size=x.size()[2:], mode='bilinear', align_corners=True)
predict3 = F.upsample(predict3, size=x.size()[2:], mode='bilinear', align_corners=True)
predict4 = F.upsample(predict4, size=x.size()[2:], mode='bilinear', align_corners=True)
predict5 = F.upsample(predict5, size=x.size()[2:], mode='bilinear', align_corners=True)
predict6 = F.upsample(predict6, size=x.size()[2:], mode='bilinear', align_corners=True)
if self.training:
return predict0, predict1, predict2, predict3, predict4, predict5, predict6
return F.sigmoid(predict6)
class _ASPP(nn.Module):
# this module is proposed in deeplabv3 and we use it in all of our baselines
def __init__(self, in_dim):
super(_ASPP, self).__init__()
down_dim = in_dim / 2
self.conv1 = nn.Sequential(
nn.Conv2d(in_dim, down_dim, kernel_size=1), nn.BatchNorm2d(down_dim), nn.PReLU()
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_dim, down_dim, kernel_size=3, dilation=2, padding=2), nn.BatchNorm2d(down_dim), nn.PReLU()
)
self.conv3 = nn.Sequential(
nn.Conv2d(in_dim, down_dim, kernel_size=3, dilation=4, padding=4), nn.BatchNorm2d(down_dim), nn.PReLU()
)
self.conv4 = nn.Sequential(
nn.Conv2d(in_dim, down_dim, kernel_size=3, dilation=6, padding=6), nn.BatchNorm2d(down_dim), nn.PReLU()
)
self.conv5 = nn.Sequential(
nn.Conv2d(in_dim, down_dim, kernel_size=1), nn.BatchNorm2d(down_dim), nn.PReLU()
)
self.fuse = nn.Sequential(
nn.Conv2d(5 * down_dim, in_dim, kernel_size=1), nn.BatchNorm2d(in_dim), nn.PReLU()
)
def forward(self, x):
conv1 = self.conv1(x)
conv2 = self.conv2(x)
conv3 = self.conv3(x)
conv4 = self.conv4(x)
conv5 = F.upsample(self.conv5(F.adaptive_avg_pool2d(x, 1)), size=x.size()[2:], mode='bilinear',
align_corners=True)
return self.fuse(torch.cat((conv1, conv2, conv3, conv4, conv5), 1))