-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathhubconf.py
119 lines (100 loc) · 3.95 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import torch
_COTRACKER2_URL = (
"https://huggingface.co/facebook/cotracker/resolve/main/cotracker2.pth"
)
_COTRACKER2v1_URL = (
"https://huggingface.co/facebook/cotracker/resolve/main/cotracker2v1.pth"
)
_COTRACKER3_SCALED_OFFLINE_URL = (
"https://huggingface.co/facebook/cotracker3/resolve/main/scaled_offline.pth"
)
_COTRACKER3_SCALED_ONLINE_URL = (
"https://huggingface.co/facebook/cotracker3/resolve/main/scaled_online.pth"
)
def _make_cotracker_predictor(
*, pretrained: bool = True, online=False, version="3", **kwargs
):
if online:
from cotracker.predictor import CoTrackerOnlinePredictor
if version == "2":
predictor = CoTrackerOnlinePredictor(checkpoint=None, window_len=8, v2=True)
elif version == "2.1":
predictor = CoTrackerOnlinePredictor(
checkpoint=None, window_len=16, v2=True
)
elif version == "3":
predictor = CoTrackerOnlinePredictor(
checkpoint=None, window_len=16, v2=False
)
else:
from cotracker.predictor import CoTrackerPredictor
if version == "2":
predictor = CoTrackerPredictor(checkpoint=None, window_len=8, v2=True)
elif version == "2.1":
predictor = CoTrackerPredictor(checkpoint=None, window_len=16, v2=True)
elif version == "3":
predictor = CoTrackerPredictor(checkpoint=None, window_len=60, v2=False)
if pretrained:
if version == "2":
state_dict = torch.hub.load_state_dict_from_url(
_COTRACKER2_URL, map_location="cpu"
)
elif version == "2.1":
state_dict = torch.hub.load_state_dict_from_url(
_COTRACKER2v1_URL, map_location="cpu"
)
elif version == "3":
if online:
state_dict = torch.hub.load_state_dict_from_url(
_COTRACKER3_SCALED_ONLINE_URL, map_location="cpu"
)
else:
state_dict = torch.hub.load_state_dict_from_url(
_COTRACKER3_SCALED_OFFLINE_URL, map_location="cpu"
)
else:
raise Exception("Provided version does not exist")
predictor.model.load_state_dict(state_dict)
return predictor
def cotracker2(*, pretrained: bool = True, **kwargs):
"""
CoTracker2 with stride 4 and window length 8. Can track up to 265*265 points jointly.
"""
return _make_cotracker_predictor(pretrained=pretrained, online=False, version="2", **kwargs)
def cotracker2_online(*, pretrained: bool = True, **kwargs):
"""
Online CoTracker2 with stride 4 and window length 8. Can track up to 265*265 points jointly.
"""
return _make_cotracker_predictor(pretrained=pretrained, online=True, version="2", **kwargs)
def cotracker2v1(*, pretrained: bool = True, **kwargs):
"""
CoTracker2 with stride 4 and window length 16.
"""
return _make_cotracker_predictor(
pretrained=pretrained, online=False, version="2.1", **kwargs
)
def cotracker2v1_online(*, pretrained: bool = True, **kwargs):
"""
Online CoTracker2 with stride 4 and window length 16.
"""
return _make_cotracker_predictor(
pretrained=pretrained, online=True, version="2.1", **kwargs
)
def cotracker3_offline(*, pretrained: bool = True, **kwargs):
"""
Scaled offline CoTracker3 with stride 4 and window length 16.
"""
return _make_cotracker_predictor(
pretrained=pretrained, online=False, version="3", **kwargs
)
def cotracker3_online(*, pretrained: bool = True, **kwargs):
"""
Scaled online CoTracker3 with stride 4 and window length 16.
"""
return _make_cotracker_predictor(
pretrained=pretrained, online=True, version="3", **kwargs
)