-
Notifications
You must be signed in to change notification settings - Fork 26
/
eval.py
175 lines (139 loc) · 4.99 KB
/
eval.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
import argparse
import time
from pathlib import Path
import numpy as np
import onnxruntime as ort
import torch
from tqdm import tqdm
from depth_anything.dpt import DPT_DINOv2
from depth_anything.util.transform import load_image
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"framework",
type=str,
choices=["torch", "ort"],
help="The framework to measure inference time. Options are 'torch' for PyTorch and 'ort' for ONNXRuntime.",
)
parser.add_argument(
"--megadepth_path",
type=Path,
default=Path("megadepth_test_1500"),
required=False,
help="Path to the root of the MegaDepth dataset.",
)
# PyTorch-specific args
parser.add_argument(
"--model",
type=str,
choices=["s", "b", "l"],
required=False,
help="Model size variant. Available options: 's', 'b', 'l'.",
)
# ONNXRuntime-specific args
parser.add_argument(
"--onnx_path",
type=str,
default=None,
required=False,
help="Path to ONNX model.",
)
return parser.parse_args()
def get_megadepth_images(path: Path):
sort_key = lambda p: int(p.stem.split("_")[0])
images = sorted(
list((path / "Undistorted_SfM/0015/images").glob("*.jpg")), key=sort_key
) + sorted(list((path / "Undistorted_SfM/0022/images").glob("*.jpg")), key=sort_key)
return images
def create_models(framework: str, model=None, onnx_path=None):
if framework == "torch":
device = torch.device("cuda")
assert model is not None, "Model size variant must be specified."
if model == "s":
depth_anything = DPT_DINOv2(
encoder="vits", features=64, out_channels=[48, 96, 192, 384]
)
elif model == "b":
depth_anything = DPT_DINOv2(
encoder="vitb", features=128, out_channels=[96, 192, 384, 768]
)
else: # model == "l"
depth_anything = DPT_DINOv2(
encoder="vitl", features=256, out_channels=[256, 512, 1024, 1024]
)
depth_anything.to(device).load_state_dict(
torch.hub.load_state_dict_from_url(
f"https://huggingface.co/spaces/LiheYoung/Depth-Anything/resolve/main/checkpoints/depth_anything_vit{model}14.pth",
map_location="cpu",
),
strict=True,
)
depth_anything.eval()
elif framework == "ort":
sess_opts = ort.SessionOptions()
# sess_opts.intra_op_num_threads = 1
# sess_opts.enable_profiling = True
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
assert onnx_path is not None, "ONNX model path must be specified."
depth_anything = ort.InferenceSession(
onnx_path,
sess_options=sess_opts,
providers=providers,
)
return depth_anything
def measure_inference(framework: str, depth_anything, image) -> float:
if framework == "torch":
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
with torch.no_grad():
result = depth_anything(image)
end.record()
torch.cuda.synchronize()
return start.elapsed_time(end)
elif framework == "ort":
inputs = {"image": image}
outputs = ["depth"]
# Prepare IO-Bindings
binding = depth_anything.io_binding()
for name, arr in inputs.items():
binding.bind_cpu_input(name, arr)
for name in outputs:
binding.bind_output(name, "cuda")
# Measure only matching time
start = time.perf_counter()
result = depth_anything.run_with_iobinding(binding)
end = time.perf_counter()
return (end - start) * 1000
def evaluate(
framework, megadepth_path=Path("megadepth_test_1500"), model=None, onnx_path=None
):
images = get_megadepth_images(megadepth_path)
depth_anything = create_models(
framework=framework, model=model, onnx_path=onnx_path
)
# Warmup
for img in images[:10]:
image, _ = load_image(str(img))
if framework == "torch":
image = torch.from_numpy(image).cuda()
elif framework == "ort":
pass
_ = measure_inference(framework, depth_anything, image)
# Measure
timings = []
for img in tqdm(images[10:]):
image, _ = load_image(str(img))
if framework == "torch":
image = torch.from_numpy(image).cuda()
elif framework == "ort":
pass
inference_time = measure_inference(framework, depth_anything, image)
timings.append(inference_time)
# Results
timings = np.array(timings)
print(f"Mean inference time: {timings.mean():.2f} +/- {timings.std():.2f} ms")
print(f"Median inference time: {np.median(timings):.2f} ms")
if __name__ == "__main__":
args = parse_args()
evaluate(**vars(args))