-
Notifications
You must be signed in to change notification settings - Fork 1
/
hubconf.py
82 lines (71 loc) · 2.43 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
dependencies = [
'torch',
'torchvision',
'pytorch_lightning',
]
import torch
from models import helper
DEFAULT_AGG_CONFIG = {
'in_channels': 768,
'in_h': 16,
'in_w': 16,
'out_channels': 1024,
'mix_depth': 2,
'mlp_ratio': 1,
'out_rows': 4
}
class VPRModel(torch.nn.Module):
"""
VPR Model with a backbone and an aggregator.
Args:
backbone_arch (str): Architecture of the backbone.
pretrained (bool): Whether to use a pretrained backbone.
layer1 (int): Layer index for backbone.
use_cls (bool): Whether to use classification token.
norm_descs (bool): Whether to normalize descriptors.
agg_arch (str): Architecture of the aggregator (e.g., DinoMixVPR).
agg_config (dict): Configuration for the aggregator.
"""
def __init__(self,
# ---- Backbone
backbone_arch='dinov2_vitb14',
pretrained=True,
layer1=7,
use_cls=False,
norm_descs=True,
# ---- Aggregator
agg_arch='DinoMixVPR',
agg_config={},
):
super().__init__()
self.backbone_arch = backbone_arch
self.pretrained = pretrained
self.layer1 = layer1
self.use_cls = use_cls
self.norm_descs = norm_descs
self.agg_arch = agg_arch
self.agg_config = agg_config
self.backbone = helper.get_backbone(self.backbone_arch, self.pretrained, layer1=self.layer1, use_cls=self.use_cls, norm_descs=self.norm_descs)
self.aggregator = helper.get_aggregator(self.agg_arch, self.agg_config)
def forward(self, x):
x = self.backbone(x)
x = self.aggregator(x)
return x
def dino_mix(pretrained=True, **kwargs):
model = VPRModel(
backbone_arch='dinov2_vitb14',
pretrained=pretrained,
layer1=7,
use_cls=False,
norm_descs=True,
agg_arch='DinoMixVPR',
agg_config=DEFAULT_AGG_CONFIG,
**kwargs
)
if pretrained:
checkpoint_url = "https://github.com/GaoShuang98/DINO-Mix/releases/download/v1.0.0/dinov2_vitb14_mix.ckpt"
state_dict = torch.hub.load_state_dict_from_url(checkpoint_url, progress=True)
model.load_state_dict(state_dict['state_dict'])
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
return model