-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathStudentModels.py
69 lines (60 loc) · 2.29 KB
/
StudentModels.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
import torch
import torch.nn as nn
import torchvision.models as models
def load_model(arch='resnet18',
pretrained=True):
if arch.startswith('resnet') :
model = models.__dict__[arch](pretrained=pretrained)
elif arch.startswith('densenet'):
model = models.__dict__[arch](pretrained=True)
else :
raise("Finetuning not supported on this architecture yet")
return model
class FineTuneModel(nn.Module):
def __init__(self, original_model, arch, num_classes):
super(FineTuneModel, self).__init__()
self.num_classes = num_classes
if arch.startswith('resnet') :
# Everything except the last linear layer
self.features = nn.Sequential(
*list(original_model.children())[:-3],
nn.AvgPool2d(7, stride=1),
)
self.classifier = nn.Sequential(
nn.Linear(256, num_classes)
)
self.modelName = 'resnet'
self.mean = (0.485, 0.456, 0.406)
self.std = (0.229, 0.224, 0.225)
elif arch.startswith('densenet161'):
self.features = nn.Sequential(
*list(original_model.features.children())[:-3],
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=7, stride=1)
)
self.classifier = nn.Sequential(
nn.Linear(2112, num_classes)
)
self.modelName = 'densenet'
self.mean = (0.485, 0.456, 0.406)
self.std = (0.229, 0.224, 0.225)
else :
raise("Finetuning not supported on this architecture yet")
def freeze(self):
print('Features frozen')
# Freeze those weights
for p in self.features.parameters():
p.requires_grad = False
def unfreeze(self):
print('Features unfrozen')
# Freeze those weights
for p in self.features.parameters():
p.requires_grad = True
def forward(self, x):
f = self.features(x)
if self.modelName == 'resnet' :
f = f.view(f.size(0), -1)
elif self.modelName == 'densenet' :
f = f.view(f.size(0), -1)
y = self.classifier(f)
return y