forked from SJTUzhanglj/FCN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
151 lines (134 loc) · 5.56 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
import numpy as np
import torch
import torch.nn as nn
def get_upsample_filter(size):
"""Make a 2D bilinear kernel suitable for upsampling"""
factor = (size + 1) // 2
if size % 2 == 1:
center = factor - 1
else:
center = factor - 0.5
og = np.ogrid[:size, :size]
filter = (1 - abs(og[0] - center) / factor) * \
(1 - abs(og[1] - center) / factor)
return torch.from_numpy(filter).float()
class FCN8s(nn.Module):
def __init__(self, n_class=21):
super(FCN8s, self).__init__()
self.features_123 = nn.Sequential(
# conv1
nn.Conv2d(3, 64, 3, padding=100),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, 3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/2
# conv2
nn.Conv2d(64, 128, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, 3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/4
# conv3
nn.Conv2d(128, 256, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, 3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/8
)
self.features_4 = nn.Sequential(
# conv4
nn.Conv2d(256, 512, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/16
)
self.features_5 = nn.Sequential(
# conv5 features
nn.Conv2d(512, 512, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/32
)
self.classifier = nn.Sequential(
# fc6
nn.Conv2d(512, 4096, 7),
nn.ReLU(inplace=True),
nn.Dropout2d(),
# fc7
nn.Conv2d(4096, 4096, 1),
nn.ReLU(inplace=True),
nn.Dropout2d(),
# score_fr
nn.Conv2d(4096, n_class, 1),
)
self.score_feat3 = nn.Conv2d(256, n_class, 1)
self.score_feat4 = nn.Conv2d(512, n_class, 1)
self.upscore = nn.ConvTranspose2d(n_class, n_class, 16, stride=8,
bias=False)
self.upscore_4 = nn.ConvTranspose2d(n_class, n_class, 4, stride=2,
bias=False)
self.upscore_5 = nn.ConvTranspose2d(n_class, n_class, 4, stride=2,
bias=False)
def forward(self, x):
feat3 = self.features_123(x) #1/8
feat4 = self.features_4(feat3) #1/16
feat5 = self.features_5(feat4) #1/32
score5 = self.classifier(feat5)
upscore5 = self.upscore_5(score5)
score4 = self.score_feat4(feat4)
score4 = score4[:, :, 5:5+upscore5.size()[2], 5:5+upscore5.size()[3]].contiguous()
score4 += upscore5
score3 = self.score_feat3(feat3)
upscore4 = self.upscore_4(score4)
score3 = score3[:, :, 9:9+upscore4.size()[2], 9:9+upscore4.size()[3]].contiguous()
score3 += upscore4
h = self.upscore(score3)
h = h[:, :, 28:28+x.size()[2], 28:28+x.size()[3]].contiguous()
return h
def copy_params_from_vgg16(self, vgg16, copy_fc8=True, init_upscore=True):
for l1, l2 in zip(vgg16.features, [self.features_123,self.features_4,self.features_5]):
if (isinstance(l1, nn.Conv2d) and
isinstance(l2, nn.Conv2d)):
assert l1.weight.size() == l2.weight.size()
assert l1.bias.size() == l2.bias.size()
l2.weight.data = l1.weight.data
l2.bias.data = l1.bias.data
for i1, i2 in zip([0, 3], [0, 3]):
l1 = vgg16.classifier[i1]
l2 = self.classifier[i2]
l2.weight.data = l1.weight.data.view(l2.weight.data.size())
l2.bias.data = l1.bias.data.view(l2.bias.data.size())
n_class = self.classifier[6].weight.size()[0]
if copy_fc8:
l1 = vgg16.classifier[6]
l2 = self.classifier[6]
l2.weight.data = l1.weight.data[:n_class, :].view(l2.weight.size())
l2.bias.data = l1.bias.data[:n_class]
if init_upscore:
# initialize upscore layer
c1, c2, h, w = self.upscore.weight.data.size()
assert c1 == c2 == n_class
assert h == w
weight = get_upsample_filter(h)
self.upscore.weight.data = \
weight.view(1, 1, h, w).repeat(c1, c2, 1, 1)
c1, c2, h, w = self.upscore_4.weight.data.size()
assert c1 == c2 == n_class
assert h == w
weight = get_upsample_filter(h)
self.upscore_4.weight.data = \
weight.view(1, 1, h, w).repeat(c1, c2, 1, 1)
c1, c2, h, w = self.upscore_5.weight.data.size()
assert c1 == c2 == n_class
assert h == w
weight = get_upsample_filter(h)
self.upscore_5.weight.data = \
weight.view(1, 1, h, w).repeat(c1, c2, 1, 1)