forked from seoungwugoh/RGMP
-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.py
135 lines (109 loc) · 4.78 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
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch.utils import data
import torch.utils.model_zoo as model_zoo
from torchvision import models
class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
self.conv1_p = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=True)
resnet = models.resnet50(pretrained=True)
self.conv1 = resnet.conv1
self.bn1 = resnet.bn1
self.relu = resnet.relu # 1/2, 64
self.maxpool = resnet.maxpool
self.res2 = resnet.layer1 # 1/4, 256
self.res3 = resnet.layer2 # 1/8, 512
self.res4 = resnet.layer3 # 1/16, 1024
self.res5 = resnet.layer4 # 1/32, 2048
# freeze BNs
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
for p in m.parameters():
p.requires_grad = False
self.register_buffer('mean', torch.FloatTensor([0.485, 0.456, 0.406]).view(1,3,1,1))
self.register_buffer('std', torch.FloatTensor([0.229, 0.224, 0.225]).view(1,3,1,1))
def forward(self, in_f, in_p):
f = (in_f - Variable(self.mean)) / Variable(self.std)
p = torch.unsqueeze(in_p, dim=1).float() # add channel dim
x = self.conv1(f) + self.conv1_p(p)# + self.conv1_n(n)
x = self.bn1(x)
c1 = self.relu(x) # 1/2, 64
x = self.maxpool(c1) # 1/4, 64
r2 = self.res2(x) # 1/4, 64
r3 = self.res3(r2) # 1/8, 128
r4 = self.res4(r3) # 1/16, 256
r5 = self.res5(r4) # 1/32, 512
return r5, r4, r3, r2
class GC(nn.Module):
def __init__(self, inplanes, planes, kh=7, kw=7):
super(GC, self).__init__()
self.conv_l1 = nn.Conv2d(inplanes, 256, kernel_size=(kh, 1),
padding=(int(kh/2), 0))
self.conv_l2 = nn.Conv2d(256, planes, kernel_size=(1, kw),
padding=(0, int(kw/2)))
self.conv_r1 = nn.Conv2d(inplanes, 256, kernel_size=(1, kw),
padding=(0, int(kw/2)))
self.conv_r2 = nn.Conv2d(256, planes, kernel_size=(kh, 1),
padding=(int(kh/2), 0))
def forward(self, x):
x_l = self.conv_l2(self.conv_l1(x))
x_r = self.conv_r2(self.conv_r1(x))
x = x_l + x_r
return x
class Refine(nn.Module):
def __init__(self, inplanes, planes, scale_factor=2):
super(Refine, self).__init__()
self.convFS1 = nn.Conv2d(inplanes, planes, kernel_size=3, padding=1)
self.convFS2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1)
self.convFS3 = nn.Conv2d(planes, planes, kernel_size=3, padding=1)
self.convMM1 = nn.Conv2d(planes, planes, kernel_size=3, padding=1)
self.convMM2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1)
self.scale_factor = scale_factor
def forward(self, f, pm):
s = self.convFS1(f)
sr = self.convFS2(F.relu(s))
sr = self.convFS3(F.relu(sr))
s = s + sr
m = s + F.upsample(pm, scale_factor=self.scale_factor, mode='bilinear')
mr = self.convMM1(F.relu(m))
mr = self.convMM2(F.relu(mr))
m = m + mr
return m
class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
mdim = 256
self.GC = GC(4096, mdim) # 1/32 -> 1/32
self.convG1 = nn.Conv2d(mdim, mdim, kernel_size=3, padding=1)
self.convG2 = nn.Conv2d(mdim, mdim, kernel_size=3, padding=1)
self.RF4 = Refine(1024, mdim) # 1/16 -> 1/8
self.RF3 = Refine(512, mdim) # 1/8 -> 1/4
self.RF2 = Refine(256, mdim) # 1/4 -> 1
self.pred5 = nn.Conv2d(mdim, 2, kernel_size=(3,3), padding=(1,1), stride=1)
self.pred4 = nn.Conv2d(mdim, 2, kernel_size=(3,3), padding=(1,1), stride=1)
self.pred3 = nn.Conv2d(mdim, 2, kernel_size=(3,3), padding=(1,1), stride=1)
self.pred2 = nn.Conv2d(mdim, 2, kernel_size=(3,3), padding=(1,1), stride=1)
def forward(self, r5, x5, r4, r3, r2):
x = torch.cat((r5, x5), dim=1)
x = self.GC(x)
r = self.convG1(F.relu(x))
r = self.convG2(F.relu(r))
m5 = x + r # out: 1/32, 64
m4 = self.RF4(r4, m5) # out: 1/16, 64
m3 = self.RF3(r3, m4) # out: 1/8, 64
m2 = self.RF2(r2, m3) # out: 1/4, 64
p2 = self.pred2(F.relu(m2))
p3 = self.pred3(F.relu(m3))
p4 = self.pred4(F.relu(m4))
p5 = self.pred5(F.relu(m5))
p = F.upsample(p2, scale_factor=4, mode='bilinear')
return p, p2, p3, p4, p5
class RGMP(nn.Module):
def __init__(self):
super(RGMP, self).__init__()
self.Encoder = Encoder()
self.Decoder = Decoder()