Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/transformers/models/qwen2_5_vl/modular_qwen2_5_vl.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ class Qwen2_5_VLProcessorKwargs(ProcessingKwargs, total=False):
"padding": False,
"return_mm_token_type_ids": False,
},
"videos_kwargs": {"return_metadata": True},
}


Expand Down Expand Up @@ -922,10 +923,17 @@ def __call__(
image_grid_thw = image_inputs["image_grid_thw"]

if videos is not None:
fps = output_kwargs["videos_kwargs"].get("fps", 2.0)
videos_inputs = self.video_processor(videos=videos, **output_kwargs["videos_kwargs"])
video_grid_thw = videos_inputs["video_grid_thw"]

# Get video metadata
if not kwargs.get("return_metadata"):
video_metadata = videos_inputs.pop("video_metadata")
else:
video_metadata = videos_inputs["video_metadata"]

fps = [metadata.sampled_fps for metadata in video_metadata]

if isinstance(fps, (int, float)):
second_per_grid_ts = [self.video_processor.temporal_patch_size / fps] * len(video_grid_thw)
elif hasattr(fps, "__len__") and len(fps) == len(video_grid_thw):
Expand Down
10 changes: 9 additions & 1 deletion src/transformers/models/qwen2_5_vl/processing_qwen2_5_vl.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Qwen2_5_VLProcessorKwargs(ProcessingKwargs, total=False):
"padding": False,
"return_mm_token_type_ids": False,
},
"videos_kwargs": {"return_metadata": True},
}


Expand Down Expand Up @@ -129,10 +130,17 @@ def __call__(
image_grid_thw = image_inputs["image_grid_thw"]

if videos is not None:
fps = output_kwargs["videos_kwargs"].get("fps", 2.0)
videos_inputs = self.video_processor(videos=videos, **output_kwargs["videos_kwargs"])
video_grid_thw = videos_inputs["video_grid_thw"]

# Get video metadata
if not kwargs.get("return_metadata"):
video_metadata = videos_inputs.pop("video_metadata")
else:
video_metadata = videos_inputs["video_metadata"]

fps = [metadata.sampled_fps for metadata in video_metadata]

if isinstance(fps, (int, float)):
second_per_grid_ts = [self.video_processor.temporal_patch_size / fps] * len(video_grid_thw)
elif hasattr(fps, "__len__") and len(fps) == len(video_grid_thw):
Expand Down
20 changes: 14 additions & 6 deletions src/transformers/video_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ def timestamps(self) -> list[float]:
raise ValueError("Cannot infer video `timestamps` when `fps` or `frames_indices` is None.")
return [frame_idx / self.fps for frame_idx in self.frames_indices]

@property
def sampled_fps(self) -> float:
"FPS of the sampled video."
if self.frames_indices is None or self.total_num_frames is None or self.fps is None:
return self.fps or 24
return len(self.frames_indices) / self.total_num_frames * self.fps

def update(self, dictionary):
for key, value in dictionary.items():
if hasattr(self, key):
Expand Down Expand Up @@ -372,8 +379,8 @@ def sample_indices_fn(metadata, **kwargs):
height=int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)),
width=int(video.get(cv2.CAP_PROP_FRAME_WIDTH)),
)
indices = sample_indices_fn(metadata=metadata, **kwargs)

indices = sample_indices_fn(metadata=metadata, **kwargs)
index = 0
frames = []
while video.isOpened():
Expand Down Expand Up @@ -486,8 +493,8 @@ def sample_indices_fn(metadata, **kwargs):
height=container.streams.video[0].height,
width=container.streams.video[0].width,
)
indices = sample_indices_fn(metadata=metadata, **kwargs)

indices = sample_indices_fn(metadata=metadata, **kwargs)
frames = []
container.seek(0)
end_index = indices[-1]
Expand Down Expand Up @@ -548,7 +555,6 @@ def sample_indices_fn(metadata, **kwargs):
)

indices = sample_indices_fn(metadata=metadata, **kwargs)

video = video[indices].contiguous()
metadata.update(
{
Expand Down Expand Up @@ -596,16 +602,18 @@ def sample_indices_fn(metadata, **kwargs):
num_ffmpeg_threads=0,
device=kwargs.get("device", "cpu"),
)
total_num_frames = decoder.metadata.num_frames
video_fps = decoder.metadata.average_fps
metadata = VideoMetadata(
total_num_frames=decoder.metadata.num_frames,
fps=decoder.metadata.average_fps,
total_num_frames=total_num_frames,
fps=video_fps,
duration=decoder.metadata.duration_seconds,
video_backend="torchcodec",
height=decoder.metadata.height,
width=decoder.metadata.width,
)
indices = sample_indices_fn(metadata=metadata, **kwargs)

indices = sample_indices_fn(metadata=metadata, **kwargs)
video = decoder.get_frames_at(indices=indices).data.contiguous()
metadata.frames_indices = indices
return video, metadata
Expand Down