

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


if is_torch_available():
				import torch

if is_vision_available():
				from PIL import Image

				from transformers import CLIPImageProcessor

class   __UpperCamelCase      (   unittest.TestCase  ):




	def __init__(						self		:  Dict						,		lowerCAmelCase		:  List[str]						,		lowerCAmelCase		:  Optional[Any]=7						,		lowerCAmelCase		:  int=3						,		lowerCAmelCase		:  Union[str, Any]=18						,		lowerCAmelCase		:  Optional[Any]=30						,		lowerCAmelCase		:  Optional[int]=400						,		lowerCAmelCase		:  str=True						,		lowerCAmelCase		:  Any=None						,		lowerCAmelCase		:  str=True						,		lowerCAmelCase		:  str=None						,		lowerCAmelCase		:  Union[str, Any]=True						,		lowerCAmelCase		:  Optional[Any]=[0.48_145_466, 0.4_578_275, 0.40_821_073]						,		lowerCAmelCase		:  Dict=[0.26_862_954, 0.26_130_258, 0.27_577_711]						,		lowerCAmelCase		:  List[str]=True						,		):


		'''simple docstring'''







		UpperCAmelCase_    			=     size if size is not None else {"shortest_edge": 20}
		UpperCAmelCase_    			=     crop_size if crop_size is not None else {"height": 18, "width": 18}
		UpperCAmelCase_    			=     parent
		UpperCAmelCase_    			=     batch_size
		UpperCAmelCase_    			=     num_channels
		UpperCAmelCase_    			=     image_size
		UpperCAmelCase_    			=     min_resolution
		UpperCAmelCase_    			=     max_resolution
		UpperCAmelCase_    			=     do_resize
		UpperCAmelCase_    			=     size
		UpperCAmelCase_    			=     do_center_crop
		UpperCAmelCase_    			=     crop_size
		UpperCAmelCase_    			=     do_normalize
		UpperCAmelCase_    			=     image_mean
		UpperCAmelCase_    			=     image_std
		UpperCAmelCase_    			=     do_convert_rgb




	def  __A   (						self		:  int							):


		'''simple docstring'''







		return {
		    "do_resize": self.do_resize,
		    "size": self.size,
		    "do_center_crop": self.do_center_crop,
		    "crop_size": self.crop_size,
		    "do_normalize": self.do_normalize,
		    "image_mean": self.image_mean,
		    "image_std": self.image_std,
		    "do_convert_rgb": self.do_convert_rgb,
		}




	def  __A   (						self		:  Optional[Any]						,		lowerCAmelCase		:  Union[str, Any]=False						,		lowerCAmelCase		:  Optional[int]=False						,		lowerCAmelCase		:  Tuple=False							):


		'''simple docstring'''







		assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

		if equal_resolution:
			UpperCAmelCase_    			=     []
			for i in range(self.batch_size							):
				image_inputs.append(
				    np.random.randint(
				        255						,		size=(self.num_channels, self.max_resolution, self.max_resolution)						,		dtype=np.uinta							)							)
		else:
			UpperCAmelCase_    			=     []
			for i in range(self.batch_size							):
				UpperCAmelCase_			,				UpperCAmelCase_    			=     np.random.choice(np.arange(self.min_resolution						,		self.max_resolution							)						,		2							)
				image_inputs.append(np.random.randint(255						,		size=(self.num_channels, width, height)						,		dtype=np.uinta							)							)

		if not numpify and not torchify:
			# PIL expects the channel dimension as last dimension
			UpperCAmelCase_    			=     [Image.fromarray(np.moveaxis(lowerCAmelCase						,		0						,		-1							)							) for x in image_inputs]

		if torchify:
			UpperCAmelCase_    			=     [torch.from_numpy(lowerCAmelCase							) for x in image_inputs]

		return image_inputs


