forked from perrying/ssn-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
67 lines (52 loc) · 1.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
import torch
import torch.nn as nn
from lib.ssn.ssn import ssn_iter, sparse_ssn_iter
def conv_bn_relu(in_c, out_c):
return nn.Sequential(
nn.Conv2d(in_c, out_c, 3, padding=1, bias=False),
nn.BatchNorm2d(out_c),
nn.ReLU(True)
)
class SSNModel(nn.Module):
def __init__(self, feature_dim, nspix, n_iter=10):
super().__init__()
self.nspix = nspix
self.n_iter = n_iter
self.scale1 = nn.Sequential(
conv_bn_relu(5, 64),
conv_bn_relu(64, 64)
)
self.scale2 = nn.Sequential(
nn.MaxPool2d(3, 2, padding=1),
conv_bn_relu(64, 64),
conv_bn_relu(64, 64)
)
self.scale3 = nn.Sequential(
nn.MaxPool2d(3, 2, padding=1),
conv_bn_relu(64, 64),
conv_bn_relu(64, 64)
)
self.output_conv = nn.Sequential(
nn.Conv2d(64*3+5, feature_dim-5, 3, padding=1),
nn.ReLU(True)
)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal_(m.weight, 0, 0.001)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, x):
pixel_f = self.feature_extract(x)
if self.training:
return ssn_iter(pixel_f, self.nspix, self.n_iter)
else:
return sparse_ssn_iter(pixel_f, self.nspix, self.n_iter)
def feature_extract(self, x):
s1 = self.scale1(x)
s2 = self.scale2(s1)
s3 = self.scale3(s2)
s2 = nn.functional.interpolate(s2, size=s1.shape[-2:], mode="bilinear", align_corners=False)
s3 = nn.functional.interpolate(s3, size=s1.shape[-2:], mode="bilinear", align_corners=False)
cat_feat = torch.cat([x, s1, s2, s3], 1)
feat = self.output_conv(cat_feat)
return torch.cat([feat, x], 1)