


'''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 SegformerImageProcessor






class 				__lowerCAmelCase      (      unittest.TestCase	):

						"""simple docstring"""






						def __init__(     self       :			int   ,     lowerCAmelCase       :			Any   ,     lowerCAmelCase       :			str=7   ,     lowerCAmelCase       :			List[str]=3   ,     lowerCAmelCase       :			Optional[int]=30   ,     lowerCAmelCase       :			str=4_00   ,     lowerCAmelCase       :			Any=True   ,     lowerCAmelCase       :			Optional[Any]=None   ,     lowerCAmelCase       :			Dict=True   ,     lowerCAmelCase       :			Optional[int]=[0.5, 0.5, 0.5]   ,     lowerCAmelCase       :			str=[0.5, 0.5, 0.5]   ,     lowerCAmelCase       :			str=False   ,     ):
													A_ 						=		size if 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
													A_ 						=		size
													A_ 						=		do_normalize
													A_ 						=		image_mean
													A_ 						=		image_std
													A_ 						=		do_reduce_labels






						def 							_UpperCAmelCase					(     self       :			List[str]  ):
													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 			a_							(  ):
							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_							(  ):
							A_ 						=		load_dataset("hf-internal-testing/fixtures_ade20k"	,     split="test"   )

							A_ 						=		Image.open(dataset[0]["file"]   )
							A_ 						=		Image.open(dataset[1]["file"]   )
							A_ 						=		Image.open(dataset[2]["file"]   )
							A_ 						=		Image.open(dataset[3]["file"]   )

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







@require_torch
@require_vision
class 				__lowerCAmelCase      (      lowercase							, unittest.TestCase	):

						"""simple docstring"""






						_UpperCAmelCase		:					Dict      	=SegformerImageProcessor if is_vision_available() else None






						def 							_UpperCAmelCase					(     self       :			str  ):
													A_ 						=		SegformerImageProcessingTester(self  )






						@property
						def 							_UpperCAmelCase					(     self       :			Optional[Any]  ):
													return self.image_processor_tester.prepare_image_processor_dict()






						def 							_UpperCAmelCase					(     self       :			Optional[Any]  ):
													A_ 						=		self.image_processing_class(**self.image_processor_dict  )
													self.assertTrue(hasattr(lowerCAmelCase   ,     "do_resize"  )  )
													self.assertTrue(hasattr(lowerCAmelCase   ,     "size"  )  )
													self.assertTrue(hasattr(lowerCAmelCase   ,     "do_normalize"  )  )
													self.assertTrue(hasattr(lowerCAmelCase   ,     "image_mean"  )  )
													self.assertTrue(hasattr(lowerCAmelCase   ,     "image_std"  )  )
													self.assertTrue(hasattr(lowerCAmelCase   ,     "do_reduce_labels"  )  )






						def 							_UpperCAmelCase					(     self       :			str  ):
													A_ 						=		self.image_processing_class.from_dict(self.image_processor_dict  )
													self.assertEqual(image_processor.size   ,     {"height": 30, "width": 30}  )
													self.assertEqual(image_processor.do_reduce_labels   ,     lowerCAmelCase  )

													A_ 						=		self.image_processing_class.from_dict(self.image_processor_dict   ,     size=42   ,     reduce_labels=lowerCAmelCase  )
													self.assertEqual(image_processor.size   ,     {"height": 42, "width": 42}  )
													self.assertEqual(image_processor.do_reduce_labels   ,     lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Tuple  ):
													pass






						def 							_UpperCAmelCase					(     self       :			int  ):
													# 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=lowerCAmelCase  )
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase   ,     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.size["height"],
													        self.image_processor_tester.size["width"],
													    )   ,     )

													# Test batched
													A_ 						=		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.size["height"],
													        self.image_processor_tester.size["width"],
													    )   ,     )






						def 							_UpperCAmelCase					(     self       :			Dict  ):
													# 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=lowerCAmelCase   ,     numpify=lowerCAmelCase  )
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase   ,     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.size["height"],
													        self.image_processor_tester.size["width"],
													    )   ,     )

													# Test batched
													A_ 						=		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.size["height"],
													        self.image_processor_tester.size["width"],
													    )   ,     )






						def 							_UpperCAmelCase					(     self       :			Optional[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=lowerCAmelCase   ,     torchify=lowerCAmelCase  )
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase   ,     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.size["height"],
													        self.image_processor_tester.size["width"],
													    )   ,     )

													# Test batched
													A_ 						=		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.size["height"],
													        self.image_processor_tester.size["width"],
													    )   ,     )






						def 							_UpperCAmelCase					(     self       :			List[str]  ):
													# 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=lowerCAmelCase   ,     torchify=lowerCAmelCase  )
													A_ 						=		[]
													for image in image_inputs:
																				self.assertIsInstance(lowerCAmelCase   ,     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.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_55  )

													# Test batched
													A_ 						=		image_processing(lowerCAmelCase   ,     lowerCAmelCase   ,     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_55  )

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

													A_ 						=		image_processing(lowerCAmelCase   ,     lowerCAmelCase   ,     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_55  )

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

													A_ 						=		image_processing(lowerCAmelCase   ,     lowerCAmelCase   ,     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_55  )






						def 							_UpperCAmelCase					(     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(lowerCAmelCase   ,     lowerCAmelCase   ,     return_tensors="pt"  )
													self.assertTrue(encoding["labels"].min().item() >= 0  )
													self.assertTrue(encoding["labels"].max().item() <= 1_50  )

													A_ 						=		True
													A_ 						=		image_processing(lowerCAmelCase   ,     lowerCAmelCase   ,     return_tensors="pt"  )
													self.assertTrue(encoding["labels"].min().item() >= 0  )
													self.assertTrue(encoding["labels"].max().item() <= 2_55  )









'''simple docstring'''
import shutil
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import SpeechaTextTokenizer, is_speech_available
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


if is_speech_available():
							from transformers import SpeechaTextFeatureExtractor, SpeechaTextProcessor


__SCREAMING_SNAKE_CASE				:							int		       =		get_tests_dir('''fixtures/test_sentencepiece.model''')






@require_torch
@require_torchaudio
@require_sentencepiece
class 				__lowerCAmelCase      (      unittest.TestCase	):

						"""simple docstring"""






						def 							_UpperCAmelCase					(     self       :			List[str]  ):
													A_ 						=		tempfile.mkdtemp()

													A_ 						=		["<s>", "<pad>", "</s>", "<unk>", "▁This", "▁is", "▁a", "▁t", "est"]
													A_ 						=		dict(zip(lowerCAmelCase   ,     range(len(lowerCAmelCase  )  )  )  )
													A_ 						=		Path(self.tmpdirname  )
													save_json(lowerCAmelCase   ,     save_dir / VOCAB_FILES_NAMES["vocab_file"]  )
													if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists():
																				copyfile(lowerCAmelCase   ,     save_dir / VOCAB_FILES_NAMES["spm_file"]  )

													A_ 						=		SpeechaTextTokenizer.from_pretrained(self.tmpdirname  )
													tokenizer.save_pretrained(self.tmpdirname  )

													A_ 						=		{
													    "feature_size": 24,
													    "num_mel_bins": 24,
													    "padding_value": 0.0,
													    "sampling_rate": 1_60_00,
													    "return_attention_mask": False,
													    "do_normalize": True,
													}
													save_json(lowerCAmelCase   ,     save_dir / FEATURE_EXTRACTOR_NAME  )






						def 							_UpperCAmelCase					(     self       :			Union[str, Any]   ,     **lowerCAmelCase       :			str  ):
													return SpeechaTextTokenizer.from_pretrained(self.tmpdirname   ,     **lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			List[Any]   ,     **lowerCAmelCase       :			Optional[Any]  ):
													return SpeechaTextFeatureExtractor.from_pretrained(self.tmpdirname   ,     **lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Any  ):
													shutil.rmtree(self.tmpdirname  )






						def 							_UpperCAmelCase					(     self       :			Union[str, Any]  ):
													A_ 						=		self.get_tokenizer()
													A_ 						=		self.get_feature_extractor()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													processor.save_pretrained(self.tmpdirname  )
													A_ 						=		SpeechaTextProcessor.from_pretrained(self.tmpdirname  )

													self.assertEqual(processor.tokenizer.get_vocab()   ,     tokenizer.get_vocab()  )
													self.assertIsInstance(processor.tokenizer   ,     lowerCAmelCase  )

													self.assertEqual(processor.feature_extractor.to_json_string()   ,     feature_extractor.to_json_string()  )
													self.assertIsInstance(processor.feature_extractor   ,     lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Dict  ):
													A_ 						=		SpeechaTextProcessor(
													    tokenizer=self.get_tokenizer()   ,     feature_extractor=self.get_feature_extractor()  )
													processor.save_pretrained(self.tmpdirname  )

													A_ 						=		self.get_tokenizer(bos_token="(BOS)"   ,     eos_token="(EOS)"  )
													A_ 						=		self.get_feature_extractor(do_normalize=lowerCAmelCase   ,     padding_value=1.0  )

													A_ 						=		SpeechaTextProcessor.from_pretrained(
													    self.tmpdirname   ,     bos_token="(BOS)"   ,     eos_token="(EOS)"   ,     do_normalize=lowerCAmelCase   ,     padding_value=1.0  )

													self.assertEqual(processor.tokenizer.get_vocab()   ,     tokenizer_add_kwargs.get_vocab()  )
													self.assertIsInstance(processor.tokenizer   ,     lowerCAmelCase  )

													self.assertEqual(processor.feature_extractor.to_json_string()   ,     feature_extractor_add_kwargs.to_json_string()  )
													self.assertIsInstance(processor.feature_extractor   ,     lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			str  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													A_ 						=		floats_list((3, 10_00)  )

													A_ 						=		feature_extractor(lowerCAmelCase   ,     return_tensors="np"  )
													A_ 						=		processor(lowerCAmelCase   ,     return_tensors="np"  )

													for key in input_feat_extract.keys():
																				self.assertAlmostEqual(input_feat_extract[key].sum()   ,     input_processor[key].sum()   ,     delta=1e-2  )






						def 							_UpperCAmelCase					(     self       :			Optional[int]  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													A_ 						=		"This is a test string"

													A_ 						=		processor(text=lowerCAmelCase  )

													A_ 						=		tokenizer(lowerCAmelCase  )

													for key in encoded_tok.keys():
																				self.assertListEqual(encoded_tok[key]   ,     encoded_processor[key]  )






						def 							_UpperCAmelCase					(     self       :			int  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													A_ 						=		[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

													A_ 						=		processor.batch_decode(lowerCAmelCase  )
													A_ 						=		tokenizer.batch_decode(lowerCAmelCase  )

													self.assertListEqual(lowerCAmelCase   ,     lowerCAmelCase  )






						def 							_UpperCAmelCase					(     self       :			Tuple  ):
													A_ 						=		self.get_feature_extractor()
													A_ 						=		self.get_tokenizer()

													A_ 						=		SpeechaTextProcessor(tokenizer=lowerCAmelCase   ,     feature_extractor=lowerCAmelCase  )

													self.assertListEqual(
													    processor.model_input_names   ,     feature_extractor.model_input_names   ,     msg="`processor` and `feature_extractor` model input names do not match"   ,     )





