Skip to content

vllm.transformers_utils.processors.hunyuan_vl_image

Image processor class for HunYuanVL.

logger module-attribute

logger = get_logger(__name__)

HunYuanVLImageProcessor

Bases: BaseImageProcessor

Source code in vllm/transformers_utils/processors/hunyuan_vl_image.py
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
class HunYuanVLImageProcessor(BaseImageProcessor):
    model_input_names = [
        "pixel_values",
        "image_grid_thw",
        "pixel_values_videos",
        "video_grid_thw",
    ]

    def __init__(
        self,
        do_resize: bool = True,
        size: dict[str, int] | None = None,
        resample: PILImageResampling = PILImageResampling.BICUBIC,
        do_rescale: bool = True,
        rescale_factor: int | float = 1 / 255,
        do_normalize: bool = True,
        image_mean: float | list[float] | None = None,
        image_std: float | list[float] | None = None,
        do_convert_rgb: bool = True,
        min_pixels: int | None = None,
        max_pixels: int | None = None,
        patch_size: int = 16,
        temporal_patch_size: int = 2,
        merge_size: int = 2,
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)
        if size is not None and (
            "shortest_edge" not in size or "longest_edge" not in size
        ):
            raise ValueError(
                "size must contain 'shortest_edge' and 'longest_edge' keys."
            )
        else:
            size = {"shortest_edge": 512 * 512, "longest_edge": 2048 * 2048}
        # backward compatibility: override size with min_pixels and max_pixels
        # if they are provided.
        if min_pixels is not None:
            size["shortest_edge"] = min_pixels
        if max_pixels is not None:
            size["longest_edge"] = max_pixels
        self.min_pixels = size["shortest_edge"]
        self.max_pixels = size["longest_edge"]
        self.size = size

        self.do_resize = do_resize
        self.resample = resample
        self.do_rescale = do_rescale
        self.rescale_factor = rescale_factor
        self.do_normalize = do_normalize
        self.image_mean = image_mean if image_mean is not None else OPENAI_CLIP_MEAN
        self.image_std = image_std if image_std is not None else OPENAI_CLIP_STD

        self.patch_size = patch_size
        self.temporal_patch_size = temporal_patch_size
        self.merge_size = merge_size
        self.do_convert_rgb = do_convert_rgb

        # hard-code

    def _preprocess(
        self,
        images: ImageInput | VideoInput,
        do_resize: bool | None = None,
        size: dict[str, int] | None = None,
        resample: PILImageResampling = None,
        do_rescale: bool | None = None,
        rescale_factor: float | None = None,
        do_normalize: bool | None = None,
        image_mean: float | list[float] | None = None,
        image_std: float | list[float] | None = None,
        patch_size: int = 16,
        temporal_patch_size: int = 2,
        merge_size: int = 2,
        do_convert_rgb: bool | None = None,
        data_format: ChannelDimension | None = ChannelDimension.FIRST,
        input_data_format: str | ChannelDimension | None = None,
    ):
        """
        Preprocess an image or batch of images. Copy of the `preprocess` method from `CLIPImageProcessor`.

        Args:
            images (`ImageInput`):
                Image or batch of images to preprocess. Expects pixel values ranging from 0 to 255. If pixel values range from 0 to 1, set `do_rescale=False`.
            do_resize (`bool`, *optional*, defaults to `self.do_resize`):
                Whether to resize the image.
            size (`dict[str, int]`, *optional*, defaults to `self.size`):
                Size of the image after resizing. `shortest_edge` and `longest_edge` keys must be present.
            resample (`PILImageResampling`, *optional*, defaults to `self.resample`):
                Resampling filter to use if resizing the image. This can be one of the `PILImageResampling` enums.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image.
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                Scale factor to use if rescaling the image.
            do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
                Whether to normalize the image.
            image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`):
                Mean to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image.
            image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`):
                Standard deviation to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image.
            patch_size (`int`, *optional*, defaults to `self.patch_size`):
                The spatial patch size of the vision encoder.
            temporal_patch_size (`int`, *optional*, defaults to `self.temporal_patch_size`):
                The temporal patch size of the vision encoder.
            merge_size (`int`, *optional*, defaults to `self.merge_size`):
                The merge size of the vision encoder to llm encoder.
            do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`):
                Whether to convert the image to RGB.
            data_format (`ChannelDimension`, *optional*, defaults to `ChannelDimension.FIRST`):
                The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - Unset: Use the channel dimension format of the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.   - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
        """  # noqa: E501
        images = make_list_of_images(images)

        if do_convert_rgb:
            images = [convert_to_rgb(image) for image in images]

        width, height = images[0].width, images[0].height
        resized_width, resized_height = width, height
        processed_images = []
        for image in images:
            if do_resize:
                resized_width, resized_height = smart_resize(
                    width,
                    height,
                    factor=patch_size * merge_size,
                    min_pixels=self.min_pixels,
                    max_pixels=self.max_pixels,
                )
                image = image.resize((resized_width, resized_height))

            if do_normalize:
                image = transforms.Compose(
                    [
                        transforms.ToTensor(),
                        transforms.Normalize(self.image_mean, self.image_std),
                    ]
                )(image)
            processed_images.append(image)

        patches = np.array(processed_images)
        channel = patches.shape[1]
        grid_t = patches.shape[0] // temporal_patch_size
        grid_h, grid_w = resized_height // patch_size, resized_width // patch_size
        patches = patches.reshape(
            1,
            channel,
            grid_h // merge_size,
            merge_size,
            patch_size,
            grid_w // merge_size,
            merge_size,
            patch_size,
        )
        patches = patches.transpose(0, 2, 3, 5, 6, 1, 4, 7)
        flatten_patches = patches.reshape(
            1 * grid_h * grid_w, channel * patch_size * patch_size
        )

        return flatten_patches, (grid_t, grid_h, grid_w)

    def preprocess(
        self,
        images: ImageInput,
        videos: VideoInput = None,
        do_resize: bool | None = None,
        size: dict[str, int] | None = None,
        min_pixels: int | None = None,
        max_pixels: int | None = None,
        resample: PILImageResampling = None,
        do_rescale: bool | None = None,
        rescale_factor: float | None = None,
        do_normalize: bool | None = None,
        image_mean: float | list[float] | None = None,
        image_std: float | list[float] | None = None,
        patch_size: int | None = None,
        temporal_patch_size: int | None = None,
        merge_size: int | None = None,
        do_convert_rgb: bool | None = None,
        return_tensors: str | TensorType | None = None,
        data_format: ChannelDimension | None = ChannelDimension.FIRST,
        input_data_format: str | ChannelDimension | None = None,
    ):
        """
        Args:
            images (`ImageInput`):
                Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
                passing in images with pixel values between 0 and 1, set `do_rescale=False`.
            videos (`VideoInput`):
                Video to preprocess. Expects a single or batch of videos with pixel values ranging from 0 to 255. If
                passing in videos with pixel values between 0 and 1, set `do_rescale=False`.
            do_resize (`bool`, *optional*, defaults to `self.do_resize`):
                Whether to resize the image.
            size (`dict[str, int]`, *optional*, defaults to `self.size`):
                Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with
                the longest edge resized to keep the input aspect ratio.
            resample (`int`, *optional*, defaults to `self.resample`):
                Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only
                has an effect if `do_resize` is set to `True`.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image.
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                Rescale factor to rescale the image by if `do_rescale` is set to `True`.
            do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
                Whether to normalize the image.
            image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`):
                Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`.
            image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`):
                Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to
                `True`.
            min_pixels (`int`, *optional*, defaults to `self.min_pixels`):
                The min pixels of the image to resize the image.
            max_pixels (`int`, *optional*, defaults to `self.max_pixels`):
                The max pixels of the image to resize the image.
            patch_size (`int`, *optional*, defaults to `self.patch_size`):
                The spatial patch size of the vision encoder.
            temporal_patch_size (`int`, *optional*, defaults to `self.temporal_patch_size`):
                The temporal patch size of the vision encoder.
            merge_size (`int`, *optional*, defaults to `self.merge_size`):
                The merge size of the vision encoder to llm encoder.
            do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`):
                Whether to convert the image to RGB.
            return_tensors (`str` or `TensorType`, *optional*):
                The type of tensors to return. Can be one of:
                - Unset: Return a list of `np.ndarray`.
                - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
                - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
                - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
            data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
                The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - Unset: Use the channel dimension format of the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.

        """  # noqa: E501
        min_pixels = min_pixels if min_pixels is not None else self.min_pixels
        max_pixels = max_pixels if max_pixels is not None else self.max_pixels

        if size is not None:
            if "shortest_edge" not in size or "longest_edge" not in size:
                raise ValueError(
                    "size must contain 'shortest_edge' and 'longest_edge' keys."
                )
            min_pixels = size["shortest_edge"]
        elif min_pixels is not None and max_pixels is not None:
            # backward compatibility: override size with min_pixels and max_pixels
            # if they are provided.
            size = {"shortest_edge": min_pixels, "longest_edge": max_pixels}
        else:
            size = {**self.size}

        do_resize = do_resize if do_resize is not None else self.do_resize

        resample = resample if resample is not None else self.resample
        do_rescale = do_rescale if do_rescale is not None else self.do_rescale
        rescale_factor = (
            rescale_factor if rescale_factor is not None else self.rescale_factor
        )
        do_normalize = do_normalize if do_normalize is not None else self.do_normalize
        image_mean = image_mean if image_mean is not None else self.image_mean
        image_std = image_std if image_std is not None else self.image_std
        patch_size = patch_size if patch_size is not None else self.patch_size
        temporal_patch_size = (
            temporal_patch_size
            if temporal_patch_size is not None
            else self.temporal_patch_size
        )
        merge_size = merge_size if merge_size is not None else self.merge_size
        do_convert_rgb = (
            do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb
        )

        if images is not None:
            images = make_flat_list_of_images(images)

        if images is not None and not valid_images(images):
            raise ValueError(
                "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
                "torch.Tensor, tf.Tensor or jax.ndarray."
            )

        validate_preprocess_arguments(
            rescale_factor=rescale_factor,
            do_normalize=do_normalize,
            image_mean=image_mean,
            image_std=image_std,
            do_resize=do_resize,
            size=size,
            resample=resample,
        )

        data = {}
        if images is not None:
            pixel_values, vision_grid_thws = [], []
            for image in images:
                patches, image_grid_thw = self._preprocess(
                    image,
                    do_resize=do_resize,
                    size=size,
                    resample=resample,
                    do_rescale=do_rescale,
                    rescale_factor=rescale_factor,
                    do_normalize=do_normalize,
                    image_mean=image_mean,
                    image_std=image_std,
                    patch_size=patch_size,
                    temporal_patch_size=temporal_patch_size,
                    merge_size=merge_size,
                    data_format=data_format,
                    do_convert_rgb=do_convert_rgb,
                    input_data_format=input_data_format,
                )
                pixel_values.extend(patches)
                vision_grid_thws.append(image_grid_thw)
            pixel_values = np.array(pixel_values)
            vision_grid_thws = np.array(vision_grid_thws)
            data.update(
                {"pixel_values": pixel_values, "image_grid_thw": vision_grid_thws}
            )

        # kept for BC only and should be removed after v5.0
        if videos is not None:
            logger.warning(
                "`HunYuanVLV1ImageProcessor` works only with image inputs "
                "and doesn't process videos anymore. "
                "This is a deprecated behavior and will be removed in v5.0. "
                "Your videos should be forwarded to `HunYuanVLV1VideoProcessor`. "
            )
            videos = make_batched_videos(videos)
            pixel_values_videos, vision_grid_thws_videos = [], []
            for images in videos:
                patches, video_grid_thw = self._preprocess(
                    images,
                    do_resize=do_resize,
                    size=size,
                    resample=resample,
                    do_rescale=do_rescale,
                    rescale_factor=rescale_factor,
                    do_normalize=do_normalize,
                    image_mean=image_mean,
                    image_std=image_std,
                    patch_size=patch_size,
                    temporal_patch_size=temporal_patch_size,
                    merge_size=merge_size,
                    data_format=data_format,
                    do_convert_rgb=do_convert_rgb,
                    input_data_format=input_data_format,
                )
                pixel_values_videos.extend(patches)
                vision_grid_thws_videos.append(video_grid_thw)
            data.update(
                {
                    "pixel_values_videos": np.array(pixel_values_videos),
                    "video_grid_thw": np.array(vision_grid_thws_videos),
                }
            )

        return BatchFeature(data=data, tensor_type=return_tensors)

    def get_number_of_image_patches(self, height: int, width: int, images_kwargs=None):
        """
        A utility that returns number of image patches for a given image size.

        Args:
            height (`int`):
                Height of the input image.
            width (`int`):
                Width of the input image.
            images_kwargs (`dict`, *optional*):
                Any kwargs to override defaults of the image processor.
        Returns:
            `int`: Number of image patches per image.
        """
        min_pixels = (
            images_kwargs["min_pixels"]
            if "min_pixels" in images_kwargs
            else self.size["shortest_edge"]
        )
        max_pixels = (
            images_kwargs["max_pixels"]
            if "max_pixels" in images_kwargs
            else self.size["longest_edge"]
        )
        patch_size = images_kwargs.get("patch_size", self.patch_size)
        merge_size = images_kwargs.get("merge_size", self.merge_size)

        factor = patch_size * merge_size
        resized_height, resized_width = smart_resize(
            height, width, factor, min_pixels=min_pixels, max_pixels=max_pixels
        )
        grid_h, grid_w = resized_height // patch_size, resized_width // patch_size
        return grid_h * (grid_w + 1) + 2

