Skip to content

Commit 738501a

Browse files
author
Daniel Flores
committed
replace sampling with accuracy test
1 parent 0e01169 commit 738501a

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

examples/decoding/custom_frame_mappings.py

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -124,56 +124,54 @@ def bench(f, file_like=False, average_over=50, warmup=2, **f_kwargs):
124124
bench(VideoDecoder, source=video_path, stream_index=stream_index, seek_mode="exact")
125125

126126
# %%
127-
# Decode frames from multiple videos with custom_frame_mappings vs exact seek_mode
127+
# Decode frames with custom_frame_mappings vs exact seek_mode
128128

129129

130-
def decode_frames_from_n_videos(video_path, seek_mode = "exact", custom_frame_mappings = None, num_videos = 10):
131-
for _ in range(num_videos):
132-
decoder = VideoDecoder(
133-
source=video_path,
134-
seek_mode=seek_mode,
135-
custom_frame_mappings=custom_frame_mappings
136-
)
137-
decoder.get_frames_in_range(start=0, stop=10)
130+
def decode_frames(video_path, seek_mode = "exact", custom_frame_mappings = None):
131+
decoder = VideoDecoder(
132+
source=video_path,
133+
seek_mode=seek_mode,
134+
custom_frame_mappings=custom_frame_mappings
135+
)
136+
decoder.get_frames_in_range(start=0, stop=10)
138137

139138

140139
for video_path, json_path in ((short_video_path, short_json_path), (long_video_path, long_json_path)):
141140
print(f"Running benchmarks on {Path(video_path).name}")
142141
print("Decoding frames with custom_frame_mappings JSON str from file:")
143142
with open(json_path, "r") as f:
144-
bench(decode_frames_from_n_videos, video_path=video_path, custom_frame_mappings=(f.read()))
143+
bench(decode_frames, video_path=video_path, custom_frame_mappings=(f.read()))
145144

146145
print("Decoding frames with seek_mode='exact':")
147-
bench(decode_frames_from_n_videos, video_path=video_path, seek_mode="exact")
146+
bench(decode_frames, video_path=video_path, seek_mode="exact")
148147

149148
# %%
150-
# Compare performance of sampling clips from multiple videos with custom_frame_mappings vs exact seek_mode
151-
152-
153-
from torchcodec import samplers
154-
155-
156-
def sample_clips_from_n_videos(video_path, seek_mode = "exact", custom_frame_mappings = None, num_videos = 10):
157-
for _ in range(num_videos):
158-
return samplers.clips_at_random_indices(
159-
decoder=VideoDecoder(
160-
source=video_path,
161-
seek_mode=seek_mode,
162-
custom_frame_mappings=custom_frame_mappings
163-
),
164-
num_clips=5,
165-
num_frames_per_clip=2,
166-
)
167-
168-
169-
for video_path, json_path in ((short_video_path, short_json_path), (long_video_path, long_json_path)):
170-
print(f"Running benchmarks on {Path(video_path).name}")
171-
print("Sampling clips with custom_frame_mappings:")
172-
with open(json_path, "r") as f:
173-
mappings = f.read()
174-
bench(sample_clips_from_n_videos, file_like=False, video_path=video_path, custom_frame_mappings=mappings)
175-
176-
print("Sampling clips with seek_mode='exact':")
177-
bench(sample_clips_from_n_videos, video_path=video_path, seek_mode="exact")
149+
# Compare frame accuracy with custom_frame_mappings vs exact seek_mode
150+
video_path = short_video_path
151+
json_path = short_json_path
152+
with open(json_path, "r") as f:
153+
custom_frame_mappings = f.read()
154+
custom_frame_mappings_decoder = VideoDecoder(
155+
source=video_path,
156+
custom_frame_mappings=custom_frame_mappings
157+
)
158+
159+
exact_decoder = VideoDecoder(short_video_path, seek_mode="exact")
160+
approx_decoder = VideoDecoder(short_video_path, seek_mode="approximate")
161+
162+
print("Metadata of short video with custom_frame_mappings:")
163+
print(custom_frame_mappings_decoder.metadata)
164+
print("Metadata of short video with seek_mode='exact':")
165+
print(exact_decoder.metadata)
166+
print("Metadata of short video with seek_mode='approximate':")
167+
print(approx_decoder.metadata)
168+
169+
for i in range(len(approx_decoder)):
170+
torch.testing.assert_close(
171+
approx_decoder.get_frame_at(i).data,
172+
custom_frame_mappings_decoder.get_frame_at(i).data,
173+
atol=0, rtol=0,
174+
)
175+
print("Frame seeking is the same for this video!")
178176

179177
# %%

0 commit comments

Comments
 (0)