-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
197 lines (156 loc) · 6.84 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
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
"""
https://arxiv.org/abs/1611.05431
official code:
https://github.com/facebookresearch/ResNeXt
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict
from torch.autograd import Variable
"""
NOTICE:
BasicBlock_B is not implemented
BasicBlock_C is recommendation
The full architecture consist of BasicBlock_A is not implemented.
"""
class ResBottleBlock(nn.Module):
def __init__(self, in_planes, bottleneck_width=4, stride=1, expansion=1):
super(ResBottleBlock, self).__init__()
self.conv0=nn.Conv2d(in_planes,bottleneck_width,1,stride=1,bias=False)
self.bn0 = nn.BatchNorm2d(bottleneck_width)
self.conv1=nn.Conv2d(bottleneck_width,bottleneck_width,3,stride=stride,padding=1,bias=False)
self.bn1=nn.BatchNorm2d(bottleneck_width)
self.conv2=nn.Conv2d(bottleneck_width,expansion*in_planes,1,bias=False)
self.bn2=nn.BatchNorm2d(expansion*in_planes)
self.shortcut=nn.Sequential()
if stride!=1 or expansion!=1:
self.shortcut=nn.Sequential(
nn.Conv2d(in_planes,in_planes*expansion,1,stride=stride,bias=False)
)
def forward(self, x):
out = F.relu(self.bn0(self.conv0(x)))
out = F.relu(self.bn1(self.conv1(out)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class BasicBlock_A(nn.Module):
def __init__(self, in_planes, num_paths=32, bottleneck_width=4, expansion=1, stride=1):
super(BasicBlock_A,self).__init__()
self.num_paths = num_paths
for i in range(num_paths):
setattr(self,'path'+str(i),self._make_path(in_planes,bottleneck_width,stride,expansion))
# self.paths=self._make_path(in_planes,bottleneck_width,stride,expansion)
self.conv0=nn.Conv2d(in_planes*expansion,expansion*in_planes,1,stride=1,bias=False)
self.bn0 = nn.BatchNorm2d(in_planes * expansion)
self.shortcut = nn.Sequential()
if stride != 1 or expansion != 1:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, in_planes * expansion, 1, stride=stride, bias=False)
)
def forward(self, x):
out = self.path0(x)
for i in range(1,self.num_paths):
if hasattr(self,'path'+str(i)):
out+getattr(self,'path'+str(i))(x)
# out+=self.paths(x)
# getattr
# out = torch.sum(out, dim=1)
out = self.bn0(out)
out += self.shortcut(x)
out = F.relu(out)
return out
def _make_path(self, in_planes, bottleneck_width, stride, expansion):
layers = []
layers.append(ResBottleBlock(
in_planes, bottleneck_width, stride, expansion))
return nn.Sequential(*layers)
class BasicBlock_C(nn.Module):
"""
increasing cardinality is a more effective way of
gaining accuracy than going deeper or wider
"""
def __init__(self, in_planes, bottleneck_width=4, cardinality=32, stride=1, expansion=2):
super(BasicBlock_C, self).__init__()
inner_width = cardinality * bottleneck_width
self.expansion = expansion
self.basic = nn.Sequential(OrderedDict(
[
('conv1_0', nn.Conv2d(in_planes, inner_width, 1, stride=1, bias=False)),
('bn1', nn.BatchNorm2d(inner_width)),
('act0', nn.ReLU()),
('conv3_0', nn.Conv2d(inner_width, inner_width, 3, stride=stride, padding=1, groups=cardinality, bias=False)),
('bn2', nn.BatchNorm2d(inner_width)),
('act1', nn.ReLU()),
('conv1_1', nn.Conv2d(inner_width, inner_width * self.expansion, 1, stride=1, bias=False)),
('bn3', nn.BatchNorm2d(inner_width * self.expansion))
]
))
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != inner_width * self.expansion:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, inner_width * self.expansion, 1, stride=stride, bias=False)
)
self.bn0 = nn.BatchNorm2d(self.expansion * inner_width)
def forward(self, x):
out = self.basic(x)
out += self.shortcut(x)
out = F.relu(self.bn0(out))
return out
class ResNeXt(nn.Module):
def __init__(self, num_blocks, cardinality, bottleneck_width, expansion=2, num_classes=10):
super(ResNeXt, self).__init__()
self.cardinality = cardinality
self.bottleneck_width = bottleneck_width
self.in_planes = 64
self.expansion = expansion
self.conv0 = nn.Conv2d(3, self.in_planes, kernel_size=3, stride=1, padding=1)
self.bn0 = nn.BatchNorm2d(self.in_planes)
self.pool0 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1=self._make_layer(num_blocks[0],1)
self.layer2=self._make_layer(num_blocks[1],2)
self.layer3=self._make_layer(num_blocks[2],2)
self.layer4=self._make_layer(num_blocks[3],2)
self.linear = nn.Linear(self.cardinality * self.bottleneck_width, num_classes)
def forward(self, x):
out = F.relu(self.bn0(self.conv0(x)))
# out = self.pool0(out)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = F.avg_pool2d(out, 4)
out = out.view(out.size(0), -1)
out = self.linear(out)
return out
def _make_layer(self, num_blocks, stride):
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(BasicBlock_C(self.in_planes, self.bottleneck_width, self.cardinality, stride, self.expansion))
self.in_planes = self.expansion * self.bottleneck_width * self.cardinality
self.bottleneck_width *= 2
return nn.Sequential(*layers)
def resnext26_2x64d():
return ResNeXt(num_blocks=[2, 2, 2, 2], cardinality=2, bottleneck_width=64)
def resnext26_4x32d():
return ResNeXt(num_blocks=[2, 2, 2, 2], cardinality=4, bottleneck_width=32)
def resnext26_8x16d():
return ResNeXt(num_blocks=[2, 2, 2, 2], cardinality=8, bottleneck_width=16)
def resnext26_16x8d():
return ResNeXt(num_blocks=[2, 2, 2, 2], cardinality=16, bottleneck_width=8)
def resnext26_32x4d():
return ResNeXt(num_blocks=[2, 2, 2, 2], cardinality=32, bottleneck_width=4)
def resnext26_64x2d():
return ResNeXt(num_blocks=[2, 2, 2, 2], cardinality=32, bottleneck_width=4)
def resnext50_2x64d():
return ResNeXt(num_blocks=[3, 4, 6, 3], cardinality=2, bottleneck_width=64)
def resnext50_32x4d():
return ResNeXt(num_blocks=[3, 4, 6, 3], cardinality=32, bottleneck_width=4)
# def test():
# net = resnext50_2x64d()
# # print(net)
# data = Variable(torch.rand(1, 3, 32, 32))
# output = net(data)
# print(output.size())