



'''simple docstring'''





import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     MODEL_MAPPING,
     SegformerForImageClassification,
     SegformerForSemanticSegmentation,
     SegformerModel,
 )
 from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import SegformerImageProcessor


class 					snake_case			(UpperCamelCase ):



       def 				_a   (					self   )	->				List[str]:
           lowercase__									=    self.config_class(**self.inputs_dict   )
           self.parent.assertTrue(hasattr(UpperCAmelCase_  ,"hidden_sizes"   )   )
           self.parent.assertTrue(hasattr(UpperCAmelCase_  ,"num_attention_heads"   )   )
           self.parent.assertTrue(hasattr(UpperCAmelCase_  ,"num_encoder_blocks"   )   )





class 					snake_case			:



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=13  ,UpperCAmelCase_=64  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=[2, 2, 2, 2]  ,UpperCAmelCase_=[8, 4, 2, 1]  ,UpperCAmelCase_=[16, 32, 64, 128]  ,UpperCAmelCase_=[1, 4, 8, 16]  ,UpperCAmelCase_=[1, 2, 4, 8]  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=3  ,UpperCAmelCase_=None  ,)	->				Tuple:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    image_size
           lowercase__									=    num_channels
           lowercase__									=    num_encoder_blocks
           lowercase__									=    sr_ratios
           lowercase__									=    depths
           lowercase__									=    hidden_sizes
           lowercase__									=    downsampling_rates
           lowercase__									=    num_attention_heads
           lowercase__									=    is_training
           lowercase__									=    use_labels
           lowercase__									=    hidden_act
           lowercase__									=    hidden_dropout_prob
           lowercase__									=    attention_probs_dropout_prob
           lowercase__									=    initializer_range
           lowercase__									=    num_labels
           lowercase__									=    scope



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

           lowercase__									=    None
           if self.use_labels:
               lowercase__									=    ids_tensor([self.batch_size, self.image_size, self.image_size]  ,self.num_labels   )

           lowercase__									=    self.get_config()
           return config, pixel_values, labels



       def 				_a   (					self   )	->				Any:
           return SegformerConfig(
               image_size=self.image_size  ,num_channels=self.num_channels  ,num_encoder_blocks=self.num_encoder_blocks  ,depths=self.depths  ,hidden_sizes=self.hidden_sizes  ,num_attention_heads=self.num_attention_heads  ,hidden_act=self.hidden_act  ,hidden_dropout_prob=self.hidden_dropout_prob  ,attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,initializer_range=self.initializer_range  ,)



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Any:
           lowercase__									=    SegformerModel(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(UpperCAmelCase_   )
           lowercase__									=    lowercase__									=    self.image_size // (self.downsampling_rates[-1] * 2)
           self.parent.assertEqual(
               result.last_hidden_state.shape  ,(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				List[Any]:
           lowercase__									=    self.num_labels
           lowercase__									=    SegformerForSemanticSegmentation(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(UpperCAmelCase_   )
           self.parent.assertEqual(
               result.logits.shape  ,(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)   )
           lowercase__									=    model(UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(
               result.logits.shape  ,(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)   )
           self.parent.assertGreater(result.loss  ,0.0   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Any:
           lowercase__									=    1
           lowercase__									=    SegformerForSemanticSegmentation(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    torch.randint(0  ,1  ,(self.batch_size, self.image_size, self.image_size)   ).to(UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertGreater(result.loss  ,0.0   )







       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    self.prepare_config_and_inputs()
           lowercase__							,							lowercase__							,							lowercase__									=    config_and_inputs
           lowercase__									=    {"pixel_values": pixel_values}
           return config, inputs_dict





@require_torch
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:List[str]     =       (
           (
               SegformerModel,
               SegformerForSemanticSegmentation,
               SegformerForImageClassification,
           )
           if is_torch_available()
           else ()
       )
       lowerCAmelCase__	:Dict     =       (
           {
               "feature-extraction": SegformerModel,
               "image-classification": SegformerForImageClassification,
               "image-segmentation": SegformerForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       lowerCAmelCase__	:Optional[int]     =       True
       lowerCAmelCase__	:str     =       False
       lowerCAmelCase__	:Tuple     =       False
       lowerCAmelCase__	:List[str]     =       False



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    SegformerModelTester(self   )
           lowercase__									=    SegformerConfigTester(self  ,config_class=UpperCAmelCase_   )



       def 				_a   (					self   )	->				int:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				Tuple:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_binary_image_segmentation(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_segmentation(*UpperCAmelCase_   )



       @unittest.skip("SegFormer does not use inputs_embeds"   )
       def 				_a   (					self   )	->				Union[str, Any]:
           pass



       @unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods"   )
       def 				_a   (					self   )	->				Dict:
           pass



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               lowercase__									=    model_class(UpperCAmelCase_   )
               lowercase__									=    inspect.signature(model.forward   )
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               lowercase__									=    [*signature.parameters.keys()]

               lowercase__									=    ["pixel_values"]
               self.assertListEqual(arg_names[:1]  ,UpperCAmelCase_   )



       def 				_a   (					self   )	->				int:
           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()
           lowercase__									=    True

           for model_class in self.all_model_classes:
               lowercase__									=    True
               lowercase__									=    False
               lowercase__									=    True
               lowercase__									=    model_class(UpperCAmelCase_   )
               model.to(UpperCAmelCase_   )
               model.eval()
               with torch.no_grad():
                   lowercase__									=    model(**self._prepare_for_class(UpperCAmelCase_  ,UpperCAmelCase_   )   )
               lowercase__									=    outputs.attentions

               lowercase__									=    sum(self.model_tester.depths   )
               self.assertEqual(len(UpperCAmelCase_   )  ,UpperCAmelCase_   )

               # check that output_attentions also work using config
               del inputs_dict["output_attentions"]
               lowercase__									=    True
               lowercase__									=    model_class(UpperCAmelCase_   )
               model.to(UpperCAmelCase_   )
               model.eval()
               with torch.no_grad():
                   lowercase__									=    model(**self._prepare_for_class(UpperCAmelCase_  ,UpperCAmelCase_   )   )
               lowercase__									=    outputs.attentions

               self.assertEqual(len(UpperCAmelCase_   )  ,UpperCAmelCase_   )

               # verify the first attentions (first block, first layer)
               lowercase__									=    (self.model_tester.image_size // 4) ** 2
               lowercase__									=    (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
               self.assertListEqual(
                   list(attentions[0].shape[-3:]   )  ,[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]  ,)

               # verify the last attentions (last block, last layer)
               lowercase__									=    (self.model_tester.image_size // 32) ** 2
               lowercase__									=    (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
               self.assertListEqual(
                   list(attentions[-1].shape[-3:]   )  ,[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]  ,)
               lowercase__									=    len(UpperCAmelCase_   )

               # Check attention is always last and order is fine
               lowercase__									=    True
               lowercase__									=    True
               lowercase__									=    model_class(UpperCAmelCase_   )
               model.to(UpperCAmelCase_   )
               model.eval()
               with torch.no_grad():
                   lowercase__									=    model(**self._prepare_for_class(UpperCAmelCase_  ,UpperCAmelCase_   )   )

               self.assertEqual(out_len + 1  ,len(UpperCAmelCase_   )   )

               lowercase__									=    outputs.attentions

               self.assertEqual(len(UpperCAmelCase_   )  ,UpperCAmelCase_   )
               # verify the first attentions (first block, first layer)
               lowercase__									=    (self.model_tester.image_size // 4) ** 2
               lowercase__									=    (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
               self.assertListEqual(
                   list(self_attentions[0].shape[-3:]   )  ,[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]  ,)



       def 				_a   (					self   )	->				Optional[int]:
           def check_hidden_states_output(UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   ):
               lowercase__									=    model_class(UpperCAmelCase_   )
               model.to(UpperCAmelCase_   )
               model.eval()

               with torch.no_grad():
                   lowercase__									=    model(**self._prepare_for_class(UpperCAmelCase_  ,UpperCAmelCase_   )   )

               lowercase__									=    outputs.hidden_states

               lowercase__									=    self.model_tester.num_encoder_blocks
               self.assertEqual(len(UpperCAmelCase_   )  ,UpperCAmelCase_   )

               # verify the first hidden states (first block)
               self.assertListEqual(
                   list(hidden_states[0].shape[-3:]   )  ,[
                       self.model_tester.hidden_sizes[0],
                       self.model_tester.image_size // 4,
                       self.model_tester.image_size // 4,
                   ]  ,)

           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               lowercase__									=    True
               check_hidden_states_output(UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               lowercase__									=    True

               check_hidden_states_output(UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )



       def 				_a   (					self   )	->				str:
           if not self.model_tester.is_training:
               return

           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()
           lowercase__									=    True

           for model_class in self.all_model_classes:
               if model_class in get_values(UpperCAmelCase_   ):
                   continue

               lowercase__									=    model_class(UpperCAmelCase_   )
               model.to(UpperCAmelCase_   )
               model.train()
               lowercase__									=    self._prepare_for_class(UpperCAmelCase_  ,UpperCAmelCase_  ,return_labels=UpperCAmelCase_   )
               lowercase__									=    model(**UpperCAmelCase_   ).loss
               loss.backward()



       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."   )
       def 				_a   (					self   )	->				int:
           pass







       @slow
       def 				_a   (					self   )	->				Dict:
           for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__									=    SegformerModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )





def 							lowerCamelCase	(					):





    '''simple docstring'''


    lowercase__									=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
    return image



@require_torch
class 					snake_case			(unittest.TestCase ):



       @slow
       def 				_a   (					self   )	->				int:
           # only resize + normalize
           lowercase__									=    SegformerImageProcessor(
               image_scale=(512, 512)  ,keep_ratio=UpperCAmelCase_  ,align=UpperCAmelCase_  ,do_random_crop=UpperCAmelCase_   )
           lowercase__									=    SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"   ).to(
               UpperCAmelCase_   )

           lowercase__									=    prepare_img()
           lowercase__									=    image_processor(images=UpperCAmelCase_  ,return_tensors="pt"   )
           lowercase__									=    encoded_inputs.pixel_values.to(UpperCAmelCase_   )

           with torch.no_grad():
               lowercase__									=    model(UpperCAmelCase_   )

           lowercase__									=    torch.Size((1, model.config.num_labels, 128, 128)   )
           self.assertEqual(outputs.logits.shape  ,UpperCAmelCase_   )

           lowercase__									=    torch.tensor(
               [
                   [[-4.63_10, -5.52_32, -6.23_56], [-5.19_21, -6.14_44, -6.59_96], [-5.44_24, -6.27_90, -6.75_74]],
                   [[-12.13_91, -13.31_22, -13.95_54], [-12.87_32, -13.93_52, -14.35_63], [-12.94_38, -13.82_26, -14.25_13]],
                   [[-12.51_34, -13.46_86, -14.49_15], [-12.86_69, -14.43_43, -14.77_58], [-13.25_23, -14.58_19, -15.06_94]],
               ]   ).to(UpperCAmelCase_   )
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]  ,UpperCAmelCase_  ,atol=1E-4   )   )



       @slow
       def 				_a   (					self   )	->				str:
           # only resize + normalize
           lowercase__									=    SegformerImageProcessor(
               image_scale=(512, 512)  ,keep_ratio=UpperCAmelCase_  ,align=UpperCAmelCase_  ,do_random_crop=UpperCAmelCase_   )
           lowercase__									=    SegformerForSemanticSegmentation.from_pretrained(
               "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"   ).to(UpperCAmelCase_   )

           lowercase__									=    prepare_img()
           lowercase__									=    image_processor(images=UpperCAmelCase_  ,return_tensors="pt"   )
           lowercase__									=    encoded_inputs.pixel_values.to(UpperCAmelCase_   )

           with torch.no_grad():
               lowercase__									=    model(UpperCAmelCase_   )

           lowercase__									=    torch.Size((1, model.config.num_labels, 128, 128)   )
           self.assertEqual(outputs.logits.shape  ,UpperCAmelCase_   )

           lowercase__									=    torch.tensor(
               [
                   [[-13.57_48, -13.91_11, -12.65_00], [-14.35_00, -15.36_83, -14.23_28], [-14.75_32, -16.04_24, -15.60_87]],
                   [[-17.16_51, -15.87_25, -12.96_53], [-17.25_80, -17.37_18, -14.82_23], [-16.60_58, -16.87_83, -16.74_52]],
                   [[-3.64_56, -3.02_09, -1.42_03], [-3.07_97, -3.19_59, -2.00_00], [-1.87_57, -1.92_17, -1.69_97]],
               ]   ).to(UpperCAmelCase_   )
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]  ,UpperCAmelCase_  ,atol=1E-1   )   )







       @slow
       def 				_a   (					self   )	->				List[Any]:
           # only resize + normalize
           lowercase__									=    SegformerImageProcessor(
               image_scale=(512, 512)  ,keep_ratio=UpperCAmelCase_  ,align=UpperCAmelCase_  ,do_random_crop=UpperCAmelCase_   )
           lowercase__									=    SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"   ).to(
               UpperCAmelCase_   )

           lowercase__									=    prepare_img()
           lowercase__									=    image_processor(images=UpperCAmelCase_  ,return_tensors="pt"   )
           lowercase__									=    encoded_inputs.pixel_values.to(UpperCAmelCase_   )

           with torch.no_grad():
               lowercase__									=    model(UpperCAmelCase_   )

           lowercase__									=    outputs.logits.detach().cpu()

           lowercase__									=    image_processor.post_process_semantic_segmentation(outputs=UpperCAmelCase_  ,target_sizes=[(500, 300)]   )
           lowercase__									=    torch.Size((500, 300)   )
           self.assertEqual(segmentation[0].shape  ,UpperCAmelCase_   )

           lowercase__									=    image_processor.post_process_semantic_segmentation(outputs=UpperCAmelCase_   )
           lowercase__									=    torch.Size((128, 128)   )
           self.assertEqual(segmentation[0].shape  ,UpperCAmelCase_   )









'''simple docstring'''





import json
from typing import Iterator, List, Union

from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers
from tokenizers.implementations.base_tokenizer import BaseTokenizer
from tokenizers.models import Unigram
from tokenizers.processors import TemplateProcessing


class 					snake_case			(UpperCamelCase ):



       def __init__(					self  ,UpperCAmelCase_ = "▁"  ,UpperCAmelCase_ = True  ,UpperCAmelCase_ = "<unk>"  ,UpperCAmelCase_ = "</s>"  ,UpperCAmelCase_ = "<pad>"  ,)	->				Optional[int]:
           lowercase__									=    {
               "pad": {"id": 0, "token": pad_token},
               "eos": {"id": 1, "token": eos_token},
               "unk": {"id": 2, "token": unk_token},
           }

           lowercase__									=    [None] * len(self.special_tokens   )
           for token_dict in self.special_tokens.values():
               lowercase__									=    token_dict["token"]

           lowercase__									=    Tokenizer(Unigram()   )

           lowercase__									=    normalizers.Sequence(
               [
                   normalizers.Nmt(),
                   normalizers.NFKC(),
                   normalizers.Replace(Regex(" {2,}"   )  ," "   ),
                   normalizers.Lowercase(),
               ]   )
           lowercase__									=    pre_tokenizers.Sequence(
               [
                   pre_tokenizers.Metaspace(replacement=UpperCAmelCase_  ,add_prefix_space=UpperCAmelCase_   ),
                   pre_tokenizers.Digits(individual_digits=UpperCAmelCase_   ),
                   pre_tokenizers.Punctuation(),
               ]   )
           lowercase__									=    decoders.Metaspace(replacement=UpperCAmelCase_  ,add_prefix_space=UpperCAmelCase_   )

           lowercase__									=    TemplateProcessing(
               single=F'''$A {self.special_tokens['eos']['token']}'''  ,special_tokens=[(self.special_tokens["eos"]["token"], self.special_tokens["eos"]["id"])]  ,)

           lowercase__									=    {
               "model": "SentencePieceUnigram",
               "replacement": replacement,
               "add_prefix_space": add_prefix_space,
           }

           super().__init__(UpperCAmelCase_  ,UpperCAmelCase_   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_ = 8_000  ,UpperCAmelCase_ = True  ,)	->				List[str]:
           lowercase__									=    trainers.UnigramTrainer(
               vocab_size=UpperCAmelCase_  ,special_tokens=self.special_tokens_list  ,show_progress=UpperCAmelCase_  ,)

           if isinstance(UpperCAmelCase_  ,UpperCAmelCase_   ):
               lowercase__									=    [files]
           self._tokenizer.train(UpperCAmelCase_  ,trainer=UpperCAmelCase_   )

           self.add_unk_id()



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_ = 8_000  ,UpperCAmelCase_ = True  ,)	->				Union[str, Any]:
           lowercase__									=    trainers.UnigramTrainer(
               vocab_size=UpperCAmelCase_  ,special_tokens=self.special_tokens_list  ,show_progress=UpperCAmelCase_  ,)

           self._tokenizer.train_from_iterator(UpperCAmelCase_  ,trainer=UpperCAmelCase_   )

           self.add_unk_id()







       def 				_a   (					self   )	->				str:
           lowercase__									=    json.loads(self._tokenizer.to_str()   )

           lowercase__									=    self.special_tokens["unk"]["id"]

           lowercase__									=    Tokenizer.from_str(json.dumps(UpperCAmelCase_   )   )




