forked from rwightman/posenet-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
50 lines (37 loc) · 1.49 KB
/
benchmark.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
import torch
import time
import argparse
import os
import posenet
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=int, default=101)
parser.add_argument('--image_dir', type=str, default='./images')
parser.add_argument('--num_images', type=int, default=1000)
args = parser.parse_args()
def main():
with torch.no_grad():
model = posenet.load_model(args.model)
model = model.cuda()
output_stride = model.output_stride
num_images = args.num_images
filenames = [
f.path for f in os.scandir(args.image_dir) if f.is_file() and f.path.endswith(('.png', '.jpg'))]
if len(filenames) > num_images:
filenames = filenames[:num_images]
images = {f: posenet.read_imgfile(f, 1.0, output_stride)[0] for f in filenames}
start = time.time()
for i in range(num_images):
input_image = torch.Tensor(images[filenames[i % len(filenames)]]).cuda()
results = model(input_image)
heatmaps, offsets, displacement_fwd, displacement_bwd = results
output = posenet.decode_multiple_poses(
heatmaps.squeeze(0),
offsets.squeeze(0),
displacement_fwd.squeeze(0),
displacement_bwd.squeeze(0),
output_stride=output_stride,
max_pose_detections=10,
min_pose_score=0.25)
print('Average FPS:', num_images / (time.time() - start))
if __name__ == "__main__":
main()