do_convert_rgb instance-attribute

do_convert_rgb = do_convert_rgb

do_normalize instance-attribute

do_normalize = do_normalize

do_rescale instance-attribute

do_rescale = do_rescale

do_resize instance-attribute

do_resize = do_resize

image_mean instance-attribute

image_mean = (
    image_mean
    if image_mean is not None
    else OPENAI_CLIP_MEAN
)

image_std instance-attribute

image_std = (
    image_std if image_std is not None else OPENAI_CLIP_STD
)

max_pixels instance-attribute

max_pixels = size['longest_edge']

merge_size instance-attribute

merge_size = merge_size

min_pixels instance-attribute

min_pixels = size['shortest_edge']

model_input_names class-attribute instance-attribute

model_input_names = [
    "pixel_values",
    "image_grid_thw",
    "pixel_values_videos",
    "video_grid_thw",
]

patch_size instance-attribute

patch_size = patch_size

resample instance-attribute

resample = resample

rescale_factor instance-attribute

rescale_factor = rescale_factor

size instance-attribute

size = size

temporal_patch_size instance-attribute

temporal_patch_size = temporal_patch_size

__init__

__init__(
    do_resize: bool = True,
    size: dict[str, int] | None = None,
    resample: PILImageResampling = BICUBIC,
    do_rescale: bool = True,
    rescale_factor: int | float = 1 / 255,
    do_normalize: bool = True,
    image_mean: float | list[float] | None = None,
    image_std: float | list[float] | None = None,
    do_convert_rgb: bool = True,
    min_pixels: int | None = None,
    max_pixels: int | None = None,
    patch_size: int = 16,
    temporal_patch_size: int = 2,
    merge_size: int = 2,
    **kwargs,
) -> None
Source code in vllm/transformers_utils/processors/hunyuan_vl_image.py
def __init__(
    self,
    do_resize: bool = True,
    size: dict[str, int] | None = None,
    resample: PILImageResampling = PILImageResampling.BICUBIC,
    do_rescale: bool = True,
    rescale_factor: int | float = 1 / 255,
    do_normalize: bool = True,
    image_mean: float | list[float] | None = None,
    image_std: float | list[float] | None = None,
    do_convert_rgb: bool = True,
    min_pixels: int | None = None,
    max_pixels: int | None = None,
    patch_size: int = 16,
    temporal_patch_size: int = 2,
    merge_size: int = 2,
    **kwargs,
) -> None:
    super().__init__(**kwargs)
    if size is not None and (
        "shortest_edge" not in size or "longest_edge" not in size
    ):
        raise ValueError(
            "size must contain 'shortest_edge' and 'longest_edge' keys."
        )
    else:
        size = {"shortest_edge": 512 * 512, "longest_edge": 2048 * 2048}
    # backward compatibility: override size with min_pixels and max_pixels
    # if they are provided.
    if min_pixels is not None:
        size["shortest_edge"] = min_pixels
    if max_pixels is not None:
        size["longest_edge"] = max_pixels
    self.min_pixels = size["shortest_edge"]
    self.max_pixels = size["longest_edge"]
    self.size = size

    self.do_resize = do_resize
    self.resample = resample
    self.do_rescale = do_rescale
    self.rescale_factor = rescale_factor
    self.do_normalize = do_normalize
    self.image_mean = image_mean if image_mean is not None else OPENAI_CLIP_MEAN
    self.image_std = image_std if image_std is not None else OPENAI_CLIP_STD

    self.patch_size = patch_size
    self.temporal_patch_size = temporal_patch_size
    self.merge_size = merge_size
    self.do_convert_rgb = do_convert_rgb

