-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
198 lines (172 loc) · 5.87 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
196
197
198
# import libraries
import math
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
def weights_init(m):
classname = m.__class__.__name__
print (classname)
if classname.find('Conv2d') != -1:
init.xavier_uniform(m.weight.data)
init.constant(m.bias.data, 0.1)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
def transfer_weights(model_from, model_to):
wf = copy.deepcopy(model_from.state_dict())
wt = model_to.state_dict()
for k in wt.keys():
if not k in wf:
wf[k] = wt[k]
model_to.load_state_dict(wf)
def convert_vgg(vgg16):
net = vgg()
vgg_items = list(net.state_dict().items())
vgg16_items = list(vgg16.items())
pretrain_model = {}
j = 0
for k, v in net.state_dict().items():
v = vgg16_items[j][1]
k = vgg_items[j][0]
pretrain_model[k] = v
j += 1
return pretrain_model
class vgg(nn.Module):
def __init__(self):
super(vgg, self).__init__()
# conv1
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, 3, padding=35),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, 3, padding=1),
nn.ReLU(inplace=True),
)
# conv2
self.conv2 = nn.Sequential(
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/2
nn.Conv2d(64, 128, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, 3, padding=1),
nn.ReLU(inplace=True),
)
# conv3
self.conv3 = nn.Sequential(
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/4
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),
)
# conv4
self.conv4 = nn.Sequential(
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/8
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),
)
# conv5
self.conv5 = nn.Sequential(
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/16
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),
)
def forward(self, x):
conv1 = self.conv1(x)
conv2 = self.conv2(conv1)
conv3 = self.conv3(conv2)
conv4 = self.conv4(conv3)
conv5 = self.conv5(conv4)
return conv5
class HED(nn.Module):
def __init__(self):
super(HED, self).__init__()
# conv1
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, 3, padding=1),
nn.ReLU(inplace=True),
)
# conv2
self.conv2 = nn.Sequential(
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/2
nn.Conv2d(64, 128, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, 3, padding=1),
nn.ReLU(inplace=True),
)
# conv3
self.conv3 = nn.Sequential(
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/4
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),
)
# conv4
self.conv4 = nn.Sequential(
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/8
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),
)
# conv5
self.conv5 = nn.Sequential(
nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/16
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),
)
self.dsn1 = nn.Conv2d(64, 1, 1)
self.dsn2 = nn.Conv2d(128, 1, 1)
self.dsn3 = nn.Conv2d(256, 1, 1)
self.dsn4 = nn.Conv2d(512, 1, 1)
self.dsn5 = nn.Conv2d(512, 1, 1)
self.fuse = nn.Conv2d(5, 1, 1)
def forward(self, x):
h = x.size(2)
w = x.size(3)
conv1 = self.conv1(x)
conv2 = self.conv2(conv1)
conv3 = self.conv3(conv2)
conv4 = self.conv4(conv3)
# conv5 = self.conv5(conv4)
conv5 = conv4
## side output
d1 = self.dsn1(conv1)
d2 = F.upsample_bilinear(self.dsn2(conv2), size=(h,w))
d3 = F.upsample_bilinear(self.dsn3(conv3), size=(h,w))
d4 = F.upsample_bilinear(self.dsn4(conv4), size=(h,w))
d5 = F.upsample_bilinear(self.dsn5(conv5), size=(h,w))
# print('dsn1 shape : ', self.dsn1(conv1).shape)
# print('dsn2 shape : ', self.dsn2(conv2).shape)
# print('dsn3 shape : ', self.dsn3(conv3).shape)
# print('dsn4 shape : ', self.dsn4(conv4).shape)
# print('dsn5 shape : ', self.dsn5(conv5).shape)
# dsn fusion output
fuse = self.fuse(torch.cat((d1, d2, d3, d4, d5), 1))
d1 = F.sigmoid(d1)
d2 = F.sigmoid(d2)
d3 = F.sigmoid(d3)
d4 = F.sigmoid(d4)
d5 = F.sigmoid(d5)
fuse = F.sigmoid(fuse)
return d1, d2, d3, d4, d5, fuse