-
Notifications
You must be signed in to change notification settings - Fork 10
/
hubconf.py
51 lines (41 loc) · 1.4 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
dependencies = ["torch"]
import torch
from midas.midas_net import MidasNet
def MiDaS(pretrained=True, **kwargs):
""" # This docstring shows up in hub.help()
MiDaS model for monocular depth estimation
pretrained (bool): load pretrained weights into model
"""
model = MidasNet()
if pretrained:
checkpoint = (
"https://github.com/intel-isl/MiDaS/releases/download/v2/model-f46da743.pt"
)
state_dict = torch.hub.load_state_dict_from_url(
checkpoint, progress=True, check_hash=True
)
model.load_state_dict(state_dict)
return model
def transforms():
import cv2
from torchvision.transforms import Compose
from midas.transforms import Resize, NormalizeImage, PrepareForNet
from midas import transforms
transforms.default_transform = Compose(
[
lambda img: {"image": img / 255.0},
Resize(
384,
384,
resize_target=None,
keep_aspect_ratio=True,
ensure_multiple_of=32,
resize_method="upper_bound",
image_interpolation_method=cv2.INTER_CUBIC,
),
NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
PrepareForNet(),
lambda sample: torch.from_numpy(sample["image"]).unsqueeze(0),
]
)
return transforms