_preprocess

_preprocess(
    images: ImageInput | VideoInput,
    do_resize: bool | None = None,
    size: dict[str, int] | None = None,
    resample: PILImageResampling = None,
    do_rescale: bool | None = None,
    rescale_factor: float | None = None,
    do_normalize: bool | None = None,
    image_mean: float | list[float] | None = None,
    image_std: float | list[float] | None = None,
    patch_size: int = 16,
    temporal_patch_size: int = 2,
    merge_size: int = 2,
    do_convert_rgb: bool | None = None,
    data_format: ChannelDimension | None = FIRST,
    input_data_format: str | ChannelDimension | None = None,
)

Preprocess an image or batch of images. Copy of the preprocess method from CLIPImageProcessor.

Parameters:

Name Type Description Default
images `ImageInput`

Image or batch of images to preprocess. Expects pixel values ranging from 0 to 255. If pixel values range from 0 to 1, set do_rescale=False.

required
do_resize `bool`, *optional*, defaults to `self.do_resize`

Whether to resize the image.

None
size `dict[str, int]`, *optional*, defaults to `self.size`

Size of the image after resizing. shortest_edge and longest_edge keys must be present.

None
resample `PILImageResampling`, *optional*, defaults to `self.resample`

Resampling filter to use if resizing the image. This can be one of the PILImageResampling enums.

