/
depth_inference.py
53 lines (39 loc) · 1.45 KB
/
depth_inference.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
import os
import urllib.request
import cv2
import matplotlib.pyplot as plt
import torch
class MiDaS:
def __init__(self, use_large_model):
os.makedirs("depth_model", exist_ok=True)
torch.hub.set_dir("depth_model")
if use_large_model:
self.midas = torch.hub.load(
"intel-isl/MiDaS", "MiDaS", _use_new_zipfile_serialization=False
)
else:
self.midas = torch.hub.load(
"intel-isl/MiDaS", "MiDaS_small", _use_new_zipfile_serialization=False
)
self.device = (
torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
)
self.midas.to(self.device)
self.midas.eval()
midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")
if use_large_model:
self.transform = midas_transforms.default_transform
else:
self.transform = midas_transforms.small_transform
def inference(self, img):
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
input_batch = self.transform(img).to(self.device)
with torch.no_grad():
prediction = self.midas(input_batch)
prediction = torch.nn.functional.interpolate(
prediction.unsqueeze(1),
size=img.shape[:2],
mode="bicubic",
align_corners=False,
).squeeze()
return prediction.detach().cpu().numpy()