-
Notifications
You must be signed in to change notification settings - Fork 26
/
models.py
91 lines (72 loc) · 3.06 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
# Copyright 2017-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import torch.utils.model_zoo as model_zoo
import torchvision.models.resnet as resnet
import torch
from task2vec import ProbeNetwork
_MODELS = {}
def _add_model(model_fn):
_MODELS[model_fn.__name__] = model_fn
return model_fn
class ResNet(resnet.ResNet, ProbeNetwork):
def __init__(self, block, layers, num_classes=1000):
super(ResNet, self).__init__(block, layers, num_classes)
# Saves the ordered list of layers. We need this to forward from an arbitrary intermediate layer.
self.layers = [
self.conv1, self.bn1, self.relu,
self.maxpool, self.layer1, self.layer2,
self.layer3, self.layer4, self.avgpool,
lambda z: torch.flatten(z, 1), self.fc
]
@property
def classifier(self):
return self.fc
# @ProbeNetwork.classifier.setter
# def classifier(self, val):
# self.fc = val
# Modified forward method that allows to start feeding the cached activations from an intermediate
# layer of the network
def forward(self, x, start_from=0):
"""Replaces the default forward so that we can forward features starting from any intermediate layer."""
for layer in self.layers[start_from:]:
x = layer(x)
return x
@_add_model
def resnet18(pretrained=False, num_classes=1000):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model: ProbeNetwork = ResNet(resnet.BasicBlock, [2, 2, 2, 2], num_classes=num_classes)
if pretrained:
state_dict = model_zoo.load_url(resnet.model_urls['resnet18'])
state_dict = {k: v for k, v in state_dict.items() if 'fc' not in k}
model.load_state_dict(state_dict, strict=False)
return model
@_add_model
def resnet34(pretrained=False, num_classes=1000):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(resnet.BasicBlock, [3, 4, 6, 3], num_classes=num_classes)
if pretrained:
state_dict = model_zoo.load_url(resnet.model_urls['resnet34'])
state_dict = {k: v for k, v in state_dict.items() if 'fc' not in k}
model.load_state_dict(state_dict, strict=False)
return model
def get_model(model_name, pretrained=False, num_classes=1000):
try:
return _MODELS[model_name](pretrained=pretrained, num_classes=num_classes)
except KeyError:
raise ValueError(f"Architecture {model_name} not implemented.")