





'''simple docstring'''


import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

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


if is_torch_available():
 import torch

 from transformers import UperNetForSemanticSegmentation
 from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor
class 				a_       :





     def __init__(			self		: List[str]     ,				lowercase		: List[Any]     ,				lowercase		: Union[str, Any]=13     ,				lowercase		: Tuple=32     ,				lowercase		: Tuple=3     ,				lowercase		: int=4     ,				lowercase		: List[str]=[10, 20, 30, 40]     ,				lowercase		: Any=[2, 2, 3, 2]     ,				lowercase		: Optional[Any]=True     ,				lowercase		: List[Any]=True     ,				lowercase		: int=37     ,				lowercase		: Dict="gelu"     ,				lowercase		: Optional[int]=10     ,				lowercase		: int=0.02     ,				lowercase		: Union[str, Any]=["stage2", "stage3", "stage4"]     ,				lowercase		: Tuple=3     ,				lowercase		: Any=None     ,				):





        """simple docstring"""




        lowercase_      :List[Any]         =			parent
        lowercase_      :Any         =			batch_size
        lowercase_      :Dict         =			image_size
        lowercase_      :Union[str, Any]         =			num_channels
        lowercase_      :Optional[Any]         =			num_stages
        lowercase_      :Tuple         =			hidden_sizes
        lowercase_      :List[Any]         =			depths
        lowercase_      :str         =			is_training
        lowercase_      :List[Any]         =			use_labels
        lowercase_      :List[str]         =			intermediate_size
        lowercase_      :Dict         =			hidden_act
        lowercase_      :Optional[int]         =			type_sequence_label_size
        lowercase_      :int         =			initializer_range
        lowercase_      :str         =			out_features
        lowercase_      :Optional[int]         =			num_labels
        lowercase_      :Tuple         =			scope
        lowercase_      :int         =			num_stages





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Optional[int]         =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

        lowercase_      :List[str]         =			None
        if self.use_labels:
           lowercase_      :str         =			ids_tensor([self.batch_size]     ,				self.type_sequence_label_size						)

        lowercase_      :Dict         =			self.get_config()

        return config, pixel_values, labels





     def 							lowercase__				(			self		: Optional[Any]						):





        """simple docstring"""




        return ConvNextConfig(
            num_channels=self.num_channels     ,				num_stages=self.num_stages     ,				hidden_sizes=self.hidden_sizes     ,				depths=self.depths     ,				is_training=self.is_training     ,				intermediate_size=self.intermediate_size     ,				hidden_act=self.hidden_act     ,				out_features=self.out_features     ,				)





     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        return UperNetConfig(
            backbone_config=self.get_backbone_config()     ,				hidden_size=512     ,				pool_scales=[1, 2, 3, 6]     ,				use_auxiliary_head=lowercase     ,				auxiliary_loss_weight=0.4     ,				auxiliary_in_channels=40     ,				auxiliary_channels=256     ,				auxiliary_num_convs=1     ,				auxiliary_concat_input=lowercase     ,				loss_ignore_index=255     ,				num_labels=self.num_labels     ,				)





     def 							lowercase__				(			self		: List[Any]     ,				lowercase		: Optional[Any]     ,				lowercase		: Optional[Any]     ,				lowercase		: int						):





        """simple docstring"""




        lowercase_      :Tuple         =			UperNetForSemanticSegmentation(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_      :List[Any]         =			model(lowercase						)
        self.parent.assertEqual(
            result.logits.shape     ,				(self.batch_size, self.num_labels, self.image_size, self.image_size)						)







     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        lowercase_      :Any         =			self.prepare_config_and_inputs()
        (
            (
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							
        )      :str         =			config_and_inputs
        lowercase_      :Any         =			{"pixel_values": pixel_values}
        return config, inputs_dict







@require_torch
class 				a_       (    _lowerCAmelCase     ,						_lowerCAmelCase     ,						unittest.TestCase							):
     __A								=      (UperNetForSemanticSegmentation,) if is_torch_available() else ()
     __A								=      {"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
     __A								=      False
     __A								=      False
     __A								=      False
     __A								=      False
     __A								=      False
     __A								=      False





     def 							lowercase__				(			self		: Optional[Any]						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			UperNetModelTester(self						)
        lowercase_      :List[str]         =			ConfigTester(self     ,				config_class=lowercase     ,				has_text_modality=lowercase     ,				hidden_size=37						)





     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        self.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()





     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        return





     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        lowercase_      ,							lowercase_      :Optional[Any]         =			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           lowercase_      :List[Any]         =			model_class(lowercase						)
           lowercase_      :Union[str, Any]         =			inspect.signature(model.forward						)
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowercase_      :List[Any]         =			[*signature.parameters.keys()]

           lowercase_      :int         =			["pixel_values"]
           self.assertListEqual(arg_names[:1]     ,				lowercase						)





     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        lowercase_      :List[Any]         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_semantic_segmentation(*lowercase						)





     @unittest.skip(reason="UperNet does not use inputs_embeds"						)
     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        pass





     @unittest.skip(reason="UperNet does not support input and output embeddings"						)
     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        pass





     @unittest.skip(reason="UperNet does not have a base model"						)
     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        pass





     @unittest.skip(reason="UperNet does not have a base model"						)
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        pass





     @require_torch_multi_gpu
     @unittest.skip(reason="UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`"						)
     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        pass





     @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
     def 							lowercase__				(			self		: Any						):





        """simple docstring"""




        pass





     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        def check_hidden_states_output(lowercase		: Union[str, Any]     ,				lowercase		: Tuple     ,				lowercase		: Dict						):
           lowercase_      :Any         =			model_class(lowercase						)
           model.to(lowercase						)
           model.eval()

           with torch.no_grad():
              lowercase_      :List[Any]         =			model(**self._prepare_for_class(lowercase     ,				lowercase						)						)

           lowercase_      :Dict         =			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

           lowercase_      :Any         =			self.model_tester.num_stages
           self.assertEqual(len(lowercase						)     ,				expected_num_stages + 1						)

           # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
           self.assertListEqual(
               list(hidden_states[0].shape[-2:]						)     ,				[self.model_tester.image_size // 4, self.model_tester.image_size // 4]     ,				)

        lowercase_      ,							lowercase_      :Any         =			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           lowercase_      :Optional[int]         =			True
           check_hidden_states_output(lowercase     ,				lowercase     ,				lowercase						)

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           lowercase_      :str         =			True

           check_hidden_states_output(lowercase     ,				lowercase     ,				lowercase						)





     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        lowercase_      ,							lowercase_      :Dict         =			self.model_tester.prepare_config_and_inputs_for_common()

        lowercase_      :Optional[int]         =			_config_zero_init(lowercase						)
        lowercase_      :Union[str, Any]         =			_config_zero_init(configs_no_init.backbone_config						)
        for model_class in self.all_model_classes:
           lowercase_      :Optional[Any]         =			model_class(config=lowercase						)
           for name, param in model.named_parameters():
              if param.requires_grad:
                 self.assertIn(
                     ((param.data.mean() * 1e9).round() / 1e9).item()     ,				[0.0, 1.0]     ,				msg=F'Parameter {name} of model {model_class} seems not properly initialized'     ,				)





     @unittest.skip(reason="UperNet does not have tied weights"						)
     def 							lowercase__				(			self		: List[str]						):





        """simple docstring"""




        pass







     @slow
     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase_      :Optional[int]         =			UperNetForSemanticSegmentation.from_pretrained(lowercase						)
           self.assertIsNotNone(lowercase						)







def 							UpperCAmelCase_  (						):
   lowercase_      :Any         =			hf_hub_download(
       repo_id="hf-internal-testing/fixtures_ade20k"     ,repo_type="dataset"     ,filename="ADE_val_00000001.jpg"			)
   lowercase_      :Any         =			Image.open(__lowerCamelCase			).convert("RGB"			)
   return image


@require_torch
@require_vision
@slow
class 				a_       (    unittest.TestCase							):





     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        lowercase_      :List[str]         =			AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"						)
        lowercase_      :Any         =			UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"						).to(lowercase						)

        lowercase_      :Optional[int]         =			prepare_img()
        lowercase_      :int         =			processor(images=lowercase     ,				return_tensors="pt"						).to(lowercase						)

        with torch.no_grad():
           lowercase_      :int         =			model(**lowercase						)

        lowercase_      :Union[str, Any]         =			torch.Size((1, model.config.num_labels, 512, 512)						)
        self.assertEqual(outputs.logits.shape     ,				lowercase						)

        lowercase_      :str         =			torch.tensor(
            [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]]						).to(lowercase						)
        self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]     ,				lowercase     ,				atol=1e-4						)						)







     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        lowercase_      :Dict         =			AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"						)
        lowercase_      :Optional[Any]         =			UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"						).to(lowercase						)

        lowercase_      :str         =			prepare_img()
        lowercase_      :Optional[int]         =			processor(images=lowercase     ,				return_tensors="pt"						).to(lowercase						)

        with torch.no_grad():
           lowercase_      :List[str]         =			model(**lowercase						)

        lowercase_      :Dict         =			torch.Size((1, model.config.num_labels, 512, 512)						)
        self.assertEqual(outputs.logits.shape     ,				lowercase						)

        lowercase_      :Dict         =			torch.tensor(
            [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]]						).to(lowercase						)
        self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]     ,				lowercase     ,				atol=1e-4						)						)