None
do_rescale `bool`, *optional*, defaults to `self.do_rescale`

Whether to rescale the image.

None
rescale_factor `float`, *optional*, defaults to `self.rescale_factor`

Scale factor to use if rescaling the image.

None
do_normalize `bool`, *optional*, defaults to `self.do_normalize`

Whether to normalize the image.

None
image_mean `float` or `list[float]`, *optional*, defaults to `self.image_mean`

Mean to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image.

None
image_std `float` or `list[float]`, *optional*, defaults to `self.image_std`

Standard deviation to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image.

None
patch_size `int`, *optional*, defaults to `self.patch_size`

The spatial patch size of the vision encoder.

16
temporal_patch_size `int`, *optional*, defaults to `self.temporal_patch_size`

The temporal patch size of the vision encoder.

2
merge_size `int`, *optional*, defaults to `self.merge_size`

The merge size of the vision encoder to llm encoder.

2
do_convert_rgb `bool`, *optional*, defaults to `self.do_convert_rgb`

Whether to convert the image to RGB.

None
data_format `ChannelDimension`, *optional*, defaults to `ChannelDimension.FIRST`

The channel dimension format for the output image. Can be one of: - "channels_first" or ChannelDimension.FIRST: image in (num_channels, height, width) format. - "channels_last" or ChannelDimension.LAST: image in (height, width, num_channels) format. - Unset: Use the channel dimension format of the input image.

