Skip to content
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

[VideoMAE] Improve code examples #18919

Merged
merged 2 commits into from
Sep 7, 2022
Merged
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
29 changes: 13 additions & 16 deletions src/transformers/models/videomae/modeling_videomae.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,21 +598,18 @@ def forward(
>>> file_path = hf_hub_download(
... repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset"
... )
>>> vr = VideoReader(file_path, num_threads=1, ctx=cpu(0))
>>> videoreader = VideoReader(file_path, num_threads=1, ctx=cpu(0))

>>> # sample 16 frames
>>> vr.seek(0)
>>> indices = sample_frame_indices(clip_len=16, frame_sample_rate=4, seg_len=len(vr))
>>> buffer = vr.get_batch(indices).asnumpy()

>>> # create a list of NumPy arrays
>>> video = [buffer[i] for i in range(buffer.shape[0])]
>>> videoreader.seek(0)
>>> indices = sample_frame_indices(clip_len=16, frame_sample_rate=4, seg_len=len(videoreader))
>>> video = videoreader.get_batch(indices).asnumpy()

>>> feature_extractor = VideoMAEFeatureExtractor.from_pretrained("MCG-NJU/videomae-base")
>>> model = VideoMAEModel.from_pretrained("MCG-NJU/videomae-base")

>>> # prepare video for the model
>>> inputs = feature_extractor(video, return_tensors="pt")
>>> inputs = feature_extractor(list(video), return_tensors="pt")

>>> # forward pass
>>> outputs = model(**inputs)
Expand Down Expand Up @@ -943,10 +940,13 @@ def forward(
```python
>>> from decord import VideoReader, cpu
>>> import torch
>>> import numpy as np

>>> from transformers import VideoMAEFeatureExtractor, VideoMAEForVideoClassification
>>> from huggingface_hub import hf_hub_download

>>> np.random.seed(0)


>>> def sample_frame_indices(clip_len, frame_sample_rate, seg_len):
... converted_len = int(clip_len * frame_sample_rate)
Expand All @@ -961,20 +961,17 @@ def forward(
>>> file_path = hf_hub_download(
... repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset"
... )
>>> vr = VideoReader(file_path, num_threads=1, ctx=cpu(0))
>>> videoreader = VideoReader(file_path, num_threads=1, ctx=cpu(0))

>>> # sample 16 frames
>>> vr.seek(0)
>>> indices = sample_frame_indices(clip_len=16, frame_sample_rate=4, seg_len=len(vr))
>>> buffer = vr.get_batch(indices).asnumpy()

>>> # create a list of NumPy arrays
>>> video = [buffer[i] for i in range(buffer.shape[0])]
>>> videoreader.seek(0)
>>> indices = sample_frame_indices(clip_len=16, frame_sample_rate=4, seg_len=len(videoreader))
>>> video = videoreader.get_batch(indices).asnumpy()

>>> feature_extractor = VideoMAEFeatureExtractor.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics")
>>> model = VideoMAEForVideoClassification.from_pretrained("MCG-NJU/videomae-base-finetuned-kinetics")

>>> inputs = feature_extractor(video, return_tensors="pt")
>>> inputs = feature_extractor(list(video), return_tensors="pt")

>>> with torch.no_grad():
... outputs = model(**inputs)
Expand Down