@require_torch
@require_vision
class   __UpperCamelCase      (   lowercase       ,       unittest.TestCase  ):
	SCREAMING_SNAKE_CASE__      			=       CLIPImageProcessor if is_vision_available() else None




	def  __A   (						self		:  Optional[Any]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     CLIPImageProcessingTester(self							)




	@property
	def  __A   (						self		:  List[Any]							):


		'''simple docstring'''







		return self.image_processor_tester.prepare_image_processor_dict()




	def  __A   (						self		:  Any							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.image_processing_class(**self.image_processor_dict							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"do_resize"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"size"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"do_center_crop"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"center_crop"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"do_normalize"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"image_mean"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"image_std"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"do_convert_rgb"							)							)




	def  __A   (						self		:  int							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.image_processing_class.from_dict(self.image_processor_dict							)
		self.assertEqual(image_processor.size						,		{"shortest_edge": 20}							)
		self.assertEqual(image_processor.crop_size						,		{"height": 18, "width": 18}							)

		UpperCAmelCase_    			=     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  __A   (						self		:  List[str]							):


		'''simple docstring'''







		pass




	def  __A   (						self		:  Dict							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.image_processing_class(**self.image_processor_dict							)
		# create random PIL images
		UpperCAmelCase_    			=     self.image_processor_tester.prepare_inputs(equal_resolution=lowerCAmelCase							)
		for image in image_inputs:
			self.assertIsInstance(lowerCAmelCase						,		Image.Image							)

		# Test not batched input
		UpperCAmelCase_    			=     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
		UpperCAmelCase_    			=     image_processing(lowerCAmelCase						,		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  __A   (						self		:  Optional[int]							):


		'''simple docstring'''







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

		# Test not batched input
		UpperCAmelCase_    			=     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
		UpperCAmelCase_    			=     image_processing(lowerCAmelCase						,		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  __A   (						self		:  str							):


		'''simple docstring'''







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

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


@require_torch
@require_vision
class   __UpperCamelCase      (   lowercase       ,       unittest.TestCase  ):
	SCREAMING_SNAKE_CASE__      			=       CLIPImageProcessor if is_vision_available() else None




	def  __A   (						self		:  List[str]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     CLIPImageProcessingTester(self						,		num_channels=4							)
		UpperCAmelCase_    			=     3




	@property
	def  __A   (						self		:  str							):


		'''simple docstring'''







		return self.image_processor_tester.prepare_image_processor_dict()




	def  __A   (						self		:  List[str]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.image_processing_class(**self.image_processor_dict							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"do_resize"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"size"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"do_center_crop"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"center_crop"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"do_normalize"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"image_mean"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"image_std"							)							)
		self.assertTrue(hasattr(lowerCAmelCase						,		"do_convert_rgb"							)							)




	def  __A   (						self		:  List[str]							):


		'''simple docstring'''







		pass




	def  __A   (						self		:  Dict							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.image_processing_class(**self.image_processor_dict							)
		# create random PIL images
		UpperCAmelCase_    			=     self.image_processor_tester.prepare_inputs(equal_resolution=lowerCAmelCase							)
		for image in image_inputs:
			self.assertIsInstance(lowerCAmelCase						,		Image.Image							)

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

		# Test batched
		UpperCAmelCase_    			=     image_processing(lowerCAmelCase						,		return_tensors="pt"							).pixel_values
		self.assertEqual(
		    encoded_images.shape						,		(
		        self.image_processor_tester.batch_size,
		        self.expected_encoded_image_num_channels,
		        self.image_processor_tester.crop_size["height"],
		        self.image_processor_tester.crop_size["width"],
		    )						,		)


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


_a:      Tuple								=     {
    """configuration_segformer""": ["""SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SegformerConfig""", """SegformerOnnxConfig"""]
}

try:
				if not is_vision_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_a:      List[str]								=     ["""SegformerFeatureExtractor"""]
				_a:      Union[str, Any]								=     ["""SegformerImageProcessor"""]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_a:      Any								=     [
				    """SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """SegformerDecodeHead""",
				    """SegformerForImageClassification""",
				    """SegformerForSemanticSegmentation""",
				    """SegformerLayer""",
				    """SegformerModel""",
				    """SegformerPreTrainedModel""",
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_a:      Optional[Any]								=     [
				    """TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """TFSegformerDecodeHead""",
				    """TFSegformerForImageClassification""",
				    """TFSegformerForSemanticSegmentation""",
				    """TFSegformerModel""",
				    """TFSegformerPreTrainedModel""",
				]


if TYPE_CHECKING:
				from .configuration_segformer import SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SegformerConfig, SegformerOnnxConfig

				try:
								if not is_vision_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .feature_extraction_segformer import SegformerFeatureExtractor
								from .image_processing_segformer import SegformerImageProcessor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_segformer import (
								    SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    SegformerDecodeHead,
								    SegformerForImageClassification,
								    SegformerForSemanticSegmentation,
								    SegformerLayer,
								    SegformerModel,
								    SegformerPreTrainedModel,
								)
				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_segformer import (
								    TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFSegformerDecodeHead,
								    TFSegformerForImageClassification,
								    TFSegformerForSemanticSegmentation,
								    TFSegformerModel,
								    TFSegformerPreTrainedModel,
								)

else:
				import sys

				_a:      Optional[int]								=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)