


"""simple docstring"""







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 BeitImageProcessor






class 				__A    (	unittest.TestCase      ):






 """simple docstring"""




 def __init__(		self			,   __A			,   __A=7			,   __A=3			,   __A=18			,   __A=30			,   __A=400			,   __A=True			,   __A=None			,   __A=True			,   __A=None			,   __A=True			,   __A=[0.5, 0.5, 0.5]			,   __A=[0.5, 0.5, 0.5]			,   __A=False			,   ) ->					Dict:
     a					=size if size is not None else {'''height''': 20, '''width''': 20}
     a					=crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
     a					=parent
     a					=batch_size
     a					=num_channels
     a					=image_size
     a					=min_resolution
     a					=max_resolution
     a					=do_resize
     a					=size
     a					=do_center_crop
     a					=crop_size
     a					=do_normalize
     a					=image_mean
     a					=image_std
     a					=do_reduce_labels





 def 				SCREAMING_SNAKE_CASE							(		self						) ->					int:
     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_reduce_labels": self.do_reduce_labels,
     }

def _A						(						):







    """simple docstring"""
    a					=load_dataset('''hf-internal-testing/fixtures_ade20k'''	,   split='''test'''		)

    a					=Image.open(dataset[0]['''file''']		)
    a					=Image.open(dataset[1]['''file''']		)

    return image, map
def _A						(						):







    """simple docstring"""
    a					=load_dataset('''hf-internal-testing/fixtures_ade20k'''	,   split='''test'''		)

    a					=Image.open(ds[0]['''file''']		)
    a					=Image.open(ds[1]['''file''']		)
    a					=Image.open(ds[2]['''file''']		)
    a					=Image.open(ds[3]['''file''']		)

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






