-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval.py
More file actions
187 lines (154 loc) 路 5.12 KB
/
Copy patheval.py
File metadata and controls
187 lines (154 loc) 路 5.12 KB
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
import argparse
import time
import numpy as np
from PIL import Image
from tqdm import tqdm
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(
"--img_path",
type=str,
default="assets/sample.jpg",
required=False,
help="Path to the root of the MegaDepth dataset.",
)
parser.add_argument(
"--img_size",
nargs=2,
type=int,
default=[512, 512],
required=False,
help="Image size for inference. Please provide two integers (height width). Ensure that you have enough memory.",
)
# ONNXRuntime-specific args
parser.add_argument(
"--onnx_path",
type=str,
default=None,
required=False,
help="Path to ONNX model (end2end).",
)
# parser.add_argument(
# "--fp16",
# action="store_true",
# help="Whether to enable half-precision for ONNXRuntime.",
# )
parser.add_argument(
"--trt",
action="store_true",
help="Whether to use TensorRT Execution Provider.",
)
return parser.parse_args()
def create_models(framework: str, fp16=False, onnx_path=None, trt=False):
if framework == "torch":
device = torch.device("cuda")
model = DocShadow()
load_checkpoint(model, "sd7k", device)
model.eval().to(device)
elif framework == "ort":
if onnx_path is None:
onnx_path = (
f"weights/docshadow_sd7k"
f"{'_fp16' if fp16 and not trt else ''}"
".onnx"
)
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
sess_opts = ort.SessionOptions()
if trt:
providers.insert(
0,
(
"TensorrtExecutionProvider",
{
"trt_fp16_enable": fp16,
"trt_engine_cache_enable": True,
"trt_engine_cache_path": "weights/cache",
"trt_builder_optimization_level": 5,
},
),
)
model = ort.InferenceSession(
onnx_path, sess_options=sess_opts, providers=providers
)
return model
def get_inputs(framework: str, img_path, img_size, fp16, trt):
img = Image.open(img_path).convert("RGB")
H, W = img_size
img = img.resize((W, H))
if framework == "torch":
image = to_tensor(img)[None].cuda()
elif framework == "ort":
image = DocShadowRunner.preprocess(np.array(img))
if fp16 and not trt:
image = image.astype(np.float16)
return image
def measure_inference(framework: str, model, images, fp16) -> float:
if framework == "torch":
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
with torch.inference_mode():
result = model(images)
end.record()
torch.cuda.synchronize()
return start.elapsed_time(end)
elif framework == "ort":
model_inputs = {"image": images}
model_outputs = ["result"]
# Prepare IO-Bindings
binding = model.io_binding()
for name, arr in model_inputs.items():
binding.bind_cpu_input(name, arr)
for name in model_outputs:
binding.bind_output(name, "cuda")
# Measure only matching time
start = time.perf_counter()
result = model.run_with_iobinding(binding)
end = time.perf_counter()
return (end - start) * 1000
def evaluate(
framework: str,
img_path="assets/sample.jpg",
img_size=[512, 512],
fp16=False,
onnx_path=None,
trt=False,
):
model = create_models(
framework,
fp16=fp16,
onnx_path=onnx_path,
trt=trt,
)
# Warmup
for _ in tqdm(range(5)):
images = get_inputs(framework, img_path, img_size=img_size, fp16=fp16, trt=trt)
_ = measure_inference(framework, model, images, fp16=fp16)
# Measure
timings = []
for _ in tqdm(range(1000)):
images = get_inputs(framework, img_path, img_size=img_size, fp16=fp16, trt=trt)
inference_time = measure_inference(framework, model, images, fp16=fp16)
timings.append(inference_time)
# Results
timings = np.array(timings)
print(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()
if args.framework == "torch":
import torch
from torchvision.transforms.functional import to_tensor
from DocShadow.models import DocShadow
from DocShadow.utils import load_checkpoint
elif args.framework == "ort":
import onnxruntime as ort
from onnx_runner import DocShadowRunner
evaluate(**vars(args))