FIRST
input_data_format `ChannelDimension` or `str`, *optional*

The channel dimension format for the input image. Can be one of: - "channels_first" or ChannelDimension.FIRST: image in (num_channels, height, width) format. - "channels_last" or ChannelDimension.LAST: image in (height, width, num_channels) format. - "none" or ChannelDimension.NONE: image in (height, width) format. - "none" or ChannelDimension.NONE: image in (height, width) format.

None
Source code in vllm/transformers_utils/processors/hunyuan_vl_image.py
def _preprocess(
    self,
    images: ImageInput | VideoInput,
    do_resize: bool | None = None,
    size: dict[str, int] | None = None,
    resample: PILImageResampling = None,
    do_rescale: bool | None = None,
    rescale_factor: float | None = None,
    do_normalize: bool | None = None,
    image_mean: float | list[float] | None = None,
    image_std: float | list[float] | None = None,
    patch_size: int = 16,
    temporal_patch_size: int = 2,
    merge_size: int = 2,
    do_convert_rgb: bool | None = None,
    data_format: ChannelDimension | None = ChannelDimension.FIRST,
    input_data_format: str | ChannelDimension | None = None,
):
    """
    Preprocess an image or batch of images. Copy of the `preprocess` method from `CLIPImageProcessor`.

    Args:
        images (`ImageInput`):
            Image or batch of images to preprocess. Expects pixel values ranging from 0 to 255. If pixel values range from 0 to 1, set `do_rescale=False`.
        do_resize (`bool`, *optional*, defaults to `self.do_resize`):
            Whether to resize the image.
        size (`dict[str, int]`, *optional*, defaults to `self.size`):
            Size of the image after resizing. `shortest_edge` and `longest_edge` keys must be present.
        resample (`PILImageResampling`, *optional*, defaults to `self.resample`):
            Resampling filter to use if resizing the image. This can be one of the `PILImageResampling` enums.
        do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
            Whether to rescale the image.
        rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
            Scale factor to use if rescaling the image.
        do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
            Whether to normalize the image.
        image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`):
            Mean to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image.
        image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`):
            Standard deviation to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image.
        patch_size (`int`, *optional*, defaults to `self.patch_size`):
            The spatial patch size of the vision encoder.
        temporal_patch_size (`int`, *optional*, defaults to `self.temporal_patch_size`):
            The temporal patch size of the vision encoder.
        merge_size (`int`, *optional*, defaults to `self.merge_size`):
            The merge size of the vision encoder to llm encoder.
        do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`):
            Whether to convert the image to RGB.
        data_format (`ChannelDimension`, *optional*, defaults to `ChannelDimension.FIRST`):
            The channel dimension format for the output image. Can be one of:
            - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
            - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            - Unset: Use the channel dimension format of the input image.
        input_data_format (`ChannelDimension` or `str`, *optional*):
            The channel dimension format for the input image. Can be one of:
            - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
            - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.   - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
    """  # noqa: E501
    images = make_list_of_images(images)

    if do_convert_rgb:
        images = [convert_to_rgb(image) for image in images]

    width, height = images[0].width, images[0].height
    resized_width, resized_height = width, height
    processed_images = []
    for image in images:
        if do_resize:
            resized_width, resized_height = smart_resize(
                width,
                height,
                factor=patch_size * merge_size,
                min_pixels=self.min_pixels,
                max_pixels=self.max_pixels,
            )
            image = image.resize((resized_width, resized_height))

        if do_normalize:
            image = transforms.Compose(
                [
                    transforms.ToTensor(),
                    transforms.Normalize(self.image_mean, self.image_std),
                ]
            )(image)
        processed_images.append(image)

    patches = np.array(processed_images)
    channel = patches.shape[1]
    grid_t = patches.shape[0] // temporal_patch_size
    grid_h, grid_w = resized_height // patch_size, resized_width // patch_size
    patches = patches.reshape(
        1,
        channel,
        grid_h // merge_size,
        merge_size,
        patch_size,
        grid_w // merge_size,
        merge_size,
        patch_size,
    )
    patches = patches.transpose(0, 2, 3, 5, 6, 1, 4, 7)
    flatten_patches = patches.reshape(
        1 * grid_h * grid_w, channel * patch_size * patch_size
    )

    return flatten_patches, (grid_t, grid_h, grid_w)

