


'''simple docstring'''


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
				import torch

if is_vision_available():
				from PIL import Image

				from transformers import PoolFormerImageProcessor





class        lowercase__    (		unittest.TestCase   ):





							'''simple docstring'''


							def __init__(       self       ,   __snake_case       ,   __snake_case=7       ,   __snake_case=3       ,   __snake_case=30       ,   __snake_case=400       ,   __snake_case=True       ,   __snake_case=None       ,   __snake_case=0.9       ,   __snake_case=None       ,   __snake_case=True       ,   __snake_case=[0.5, 0.5, 0.5]       ,   __snake_case=[0.5, 0.5, 0.5]       ,   ):
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= size if size is not None else {"""shortest_edge""": 30}
											_SCREAMING_SNAKE_CASE					:			List[Any]							= crop_size if crop_size is not None else {"""height""": 30, """width""": 30}
											_SCREAMING_SNAKE_CASE					:			int							= parent
											_SCREAMING_SNAKE_CASE					:			List[Any]							= batch_size
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= num_channels
											_SCREAMING_SNAKE_CASE					:			str							= min_resolution
											_SCREAMING_SNAKE_CASE					:			str							= max_resolution
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= do_resize_and_center_crop
											_SCREAMING_SNAKE_CASE					:			Dict							= size
											_SCREAMING_SNAKE_CASE					:			List[Any]							= crop_pct
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= crop_size
											_SCREAMING_SNAKE_CASE					:			str							= do_normalize
											_SCREAMING_SNAKE_CASE					:			Tuple							= image_mean
											_SCREAMING_SNAKE_CASE					:			str							= image_std






							def UpperCAmelCase_      (       self    ):
											return {
											    "size": self.size,
											    "do_resize_and_center_crop": self.do_resize_and_center_crop,
											    "crop_pct": self.crop_pct,
											    "crop_size": self.crop_size,
											    "do_normalize": self.do_normalize,
											    "image_mean": self.image_mean,
											    "image_std": self.image_std,
											}





@require_torch
@require_vision
class        lowercase__    (		_snake_case	,	unittest.TestCase   ):





							'''simple docstring'''


							A_				: Tuple       =    PoolFormerImageProcessor if is_vision_available() else None






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			List[str]							= PoolFormerImageProcessingTester(self    )






							@property
							def UpperCAmelCase_      (       self    ):
											return self.image_processor_tester.prepare_image_processor_dict()






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= self.image_processing_class(**self.image_processor_dict    )
											self.assertTrue(hasattr(__snake_case       ,   """do_resize_and_center_crop"""    )    )
											self.assertTrue(hasattr(__snake_case       ,   """size"""    )    )
											self.assertTrue(hasattr(__snake_case       ,   """crop_pct"""    )    )
											self.assertTrue(hasattr(__snake_case       ,   """do_normalize"""    )    )
											self.assertTrue(hasattr(__snake_case       ,   """image_mean"""    )    )
											self.assertTrue(hasattr(__snake_case       ,   """image_std"""    )    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Dict							= self.image_processing_class.from_dict(self.image_processor_dict    )
											self.assertEqual(image_processor.size       ,   {"""shortest_edge""": 30}    )
											self.assertEqual(image_processor.crop_size       ,   {"""height""": 30, """width""": 30}    )

											_SCREAMING_SNAKE_CASE					:			Dict							= self.image_processing_class.from_dict(self.image_processor_dict       ,   size=42       ,   crop_size=84    )
											self.assertEqual(image_processor.size       ,   {"""shortest_edge""": 42}    )
											self.assertEqual(image_processor.crop_size       ,   {"""height""": 84, """width""": 84}    )






							def UpperCAmelCase_      (       self    ):
											pass






							def UpperCAmelCase_      (       self    ):
											# Initialize image_processing
											_SCREAMING_SNAKE_CASE					:			List[str]							= self.image_processing_class(**self.image_processor_dict    )
											# create random PIL images
											_SCREAMING_SNAKE_CASE					:			str							= prepare_image_inputs(self.image_processor_tester       ,   equal_resolution=__snake_case    )
											for image in image_inputs:
															self.assertIsInstance(__snake_case       ,   Image.Image    )

											# Test not batched input
											_SCREAMING_SNAKE_CASE					:			int							= image_processing(image_inputs[0]       ,   return_tensors="""pt"""    ).pixel_values

											self.assertEqual(
											    encoded_images.shape       ,   (
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )       ,   )

											# Test batched
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= image_processing(__snake_case       ,   return_tensors="""pt"""    ).pixel_values
											self.assertEqual(
											    encoded_images.shape       ,   (
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )       ,   )






							def UpperCAmelCase_      (       self    ):
											# Initialize image_processing
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= self.image_processing_class(**self.image_processor_dict    )
											# create random numpy tensors
											_SCREAMING_SNAKE_CASE					:			List[Any]							= prepare_image_inputs(self.image_processor_tester       ,   equal_resolution=__snake_case       ,   numpify=__snake_case    )
											for image in image_inputs:
															self.assertIsInstance(__snake_case       ,   np.ndarray    )

											# Test not batched input
											_SCREAMING_SNAKE_CASE					:			int							= image_processing(image_inputs[0]       ,   return_tensors="""pt"""    ).pixel_values
											self.assertEqual(
											    encoded_images.shape       ,   (
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )       ,   )

											# Test batched
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= image_processing(__snake_case       ,   return_tensors="""pt"""    ).pixel_values
											self.assertEqual(
											    encoded_images.shape       ,   (
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )       ,   )






							def UpperCAmelCase_      (       self    ):
											# Initialize image_processing
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= self.image_processing_class(**self.image_processor_dict    )
											# create random PyTorch tensors
											_SCREAMING_SNAKE_CASE					:			Dict							= prepare_image_inputs(self.image_processor_tester       ,   equal_resolution=__snake_case       ,   torchify=__snake_case    )
											for image in image_inputs:
															self.assertIsInstance(__snake_case       ,   torch.Tensor    )

											# Test not batched input
											_SCREAMING_SNAKE_CASE					:			List[Any]							= image_processing(image_inputs[0]       ,   return_tensors="""pt"""    ).pixel_values
											self.assertEqual(
											    encoded_images.shape       ,   (
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )       ,   )

											# Test batched
											_SCREAMING_SNAKE_CASE					:			List[str]							= image_processing(__snake_case       ,   return_tensors="""pt"""    ).pixel_values
											self.assertEqual(
											    encoded_images.shape       ,   (
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size["""height"""],
											        self.image_processor_tester.crop_size["""width"""],
											    )       ,   )








'''simple docstring'''


from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


UpperCAmelCase_						:  int				  =						logging.get_logger(__name__)


if is_vision_available():
				import PIL





class        lowercase__    (		_snake_case   ):





							'''simple docstring'''


							A_				: int       =    ["""pixel_values"""]






							def __init__(       self       ,   __snake_case = True       ,   __snake_case = None       ,   __snake_case = PILImageResampling.BICUBIC       ,   __snake_case = True       ,   __snake_case = None       ,   __snake_case = True       ,   __snake_case = 1 / 255       ,   __snake_case = True       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = True       ,   **__snake_case       ,   ):
											super().__init__(**__snake_case    )
											_SCREAMING_SNAKE_CASE					:			int							= size if size is not None else {"""shortest_edge""": 224}
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= get_size_dict(__snake_case       ,   default_to_square=__snake_case    )
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
											_SCREAMING_SNAKE_CASE					:			Any							= get_size_dict(__snake_case       ,   default_to_square=__snake_case       ,   param_name="""crop_size"""    )

											_SCREAMING_SNAKE_CASE					:			str							= do_resize
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= size
											_SCREAMING_SNAKE_CASE					:			List[str]							= resample
											_SCREAMING_SNAKE_CASE					:			str							= do_center_crop
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= crop_size
											_SCREAMING_SNAKE_CASE					:			Tuple							= do_rescale
											_SCREAMING_SNAKE_CASE					:			Dict							= rescale_factor
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= do_normalize
											_SCREAMING_SNAKE_CASE					:			List[Any]							= image_mean if image_mean is not None else OPENAI_CLIP_MEAN
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= image_std if image_std is not None else OPENAI_CLIP_STD
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= do_convert_rgb






							def UpperCAmelCase_      (       self       ,   __snake_case       ,   __snake_case       ,   __snake_case = PILImageResampling.BICUBIC       ,   __snake_case = None       ,   **__snake_case       ,   ):
											_SCREAMING_SNAKE_CASE					:			int							= get_size_dict(__snake_case       ,   default_to_square=__snake_case    )
											if "shortest_edge" not in size:
															raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"""    )
											_SCREAMING_SNAKE_CASE					:			Any							= get_resize_output_image_size(__snake_case       ,   size=size["""shortest_edge"""]       ,   default_to_square=__snake_case    )
											return resize(__snake_case       ,   size=__snake_case       ,   resample=__snake_case       ,   data_format=__snake_case       ,   **__snake_case    )






							def UpperCAmelCase_      (       self       ,   __snake_case       ,   __snake_case       ,   __snake_case = None       ,   **__snake_case       ,   ):
											_SCREAMING_SNAKE_CASE					:			List[str]							= get_size_dict(__snake_case    )
											if "height" not in size or "width" not in size:
															raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}"""    )
											return center_crop(__snake_case       ,   size=(size["""height"""], size["""width"""])       ,   data_format=__snake_case       ,   **__snake_case    )






							def UpperCAmelCase_      (       self       ,   __snake_case       ,   __snake_case       ,   __snake_case = None       ,   **__snake_case       ,   ):
											return rescale(__snake_case       ,   scale=__snake_case       ,   data_format=__snake_case       ,   **__snake_case    )






							def UpperCAmelCase_      (       self       ,   __snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case = None       ,   **__snake_case       ,   ):
											return normalize(__snake_case       ,   mean=__snake_case       ,   std=__snake_case       ,   data_format=__snake_case       ,   **__snake_case    )






							def UpperCAmelCase_      (       self       ,   __snake_case       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = None       ,   __snake_case = ChannelDimension.FIRST       ,   **__snake_case       ,   ):
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= do_resize if do_resize is not None else self.do_resize
											_SCREAMING_SNAKE_CASE					:			Any							= size if size is not None else self.size
											_SCREAMING_SNAKE_CASE					:			List[str]							= get_size_dict(__snake_case       ,   param_name="""size"""       ,   default_to_square=__snake_case    )
											_SCREAMING_SNAKE_CASE					:			Any							= resample if resample is not None else self.resample
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= do_center_crop if do_center_crop is not None else self.do_center_crop
											_SCREAMING_SNAKE_CASE					:			str							= crop_size if crop_size is not None else self.crop_size
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= get_size_dict(__snake_case       ,   param_name="""crop_size"""       ,   default_to_square=__snake_case    )
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= do_rescale if do_rescale is not None else self.do_rescale
											_SCREAMING_SNAKE_CASE					:			int							= rescale_factor if rescale_factor is not None else self.rescale_factor
											_SCREAMING_SNAKE_CASE					:			Tuple							= do_normalize if do_normalize is not None else self.do_normalize
											_SCREAMING_SNAKE_CASE					:			List[str]							= image_mean if image_mean is not None else self.image_mean
											_SCREAMING_SNAKE_CASE					:			List[Any]							= image_std if image_std is not None else self.image_std
											_SCREAMING_SNAKE_CASE					:			Any							= do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= make_list_of_images(__snake_case    )

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

											if do_resize and size is None:
															raise ValueError("""Size must be specified if do_resize is True."""    )

											if do_center_crop and crop_size is None:
															raise ValueError("""Crop size must be specified if do_center_crop is True."""    )

											if do_rescale and rescale_factor is None:
															raise ValueError("""Rescale factor must be specified if do_rescale is True."""    )

											if do_normalize and (image_mean is None or image_std is None):
															raise ValueError("""Image mean and std must be specified if do_normalize is True."""    )

											# PIL RGBA images are converted to RGB
											if do_convert_rgb:
															_SCREAMING_SNAKE_CASE					:			Optional[int]							= [convert_to_rgb(__snake_case    ) for image in images]

											# All transformations expect numpy arrays.
											_SCREAMING_SNAKE_CASE					:			str							= [to_numpy_array(__snake_case    ) for image in images]

											if do_resize:
															_SCREAMING_SNAKE_CASE					:			Optional[Any]							= [self.resize(image=__snake_case       ,   size=__snake_case       ,   resample=__snake_case    ) for image in images]

											if do_center_crop:
															_SCREAMING_SNAKE_CASE					:			Tuple							= [self.center_crop(image=__snake_case       ,   size=__snake_case    ) for image in images]

											if do_rescale:
															_SCREAMING_SNAKE_CASE					:			List[Any]							= [self.rescale(image=__snake_case       ,   scale=__snake_case    ) for image in images]

											if do_normalize:
															_SCREAMING_SNAKE_CASE					:			Dict							= [self.normalize(image=__snake_case       ,   mean=__snake_case       ,   std=__snake_case    ) for image in images]

											_SCREAMING_SNAKE_CASE					:			str							= [to_channel_dimension_format(__snake_case       ,   __snake_case    ) for image in images]

											_SCREAMING_SNAKE_CASE					:			int							= {"""pixel_values""": images}
											return BatchFeature(data=__snake_case       ,   tensor_type=__snake_case    )




