



import unittest

import numpy as np
from datasets import load_dataset

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 SegformerImageProcessor


class 	lowerCamelCase__ (		unittest.TestCase   ):


						def __init__( self						:  Optional[int]      ,       _a						:  Any      ,       _a						:  Any=7      ,       _a						:  Optional[Any]=3      ,       _a						:  List[Any]=3_0      ,       _a						:  List[Any]=4_0_0      ,       _a						:  int=True      ,       _a						:  Optional[int]=None      ,       _a						:  List[str]=True      ,       _a						:  int=[0.5, 0.5, 0.5]      ,       _a						:  Dict=[0.5, 0.5, 0.5]      ,       _a						:  int=False      ,       ):
													a__:       List[str]											=size if size is not None else {"height": 3_0, "width": 3_0}
													a__:       List[Any]											=parent
													a__:       str											=batch_size
													a__:       Union[str, Any]											=num_channels
													a__:       Any											=min_resolution
													a__:       int											=max_resolution
													a__:       List[Any]											=do_resize
													a__:       int											=size
													a__:       str											=do_normalize
													a__:       List[str]											=image_mean
													a__:       Dict											=image_std
													a__:       Optional[int]											=do_reduce_labels







						def   _lowerCamelCase			( self						:  List[Any]			):
													return {
													    "do_resize": self.do_resize,
													    "size": self.size,
													    "do_normalize": self.do_normalize,
													    "image_mean": self.image_mean,
													    "image_std": self.image_std,
													    "do_reduce_labels": self.do_reduce_labels,
													}





def 						__lowerCamelCase       (      ):
							a__:       Any											=load_dataset("hf-internal-testing/fixtures_ade20k"     , split="test"						)

							a__:       Tuple											=Image.open(dataset[0]["file"]						)
							a__:       List[Any]											=Image.open(dataset[1]["file"]						)

							return image, map





def 						__lowerCamelCase       (      ):
							a__:       Union[str, Any]											=load_dataset("hf-internal-testing/fixtures_ade20k"     , split="test"						)

							a__:       str											=Image.open(dataset[0]["file"]						)
							a__:       List[str]											=Image.open(dataset[1]["file"]						)
							a__:       List[Any]											=Image.open(dataset[2]["file"]						)
							a__:       Dict											=Image.open(dataset[3]["file"]						)

							return [imagea, imagea], [mapa, mapa]