get_number_of_image_patches

get_number_of_image_patches(
    height: int, width: int, images_kwargs=None
)

A utility that returns number of image patches for a given image size.

Parameters:

Name Type Description Default
height `int`

Height of the input image.

required
width `int`

Width of the input image.

required
images_kwargs `dict`, *optional*

Any kwargs to override defaults of the image processor.

None

Returns: int: Number of image patches per image.

Source code in vllm/transformers_utils/processors/hunyuan_vl_image.py
def get_number_of_image_patches(self, height: int, width: int, images_kwargs=None):
    """
    A utility that returns number of image patches for a given image size.

    Args:
        height (`int`):
            Height of the input image.
        width (`int`):
            Width of the input image.
        images_kwargs (`dict`, *optional*):
            Any kwargs to override defaults of the image processor.
    Returns:
        `int`: Number of image patches per image.
    """
    min_pixels = (
        images_kwargs["min_pixels"]
        if "min_pixels" in images_kwargs
        else self.size["shortest_edge"]
    )
    max_pixels = (
        images_kwargs["max_pixels"]
        if "max_pixels" in images_kwargs
        else self.size["longest_edge"]
    )
    patch_size = images_kwargs.get("patch_size", self.patch_size)
    merge_size = images_kwargs.get("merge_size", self.merge_size)

    factor = patch_size * merge_size
    resized_height, resized_width = smart_resize(
        height, width, factor, min_pixels=min_pixels, max_pixels=max_pixels
    )
    grid_h, grid_w = resized_height // patch_size, resized_width // patch_size
    return grid_h * (grid_w + 1) + 2

preprocess

preprocess(
    images: ImageInput,
    videos: VideoInput = None,
    do_resize: bool | None = None,
    size: dict[str, int] | None = None,
    min_pixels: int | None = None,
    max_pixels: int | None = None,
    resample: PILImageResampling = None,
    do_rescale: bool | None = None,
    rescale_factor: float | None = None,
    do_normalize: bool | None = None,
    image_mean: float | list[float] | None = None,
    image_std: float | list[float] | None = None,
    patch_size: int | None = None,
    temporal_patch_size: int | None = None,
    merge_size: int | None = None,
    do_convert_rgb: bool | None = None,
    return_tensors: str | TensorType | None = None,
    data_format: ChannelDimension | None = FIRST,
    input_data_format: str | ChannelDimension | None = None,
)

Parameters:

Name Type Description Default
images `ImageInput`

Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set do_rescale=False.

required
videos `VideoInput`

Video to preprocess. Expects a single or batch of videos with pixel values ranging from 0 to 255. If passing in videos with pixel values between 0 and 1, set do_rescale=False.

None
do_resize `bool`, *optional*, defaults to `self.do_resize`

Whether to resize the image.

None
size `dict[str, int]`, *optional*, defaults to `self.size`

Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio.

None
resample `int`, *optional*, defaults to `self.resample`

Resampling filter to use if resizing the image. This can be one of the enum PILImageResampling. Only has an effect if do_resize is set to True.

None
do_rescale `bool`, *optional*, defaults to `self.do_rescale`

Whether to rescale the image.

None
rescale_factor `float`, *optional*, defaults to `self.rescale_factor`

Rescale factor to rescale the image by if do_rescale is set to True.

None
do_normalize `bool`, *optional*, defaults to `self.do_normalize`

Whether to normalize the image.

None
image_mean `float` or `list[float]`, *optional*, defaults to `self.image_mean`

Image mean to use for normalization. Only has an effect if do_normalize is set to True.

None
image_std `float` or `list[float]`, *optional*, defaults to `self.image_std`

Image standard deviation to use for normalization. Only has an effect if do_normalize is set to True.

None
min_pixels `int`, *optional*, defaults to `self.min_pixels`

The min pixels of the image to resize the image.

None
max_pixels `int`, *optional*, defaults to `self.max_pixels`

The max pixels of the image to resize the image.

None
patch_size `int`, *optional*, defaults to `self.patch_size`

The spatial patch size of the vision encoder.

None
temporal_patch_size `int`, *optional*, defaults to `self.temporal_patch_size`

The temporal patch size of the vision encoder.

None
merge_size `int`, *optional*, defaults to `self.merge_size`

The merge size of the vision encoder to llm encoder.

None
do_convert_rgb `bool`, *optional*, defaults to `self.do_convert_rgb`

Whether to convert the image to RGB.

None
return_tensors `str` or `TensorType`, *optional*

The type of tensors to return. Can be one of: - Unset: Return a list of np.ndarray. - TensorType.TENSORFLOW or 'tf': Return a batch of type tf.Tensor. - TensorType.PYTORCH or 'pt': Return a batch of type torch.Tensor. - TensorType.NUMPY or 'np': Return a batch of type np.ndarray. - TensorType.JAX or 'jax': Return a batch of type jax.numpy.ndarray.

