-
Notifications
You must be signed in to change notification settings - Fork 4
/
depth_model.py
210 lines (167 loc) · 5.99 KB
/
depth_model.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
"""
Build DPT depth model
- modified from https://github.com/isl-org/DPT
"""
import os
import torch
import cv2
import argparse
import util.io
from torchvision.transforms import Compose
from dpt.models import DPTDepthModel
from dpt.transforms import Resize, NormalizeImage, PrepareForNet
class DepthModel(object):
"""
Build DPT network and compute depth maps
"""
def __init__(self, model_type="dpt_hybrid", optimize=True):
"""
Build MonoDepthNN to compute depth maps.
Arguments:
model_path (str): path to saved model
"""
default_models = {
"dpt_large": "weights/dpt_large-midas-2f21e586.pt",
"dpt_hybrid": "weights/dpt_hybrid-midas-501f0c75.pt",
"dpt_hybrid_kitti": "weights/dpt_hybrid_kitti-cb926ef4.pt",
}
model_path = default_models[model_type]
self.model_type = model_type
self.optimize = optimize
# select device
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("device: ", self.device)
# load network
if model_type == "dpt_large": # DPT-Large
net_w = net_h = 384
model = DPTDepthModel(
path=model_path,
backbone="vitl16_384",
non_negative=True,
enable_attention_hooks=False,
)
normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_hybrid": # DPT-Hybrid
net_w = net_h = 384
model = DPTDepthModel(
path=model_path,
backbone="vitb_rn50_384",
non_negative=True,
enable_attention_hooks=False,
)
normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
elif model_type == "dpt_hybrid_kitti":
net_w = 1216
net_h = 352
model = DPTDepthModel(
path=model_path,
scale=0.00006016,
shift=0.00579,
invert=True,
backbone="vitb_rn50_384",
non_negative=True,
enable_attention_hooks=False,
)
normalization = NormalizeImage(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
else:
assert (
False
), f"model_type '{model_type}' not implemented, use: --model_type [dpt_large|dpt_hybrid|dpt_hybrid_kitti|dpt_hybrid_nyu|midas_v21]"
self.transform = Compose(
[
Resize(
net_w,
net_h,
resize_target=None,
keep_aspect_ratio=True,
ensure_multiple_of=32,
resize_method="minimal",
image_interpolation_method=cv2.INTER_CUBIC,
),
normalization,
PrepareForNet(),
]
)
model.eval()
if optimize and self.device == torch.device("cuda"):
model = model.to(memory_format=torch.channels_last)
model = model.half()
self.model = model.to(self.device)
@torch.no_grad()
def compute_depth(self, img, kitti_crop=False):
"""
Computes depth map
Arguments:
img (array): image (0-255)
"""
if img.ndim == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255.0
if kitti_crop is True:
height, width, _ = img.shape
top = height - 352
left = (width - 1216) // 2
img = img[top : top + 352, left : left + 1216, :]
img_input = self.transform({"image": img})["image"]
# with torch.no_grad():
sample = torch.from_numpy(img_input).to(self.device).unsqueeze(0)
if self.optimize and self.device == torch.device("cuda"):
sample = sample.to(memory_format=torch.channels_last)
sample = sample.half()
prediction = self.model.forward(sample)
prediction = (
torch.nn.functional.interpolate(
prediction.unsqueeze(1),
size=img.shape[:2],
mode="bicubic",
align_corners=False,
)
.squeeze()
.cpu()
.numpy()
)
# if self.model_type == "dpt_hybrid_kitti":
# prediction *= 256
return prediction
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-m", "--model_weights", default=None, help="path to model weights"
)
parser.add_argument(
"-t",
"--model_type",
default="dpt_hybrid",
help="model type [dpt_large|dpt_hybrid|midas_v21]",
)
parser.add_argument("--optimize", dest="optimize", action="store_true")
parser.set_defaults(optimize=True)
args = parser.parse_args()
# default_models = {
# "midas_v21": "weights/midas_v21-f6b98070.pt",
# "dpt_large": "weights/dpt_large-midas-2f21e586.pt",
# "dpt_hybrid": "weights/dpt_hybrid-midas-501f0c75.pt",
# "dpt_hybrid_kitti": "weights/dpt_hybrid_kitti-cb926ef4.pt",
# "dpt_hybrid_nyu": "weights/dpt_hybrid_nyu-2ce69ec7.pt",
# }
#
# if args.model_weights is None:
# args.model_weights = default_models[args.model_type]
# set torch options
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
# build model
model = DepthModel(
args.model_type,
args.optimize,
)
# print(model)
# read img
img_path = r"dataset\sequences_jpg\00\image_0\000000.jpg"
img = cv2.imread(img_path)
# compute depth
depth = model.compute_depth(img, kitti_crop=False)
filename = os.path.join(
"temp", os.path.splitext(os.path.basename(img_path))[0]
)
util.io.write_depth(filename.replace(".jpg", "_depth.jpg"), depth, bits=2, absolute_depth=True)