Luisgust commited on
Commit
ea725cc
·
verified ·
1 Parent(s): 6bd676b

Create vtoonify/model/bisenet/resnet.py

Browse files
Files changed (1) hide show
  1. vtoonify/model/bisenet/resnet.py +111 -0
vtoonify/model/bisenet/resnet.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #!/usr/bin/python
3
+ # -*- encoding: utf-8 -*-
4
+
5
+ import torch
6
+ import torch.nn as nn
7
+ import torch.nn.functional as F
8
+ import torch.utils.model_zoo as modelzoo
9
+
10
+ # from modules.bn import InPlaceABNSync as BatchNorm2d
11
+
12
+ resnet18_url = 'https://download.pytorch.org/models/resnet18-5c106cde.pth'
13
+
14
+
15
+ def conv3x3(in_planes, out_planes, stride=1):
16
+ """3x3 convolution with padding"""
17
+ return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
18
+ padding=1, bias=False)
19
+
20
+
21
+ class BasicBlock(nn.Module):
22
+ def __init__(self, in_chan, out_chan, stride=1):
23
+ super(BasicBlock, self).__init__()
24
+ self.conv1 = conv3x3(in_chan, out_chan, stride)
25
+ self.bn1 = nn.BatchNorm2d(out_chan)
26
+ self.conv2 = conv3x3(out_chan, out_chan)
27
+ self.bn2 = nn.BatchNorm2d(out_chan)
28
+ self.relu = nn.ReLU(inplace=True)
29
+ self.downsample = None
30
+ if in_chan != out_chan or stride != 1:
31
+ self.downsample = nn.Sequential(
32
+ nn.Conv2d(in_chan, out_chan,
33
+ kernel_size=1, stride=stride, bias=False),
34
+ nn.BatchNorm2d(out_chan),
35
+ )
36
+
37
+ def forward(self, x):
38
+ residual = self.conv1(x)
39
+ residual = F.relu(self.bn1(residual))
40
+ residual = self.conv2(residual)
41
+ residual = self.bn2(residual)
42
+
43
+ shortcut = x
44
+ if self.downsample is not None:
45
+ shortcut = self.downsample(x)
46
+
47
+ out = shortcut + residual
48
+ out = self.relu(out)
49
+ return out
50
+
51
+
52
+ def create_layer_basic(in_chan, out_chan, bnum, stride=1):
53
+ layers = [BasicBlock(in_chan, out_chan, stride=stride)]
54
+ for i in range(bnum-1):
55
+ layers.append(BasicBlock(out_chan, out_chan, stride=1))
56
+ return nn.Sequential(*layers)
57
+
58
+
59
+ class Resnet18(nn.Module):
60
+ def __init__(self):
61
+ super(Resnet18, self).__init__()
62
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
63
+ bias=False)
64
+ self.bn1 = nn.BatchNorm2d(64)
65
+ self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
66
+ self.layer1 = create_layer_basic(64, 64, bnum=2, stride=1)
67
+ self.layer2 = create_layer_basic(64, 128, bnum=2, stride=2)
68
+ self.layer3 = create_layer_basic(128, 256, bnum=2, stride=2)
69
+ self.layer4 = create_layer_basic(256, 512, bnum=2, stride=2)
70
+ self.init_weight()
71
+
72
+ def forward(self, x):
73
+ x = self.conv1(x)
74
+ x = F.relu(self.bn1(x))
75
+ x = self.maxpool(x)
76
+
77
+ x = self.layer1(x)
78
+ feat8 = self.layer2(x) # 1/8
79
+ feat16 = self.layer3(feat8) # 1/16
80
+ feat32 = self.layer4(feat16) # 1/32
81
+ return feat8, feat16, feat32
82
+
83
+ def init_weight(self):
84
+ state_dict = modelzoo.load_url(resnet18_url)
85
+ self_state_dict = self.state_dict()
86
+ for k, v in state_dict.items():
87
+ if 'fc' in k: continue
88
+ self_state_dict.update({k: v})
89
+ self.load_state_dict(self_state_dict)
90
+
91
+ def get_params(self):
92
+ wd_params, nowd_params = [], []
93
+ for name, module in self.named_modules():
94
+ if isinstance(module, (nn.Linear, nn.Conv2d)):
95
+ wd_params.append(module.weight)
96
+ if not module.bias is None:
97
+ nowd_params.append(module.bias)
98
+ elif isinstance(module, nn.BatchNorm2d):
99
+ nowd_params += list(module.parameters())
100
+ return wd_params, nowd_params
101
+
102
+
103
+ if __name__ == "__main__":
104
+ net = Resnet18()
105
+ x = torch.randn(16, 3, 224, 224)
106
+ out = net(x)
107
+ print(out[0].size())
108
+ print(out[1].size())
109
+ print(out[2].size())
110
+ net.get_params()
111
+