None
data_format `ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`

The channel dimension format for the output image. Can be one of: - "channels_first" or ChannelDimension.FIRST: image in (num_channels, height, width) format. - "channels_last" or ChannelDimension.LAST: image in (height, width, num_channels) format. - Unset: Use the channel dimension format of the input image.

FIRST
input_data_format `ChannelDimension` or `str`, *optional*

The channel dimension format for the input image. If unset, the channel dimension format is inferred from the input image. Can be one of: - "channels_first" or ChannelDimension.FIRST: image in (num_channels, height, width) format. - "channels_last" or ChannelDimension.LAST: image in (height, width, num_channels) format. - "none" or ChannelDimension.NONE: image in (height, width) format.

None
Source code in vllm/transformers_utils/processors/hunyuan_vl_image.py
def preprocess(
    self,
    images: ImageInput,
    videos: VideoInput = None,
    do_resize: bool | None = None,
    size: dict[str, int] | None = None,
    min_pixels: int | None = None,
    max_pixels: int | None = None,
    resample: PILImageResampling = None,
    do_rescale: bool | None = None,
    rescale_factor: float | None = None,
    do_normalize: bool | None = None,
    image_mean: float | list[float] | None = None,
    image_std: float | list[float] | None = None,
    patch_size: int | None = None,
    temporal_patch_size: int | None = None,
    merge_size: int | None = None,
    do_convert_rgb: bool | None = None,
    return_tensors: str | TensorType | None = None,
    data_format: ChannelDimension | None = ChannelDimension.FIRST,
    input_data_format: str | ChannelDimension | None = None,
):
    """
    Args:
        images (`ImageInput`):
            Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
            passing in images with pixel values between 0 and 1, set `do_rescale=False`.
        videos (`VideoInput`):
            Video to preprocess. Expects a single or batch of videos with pixel values ranging from 0 to 255. If
            passing in videos with pixel values between 0 and 1, set `do_rescale=False`.
        do_resize (`bool`, *optional*, defaults to `self.do_resize`):
            Whether to resize the image.
        size (`dict[str, int]`, *optional*, defaults to `self.size`):
            Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with
            the longest edge resized to keep the input aspect ratio.
        resample (`int`, *optional*, defaults to `self.resample`):
            Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only
            has an effect if `do_resize` is set to `True`.
        do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
            Whether to rescale the image.
        rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
            Rescale factor to rescale the image by if `do_rescale` is set to `True`.
        do_normalize (`bool`, *optional*, defaults to `self.do_normalize`):
            Whether to normalize the image.
        image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`):
            Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`.
        image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`):
            Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to
            `True`.
        min_pixels (`int`, *optional*, defaults to `self.min_pixels`):
            The min pixels of the image to resize the image.
        max_pixels (`int`, *optional*, defaults to `self.max_pixels`):
            The max pixels of the image to resize the image.
        patch_size (`int`, *optional*, defaults to `self.patch_size`):
            The spatial patch size of the vision encoder.
        temporal_patch_size (`int`, *optional*, defaults to `self.temporal_patch_size`):
            The temporal patch size of the vision encoder.
        merge_size (`int`, *optional*, defaults to `self.merge_size`):
            The merge size of the vision encoder to llm encoder.
        do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`):
            Whether to convert the image to RGB.
        return_tensors (`str` or `TensorType`, *optional*):
            The type of tensors to return. Can be one of:
            - Unset: Return a list of `np.ndarray`.
            - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
            - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
            - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
            - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
        data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
            The channel dimension format for the output image. Can be one of:
            - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
            - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            - Unset: Use the channel dimension format of the input image.
        input_data_format (`ChannelDimension` or `str`, *optional*):
            The channel dimension format for the input image. If unset, the channel dimension format is inferred
            from the input image. Can be one of:
            - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
            - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
            - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.

    """  # noqa: E501
    min_pixels = min_pixels if min_pixels is not None else self.min_pixels
    max_pixels = max_pixels if max_pixels is not None else self.max_pixels

    if size is not None:
        if "shortest_edge" not in size or "longest_edge" not in size:
            raise ValueError(
                "size must contain 'shortest_edge' and 'longest_edge' keys."
            )
        min_pixels = size["shortest_edge"]
    elif min_pixels is not None and max_pixels is not None:
        # backward compatibility: override size with min_pixels and max_pixels
        # if they are provided.
        size = {"shortest_edge": min_pixels, "longest_edge": max_pixels}
    else:
        size = {**self.size}

    do_resize = do_resize if do_resize is not None else self.do_resize

    resample = resample if resample is not None else self.resample
    do_rescale = do_rescale if do_rescale is not None else self.do_rescale
    rescale_factor = (
        rescale_factor if rescale_factor is not None else self.rescale_factor
    )
    do_normalize = do_normalize if do_normalize is not None else self.do_normalize
    image_mean = image_mean if image_mean is not None else self.image_mean
    image_std = image_std if image_std is not None else self.image_std
    patch_size = patch_size if patch_size is not None else self.patch_size
    temporal_patch_size = (
        temporal_patch_size
        if temporal_patch_size is not None
        else self.temporal_patch_size
    )
    merge_size = merge_size if merge_size is not None else self.merge_size
    do_convert_rgb = (
        do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb
    )

    if images is not None:
        images = make_flat_list_of_images(images)

    if images is not None and not valid_images(images):
        raise ValueError(
            "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
            "torch.Tensor, tf.Tensor or jax.ndarray."
        )

    validate_preprocess_arguments(
        rescale_factor=rescale_factor,
        do_normalize=do_normalize,
        image_mean=image_mean,
        image_std=image_std,
        do_resize=do_resize,
        size=size,
        resample=resample,
    )

    data = {}
    if images is not None:
        pixel_values, vision_grid_thws = [], []
        for image in images:
            patches, image_grid_thw = self._preprocess(
                image,
                do_resize=do_resize,
                size=size,
                resample=resample,
                do_rescale=do_rescale,
                rescale_factor=rescale_factor,
                do_normalize=do_normalize,
                image_mean=image_mean,
                image_std=image_std,
                patch_size=patch_size,
                temporal_patch_size=temporal_patch_size,
                merge_size=merge_size,
                data_format=data_format,
                do_convert_rgb=do_convert_rgb,
                input_data_format=input_data_format,
            )
            pixel_values.extend(patches)
            vision_grid_thws.append(image_grid_thw)
        pixel_values = np.array(pixel_values)
        vision_grid_thws = np.array(vision_grid_thws)
        data.update(
            {"pixel_values": pixel_values, "image_grid_thw": vision_grid_thws}
        )

    # kept for BC only and should be removed after v5.0
    if videos is not None:
        logger.warning(
            "`HunYuanVLV1ImageProcessor` works only with image inputs "
            "and doesn't process videos anymore. "
            "This is a deprecated behavior and will be removed in v5.0. "
            "Your videos should be forwarded to `HunYuanVLV1VideoProcessor`. "
        )
        videos = make_batched_videos(videos)
        pixel_values_videos, vision_grid_thws_videos = [], []
        for images in videos:
            patches, video_grid_thw = self._preprocess(
                images,
                do_resize=do_resize,
                size=size,
                resample=resample,
                do_rescale=do_rescale,
                rescale_factor=rescale_factor,
                do_normalize=do_normalize,
                image_mean=image_mean,
                image_std=image_std,
                patch_size=patch_size,
                temporal_patch_size=temporal_patch_size,
                merge_size=merge_size,
                data_format=data_format,
                do_convert_rgb=do_convert_rgb,
                input_data_format=input_data_format,
            )
            pixel_values_videos.extend(patches)
            vision_grid_thws_videos.append(video_grid_thw)
        data.update(
            {
                "pixel_values_videos": np.array(pixel_values_videos),
                "video_grid_thw": np.array(vision_grid_thws_videos),
            }
        )

    return BatchFeature(data=data, tensor_type=return_tensors)

