-
Notifications
You must be signed in to change notification settings - Fork 2
/
hubconf.py
104 lines (73 loc) · 3.11 KB
/
hubconf.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
from typing import (
Any as _Any,
Dict as _Dict,
)
import torch
from bcos.experiments.utils import Experiment as _Experiment
dependencies = ["torch", "torchvision"]
BASE = "weights/bcos"
# map (base -> (model_name -> url))
URLS: _Dict[str, _Dict[str, str]] = {
"bcos_final": {
"resnet_50": f"{BASE}/resnet_50-f46c1a4159.pth",
},
}
def _get_model(
experiment_name: str,
pretrained: bool,
progress: bool,
base_network: str = "bcos_final",
dataset: str = "ImageNet",
**model_kwargs: _Any
) -> torch.nn.Module:
"""
Helper that loads the model and attaches its config and
transform to it as `config` and `transform` respectively.
"""
# load empty model
exp = _Experiment(dataset, base_network, experiment_name)
model = exp.get_model(**model_kwargs)
# attach stuff
model.config = exp.config
model.transform = model.config["data"]["test_transform"]
# load weights if needed
if pretrained:
url = URLS[base_network][experiment_name]
state_dict = torch.load(url)
model.load_state_dict(state_dict)
return model
# ------------------------------- [model entrypoints] -------------------------------------
# TODO: add more info
def resnet18(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-18"""
return _get_model("resnet_18", pretrained, progress, **kwargs)
def resnet34(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNe-34"""
return _get_model("resnet_34", pretrained, progress, **kwargs)
def resnet50(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-50"""
return _get_model("resnet_50", pretrained, progress, **kwargs)
def resnet101(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-101"""
return _get_model("resnet_101", pretrained, progress, **kwargs)
def resnet152(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNet-152"""
return _get_model("resnet_152", pretrained, progress, **kwargs)
def resnext50_32x4d(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos ResNeXt-50 32x4d"""
return _get_model("resnext50_32x4d", pretrained, progress, **kwargs)
def densenet121(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos DenseNet-121"""
return _get_model("densenet_121", pretrained, progress, **kwargs)
def densenet161(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos DenseNet-161"""
return _get_model("densenet_161", pretrained, progress, **kwargs)
def densenet169(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos DenseNet-169"""
return _get_model("densenet_169", pretrained, progress, **kwargs)
def densenet201(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos DenseNet-201"""
return _get_model("densenet_201", pretrained, progress, **kwargs)
def vgg11_bnu(pretrained: bool = False, progress: bool = True, **kwargs):
"""B-cos VGG-11 BNU (BN without centering)"""
return _get_model("vgg_11_bnu", pretrained, progress, **kwargs)