|
| 1 | +import argparse |
1 | 2 | from pathlib import Path |
2 | 3 | from time import perf_counter_ns |
3 | 4 |
|
@@ -45,51 +46,76 @@ def report_stats(times, num_frames, unit="ms"): |
45 | 46 | return med, fps |
46 | 47 |
|
47 | 48 |
|
48 | | -def sample(sampler, **kwargs): |
49 | | - decoder = VideoDecoder(VIDEO_PATH) |
| 49 | +def sample(decoder, sampler, **kwargs): |
50 | 50 | return sampler( |
51 | 51 | decoder, |
52 | 52 | num_frames_per_clip=10, |
53 | 53 | **kwargs, |
54 | 54 | ) |
55 | 55 |
|
56 | 56 |
|
57 | | -VIDEO_PATH = Path(__file__).parent / "../../test/resources/nasa_13013.mp4" |
58 | | -NUM_EXP = 30 |
59 | | - |
60 | | -for num_clips in (1, 50): |
61 | | - print("-" * 10) |
62 | | - print(f"{num_clips = }") |
63 | | - |
64 | | - print("clips_at_random_indices ", end="") |
65 | | - times, num_frames = bench( |
66 | | - sample, clips_at_random_indices, num_clips=num_clips, num_exp=NUM_EXP, warmup=2 |
67 | | - ) |
68 | | - report_stats(times, num_frames, unit="ms") |
69 | | - |
70 | | - print("clips_at_regular_indices ", end="") |
71 | | - times, num_frames = bench( |
72 | | - sample, clips_at_regular_indices, num_clips=num_clips, num_exp=NUM_EXP, warmup=2 |
73 | | - ) |
74 | | - report_stats(times, num_frames, unit="ms") |
75 | | - |
76 | | - print("clips_at_random_timestamps ", end="") |
77 | | - times, num_frames = bench( |
78 | | - sample, |
79 | | - clips_at_random_timestamps, |
80 | | - num_clips=num_clips, |
81 | | - num_exp=NUM_EXP, |
82 | | - warmup=2, |
83 | | - ) |
84 | | - report_stats(times, num_frames, unit="ms") |
85 | | - |
86 | | - print("clips_at_regular_timestamps ", end="") |
87 | | - seconds_between_clip_starts = 13 / num_clips # approximate. video is 13s long |
88 | | - times, num_frames = bench( |
89 | | - sample, |
90 | | - clips_at_regular_timestamps, |
91 | | - seconds_between_clip_starts=seconds_between_clip_starts, |
92 | | - num_exp=NUM_EXP, |
93 | | - warmup=2, |
94 | | - ) |
95 | | - report_stats(times, num_frames, unit="ms") |
| 57 | +def run_sampler_benchmarks(device, video): |
| 58 | + NUM_EXP = 30 |
| 59 | + |
| 60 | + for num_clips in (1, 50): |
| 61 | + print("-" * 10) |
| 62 | + print(f"{num_clips = }") |
| 63 | + |
| 64 | + print("clips_at_random_indices ", end="") |
| 65 | + decoder = VideoDecoder(video, device=device) |
| 66 | + times, num_frames = bench( |
| 67 | + sample, |
| 68 | + decoder, |
| 69 | + clips_at_random_indices, |
| 70 | + num_clips=num_clips, |
| 71 | + num_exp=NUM_EXP, |
| 72 | + warmup=2, |
| 73 | + ) |
| 74 | + report_stats(times, num_frames, unit="ms") |
| 75 | + |
| 76 | + print("clips_at_regular_indices ", end="") |
| 77 | + times, num_frames = bench( |
| 78 | + sample, |
| 79 | + decoder, |
| 80 | + clips_at_regular_indices, |
| 81 | + num_clips=num_clips, |
| 82 | + num_exp=NUM_EXP, |
| 83 | + warmup=2, |
| 84 | + ) |
| 85 | + report_stats(times, num_frames, unit="ms") |
| 86 | + |
| 87 | + print("clips_at_random_timestamps ", end="") |
| 88 | + times, num_frames = bench( |
| 89 | + sample, |
| 90 | + decoder, |
| 91 | + clips_at_random_timestamps, |
| 92 | + num_clips=num_clips, |
| 93 | + num_exp=NUM_EXP, |
| 94 | + warmup=2, |
| 95 | + ) |
| 96 | + report_stats(times, num_frames, unit="ms") |
| 97 | + |
| 98 | + print("clips_at_regular_timestamps ", end="") |
| 99 | + seconds_between_clip_starts = 13 / num_clips # approximate. video is 13s long |
| 100 | + times, num_frames = bench( |
| 101 | + sample, |
| 102 | + decoder, |
| 103 | + clips_at_regular_timestamps, |
| 104 | + seconds_between_clip_starts=seconds_between_clip_starts, |
| 105 | + num_exp=NUM_EXP, |
| 106 | + warmup=2, |
| 107 | + ) |
| 108 | + report_stats(times, num_frames, unit="ms") |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + DEFAULT_VIDEO_PATH = Path(__file__).parent / "../../test/resources/nasa_13013.mp4" |
| 113 | + parser = argparse.ArgumentParser() |
| 114 | + parser.add_argument("--device", type=str, default="cpu") |
| 115 | + parser.add_argument("--video", type=str, default=str(DEFAULT_VIDEO_PATH)) |
| 116 | + args = parser.parse_args() |
| 117 | + run_sampler_benchmarks(args.device, args.video) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
0 commit comments