-
Notifications
You must be signed in to change notification settings - Fork 26.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
Use PyAV instead of Decord in examples #21572
Merged
amyeroberts
merged 8 commits into
huggingface:main
from
amyeroberts:update-docs-replace-decord-with-pyav
Mar 2, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e2a1bdc
Use PyAV instead of Decord
amyeroberts fa0abed
Get frame indices
amyeroberts 0565280
Fix number of frames
amyeroberts 5c644ab
Update src/transformers/models/videomae/image_processing_videomae.py
amyeroberts 4d88700
Fix up
amyeroberts ad89d63
Fix copies
amyeroberts 3033cca
Update timesformer doctests
amyeroberts 02e8d26
Update docstrings
amyeroberts 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -570,12 +570,35 @@ def forward( | |
Examples: | ||
|
||
```python | ||
>>> from decord import VideoReader, cpu | ||
>>> import av | ||
>>> import numpy as np | ||
|
||
>>> from transformers import AutoFeatureExtractor, TimesformerModel | ||
>>> from transformers import AutoImageProcessor, TimesformerModel | ||
>>> from huggingface_hub import hf_hub_download | ||
|
||
>>> np.random.seed(0) | ||
|
||
|
||
>>> def read_video_pyav(container, indices): | ||
... ''' | ||
... Decode the video with PyAV decoder. | ||
... Args: | ||
... container (`av.container.input.InputContainer`): PyAV container. | ||
... indices (`List[int]`): List of frame indices to decode. | ||
... Returns: | ||
... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). | ||
... ''' | ||
... frames = [] | ||
... container.seek(0) | ||
... start_index = indices[0] | ||
... end_index = indices[-1] | ||
... for i, frame in enumerate(container.decode(video=0)): | ||
... if i > end_index: | ||
... break | ||
... if i >= start_index and i in indices: | ||
... frames.append(frame) | ||
... return np.stack([x.to_ndarray(format="rgb24") for x in frames]) | ||
|
||
|
||
>>> def sample_frame_indices(clip_len, frame_sample_rate, seg_len): | ||
... converted_len = int(clip_len * frame_sample_rate) | ||
|
@@ -590,24 +613,23 @@ def forward( | |
>>> file_path = hf_hub_download( | ||
... repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset" | ||
... ) | ||
>>> videoreader = VideoReader(file_path, num_threads=1, ctx=cpu(0)) | ||
>>> container = av.open(file_path) | ||
|
||
>>> # sample 8 frames | ||
>>> videoreader.seek(0) | ||
>>> indices = sample_frame_indices(clip_len=8, frame_sample_rate=4, seg_len=len(videoreader)) | ||
>>> video = videoreader.get_batch(indices).asnumpy() | ||
>>> indices = sample_frame_indices(clip_len=8, frame_sample_rate=4, seg_len=container.streams.video[0].frames) | ||
>>> video = read_video_pyav(container, indices) | ||
|
||
>>> feature_extractor = AutoFeatureExtractor.from_pretrained("MCG-NJU/videomae-base") | ||
>>> image_processor = AutoImageProcessor.from_pretrained("MCG-NJU/videomae-base") | ||
>>> model = TimesformerModel.from_pretrained("facebook/timesformer-base-finetuned-k400") | ||
|
||
>>> # prepare video for the model | ||
>>> inputs = feature_extractor(list(video), return_tensors="pt") | ||
>>> inputs = image_processor(list(video), return_tensors="pt") | ||
|
||
>>> # forward pass | ||
>>> outputs = model(**inputs) | ||
>>> last_hidden_states = outputs.last_hidden_state | ||
>>> list(last_hidden_states.shape) | ||
[1, 1568, 768] | ||
[1, 1569, 768] | ||
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. This lost me a lot of time 😅
|
||
```""" | ||
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions | ||
output_hidden_states = ( | ||
|
@@ -676,16 +698,37 @@ def forward( | |
Examples: | ||
|
||
```python | ||
>>> from decord import VideoReader, cpu | ||
>>> import av | ||
>>> import torch | ||
>>> import numpy as np | ||
|
||
>>> from transformers import AutoFeatureExtractor, TimesformerForVideoClassification | ||
>>> from transformers import AutoImageProcessor, TimesformerForVideoClassification | ||
>>> from huggingface_hub import hf_hub_download | ||
|
||
>>> np.random.seed(0) | ||
|
||
|
||
>>> def read_video_pyav(container, indices): | ||
... ''' | ||
... Decode the video with PyAV decoder. | ||
... Args: | ||
... container (`av.container.input.InputContainer`): PyAV container. | ||
... indices (`List[int]`): List of frame indices to decode. | ||
... Returns: | ||
... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). | ||
... ''' | ||
... frames = [] | ||
... container.seek(0) | ||
... start_index = indices[0] | ||
... end_index = indices[-1] | ||
... for i, frame in enumerate(container.decode(video=0)): | ||
... if i > end_index: | ||
... break | ||
... if i >= start_index and i in indices: | ||
... frames.append(frame) | ||
... return np.stack([x.to_ndarray(format="rgb24") for x in frames]) | ||
|
||
|
||
>>> def sample_frame_indices(clip_len, frame_sample_rate, seg_len): | ||
... converted_len = int(clip_len * frame_sample_rate) | ||
... end_idx = np.random.randint(converted_len, seg_len) | ||
|
@@ -699,17 +742,16 @@ def forward( | |
>>> file_path = hf_hub_download( | ||
... repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset" | ||
... ) | ||
>>> videoreader = VideoReader(file_path, num_threads=1, ctx=cpu(0)) | ||
>>> container = av.open(file_path) | ||
|
||
>>> # sample 8 frames | ||
>>> videoreader.seek(0) | ||
>>> indices = sample_frame_indices(clip_len=8, frame_sample_rate=4, seg_len=len(videoreader)) | ||
>>> video = videoreader.get_batch(indices).asnumpy() | ||
>>> indices = sample_frame_indices(clip_len=8, frame_sample_rate=1, seg_len=container.streams.video[0].frames) | ||
>>> video = read_video_pyav(container, indices) | ||
|
||
>>> feature_extractor = AutoFeatureExtractor.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics") | ||
>>> image_processor = AutoImageProcessor.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics") | ||
>>> model = TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400") | ||
|
||
>>> inputs = feature_extractor(list(video), return_tensors="pt") | ||
>>> inputs = image_processor(list(video), return_tensors="pt") | ||
|
||
>>> with torch.no_grad(): | ||
... outputs = model(**inputs) | ||
|
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.
Not removing decord yet as it's used in some other areas e.g. video classification pipeline