@require_torch
@require_vision
class 				__A    (	_SCREAMING_SNAKE_CASE,   unittest.TestCase      ):






 """simple docstring"""




 __lowerCAmelCase			 =  BeitImageProcessor if is_vision_available() else None


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Dict:
     a					=BeitImageProcessingTester(self						)


 @property
 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[Any]:
     return self.image_processor_tester.prepare_image_processor_dict()


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Any:
     a					=self.image_processing_class(**self.image_processor_dict						)
     self.assertTrue(hasattr(__A			,   '''do_resize'''						)						)
     self.assertTrue(hasattr(__A			,   '''size'''						)						)
     self.assertTrue(hasattr(__A			,   '''do_center_crop'''						)						)
     self.assertTrue(hasattr(__A			,   '''center_crop'''						)						)
     self.assertTrue(hasattr(__A			,   '''do_normalize'''						)						)
     self.assertTrue(hasattr(__A			,   '''image_mean'''						)						)
     self.assertTrue(hasattr(__A			,   '''image_std'''						)						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Tuple:
     a					=self.image_processing_class.from_dict(self.image_processor_dict						)
     self.assertEqual(image_processor.size			,   {'''height''': 20, '''width''': 20}						)
     self.assertEqual(image_processor.crop_size			,   {'''height''': 18, '''width''': 18}						)
     self.assertEqual(image_processor.do_reduce_labels			,   __A						)

     a					=self.image_processing_class.from_dict(
         self.image_processor_dict			,   size=42			,   crop_size=84			,   reduce_labels=__A						)
     self.assertEqual(image_processor.size			,   {'''height''': 42, '''width''': 42}						)
     self.assertEqual(image_processor.crop_size			,   {'''height''': 84, '''width''': 84}						)
     self.assertEqual(image_processor.do_reduce_labels			,   __A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					int:
     pass


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     # Initialize image_processing
     a					=self.image_processing_class(**self.image_processor_dict						)
     # create random PIL images
     a					=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					=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(__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.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Tuple:
     # Initialize image_processing
     a					=self.image_processing_class(**self.image_processor_dict						)
     # create random numpy tensors
     a					=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					=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(__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.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					List[Any]:
     # Initialize image_processing
     a					=self.image_processing_class(**self.image_processor_dict						)
     # create random PyTorch tensors
     a					=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					=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(__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.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					int:
     # Initialize image_processing
     a					=self.image_processing_class(**self.image_processor_dict						)
     # create random PyTorch tensors
     a					=prepare_image_inputs(self.image_processor_tester			,   equal_resolution=__A			,   torchify=__A						)
     a					=[]
     for image in image_inputs:
         self.assertIsInstance(__A			,   torch.Tensor						)
         maps.append(torch.zeros(image.shape[-2:]						).long()						)

     # Test not batched input
     a					=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.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )
     self.assertEqual(
         encoding['''labels'''].shape			,   (
             1,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )
     self.assertEqual(encoding['''labels'''].dtype			,   torch.long						)
     self.assertTrue(encoding['''labels'''].min().item() >= 0						)
     self.assertTrue(encoding['''labels'''].max().item() <= 255						)

     # Test batched
     a					=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.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )
     self.assertEqual(
         encoding['''labels'''].shape			,   (
             self.image_processor_tester.batch_size,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )
     self.assertEqual(encoding['''labels'''].dtype			,   torch.long						)
     self.assertTrue(encoding['''labels'''].min().item() >= 0						)
     self.assertTrue(encoding['''labels'''].max().item() <= 255						)

     # Test not batched input (PIL images)
     a		,				a					=prepare_semantic_single_inputs()

     a					=image_processing(__A			,   __A			,   return_tensors='''pt'''						)
     self.assertEqual(
         encoding['''pixel_values'''].shape			,   (
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )
     self.assertEqual(
         encoding['''labels'''].shape			,   (
             1,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )
     self.assertEqual(encoding['''labels'''].dtype			,   torch.long						)
     self.assertTrue(encoding['''labels'''].min().item() >= 0						)
     self.assertTrue(encoding['''labels'''].max().item() <= 255						)

     # Test batched input (PIL images)
     a		,				a					=prepare_semantic_batch_inputs()

     a					=image_processing(__A			,   __A			,   return_tensors='''pt'''						)
     self.assertEqual(
         encoding['''pixel_values'''].shape			,   (
             2,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )
     self.assertEqual(
         encoding['''labels'''].shape			,   (
             2,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )			,   )
     self.assertEqual(encoding['''labels'''].dtype			,   torch.long						)
     self.assertTrue(encoding['''labels'''].min().item() >= 0						)
     self.assertTrue(encoding['''labels'''].max().item() <= 255						)





 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     # Initialize image_processing
     a					=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					=prepare_semantic_single_inputs()
     a					=image_processing(__A			,   __A			,   return_tensors='''pt'''						)
     self.assertTrue(encoding['''labels'''].min().item() >= 0						)
     self.assertTrue(encoding['''labels'''].max().item() <= 150						)

     a					=True
     a					=image_processing(__A			,   __A			,   return_tensors='''pt'''						)
     self.assertTrue(encoding['''labels'''].min().item() >= 0						)
     self.assertTrue(encoding['''labels'''].max().item() <= 255						)



"""simple docstring"""







from ...processing_utils import ProcessorMixin






class 				__A    (	_SCREAMING_SNAKE_CASE      ):






 """simple docstring"""




 __lowerCAmelCase			 =  "WhisperFeatureExtractor"
 __lowerCAmelCase			 =  "WhisperTokenizer"


 def __init__(		self			,   __A			,   __A						) ->					Dict:
     super().__init__(__A			,   __A						)
     a					=self.feature_extractor
     a					=False


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A=None			,   __A=None			,   __A=True						) ->					int:
     return self.tokenizer.get_decoder_prompt_ids(task=__A			,   language=__A			,   no_timestamps=__A						)


 def __call__(		self			,   *__A			,   **__A						) ->					Tuple:
     # For backward compatibility
     if self._in_target_context_manager:
         return self.current_processor(*__A			,   **__A						)

     a					=kwargs.pop('''audio'''			,   __A						)
     a					=kwargs.pop('''sampling_rate'''			,   __A						)
     a					=kwargs.pop('''text'''			,   __A						)
     if len(__A						) > 0:
         a					=args[0]
         a					=args[1:]

     if audio is None and text is None:
         raise ValueError('''You need to specify either an `audio` or `text` input to process.'''						)

     if audio is not None:
         a					=self.feature_extractor(__A			,   *__A			,   sampling_rate=__A			,   **__A						)
     if text is not None:
         a					=self.tokenizer(__A			,   **__A						)

     if text is None:
         return inputs

     elif audio is None:
         return encodings
     else:
         a					=encodings['''input_ids''']
         return inputs


 def 				SCREAMING_SNAKE_CASE							(		self			,   *__A			,   **__A						) ->					Optional[Any]:
     return self.tokenizer.batch_decode(*__A			,   **__A						)


 def 				SCREAMING_SNAKE_CASE							(		self			,   *__A			,   **__A						) ->					Union[str, Any]:
     return self.tokenizer.decode(*__A			,   **__A						)





 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A="np"						) ->					Optional[Any]:
     return self.tokenizer.get_prompt_ids(__A			,   return_tensors=__A						)