-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathp_neuro.py
222 lines (174 loc) · 6.79 KB
/
p_neuro.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from torch import nn, ones
from torchvision import models
from torch.nn.init import kaiming_normal
from torch import np
import torch
import torch.nn.functional as F
## Custom baseline
class Net(nn.Module):
def __init__(self, input_size=(3,224,224), nb_classes=17):
super(Net, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3,32,3),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32,64,3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d((3,3))
)
## Compute linear layer size
self.flat_feats = self._get_flat_feats(input_size, self.features)
self.classifier = nn.Sequential(
nn.Linear(self.flat_feats, 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Dropout(p=0.15),
nn.Linear(256, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Dropout(p=0.10),
nn.Linear(64, nb_classes)
)
## Weights initialization
def _weights_init(m):
if isinstance(m, nn.Conv2d or nn.Linear):
kaiming_normal(m.weight)
elif isinstance(m, nn.BatchNorm2d or BatchNorm1d):
m.weight.data.fill_(1)
m.bias.data.zero_()
self.apply(_weights_init)
def _get_flat_feats(self, in_size, feats):
f = feats(Variable(ones(1,*in_size)))
return int(np.prod(f.size()[1:]))
def forward(self, x):
feats = self.features(x)
flat_feats = feats.view(-1, self.flat_feats)
out = self.classifier(flat_feats)
return out
## ResNet fine-tuning
class ResNet50(nn.Module):
## We use ResNet weights from PyCaffe.
def __init__(self, num_classes):
super(ResNet50, self).__init__()
# Loading ResNet arch from PyTorch and weights from Pycaffe
original_model = models.resnet50(pretrained=False)
original_model.load_state_dict(torch.load('./zoo/resnet50.pth'))
# Everything except the last linear layer
self.features = nn.Sequential(*list(original_model.children())[:-1])
# Get number of features of last layer
num_feats = original_model.fc.in_features
# Plug our classifier
self.classifier = nn.Sequential(
nn.Linear(num_feats, num_classes)
)
# Init of last layer
for m in self.classifier:
kaiming_normal(m.weight)
# Freeze those weights
# for p in self.features.parameters():
# p.requires_grad = False
def forward(self, x):
f = self.features(x)
f = f.view(f.size(0), -1)
y = self.classifier(f)
return y
class ResNet101(nn.Module):
## We use ResNet weights from PyCaffe.
def __init__(self, num_classes):
super(ResNet101, self).__init__()
# Loading ResNet arch from PyTorch and weights from Pycaffe
original_model = models.resnet101(pretrained=False)
original_model.load_state_dict(torch.load('./zoo/resnet101.pth'))
# Everything except the last linear layer
self.features = nn.Sequential(*list(original_model.children())[:-1])
# Get number of features of last layer
num_feats = original_model.fc.in_features
# Plug our classifier
self.classifier = nn.Sequential(
nn.Linear(num_feats, num_classes)
)
# Init of last layer
for m in self.classifier:
kaiming_normal(m.weight)
# Freeze those weights
# for p in self.features.parameters():
# p.requires_grad = False
def forward(self, x):
f = self.features(x)
f = f.view(f.size(0), -1)
y = self.classifier(f)
return y
class ResNet152(nn.Module):
## We use ResNet weights from PyCaffe.
def __init__(self, num_classes):
super(ResNet152, self).__init__()
# Loading ResNet arch from PyTorch and weights from Pycaffe
original_model = models.resnet152(pretrained=False)
original_model.load_state_dict(torch.load('./zoo/resnet152.pth'))
# Everything except the last linear layer
self.features = nn.Sequential(*list(original_model.children())[:-1])
# Get number of features of last layer
num_feats = original_model.fc.in_features
# Plug our classifier
self.classifier = nn.Sequential(
nn.Linear(num_feats, num_classes)
)
# Init of last layer
for m in self.classifier:
kaiming_normal(m.weight)
# Freeze those weights
# for p in self.features.parameters():
# p.requires_grad = False
def forward(self, x):
f = self.features(x)
f = f.view(f.size(0), -1)
y = self.classifier(f)
return y
## VGG fine-tuning
class VGG16(nn.Module):
def __init__(self, nb_classes=17):
super(VGG16, self).__init__()
original_model = models.vgg16(pretrained=False)
self.features = original_model.features
self.classifier = nn.Sequential(
nn.Dropout(),
nn.Linear(25088, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes),
)
# Freeze Convolutional weights
for p in self.features.parameters():
p.requires_grad = False
def forward(self, x):
f = self.features(x)
f = f.view(f.size(0), -1)
y = self.classifier(f)
return y
class DenseNet121(nn.Module):
def __init__(self, num_classes):
super(DenseNet121, self).__init__()
original_model = models.densenet121(pretrained=True)
# Everything except the last linear layer
self.features = nn.Sequential(*list(original_model.children())[:-1])
# Get number of features of last layer
num_feats = original_model.classifier.in_features
# Plug our classifier
self.classifier = nn.Sequential(
nn.Linear(num_feats, num_classes)
)
# Init of last layer
for m in self.classifier:
kaiming_normal(m.weight)
# Freeze weights
# for p in self.features.parameters():
# p.requires_grad = False
def forward(self, x):
f = self.features(x)
out = F.relu(f, inplace=True)
out = F.avg_pool2d(out, kernel_size=7).view(f.size(0), -1)
out = self.classifier(out)
return out