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

Fix Qwen2VL processor to handle odd number of frames #35431

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ def _preprocess(
patches = np.array(processed_images)
if data_format == ChannelDimension.LAST:
patches = patches.transpose(0, 3, 1, 2)
if patches.shape[0] == 1:
patches = np.tile(patches, (self.temporal_patch_size, 1, 1, 1))
if patches.shape[0] % 2 == 1:
patches = np.concatenate([patches, patches[-1][np.newaxis]], axis=0)
channel = patches.shape[1]
grid_t = patches.shape[0] // self.temporal_patch_size
grid_h, grid_w = resized_height // self.patch_size, resized_width // self.patch_size
Expand Down
11 changes: 11 additions & 0 deletions tests/models/qwen2_vl/test_image_processing_qwen2_vl.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,14 @@ def test_nested_input(self):
# Image processor should return same pixel values, independently of ipnut format
self.assertTrue((encoded_images_nested == encoded_images).all())
self.assertTrue((image_grid_thws_nested == expected_image_grid_thws).all())

def test_odd_number_of_frames(self):
image_processing = self.image_processing_class(**self.image_processor_dict)
expected_dims_by_frames = {1: 324, 3: 648, 5: 972, 7: 1296, 9: 1620}

for num_frames, expected_dims in expected_dims_by_frames.items():
image_inputs = np.random.randint(0, 255, size=(num_frames, 256, 256, 3))
prcocess_out = image_processing(image_inputs, return_tensors="pt")
encoded_images = prcocess_out.pixel_values
expected_output_image_shape = (expected_dims, 1176)
self.assertEqual(tuple(encoded_images.shape), expected_output_image_shape)