-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
172 lines (144 loc) · 7.03 KB
/
models.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
import os, sys
import torch
import torch.nn as nn
import torchvision.models as models
import torch.autograd.variable as Variable
import numpy as np
import scipy.io as sio
from torch.nn.modules.conv import _ConvNd
from torch.nn.modules.conv import _single, _pair, _triple
import torch.nn.functional as F
class RCF(nn.Module):
def __init__(self):
super(RCF, self).__init__()
#lr 1 2 decay 1 0
self.conv1_1 = nn.Conv2d(3, 64, 3, padding=1)
self.conv1_2 = nn.Conv2d(64, 64, 3, padding=1)
self.conv2_1 = nn.Conv2d(64, 128, 3, padding=1)
self.conv2_2 = nn.Conv2d(128, 128, 3, padding=1)
self.conv3_1 = nn.Conv2d(128, 256, 3, padding=1)
self.conv3_2 = nn.Conv2d(256, 256, 3, padding=1)
self.conv3_3 = nn.Conv2d(256, 256, 3, padding=1)
self.conv4_1 = nn.Conv2d(256, 512, 3, padding=1)
self.conv4_2 = nn.Conv2d(512, 512, 3, padding=1)
self.conv4_3 = nn.Conv2d(512, 512, 3, padding=1)
self.conv5_1 = nn.Conv2d(512, 512, kernel_size=3,
stride=1, padding=2, dilation=2)
self.conv5_2 = nn.Conv2d(512, 512, kernel_size=3,
stride=1, padding=2, dilation=2)
self.conv5_3 = nn.Conv2d(512, 512, kernel_size=3,
stride=1, padding=2, dilation=2)
self.activ = nn.ReLU(inplace=True)
self.maxpool4 = nn.MaxPool2d(2, stride=1, ceil_mode=True)
self.maxpool_1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.maxpool_2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
self.maxpool_3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)
#lr 0.1 0.2 decay 1 0
self.conv1_1_down = nn.Conv2d(64, 21, 1, padding=0)
self.conv1_2_down = nn.Conv2d(64, 21, 1, padding=0)
self.conv2_1_down = nn.Conv2d(128, 21, 1, padding=0)
self.conv2_2_down = nn.Conv2d(128, 21, 1, padding=0)
self.conv3_1_down = nn.Conv2d(256, 21, 1, padding=0)
self.conv3_2_down = nn.Conv2d(256, 21, 1, padding=0)
self.conv3_3_down = nn.Conv2d(256, 21, 1, padding=0)
self.conv4_1_down = nn.Conv2d(512, 21, 1, padding=0)
self.conv4_2_down = nn.Conv2d(512, 21, 1, padding=0)
self.conv4_3_down = nn.Conv2d(512, 21, 1, padding=0)
self.conv5_1_down = nn.Conv2d(512, 21, 1, padding=0)
self.conv5_2_down = nn.Conv2d(512, 21, 1, padding=0)
self.conv5_3_down = nn.Conv2d(512, 21, 1, padding=0)
#lr 0.01 0.02 decay 1 0
self.score_dsn1 = nn.Conv2d(21, 1, 1)
self.score_dsn2 = nn.Conv2d(21, 1, 1)
self.score_dsn3 = nn.Conv2d(21, 1, 1)
self.score_dsn4 = nn.Conv2d(21, 1, 1)
self.score_dsn5 = nn.Conv2d(21, 1, 1)
#lr 0.001 0.002 decay 1 0
self.score_final = nn.Conv2d(5, 1, 1)
## Fixed the upsampling weights for the training process as per @https://github.com/xwjabc/hed
self.weight_deconv2 = make_bilinear_weights(4, 1).cuda()
self.weight_deconv3 = make_bilinear_weights(8, 1).cuda()
self.weight_deconv4 = make_bilinear_weights(16, 1).cuda()
# Wrong Deconv Filter size. Updated from RCF yun_liu
# self.weight_deconv5 = make_bilinear_weights(32, 1).cuda()
self.weight_deconv5 = make_bilinear_weights(16, 1).cuda()
def forward(self, x):
# VGG
img_H, img_W = x.shape[2], x.shape[3]
conv1_1 = self.activ(self.conv1_1(x))
conv1_2 = self.activ(self.conv1_2(conv1_1))
pool1 = self.maxpool_1(conv1_2)
conv2_1 = self.activ(self.conv2_1(pool1))
conv2_2 = self.activ(self.conv2_2(conv2_1))
pool2 = self.maxpool_2(conv2_2)
conv3_1 = self.activ(self.conv3_1(pool2))
conv3_2 = self.activ(self.conv3_2(conv3_1))
conv3_3 = self.activ(self.conv3_3(conv3_2))
pool3 = self.maxpool_3(conv3_3)
conv4_1 = self.activ(self.conv4_1(pool3))
conv4_2 = self.activ(self.conv4_2(conv4_1))
conv4_3 = self.activ(self.conv4_3(conv4_2))
pool4 = self.maxpool4(conv4_3)
conv5_1 = self.activ(self.conv5_1(pool4))
conv5_2 = self.activ(self.conv5_2(conv5_1))
conv5_3 = self.activ(self.conv5_3(conv5_2))
conv1_1_down = self.conv1_1_down(conv1_1)
conv1_2_down = self.conv1_2_down(conv1_2)
conv2_1_down = self.conv2_1_down(conv2_1)
conv2_2_down = self.conv2_2_down(conv2_2)
conv3_1_down = self.conv3_1_down(conv3_1)
conv3_2_down = self.conv3_2_down(conv3_2)
conv3_3_down = self.conv3_3_down(conv3_3)
conv4_1_down = self.conv4_1_down(conv4_1)
conv4_2_down = self.conv4_2_down(conv4_2)
conv4_3_down = self.conv4_3_down(conv4_3)
conv5_1_down = self.conv5_1_down(conv5_1)
conv5_2_down = self.conv5_2_down(conv5_2)
conv5_3_down = self.conv5_3_down(conv5_3)
so1_out = self.score_dsn1(conv1_1_down + conv1_2_down)
so2_out = self.score_dsn2(conv2_1_down + conv2_2_down)
so3_out = self.score_dsn3(conv3_1_down + conv3_2_down + conv3_3_down)
so4_out = self.score_dsn4(conv4_1_down + conv4_2_down + conv4_3_down)
so5_out = self.score_dsn5(conv5_1_down + conv5_2_down + conv5_3_down)
upsample2 = torch.nn.functional.conv_transpose2d(so2_out, self.weight_deconv2, stride=2)
upsample3 = torch.nn.functional.conv_transpose2d(so3_out, self.weight_deconv3, stride=4)
upsample4 = torch.nn.functional.conv_transpose2d(so4_out, self.weight_deconv4, stride=8)
upsample5 = torch.nn.functional.conv_transpose2d(so5_out, self.weight_deconv5, stride=8)
### center crop
so1 = crop(so1_out, img_H, img_W, 0 , 0)
so2 = crop(upsample2, img_H, img_W , 1, 1 )
so3 = crop(upsample3, img_H, img_W , 2, 2 )
so4 = crop(upsample4, img_H, img_W , 4, 4)
so5 = crop(upsample5, img_H, img_W , 0, 0)
fusecat = torch.cat((so1, so2, so3, so4, so5), dim=1)
fuse = self.score_final(fusecat)
results = [so1, so2, so3, so4, so5, fuse]
results = [torch.sigmoid(r) for r in results]
return results
# Based on BDCN Implementation @ https://github.com/pkuCactus/BDCN
def crop(data1, h, w , crop_h, crop_w):
_, _, h1, w1 = data1.size()
assert(h <= h1 and w <= w1)
data = data1[:, :, crop_h:crop_h+h, crop_w:crop_w+w]
return data
def make_bilinear_weights(size, num_channels):
factor = (size + 1) // 2
if size % 2 == 1:
center = factor - 1
else:
center = factor - 0.5
og = np.ogrid[:size, :size]
filt = (1 - abs(og[0] - center) / factor) * (1 - abs(og[1] - center) / factor)
# print(filt)
filt = torch.from_numpy(filt)
w = torch.zeros(num_channels, num_channels, size, size)
w.requires_grad = False
for i in range(num_channels):
for j in range(num_channels):
if i == j:
w[i, j] = filt
return w
def upsample(input, stride, num_channels=1):
kernel_size = stride * 2
kernel = make_bilinear_weights(kernel_size, num_channels).cuda()
return torch.nn.functional.conv_transpose2d(input, kernel, stride=stride)