-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed.py
62 lines (43 loc) · 1.62 KB
/
speed.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
import time
import argparse
import torch
from thop import profile, clever_format
from lib.config import Config
def parse_args():
parser = argparse.ArgumentParser(description="Tool to measure a model's speed")
parser.add_argument("--cfg", default="config.yaml", help="Config file")
parser.add_argument("--model_path", help="Model checkpoint path (optional)")
parser.add_argument('--iters', default=100, type=int, help="Number of times to run the model and get the average")
return parser.parse_args()
# torch.backends.cudnn.benchmark = True
def main():
args = parse_args()
cfg = Config(args.cfg)
device = torch.device('cuda')
model = cfg.get_model()
model = model.to(device)
test_parameters = cfg.get_test_parameters()
height, width = cfg['datasets']['test']['parameters']['img_size']
if args.model_path is not None:
model.load_state_dict(torch.load(args.model_path)['model'])
model.eval()
x = torch.zeros((1, 3, height, width)).to(device) + 1
# Benchmark MACs and params
macs, params = profile(model, inputs=(x,))
macs, params = clever_format([macs, params], "%.3f")
print('MACs: {}'.format(macs))
print('Params: {}'.format(params))
# GPU warmup
for _ in range(100):
model(x)
# Benchmark latency and FPS
t_all = 0
for _ in range(args.iters):
t1 = time.time()
model(x, **test_parameters)
t2 = time.time()
t_all += t2 - t1
print('Average latency (ms): {:.2f}'.format(t_all * 1000 / args.iters))
print('Average FPS: {:.2f}'.format(args.iters / t_all))
if __name__ == '__main__':
main()