-
Notifications
You must be signed in to change notification settings - Fork 11
/
backbone.py
57 lines (42 loc) · 1.58 KB
/
backbone.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
import torch
from torchvision import models
from torch.hub import load_state_dict_from_url
from torchvision.models.resnet import Bottleneck, model_urls
__all__ = ['ResNet', 'resnet50', 'resnet101']
class ResNet(models.ResNet):
"""ResNets without fully connected layer"""
def __init__(self, *args, **kwargs):
super(ResNet, self).__init__(*args, **kwargs)
self._out_features = self.fc.in_features
del self.fc
def forward(self, x):
""""""
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = x.view(-1, self._out_features)
return x
@property
def out_features(self) -> int:
"""The dimension of output features"""
return self._out_features
def _resnet(arch, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)
if pretrained:
state_dict = load_state_dict_from_url(model_urls[arch],
progress=progress)
model.load_state_dict(state_dict, strict=False)
return model
def resnet50(pretrained=False, progress=True, **kwargs):
return _resnet('resnet50', Bottleneck, [3, 4, 6, 3], pretrained, progress,
**kwargs)
def resnet101(pretrained=False, progress=True, **kwargs):
return _resnet('resnet101', Bottleneck, [3, 4, 23, 3], pretrained, progress,
**kwargs)