-
Notifications
You must be signed in to change notification settings - Fork 27.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chat template: update for processor #35953
Open
zucchini-nlp
wants to merge
8
commits into
huggingface:main
Choose a base branch
from
zucchini-nlp:chat-templates-vlms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+966
−111
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
20b13d8
update
zucchini-nlp 9826614
we need batched nested input to always process correctly
zucchini-nlp 8b97ea0
Merge branch 'main' into chat-templates-vlms
zucchini-nlp 4ae7dab
Merge branch 'main' into chat-templates-vlms
zucchini-nlp 7eb23fa
update a bit
zucchini-nlp aa08f53
fix copies
zucchini-nlp d05f2e8
Merge branch 'main' into chat-templates-vlms
zucchini-nlp 627ac03
Merge branch 'main' into chat-templates-vlms
zucchini-nlp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -530,21 +530,33 @@ def get_uniform_frame_indices(total_num_frames: int, num_frames: Optional[int] = | |
return indices | ||
|
||
|
||
def read_video_opencv(video_path: str, num_frames: Optional[int] = None): | ||
def read_video_opencv(video_path: str, num_frames: Optional[int] = None, fps: Optional[int] = None): | ||
""" | ||
Decode the video with open-cv decoder. | ||
|
||
Args: | ||
video_path (`str`): | ||
Path to the video file. | ||
num_frames (`int`, *optional*): | ||
Number of frames to sample uniformly. If not specified, all frames are sampled. | ||
Number of frames to sample uniformly. Should be passed only when `fps=None`. | ||
If not specified and `fps==None`, all frames are sampled. | ||
fps (`int`, *optional*): | ||
Number of frames to sample per second. Should be passed only when `num_frames=None`. | ||
If not specified and `num_frames==None`, all frames are sampled. | ||
|
||
Returns: | ||
np.ndarray: np array of decoded frames of shape (num_frames, height, width, 3). | ||
""" | ||
video = cv2.VideoCapture(video_path) | ||
total_num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) | ||
video_fps = video.get(cv2.CAP_PROP_FPS) | ||
if num_frames is None and fps is not None: | ||
num_frames = int(total_num_frames / video_fps * fps) | ||
if num_frames > total_num_frames: | ||
raise ValueError( | ||
f"When loading the video with fps={fps}, we identified that num_frames ({num_frames}) > total_frames ({total_num_frames}) ." | ||
f"Make sure that fps of a video is less than the requested fps for loading. Detected video_fps={video_fps}" | ||
) | ||
indices = get_uniform_frame_indices(total_num_frames, num_frames=num_frames) | ||
|
||
index = 0 | ||
|
@@ -563,42 +575,66 @@ def read_video_opencv(video_path: str, num_frames: Optional[int] = None): | |
return np.stack(frames) | ||
|
||
|
||
def read_video_decord(video_path: str, num_frames: Optional[int] = None): | ||
def read_video_decord(video_path: str, num_frames: Optional[int] = None, fps: Optional[int] = None): | ||
""" | ||
Decode the video with Decord decoder. | ||
|
||
Args: | ||
video_path (`str`): | ||
Path to the video file. | ||
num_frames (`int`, *optional*): | ||
Number of frames to sample uniformly. If not specified, all frames are sampled. | ||
Number of frames to sample uniformly. Should be passed only when `fps=None`. | ||
If not specified and `fps==None`, all frames are sampled. | ||
fps (`int`, *optional*): | ||
Number of frames to sample per second. Should be passed only when `num_frames=None`. | ||
If not specified and `num_frames==None`, all frames are sampled. | ||
|
||
Returns: | ||
np.ndarray: np array of decoded frames of shape (num_frames, height, width, 3). | ||
""" | ||
vr = VideoReader(uri=video_path, ctx=cpu(0)) # decord has problems with gpu | ||
indices = get_uniform_frame_indices(total_num_frames=len(vr), num_frames=num_frames) | ||
video_fps = vr.get_avg_fps() | ||
total_num_frames = len(vr) | ||
if num_frames is None and fps is not None: | ||
num_frames = int(total_num_frames / video_fps * fps) | ||
if num_frames > total_num_frames: | ||
raise ValueError( | ||
f"When loading the video with fps={fps}, we identified that num_frames ({num_frames}) > total_frames ({total_num_frames}) ." | ||
f"Make sure that fps of a video is less than the requested fps for loading. Detected video_fps={video_fps}" | ||
) | ||
indices = get_uniform_frame_indices(total_num_frames=total_num_frames, num_frames=num_frames) | ||
frames = vr.get_batch(indices).asnumpy() | ||
return frames | ||
|
||
|
||
def read_video_pyav(video_path: str, num_frames: Optional[int] = None): | ||
def read_video_pyav(video_path: str, num_frames: Optional[int] = None, fps: Optional[int] = None): | ||
""" | ||
Decode the video with PyAV decoder. | ||
|
||
Args: | ||
video_path (`str`): | ||
Path to the video file. | ||
num_frames (`int`, *optional*): | ||
Number of frames to sample uniformly. If not specified, all frames are sampled. | ||
Number of frames to sample uniformly. Should be passed only when `fps=None`. | ||
If not specified and `fps==None`, all frames are sampled. | ||
fps (`int`, *optional*): | ||
Number of frames to sample per second. Should be passed only when `num_frames=None`. | ||
If not specified and `num_frames==None`, all frames are sampled. | ||
|
||
Returns: | ||
np.ndarray: np array of decoded frames of shape (num_frames, height, width, 3). | ||
""" | ||
container = av.open(video_path) | ||
|
||
# sample uniformly "num_frames" frames from the video | ||
total_num_frames = container.streams.video[0].frames | ||
video_fps = container.streams.video[0].average_rate # should we better use `av_guess_frame_rate`? | ||
if num_frames is None and fps is not None: | ||
num_frames = int(total_num_frames / video_fps * fps) | ||
if num_frames > total_num_frames: | ||
raise ValueError( | ||
f"When loading the video with fps={fps}, we identified that num_frames ({num_frames}) > total_frames ({total_num_frames}) ." | ||
f"Make sure that fps of a video is less than the requested fps for loading. Detected video_fps={video_fps}" | ||
) | ||
indices = get_uniform_frame_indices(total_num_frames, num_frames=num_frames) | ||
|
||
frames = [] | ||
|
@@ -612,15 +648,19 @@ def read_video_pyav(video_path: str, num_frames: Optional[int] = None): | |
return np.stack([x.to_ndarray(format="rgb24") for x in frames]) | ||
|
||
|
||
def read_video_torchvision(video_path: str, num_frames: Optional[int] = None): | ||
def read_video_torchvision(video_path: str, num_frames: Optional[int] = None, fps: Optional[int] = None): | ||
""" | ||
Decode the video with torchvision decoder. | ||
|
||
Args: | ||
video_path (`str`): | ||
Path to the video file. | ||
num_frames (`int`, *optional*): | ||
Number of frames to sample uniformly. If not specified, all frames are sampled. | ||
Number of frames to sample uniformly. Should be passed only when `fps=None`. | ||
If not specified and `fps==None`, all frames are sampled. | ||
fps (`int`, *optional*): | ||
Number of frames to sample per second. Should be passed only when `num_frames=None`. | ||
If not specified and `num_frames==None`, all frames are sampled. | ||
|
||
Returns: | ||
np.ndarray: np array of decoded frames of shape (num_frames, height, width, 3). | ||
|
@@ -632,6 +672,15 @@ def read_video_torchvision(video_path: str, num_frames: Optional[int] = None): | |
pts_unit="sec", | ||
output_format="TCHW", | ||
) | ||
video_fps = info["video_fps"] | ||
total_num_frames = video.size(0) - 1 | ||
if num_frames is None and fps is not None: | ||
num_frames = int(total_num_frames / video_fps * fps) | ||
if num_frames > total_num_frames: | ||
raise ValueError( | ||
f"When loading the video with fps={fps}, we identified that num_frames ({num_frames}) > total_frames ({total_num_frames}) ." | ||
f"Make sure that fps of a video is less than the requested fps for loading. Detected video_fps={video_fps}" | ||
) | ||
|
||
if num_frames is not None: | ||
idx = torch.linspace(0, video.size(0) - 1, num_frames, dtype=torch.int64) | ||
|
@@ -648,7 +697,12 @@ def read_video_torchvision(video_path: str, num_frames: Optional[int] = None): | |
} | ||
|
||
|
||
def load_video(video: Union[str, "VideoInput"], num_frames: Optional[int] = None, backend: str = "opencv") -> np.array: | ||
def load_video( | ||
video: Union[str, "VideoInput"], | ||
num_frames: Optional[int] = None, | ||
fps: Optional[int] = None, | ||
backend: str = "opencv", | ||
) -> np.array: | ||
""" | ||
Loads `video` to a numpy array. | ||
|
||
|
@@ -657,12 +711,19 @@ def load_video(video: Union[str, "VideoInput"], num_frames: Optional[int] = None | |
The video to convert to the numpy array format. Can be a link to video or local path. | ||
num_frames (`int`, *optional*): | ||
Number of frames to sample uniformly. If not passed, the whole video is loaded. | ||
fps (`int`, *optional*): | ||
Number of frames to sample per second. Should be passed only when `num_frames=None`. | ||
If not specified and `num_frames==None`, all frames are sampled. | ||
backend (`str`, *optional*, defaults to `"opencv"`): | ||
The backend to use when loading the video. Can be any of ["decord", "pyav", "opencv", "torchvision"]. Defaults to "opencv". | ||
|
||
Returns: | ||
`np.array`: A numpy array of shape (num_frames, channels, height, width). | ||
""" | ||
|
||
if fps is not None and num_frames is not None: | ||
raise ValueError("`num_frames` and `fps` are mutually exclusive arguments, please use only one!") | ||
|
||
Comment on lines
+756
to
+758
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh, I see you are doing a check here.. probably better to delegate it to the function itself, but it's up to you |
||
if video.startswith("https://www.youtube.com") or video.startswith("http://www.youtube.com"): | ||
if not is_yt_dlp_available(): | ||
raise ImportError("To load a video from YouTube url you have to install `yt_dlp` first.") | ||
|
@@ -703,7 +764,7 @@ def load_video(video: Union[str, "VideoInput"], num_frames: Optional[int] = None | |
) | ||
|
||
video_decoder = VIDEO_DECODERS[backend] | ||
video = video_decoder(file_obj) | ||
video = video_decoder(file_obj, num_frames=num_frames, fps=fps) | ||
return video | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth adding a check that just one of the args provided?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think neither of them has a special priority, and users should choose only one sampling method to sample frames. Otherwise we assume it was user error, as we can't infer their actual intentions
Right, that was a question hehe, agreed and added yes. I'll look again where exactly it fits the best :)