-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
203 lines (171 loc) · 7.3 KB
/
classifier.py
File metadata and controls
203 lines (171 loc) · 7.3 KB
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
198
199
200
201
202
203
import torch.nn.functional as F
from torch import nn
class ResidualBlock(nn.Module):
def __init__(self, in_channel, out_channel, stride=1):
super().__init__()
self.left = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU(inplace=True),
nn.Conv2d(out_channel, out_channel, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(out_channel)
)
self.shortcut = nn.Sequential()
if stride != 1 or in_channel != out_channel:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_channel)
)
def forward(self, x):
out = self.left(x)
out += self.shortcut(x)
out = F.relu(out)
return out
class ConvBlock(nn.Module):
def __init__(self, in_channel, kernel_size, filters, stride):
super().__init__()
f1, f2, f3 = filters
self.layer = nn.Sequential(
nn.Conv2d(in_channel, f1, kernel_size=1, stride=stride, padding=0, bias=False),
nn.BatchNorm2d(f1),
nn.ReLU(),
nn.Conv2d(f1, f2, kernel_size=kernel_size, stride=1, padding=True, bias=False),
nn.BatchNorm2d(f2),
nn.ReLU(),
nn.Conv2d(f2, f3, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(f3),
nn.ReLU()
)
self.shortcut = nn.Conv2d(in_channel, f3, kernel_size=1, stride=stride, padding=0, bias=False)
self.batchnorm2d = nn.BatchNorm2d(f3)
self.relu = nn.ReLU()
def forward(self, x):
out = self.layer(x)
out += self.batchnorm2d(self.shortcut(x))
out = self.relu(out)
return out
class IndentityBlock(nn.Module):
def __init__(self, in_channel, kernel_size, filters):
super(IndentityBlock,self).__init__()
f1, f2, f3 = filters
self.layer = nn.Sequential(
nn.Conv2d(in_channel, f1 , kernel_size=1,stride=1, padding=0, bias=False),
nn.BatchNorm2d(f1),
nn.ReLU(),
nn.Conv2d(f1, f2, kernel_size=kernel_size, stride=1, padding=True, bias=False),
nn.BatchNorm2d(f2),
nn.ReLU(),
nn.Conv2d(f2, f3 ,kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(f3),
)
self.relu = nn.ReLU(True)
self.shorcut = nn.Sequential()
def forward(self, x):
out = self.layer(x)
out += self.shorcut(x)
out = self.relu(out)
del x
return out
class ResNet18(nn.Module):
def __init__(self, ResidualBlock, num_classes=10):
super().__init__()
self.in_channel = 64
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(),
) # 3*32*32->64*32*32
self.layer1 = self.make_layer(ResidualBlock, 64, 2, stride=1) # 64**32*32->64*32*32
self.layer2 = self.make_layer(ResidualBlock, 128, 2, stride=2) # 128*16*16-->128*16*16-
self.layer3 = self.make_layer(ResidualBlock, 256, 2, stride=2) # 256*8*8->256*8*8
self.layer4 = self.make_layer(ResidualBlock, 512, 2, stride=2) # 512*4*4->512*4*4
self.fc = nn.Linear(512, num_classes)
def make_layer(self, block, out_channels, num_blocks, stride):
strides = [stride] + [1] * (num_blocks - 1) #strides=[1,1]
layers = []
for stride in strides:
layers.append(block(self.in_channel, out_channels, stride))
self.in_channel = out_channels
return nn.Sequential(*layers)
def forward(self, x):
out = self.conv1(x)
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.fc(out)
return out
# planes == channels
class Bottleneck(nn.Module):
def __init__(self, in_planes, growth_rate):
super(Bottleneck, self).__init__()
self.bn1 = nn.BatchNorm2d(in_planes)
self.conv1 = nn.Conv2d(in_planes, 4*growth_rate, kernel_size=1, bias=False)
self.bn2 = nn.BatchNorm2d(4*growth_rate)
self.conv2 = nn.Conv2d(4*growth_rate, growth_rate, kernel_size=3, padding=1, bias=False)
def forward(self, x):
out = self.conv1(F.relu(self.bn1(x)))
out = self.conv2(F.relu(self.bn2(out)))
out = torch.cat([out,x], 1)
return out
class Transition(nn.Module):
def __init__(self, in_planes, out_planes):
super(Transition, self).__init__()
self.bn = nn.BatchNorm2d(in_planes)
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=1, bias=False)
def forward(self, x):
out = self.conv(F.relu(self.bn(x)))
out = F.avg_pool2d(out, 2)
return out
# block -> Bottleneck
class DenseNet(nn.Module):
def __init__(self, block, nblocks, growth_rate=12, reduction=0.5, num_classes=10):
super(DenseNet, self).__init__()
self.growth_rate = growth_rate
num_planes = 2*growth_rate
self.conv1 = nn.Conv2d(3, num_planes, kernel_size=3, padding=1, bias=False)
self.dense1 = self._make_dense_layers(block, num_planes, nblocks[0])
num_planes += nblocks[0]*growth_rate
out_planes = int(math.floor(num_planes*reduction))
self.trans1 = Transition(num_planes, out_planes)
num_planes = out_planes
self.dense2 = self._make_dense_layers(block, num_planes, nblocks[1])
num_planes += nblocks[1]*growth_rate
out_planes = int(math.floor(num_planes*reduction))
self.trans2 = Transition(num_planes, out_planes)
num_planes = out_planes
self.dense3 = self._make_dense_layers(block, num_planes, nblocks[2])
num_planes += nblocks[2]*growth_rate
out_planes = int(math.floor(num_planes*reduction))
self.trans3 = Transition(num_planes, out_planes)
num_planes = out_planes
self.dense4 = self._make_dense_layers(block, num_planes, nblocks[3])
num_planes += nblocks[3]*growth_rate
self.bn = nn.BatchNorm2d(num_planes)
self.linear = nn.Linear(num_planes, num_classes)
def _make_dense_layers(self, block, in_planes, nblock):
layers = []
for i in range(nblock):
layers.append(block(in_planes, self.growth_rate))
in_planes += self.growth_rate
return nn.Sequential(*layers)
def forward(self, x):
out = self.conv1(x)
out = self.trans1(self.dense1(out))
out = self.trans2(self.dense2(out))
out = self.trans3(self.dense3(out))
out = self.dense4(out)
out = F.avg_pool2d(F.relu(self.bn(out)), 4)
out = out.view(out.size(0), -1)
out = self.linear(out)
return out
def DenseNet121():
return DenseNet(Bottleneck, [6,12,24,16], growth_rate=32)
def DenseNet169():
return DenseNet(Bottleneck, [6,12,32,32], growth_rate=32)
def DenseNet201():
return DenseNet(Bottleneck, [6,12,48,32], growth_rate=32)
def DenseNet161():
return DenseNet(Bottleneck, [6,12,36,24], growth_rate=48)