-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhubconf.py
305 lines (266 loc) · 8.37 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import tarfile
from multiprocessing import cpu_count
from pathlib import Path
from typing import Literal, Tuple
import torch
import yaml
from guided_mvs_lib import __version__ as CURR_VERS
from guided_mvs_lib import models
dependencies = ["torch", "torchvision"]
## entry points for each single model
CURR_DIR = Path(__file__).parent
def _get_archive() -> str:
if not (CURR_DIR / "trained_models.tar.gz").exists():
torch.hub.download_url_to_file(
f"https://github.com/andreaconti/multi-view-guided-multi-view-stereo/releases/download/v{CURR_VERS}/trained_models.tar.gz",
str(CURR_DIR / "trained_models.tar.gz"),
)
return str(CURR_DIR / "trained_models.tar.gz")
def _load_model(
tarpath: str,
model: Literal["ucsnet", "d2hc_rmvsnet", "mvsnet", "patchmatchnet", "cas_mvsnet"] = "mvsnet",
pretrained: bool = True,
dataset: Literal["blended_mvg", "dtu_yao_blended_mvg"] = "blended_mvg",
hints: Literal["mvguided_filtered", "not_guided", "guided", "mvguided"] = "not_guided",
hints_density: float = 0.03,
):
"""
Utility function to load from the tarfile containing all the pretrained models the one choosen
"""
assert model in [
"ucsnet",
"d2hc_rmvsnet",
"mvsnet",
"patchmatchnet",
"cas_mvsnet",
]
assert dataset in ["blended_mvg", "dtu_yao_blended_mvg"]
assert hints in ["mvguided_filtered", "not_guided", "guided", "mvguided"]
# model instance
model_net = models.__dict__[model].SimpleInterfaceNet()
model_net.train_params = None
# find the correct checkpoint
if pretrained:
with tarfile.open(tarpath) as archive:
info = yaml.safe_load(archive.extractfile("trained_models/info.yaml"))
for ckpt_id, meta in info.items():
found = meta["model"] == model and meta["hints"] == hints
if hints != "not_guided":
found = found and float(meta["hints_density"]) == hints_density
if dataset == "blended_mvg":
found = found and meta["dataset"] == dataset
else:
found = (
found
and meta["dataset"] == "dtu_yao"
and "load_weights" in meta
and info[meta["load_weights"]]["dataset"] == "blended_mvg"
)
if found:
break
if not found:
raise ValueError("Model not available with the provided parameters")
model_net.load_state_dict(
{
".".join(n.split(".")[1:]): v
for n, v in torch.load(archive.extractfile(f"trained_models/{ckpt_id}.ckpt"))[
"state_dict"
].items()
}
)
model_net.train_params = meta
return model_net
def mvsnet(
pretrained: bool = True,
dataset: Literal["blended_mvg", "dtu_yao_blended_mvg"] = "blended_mvg",
hints: Literal["mvguided_filtered", "not_guided", "guided", "mvguided"] = "not_guided",
hints_density: float = 0.03,
):
"""
pretrained `MVSNet`_ network.
.. _MVSNet https://arxiv.org/pdf/1804.02505.pdf
"""
return _load_model(
_get_archive(),
"mvsnet",
pretrained=pretrained,
dataset=dataset,
hints=hints,
hints_density=hints_density,
)
def ucsnet(
pretrained: bool = True,
dataset: Literal["blended_mvg", "dtu_yao_blended_mvg"] = "blended_mvg",
hints: Literal["mvguided_filtered", "not_guided", "guided", "mvguided"] = "not_guided",
hints_density: float = 0.03,
):
"""
pretrained `UCSNet`_ network.
.. _UCSNet https://arxiv.org/pdf/1911.12012.pdf
"""
return _load_model(
_get_archive(),
"ucsnet",
pretrained=pretrained,
dataset=dataset,
hints=hints,
hints_density=hints_density,
)
def d2hc_rmvsnet(
pretrained: bool = True,
dataset: Literal["blended_mvg", "dtu_yao_blended_mvg"] = "blended_mvg",
hints: Literal["mvguided_filtered", "not_guided", "guided", "mvguided"] = "not_guided",
hints_density: float = 0.03,
):
"""
pretrained `D2HCRMVSNet`_ network.
.. _D2HCRMVSNet https://www.ecva.net/papers/eccv_2020/papers_ECCV/papers/123490647.pdf
"""
return _load_model(
_get_archive(),
"d2hc_rmvsnet",
pretrained=pretrained,
dataset=dataset,
hints=hints,
hints_density=hints_density,
)
def patchmatchnet(
pretrained: bool = True,
dataset: Literal["blended_mvg", "dtu_yao_blended_mvg"] = "blended_mvg",
hints: Literal["mvguided_filtered", "not_guided", "guided", "mvguided"] = "not_guided",
hints_density: float = 0.03,
):
"""
pretrained `PatchMatchNet`_ network.
.. _PatchMatchNet https://github.com/FangjinhuaWang/PatchmatchNet
"""
return _load_model(
_get_archive(),
"patchmatchnet",
pretrained=pretrained,
dataset=dataset,
hints=hints,
hints_density=hints_density,
)
def cas_mvsnet(
pretrained: bool = True,
dataset: Literal["blended_mvg", "dtu_yao_blended_mvg"] = "blended_mvg",
hints: Literal["mvguided_filtered", "not_guided", "guided", "mvguided"] = "not_guided",
hints_density: float = 0.03,
):
"""
pretrained `CASMVSNet`_ network.
.. _CASMVSNet https://arxiv.org/pdf/1912.06378.pdf
"""
return _load_model(
_get_archive(),
"cas_mvsnet",
pretrained=pretrained,
dataset=dataset,
hints=hints,
hints_density=hints_density,
)
## Datasets
def _load_dataset(
dataset: str,
root: str,
batch_size: int = 1,
nviews: int = 5,
ndepths: int = 128,
hints: str = "mvguided_filtered",
hints_density: float = 0.03,
filtering_window: Tuple[int, int] = (9, 9),
num_workers: int = cpu_count() // 2,
):
from guided_mvs_lib.datasets import MVSDataModule
from guided_mvs_lib.datasets.sample_preprocess import MVSSampleTransform
dm = MVSDataModule(
dataset,
batch_size=batch_size,
num_workers=num_workers,
datapath=root,
nviews=nviews,
ndepths=ndepths,
robust_train=False,
transform=MVSSampleTransform(
generate_hints=hints,
hints_perc=hints_density,
filtering_window=filtering_window,
),
)
return dm
def blended_mvs(
root: str,
batch_size: int = 1,
nviews: int = 5,
ndepths: int = 128,
hints: str = "mvguided_filtered",
hints_density: float = 0.03,
filtering_window: Tuple[int, int] = (9, 9),
num_workers: int = cpu_count() // 2,
):
"""
Utility function to load a Pytorch Lightning DataModule loading
the BlendedMVS dataset
"""
return _load_dataset(
"blended_mvs",
root=root,
batch_size=batch_size,
nviews=nviews,
ndepths=ndepths,
hints=hints,
hints_density=hints_density,
filtering_window=filtering_window,
num_workers=num_workers,
)
def blended_mvg(
root: str,
batch_size: int = 1,
nviews: int = 5,
ndepths: int = 128,
hints: str = "mvguided_filtered",
hints_density: float = 0.03,
filtering_window: Tuple[int, int] = (9, 9),
num_workers: int = cpu_count() // 2,
):
"""
Utility function to load a Pytorch Lightning DataModule loading
the BlendedMVG dataset
"""
return _load_dataset(
"blended_mvg",
root=root,
batch_size=batch_size,
nviews=nviews,
ndepths=ndepths,
hints=hints,
hints_density=hints_density,
filtering_window=filtering_window,
num_workers=num_workers,
)
def dtu(
root: str,
batch_size: int = 1,
nviews: int = 5,
ndepths: int = 128,
hints: str = "mvguided_filtered",
hints_density: float = 0.03,
filtering_window: Tuple[int, int] = (9, 9),
num_workers: int = 4, # (pretty memory aggressive)
):
"""
Utility function to load a Pytorch Lightning DataModule loading
the DTU dataset
"""
return _load_dataset(
"dtu_yao",
root=root,
batch_size=batch_size,
nviews=nviews,
ndepths=ndepths,
hints=hints,
hints_density=hints_density,
filtering_window=filtering_window,
num_workers=num_workers,
)