Skip to content

Add fast image processor for ViViT #40209

Open
msminhas93 wants to merge 2 commits into
huggingface:mainfrom
msminhas93:fast-image-processsor-vivit
Open

Add fast image processor for ViViT #40209
msminhas93 wants to merge 2 commits into
huggingface:mainfrom
msminhas93:fast-image-processsor-vivit

Conversation

@msminhas93

Copy link
Copy Markdown

What does this PR do?

This PR implements a fast PyTorch image processor for ViViT (VivitImageProcessorFast) that extends the existing BaseImageProcessorFast class. The new processor provides optimized image processing capabilities specifically designed for video understanding tasks using the ViViT model.

Key Features

  • Fast PyTorch-native implementation: Leverages PyTorch operations for efficient video frame processing
  • Video structure handling: Supports nested video inputs (batch of videos, each video is a list of frames)
  • Custom rescaling with offset: Implements ViViT's unique rescaling behavior with optional offset parameter for [-1, 1] range
  • Channel dimension handling: Automatic detection and conversion between different channel formats (CHW vs HWC)
  • Optimized preprocessing pipeline: Sequential application of resize, center crop, rescale, and normalize operations
  • Batch processing support: Efficient handling of multiple video inputs with proper tensor stacking
  • AutoImageProcessor integration: Properly registered in the auto mapping system for seamless usage

Before submitting

  • This PR implements a new fast image processor for ViViT
  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link to it if that's the case. [Contributions Welcome] Add Fast Image Processors [Contributions Welcome] Add Fast Image Processors #36978
  • Did you make sure to update the documentation with your changes? Here are the documentation guidelines, and here are tips on formatting docstrings.
  • Did you write any new necessary tests? Updated test suite to run against both slow and fast processors with 23/23 tests passing

Who can review?

@yonigozlan

@msminhas93

msminhas93 commented Aug 15, 2025

Copy link
Copy Markdown
Author

Hi @yonigozlan! Could you please take a look. Thank you.

@yonigozlan yonigozlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @msminhas93, thanks for contributing, a few things can be simplified!

Comment thread docs/source/en/model_doc/vivit.md
Comment on lines +31 to +39
def make_batched(videos) -> list[list]:
"""Make videos batched for processing."""
if isinstance(videos, (list, tuple)) and isinstance(videos[0], (list, tuple)):
return videos
elif isinstance(videos, (list, tuple)):
return [videos]
else:
return [[videos]]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have make_nested_list_of_images in image_utils for that

Comment thread src/transformers/models/vivit/image_processing_vivit_fast.py Outdated
Comment thread src/transformers/models/vivit/image_processing_vivit_fast.py
Comment thread src/transformers/models/vivit/image_processing_vivit_fast.py Outdated
Comment on lines +240 to +273
processed_videos = []
for video in images: # images is a list of videos, each video is a list of frames
# Process each frame in the video
processed_frames = []
for frame in video:
# Apply transformations to individual frame
if do_resize:
frame = self.resize(image=frame, size=size, interpolation=interpolation)

if do_center_crop:
frame = self.center_crop(frame, crop_size)

# Handle rescaling with offset parameter
if do_rescale:
frame = self.rescale(frame, rescale_factor, offset=offset)

# Handle normalization
if do_normalize:
frame = self.normalize(frame, image_mean, image_std)

processed_frames.append(frame)

# Stack frames for this video
if return_tensors:
video_tensor = torch.stack(processed_frames, dim=0)
processed_videos.append(video_tensor)
else:
processed_videos.append(processed_frames)

# Stack videos if returning tensors
if return_tensors:
processed_videos = torch.stack(processed_videos, dim=0)

return BatchFeature(data={"pixel_values": processed_videos}, tensor_type=return_tensors)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can process fully by batch still. just use the code from the parent class, and set is_nested=True in group_images_by_shape and reorder_images

Comment thread src/transformers/models/vivit/image_processing_vivit_fast.py Outdated
Comment thread tests/models/vivit/test_image_processing_vivit.py Outdated
@github-actions

github-actions Bot commented Sep 8, 2025

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: auto, vivit

@msminhas93

Copy link
Copy Markdown
Author

Thank you for the comments! I've updated my PR @yonigozlan

@msminhas93

Copy link
Copy Markdown
Author

@yonigozlan If you could please look at the PR that'll be awesome

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants