

from importlib import import_module

from .logging import get_logger


lowerCAmelCase__	   = get_logger(__name__)

class 					a__						:
			"""simple docstring"""







			def __init__(    self      ,      lowercase      ,      lowercase=None       )  ->						Tuple:
							'''simple docstring'''






							A__        =			attrs or []
							if module is not None:
											for key in module.__dict__:
															if key in attrs or not key.startswith("__"       ):
																			setattr(self      ,      lowercase      ,      getattr(lowercase      ,      lowercase       )       )
							A__        =			module._original_module if isinstance(lowercase      ,      _PatchedModuleObj       ) else module

class 					a__						:
			"""simple docstring"""







			__lowerCamelCase        =       []


			def __init__(    self      ,      lowercase      ,      lowercase      ,      lowercase      ,      lowercase=None       )  ->						str:
							'''simple docstring'''






							A__        =			obj
							A__        =			target
							A__        =			new
							A__        =			target.split("."       )[0]
							A__        =			{}
							A__        =			attrs or []


			def __enter__(    self       )  ->						str:
							'''simple docstring'''






							*A__    ,				A__        =			self.target.split("."       )

							# Patch modules:
							# it's used to patch attributes of submodules like "os.path.join";
							# in this case we need to patch "os" and "os.path"

							for i in range(len(lowercase       )       ):
											try:
															A__        =			import_module(".".join(submodules[: i + 1]       )       )
											except ModuleNotFoundError:
															continue
											# We iterate over all the globals in self.obj in case we find "os" or "os.path"
											for attr in self.obj.__dir__():
															A__        =			getattr(self.obj      ,      lowercase       )
															# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
															# This allows to patch renamed modules like "from os import path as ospath".
															if obj_attr is submodule or (
															    (isinstance(lowercase      ,      _PatchedModuleObj       ) and obj_attr._original_module is submodule)
															):
																			A__        =			obj_attr
																			# patch at top level
																			setattr(self.obj      ,      lowercase      ,      _PatchedModuleObj(lowercase      ,      attrs=self.attrs       )       )
																			A__        =			getattr(self.obj      ,      lowercase       )
																			# construct lower levels patches
																			for key in submodules[i + 1 :]:
																							setattr(lowercase      ,      lowercase      ,      _PatchedModuleObj(getattr(lowercase      ,      lowercase      ,      lowercase       )      ,      attrs=self.attrs       )       )
																							A__        =			getattr(lowercase      ,      lowercase       )
																			# finally set the target attribute
																			setattr(lowercase      ,      lowercase      ,      self.new       )

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

							if submodules:  # if it's an attribute of a submodule like "os.path.join"
											try:
															A__        =			getattr(import_module(".".join(lowercase       )       )      ,      lowercase       )
											except (AttributeError, ModuleNotFoundError):
															return
											# We iterate over all the globals in self.obj in case we find "os.path.join"
											for attr in self.obj.__dir__():
															# We don't check for the name of the global, but rather if its value *is* "os.path.join".
															# This allows to patch renamed attributes like "from os.path import join as pjoin".
															if getattr(self.obj      ,      lowercase       ) is attr_value:
																			A__        =			getattr(self.obj      ,      lowercase       )
																			setattr(self.obj      ,      lowercase      ,      self.new       )
							elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
											A__        =			globals()["__builtins__"][target_attr]
											setattr(self.obj      ,      lowercase      ,      self.new       )
							else:
											raise RuntimeError(F'Tried to patch attribute {target_attr} instead of a submodule.'       )


			def __exit__(    self      ,      *lowercase       )  ->						str:
							'''simple docstring'''






							for attr in list(self.original       ):
											setattr(self.obj      ,      lowercase      ,      self.original.pop(lowercase       )       )


			def      UpperCamelCase		(    self       )  ->						Optional[int]:
							'''simple docstring'''






							self.__enter__()
							self._active_patches.append(self       )


			def      UpperCamelCase		(    self       )  ->						Any:
							'''simple docstring'''






							try:
											self._active_patches.remove(self       )
							except ValueError:
											# If the patch hasn't been started this will fail
											return None

							return self.__exit__()




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 					a__						(					unittest.TestCase			):
			"""simple docstring"""







			def __init__(    self      ,      lowercase      ,      lowercase=7      ,      lowercase=3      ,      lowercase=30      ,      lowercase=400      ,      lowercase=True      ,      lowercase=None      ,      lowercase=0.9      ,      lowercase=None      ,      lowercase=True      ,      lowercase=[0.5, 0.5, 0.5]      ,      lowercase=[0.5, 0.5, 0.5]      ,      )  ->						str:
							'''simple docstring'''






							A__        =			size if size is not None else {"shortest_edge": 30}
							A__        =			crop_size if crop_size is not None else {"height": 30, "width": 30}
							A__        =			parent
							A__        =			batch_size
							A__        =			num_channels
							A__        =			min_resolution
							A__        =			max_resolution
							A__        =			do_resize_and_center_crop
							A__        =			size
							A__        =			crop_pct
							A__        =			crop_size
							A__        =			do_normalize
							A__        =			image_mean
							A__        =			image_std


			def      UpperCamelCase		(    self       )  ->						int:
							'''simple docstring'''






							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 					a__						(					snake_case							,	unittest.TestCase			):
			"""simple docstring"""







			__lowerCamelCase        =       PoolFormerImageProcessor if is_vision_available() else None


			def      UpperCamelCase		(    self       )  ->						str:
							'''simple docstring'''






							A__        =			PoolFormerImageProcessingTester(self       )


			@property
			def      UpperCamelCase		(    self       )  ->						List[str]:
							'''simple docstring'''






							return self.image_processor_tester.prepare_image_processor_dict()


			def      UpperCamelCase		(    self       )  ->						Any:
							'''simple docstring'''






							A__        =			self.image_processing_class(**self.image_processor_dict       )
							self.assertTrue(hasattr(lowercase      ,      "do_resize_and_center_crop"       )       )
							self.assertTrue(hasattr(lowercase      ,      "size"       )       )
							self.assertTrue(hasattr(lowercase      ,      "crop_pct"       )       )
							self.assertTrue(hasattr(lowercase      ,      "do_normalize"       )       )
							self.assertTrue(hasattr(lowercase      ,      "image_mean"       )       )
							self.assertTrue(hasattr(lowercase      ,      "image_std"       )       )


			def      UpperCamelCase		(    self       )  ->						List[str]:
							'''simple docstring'''






							A__        =			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}       )

							A__        =			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       )  ->						Optional[int]:
							'''simple docstring'''






							pass


			def      UpperCamelCase		(    self       )  ->						List[str]:
							'''simple docstring'''






							A__        =			self.image_processing_class(**self.image_processor_dict       )
							# create random PIL images
							A__        =			prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowercase       )
							for image in image_inputs:
											self.assertIsInstance(lowercase      ,      Image.Image       )

							# Test not batched input
							A__        =			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
							A__        =			image_processing(lowercase      ,      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       )  ->						Tuple:
							'''simple docstring'''






							A__        =			self.image_processing_class(**self.image_processor_dict       )
							# create random numpy tensors
							A__        =			prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowercase      ,      numpify=lowercase       )
							for image in image_inputs:
											self.assertIsInstance(lowercase      ,      np.ndarray       )

							# Test not batched input
							A__        =			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
							A__        =			image_processing(lowercase      ,      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       )  ->						Dict:
							'''simple docstring'''






							A__        =			self.image_processing_class(**self.image_processor_dict       )
							# create random PyTorch tensors
							A__        =			prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowercase      ,      torchify=lowercase       )
							for image in image_inputs:
											self.assertIsInstance(lowercase      ,      torch.Tensor       )

							# Test not batched input
							A__        =			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
							A__        =			image_processing(lowercase      ,      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"],
							    )      ,      )