smart_resize

smart_resize(
    height: int,
    width: int,
    factor: int = 16,
    min_pixels: int = 512 * 512,
    max_pixels: int = 2048 * 2048,
)

Rescales the image so that the following conditions are met:

  1. Both dimensions (height and width) are divisible by 'factor'.

  2. The total number of pixels is within the range ['min_pixels', 'max_pixels'].

  3. The aspect ratio of the image is maintained as closely as possible.

Source code in vllm/transformers_utils/processors/hunyuan_vl_image.py
def smart_resize(
    height: int,
    width: int,
    factor: int = 16,
    min_pixels: int = 512 * 512,
    max_pixels: int = 2048 * 2048,
):
    """Rescales the image so that the following conditions are met:

    1. Both dimensions (height and width) are divisible by 'factor'.

    2. The total number of pixels is within the range ['min_pixels', 'max_pixels'].

    3. The aspect ratio of the image is maintained as closely as possible.

    """
    if max(height, width) / min(height, width) > 200:
        raise ValueError(
            "absolute aspect ratio must be smaller than 200, got "
            f"{max(height, width) / min(height, width)}"
        )
    h_bar = round(height / factor) * factor
    w_bar = round(width / factor) * factor
    if h_bar * w_bar > max_pixels:
        beta = math.sqrt((height * width) / max_pixels)
        h_bar = max(factor, math.floor(height / beta / factor) * factor)
        w_bar = max(factor, math.floor(width / beta / factor) * factor)
    elif h_bar * w_bar < min_pixels:
        beta = math.sqrt(min_pixels / (height * width))
        h_bar = math.ceil(height * beta / factor) * factor
        w_bar = math.ceil(width * beta / factor) * factor
    return h_bar, w_bar