@require_torch
@require_vision
class 	lowerCamelCase__ (		_a				,					unittest.TestCase   ):
						_lowerCAmelCase								= SegformerImageProcessor if is_vision_available() else None


						def   _lowerCamelCase			( self						:  List[Any]			):
													a__:       Optional[int]											=SegformerImageProcessingTester(self			)


						@property
						def   _lowerCamelCase			( self						:  Optional[int]			):
													return self.image_processor_tester.prepare_image_processor_dict()


						def   _lowerCamelCase			( self						:  Any			):
													a__:       str											=self.image_processing_class(**self.image_processor_dict			)
													self.assertTrue(hasattr(_a      ,       "do_resize"			)			)
													self.assertTrue(hasattr(_a      ,       "size"			)			)
													self.assertTrue(hasattr(_a      ,       "do_normalize"			)			)
													self.assertTrue(hasattr(_a      ,       "image_mean"			)			)
													self.assertTrue(hasattr(_a      ,       "image_std"			)			)
													self.assertTrue(hasattr(_a      ,       "do_reduce_labels"			)			)


						def   _lowerCamelCase			( self						:  Dict			):
													a__:       str											=self.image_processing_class.from_dict(self.image_processor_dict			)
													self.assertEqual(image_processor.size      ,       {"height": 3_0, "width": 3_0}			)
													self.assertEqual(image_processor.do_reduce_labels      ,       _a			)

													a__:       Union[str, Any]											=self.image_processing_class.from_dict(self.image_processor_dict      ,       size=4_2      ,       reduce_labels=_a			)
													self.assertEqual(image_processor.size      ,       {"height": 4_2, "width": 4_2}			)
													self.assertEqual(image_processor.do_reduce_labels      ,       _a			)


						def   _lowerCamelCase			( self						:  List[str]			):
													pass


						def   _lowerCamelCase			( self						:  List[Any]			):
													# Initialize image_processing
													a__:       Optional[Any]											=self.image_processing_class(**self.image_processor_dict			)
													# create random PIL images
													a__:       Any											=prepare_image_inputs(self.image_processor_tester      ,       equal_resolution=_a			)
													for image in image_inputs:
																				self.assertIsInstance(_a      ,       Image.Image			)

													# Test not batched input
													a__:       Optional[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.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )

													# Test batched
													a__:       Any											=image_processing(_a      ,       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.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )


						def   _lowerCamelCase			( self						:  Union[str, Any]			):
													# Initialize image_processing
													a__:       List[str]											=self.image_processing_class(**self.image_processor_dict			)
													# create random numpy tensors
													a__:       List[str]											=prepare_image_inputs(self.image_processor_tester      ,       equal_resolution=_a      ,       numpify=_a			)
													for image in image_inputs:
																				self.assertIsInstance(_a      ,       np.ndarray			)

													# Test not batched input
													a__:       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.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )

													# Test batched
													a__:       Dict											=image_processing(_a      ,       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.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )


						def   _lowerCamelCase			( self						:  Tuple			):
													# Initialize image_processing
													a__:       List[str]											=self.image_processing_class(**self.image_processor_dict			)
													# create random PyTorch tensors
													a__:       Optional[Any]											=prepare_image_inputs(self.image_processor_tester      ,       equal_resolution=_a      ,       torchify=_a			)
													for image in image_inputs:
																				self.assertIsInstance(_a      ,       torch.Tensor			)

													# Test not batched input
													a__:       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.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )

													# Test batched
													a__:       int											=image_processing(_a      ,       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.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )


						def   _lowerCamelCase			( self						:  Union[str, Any]			):
													# Initialize image_processing
													a__:       List[str]											=self.image_processing_class(**self.image_processor_dict			)
													# create random PyTorch tensors
													a__:       Optional[Any]											=prepare_image_inputs(self.image_processor_tester      ,       equal_resolution=_a      ,       torchify=_a			)
													a__:       Dict											=[]
													for image in image_inputs:
																				self.assertIsInstance(_a      ,       torch.Tensor			)
																				maps.append(torch.zeros(image.shape[-2:]			).long()			)

													# Test not batched input
													a__:       Union[str, Any]											=image_processing(image_inputs[0]      ,       maps[0]      ,       return_tensors="pt"			)
													self.assertEqual(
													    encoding["pixel_values"].shape      ,       (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )
													self.assertEqual(
													    encoding["labels"].shape      ,       (
													        1,
													        self.image_processor_tester.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )
													self.assertEqual(encoding["labels"].dtype      ,       torch.long			)
													self.assertTrue(encoding["labels"].min().item() >= 0			)
													self.assertTrue(encoding["labels"].max().item() <= 2_5_5			)

													# Test batched
													a__:       Optional[Any]											=image_processing(_a      ,       _a      ,       return_tensors="pt"			)
													self.assertEqual(
													    encoding["pixel_values"].shape      ,       (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )
													self.assertEqual(
													    encoding["labels"].shape      ,       (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )
													self.assertEqual(encoding["labels"].dtype      ,       torch.long			)
													self.assertTrue(encoding["labels"].min().item() >= 0			)
													self.assertTrue(encoding["labels"].max().item() <= 2_5_5			)

													# Test not batched input (PIL images)
													a__							,			a__:       Optional[Any]											=prepare_semantic_single_inputs()

													a__:       Any											=image_processing(_a      ,       _a      ,       return_tensors="pt"			)
													self.assertEqual(
													    encoding["pixel_values"].shape      ,       (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )
													self.assertEqual(
													    encoding["labels"].shape      ,       (
													        1,
													        self.image_processor_tester.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )
													self.assertEqual(encoding["labels"].dtype      ,       torch.long			)
													self.assertTrue(encoding["labels"].min().item() >= 0			)
													self.assertTrue(encoding["labels"].max().item() <= 2_5_5			)

													# Test batched input (PIL images)
													a__							,			a__:       str											=prepare_semantic_batch_inputs()

													a__:       Any											=image_processing(_a      ,       _a      ,       return_tensors="pt"			)
													self.assertEqual(
													    encoding["pixel_values"].shape      ,       (
													        2,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )
													self.assertEqual(
													    encoding["labels"].shape      ,       (
													        2,
													        self.image_processor_tester.size["height"],
													        self.image_processor_tester.size["width"],
													    )      ,       )
													self.assertEqual(encoding["labels"].dtype      ,       torch.long			)
													self.assertTrue(encoding["labels"].min().item() >= 0			)
													self.assertTrue(encoding["labels"].max().item() <= 2_5_5			)







						def   _lowerCamelCase			( self						:  List[str]			):
													# Initialize image_processing
													a__:       Any											=self.image_processing_class(**self.image_processor_dict			)

													# ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
													a__							,			a__:       List[str]											=prepare_semantic_single_inputs()
													a__:       Optional[int]											=image_processing(_a      ,       _a      ,       return_tensors="pt"			)
													self.assertTrue(encoding["labels"].min().item() >= 0			)
													self.assertTrue(encoding["labels"].max().item() <= 1_5_0			)

													a__:       Optional[int]											=True
													a__:       Tuple											=image_processing(_a      ,       _a      ,       return_tensors="pt"			)
													self.assertTrue(encoding["labels"].min().item() >= 0			)
													self.assertTrue(encoding["labels"].max().item() <= 2_5_5			)










from ...utils import is_transformers_available
from .pipeline_latent_diffusion_superresolution import LDMSuperResolutionPipeline


if is_transformers_available():
		from .pipeline_latent_diffusion import LDMBertModel, LDMTextToImagePipeline





