-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet.py
161 lines (118 loc) · 5.29 KB
/
resnet.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
import torch.nn as nn
__all__ = ['resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152']
class ResNet(nn.Module):
def __init__(self, block, num_layer, num_classes=2, in_dim=1, base_dim=64):
super().__init__()
modules = [ConvBN(in_dim, base_dim, kernel_size=7, padding=3, stride=2)]
modules += [nn.MaxPool2d(kernel_size=3, stride=2, padding=1)]
if block is Basic:
modules += [Basic(base_dim, base_dim)
for _ in range(num_layer[0])]
modules += [block(base_dim, base_dim*2, down=True)]
modules += [block(base_dim*2, base_dim*2)
for _ in range(num_layer[1]-1)]
modules += [block(base_dim*2, base_dim*4, down=True)]
modules += [block(base_dim*4, base_dim*4)
for _ in range(num_layer[2]-1)]
modules += [block(base_dim*4, base_dim*8, down=True)]
modules += [block(base_dim*8, base_dim*8)
for _ in range(num_layer[3]-1)]
last_features = base_dim*8
elif block is Bottleneck:
modules += [block(base_dim, base_dim, base_dim*4)]
modules += [block(base_dim*4, base_dim, base_dim*4)
for _ in range(num_layer[0]-1)]
modules += [block(base_dim*4, base_dim*2, base_dim*8, down=True)]
modules += [block(base_dim*8, base_dim*2, base_dim*8)
for _ in range(num_layer[1]-1)]
modules += [block(base_dim*8, base_dim*4, base_dim*16, down=True)]
modules += [block(base_dim*16, base_dim*4, base_dim*16)
for _ in range(num_layer[2]-1)]
modules += [block(base_dim*16, base_dim*8, base_dim*32, down=True)]
modules += [block(base_dim*32, base_dim*8, base_dim*32)
for _ in range(num_layer[3]-1)]
last_features = base_dim*32
self.layer = nn.Sequential(*modules)
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc_layer = nn.Linear(last_features, num_classes)
# Initialization
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.orthogonal_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.orthogonal_(m.weight)
nn.init.constant_(m.bias, 0)
def forward(self, x):
batch_size = x.size(0)
out = self.layer(x)
out = self.avg_pool(out)
out = out.view(batch_size, -1)
out = self.fc_layer(out)
return out
class Basic(nn.Module):
def __init__(self, in_dim, out_dim, down=False):
super().__init__()
stride = 2 if down else 1
self.down = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_dim),
) if down else None
self.layer = nn.Sequential(
ConvBN(in_dim, out_dim, kernel_size=3, padding=1, stride=stride),
nn.Conv2d(out_dim, out_dim, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_dim))
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = self.layer(x)
if self.down:
x = self.down(x)
return self.relu(out + x)
class Bottleneck(nn.Module):
def __init__(self, in_dim, mid_dim, out_dim, down=False):
super().__init__()
stride = 2 if down else 1
self.down = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_dim),
) if down else None
self.dim_equalizer = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_dim),
) if in_dim != out_dim and not down else None
self.layer = nn.Sequential(
ConvBN(in_dim, mid_dim, kernel_size=1, stride=stride),
ConvBN(mid_dim, mid_dim, kernel_size=3, padding=1),
nn.Conv2d(mid_dim, out_dim, kernel_size=1, bias=False),
nn.BatchNorm2d(out_dim))
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = self.layer(x)
if self.down:
x = self.down(x)
if self.dim_equalizer:
x = self.dim_equalizer(x)
return self.relu(out + x)
class ConvBN(nn.Module):
def __init__(self, in_dim, out_dim, **kwargs):
super().__init__()
self.layer = nn.Sequential(
nn.Conv2d(in_dim, out_dim, bias=False, **kwargs),
nn.BatchNorm2d(out_dim),
nn.ReLU(inplace=True))
def forward(self, x):
return self.layer(x)
def resnet18(**kwargs):
return ResNet(Basic, num_layer=[2,2,2,2], **kwargs)
def resnet34(**kwargs):
return ResNet(Basic, num_layer=[3,4,6,3], **kwargs)
def resnet50(**kwargs):
return ResNet(Bottleneck, num_layer=[3,4,6,3], **kwargs)
def resnet101(**kwargs):
return ResNet(Bottleneck, num_layer=[3,4,23,3], **kwargs)
def resnet152(**kwargs):
return ResNet(Bottleneck, num_layer=[3,8,36,3], **kwargs)