'''simple docstring'''


import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType
class 				a_       (    _lowerCAmelCase							):
     __A								=      ["image_processor", "tokenizer"]
     __A								=      "LayoutLMv3ImageProcessor"
     __A								=      ("LayoutLMv3Tokenizer", "LayoutLMv3TokenizerFast")





     def __init__(			self		: int     ,				lowercase		: Optional[Any]=None     ,				lowercase		: List[str]=None     ,				**lowercase		: Optional[int]						):





        """simple docstring"""




        lowercase_      :int         =			None
        if "feature_extractor" in kwargs:
           warnings.warn(
               "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
               " instead."     ,				lowercase     ,				)
           lowercase_      :Optional[int]         =			kwargs.pop("feature_extractor"						)

        lowercase_      :Union[str, Any]         =			image_processor if image_processor is not None else feature_extractor
        if image_processor is None:
           raise ValueError("You need to specify an `image_processor`."						)
        if tokenizer is None:
           raise ValueError("You need to specify a `tokenizer`."						)

        super().__init__(lowercase     ,				lowercase						)





     def __call__(			self		: Optional[Any]     ,				lowercase		: List[str]     ,				lowercase		: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None     ,				lowercase		: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None     ,				lowercase		: Union[List[List[int]], List[List[List[int]]]] = None     ,				lowercase		: Optional[Union[List[int], List[List[int]]]] = None     ,				lowercase		: bool = True     ,				lowercase		: Union[bool, str, PaddingStrategy] = False     ,				lowercase		: Union[bool, str, TruncationStrategy] = None     ,				lowercase		: Optional[int] = None     ,				lowercase		: int = 0     ,				lowercase		: Optional[int] = None     ,				lowercase		: Optional[bool] = None     ,				lowercase		: Optional[bool] = None     ,				lowercase		: bool = False     ,				lowercase		: bool = False     ,				lowercase		: bool = False     ,				lowercase		: bool = False     ,				lowercase		: bool = True     ,				lowercase		: Optional[Union[str, TensorType]] = None     ,				**lowercase		: List[Any]     ,				):





        """simple docstring"""




        if self.image_processor.apply_ocr and (boxes is not None):
           raise ValueError(
               "You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True."						)

        if self.image_processor.apply_ocr and (word_labels is not None):
           raise ValueError(
               "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."						)

        # first, apply the image processor
        lowercase_      :Dict         =			self.image_processor(images=lowercase     ,				return_tensors=lowercase						)

        # second, apply the tokenizer
        if text is not None and self.image_processor.apply_ocr and text_pair is None:
           if isinstance(lowercase     ,				lowercase						):
              lowercase_      :str         =			[text]  # add batch dimension (as the image processor always adds a batch dimension)
           lowercase_      :Union[str, Any]         =			features["words"]

        lowercase_      :Optional[Any]         =			self.tokenizer(
            text=text if text is not None else features["words"]     ,				text_pair=text_pair if text_pair is not None else None     ,				boxes=boxes if boxes is not None else features["boxes"]     ,				word_labels=lowercase     ,				add_special_tokens=lowercase     ,				padding=lowercase     ,				truncation=lowercase     ,				max_length=lowercase     ,				stride=lowercase     ,				pad_to_multiple_of=lowercase     ,				return_token_type_ids=lowercase     ,				return_attention_mask=lowercase     ,				return_overflowing_tokens=lowercase     ,				return_special_tokens_mask=lowercase     ,				return_offsets_mapping=lowercase     ,				return_length=lowercase     ,				verbose=lowercase     ,				return_tensors=lowercase     ,				**lowercase     ,				)

        # add pixel values
        lowercase_      :Any         =			features.pop("pixel_values"						)
        if return_overflowing_tokens is True:
           lowercase_      :Any         =			self.get_overflowing_images(lowercase     ,				encoded_inputs["overflow_to_sample_mapping"]						)
        lowercase_      :Any         =			images

        return encoded_inputs





     def 							lowercase__				(			self		: List[Any]     ,				lowercase		: Any     ,				lowercase		: Optional[Any]						):





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			[]
        for sample_idx in overflow_to_sample_mapping:
           images_with_overflow.append(images[sample_idx]						)

        if len(lowercase						) != len(lowercase						):
           raise ValueError(
               "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
               F' {len(lowercase						)} and {len(lowercase						)}'						)

        return images_with_overflow





     def 							lowercase__				(			self		: Union[str, Any]     ,				*lowercase		: List[Any]     ,				**lowercase		: Optional[Any]						):





        """simple docstring"""




        return self.tokenizer.batch_decode(*lowercase     ,				**lowercase						)





     def 							lowercase__				(			self		: List[Any]     ,				*lowercase		: Any     ,				**lowercase		: str						):





        """simple docstring"""




        return self.tokenizer.decode(*lowercase     ,				**lowercase						)





     @property
     def 							lowercase__				(			self		: Optional[Any]						):





        """simple docstring"""




        return ["input_ids", "bbox", "attention_mask", "pixel_values"]





     @property
     def 							lowercase__				(			self		: str						):





        """simple docstring"""




        warnings.warn(
            "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."     ,				lowercase     ,				)
        return self.image_processor_class







     @property
     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        warnings.warn(
            "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."     ,				lowercase     ,				)
        return self.image_processor


