






'''simple docstring'''





import unittest

import numpy as np

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


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import ChineseCLIPImageProcessor






class 				UpperCAmelCase (       unittest.TestCase ):




    '''simple docstring'''







    def __init__(       self    ,							lowercase__    ,							lowercase__=7    ,							lowercase__=3    ,							lowercase__=18    ,							lowercase__=30    ,							lowercase__=400    ,							lowercase__=True    ,							lowercase__=None    ,							lowercase__=True    ,							lowercase__=None    ,							lowercase__=True    ,							lowercase__=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]    ,							lowercase__=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]    ,							lowercase__=True    ,							)							-> str:
         SCREAMING_SNAKE_CASE					:			Tuple    	=       size if size is not None else {'height': 224, 'width': 224}
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       crop_size if crop_size is not None else {'height': 18, 'width': 18}
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       parent
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       batch_size
         SCREAMING_SNAKE_CASE					:			Dict    	=       num_channels
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       image_size
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       min_resolution
         SCREAMING_SNAKE_CASE					:			str    	=       max_resolution
         SCREAMING_SNAKE_CASE					:			int    	=       do_resize
         SCREAMING_SNAKE_CASE					:			Dict    	=       size
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       do_center_crop
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       crop_size
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       do_normalize
         SCREAMING_SNAKE_CASE					:			Tuple    	=       image_mean
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       image_std
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       do_convert_rgb






    def       _UpperCamelCase    (       self )							-> List[Any]:
         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_convert_rgb": self.do_convert_rgb,
         }






    def       _UpperCamelCase    (       self    ,							lowercase__=False    ,							lowercase__=False    ,							lowercase__=False )							-> str:
         assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

         if equal_resolution:
              SCREAMING_SNAKE_CASE					:			List[Any]    	=       []
              for i in range(self.batch_size ):
                   image_inputs.append(
                       np.random.randint(
                           255    ,							size=(self.num_channels, self.max_resolution, self.max_resolution)    ,							dtype=np.uinta ) )
         else:
              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       []
              for i in range(self.batch_size ):
                   SCREAMING_SNAKE_CASE					:			Any    	=       np.random.choice(np.arange(self.min_resolution    ,							self.max_resolution )    ,							2 )
                   image_inputs.append(np.random.randint(255    ,							size=(self.num_channels, width, height)    ,							dtype=np.uinta ) )

         if not numpify and not torchify:
              # PIL expects the channel dimension as last dimension
              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       [Image.fromarray(np.moveaxis(lowercase__    ,							0    ,							-1 ) ) for x in image_inputs]

         if torchify:
              SCREAMING_SNAKE_CASE					:			List[str]    	=       [torch.from_numpy(lowercase__ ) for x in image_inputs]

         return image_inputs






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




    '''simple docstring'''







    snake_case__							:   List[Any]							=						ChineseCLIPImageProcessor if is_vision_available() else None






    def       _UpperCamelCase    (       self )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE					:			int    	=       ChineseCLIPImageProcessingTester(self    ,							do_center_crop=lowercase__ )






    @property
    def       _UpperCamelCase    (       self )							-> int:
         return self.image_processor_tester.prepare_image_processor_dict()






    def       _UpperCamelCase    (       self )							-> Any:
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       self.image_processing_class(**self.image_processor_dict )
         self.assertTrue(hasattr(lowercase__    ,							'do_resize' ) )
         self.assertTrue(hasattr(lowercase__    ,							'size' ) )
         self.assertTrue(hasattr(lowercase__    ,							'do_center_crop' ) )
         self.assertTrue(hasattr(lowercase__    ,							'center_crop' ) )
         self.assertTrue(hasattr(lowercase__    ,							'do_normalize' ) )
         self.assertTrue(hasattr(lowercase__    ,							'image_mean' ) )
         self.assertTrue(hasattr(lowercase__    ,							'image_std' ) )
         self.assertTrue(hasattr(lowercase__    ,							'do_convert_rgb' ) )






    def       _UpperCamelCase    (       self )							-> Any:
         SCREAMING_SNAKE_CASE					:			Dict    	=       self.image_processing_class.from_dict(self.image_processor_dict )
         self.assertEqual(image_processor.size    ,							{'height': 224, 'width': 224} )
         self.assertEqual(image_processor.crop_size    ,							{'height': 18, 'width': 18} )

         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.image_processing_class.from_dict(self.image_processor_dict    ,							size=42    ,							crop_size=84 )
         self.assertEqual(image_processor.size    ,							{'shortest_edge': 42} )
         self.assertEqual(image_processor.crop_size    ,							{'height': 84, 'width': 84} )






    def       _UpperCamelCase    (       self )							-> Union[str, Any]:
         pass






    def       _UpperCamelCase    (       self )							-> str:
         # Initialize image_processing
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       self.image_processing_class(**self.image_processor_dict )
         # create random PIL images
         SCREAMING_SNAKE_CASE					:			Dict    	=       self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__ )
         for image in image_inputs:
              self.assertIsInstance(lowercase__    ,							Image.Image )

         # Test not batched input
         SCREAMING_SNAKE_CASE					:			str    	=       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
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       image_processing(lowercase__    ,							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       _UpperCamelCase    (       self )							-> str:
         # Initialize image_processing
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       self.image_processing_class(**self.image_processor_dict )
         # create random numpy tensors
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__    ,							numpify=lowercase__ )
         for image in image_inputs:
              self.assertIsInstance(lowercase__    ,							np.ndarray )

         # Test not batched input
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       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
         SCREAMING_SNAKE_CASE					:			str    	=       image_processing(lowercase__    ,							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       _UpperCamelCase    (       self )							-> List[Any]:
         # Initialize image_processing
         SCREAMING_SNAKE_CASE					:			str    	=       self.image_processing_class(**self.image_processor_dict )
         # create random PyTorch tensors
         SCREAMING_SNAKE_CASE					:			Dict    	=       self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__    ,							torchify=lowercase__ )
         for image in image_inputs:
              self.assertIsInstance(lowercase__    ,							torch.Tensor )

         # Test not batched input
         SCREAMING_SNAKE_CASE					:			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.crop_size['height'],
                 self.image_processor_tester.crop_size['width'],
             )    ,							)

         # Test batched
         SCREAMING_SNAKE_CASE					:			Dict    	=       image_processing(lowercase__    ,							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'],
             )    ,							)






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




    '''simple docstring'''







    snake_case__							:   Tuple							=						ChineseCLIPImageProcessor if is_vision_available() else None






    def       _UpperCamelCase    (       self )							-> str:
         SCREAMING_SNAKE_CASE					:			Any    	=       ChineseCLIPImageProcessingTester(self    ,							num_channels=4    ,							do_center_crop=lowercase__ )
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       3






    @property
    def       _UpperCamelCase    (       self )							-> Any:
         return self.image_processor_tester.prepare_image_processor_dict()






    def       _UpperCamelCase    (       self )							-> List[Any]:
         SCREAMING_SNAKE_CASE					:			Any    	=       self.image_processing_class(**self.image_processor_dict )
         self.assertTrue(hasattr(lowercase__    ,							'do_resize' ) )
         self.assertTrue(hasattr(lowercase__    ,							'size' ) )
         self.assertTrue(hasattr(lowercase__    ,							'do_center_crop' ) )
         self.assertTrue(hasattr(lowercase__    ,							'center_crop' ) )
         self.assertTrue(hasattr(lowercase__    ,							'do_normalize' ) )
         self.assertTrue(hasattr(lowercase__    ,							'image_mean' ) )
         self.assertTrue(hasattr(lowercase__    ,							'image_std' ) )
         self.assertTrue(hasattr(lowercase__    ,							'do_convert_rgb' ) )






    def       _UpperCamelCase    (       self )							-> Optional[int]:
         pass






    def       _UpperCamelCase    (       self )							-> List[Any]:
         # Initialize image_processing
         SCREAMING_SNAKE_CASE					:			List[str]    	=       self.image_processing_class(**self.image_processor_dict )
         # create random PIL images
         SCREAMING_SNAKE_CASE					:			Tuple    	=       self.image_processor_tester.prepare_inputs(equal_resolution=lowercase__ )
         for image in image_inputs:
              self.assertIsInstance(lowercase__    ,							Image.Image )

         # Test not batched input
         SCREAMING_SNAKE_CASE					:			str    	=       image_processing(image_inputs[0]    ,							return_tensors='pt' ).pixel_values
         self.assertEqual(
             encoded_images.shape    ,							(
                 1,
                 self.expected_encoded_image_num_channels,
                 self.image_processor_tester.crop_size['height'],
                 self.image_processor_tester.crop_size['width'],
             )    ,							)

         # Test batched
         SCREAMING_SNAKE_CASE					:			Any    	=       image_processing(lowercase__    ,							return_tensors='pt' ).pixel_values
         self.assertEqual(
             encoded_images.shape    ,							(
                 self.image_processor_tester.batch_size,
                 self.expected_encoded_image_num_channels,
                 self.image_processor_tester.crop_size['height'],
                 self.image_processor_tester.crop_size['width'],
             )    ,							)




'''simple docstring'''





import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowerCAmelCase  :str  				=    logging.get_logger(__name__)

_lowerCAmelCase  :Optional[int]  				=    {
    """nvidia/segformer-b0-finetuned-ade-512-512""": (
        """https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json"""
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}






class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







    snake_case__							:   Optional[int]							=						"segformer"






    def __init__(       self    ,							lowercase__=3    ,							lowercase__=4    ,							lowercase__=[2, 2, 2, 2]    ,							lowercase__=[8, 4, 2, 1]    ,							lowercase__=[32, 64, 160, 256]    ,							lowercase__=[7, 3, 3, 3]    ,							lowercase__=[4, 2, 2, 2]    ,							lowercase__=[1, 2, 5, 8]    ,							lowercase__=[4, 4, 4, 4]    ,							lowercase__="gelu"    ,							lowercase__=0.0    ,							lowercase__=0.0    ,							lowercase__=0.1    ,							lowercase__=0.0_2    ,							lowercase__=0.1    ,							lowercase__=1E-6    ,							lowercase__=256    ,							lowercase__=255    ,							**lowercase__    ,							)							-> Union[str, Any]:
         super().__init__(**lowercase__ )

         if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
              warnings.warn(
                  'Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'
                  ' removed, as the behaviour will default to that of reshape_last_stage = True.'    ,							lowercase__    ,							)

         SCREAMING_SNAKE_CASE					:			int    	=       num_channels
         SCREAMING_SNAKE_CASE					:			int    	=       num_encoder_blocks
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       depths
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       sr_ratios
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       hidden_sizes
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       patch_sizes
         SCREAMING_SNAKE_CASE					:			int    	=       strides
         SCREAMING_SNAKE_CASE					:			Tuple    	=       mlp_ratios
         SCREAMING_SNAKE_CASE					:			List[str]    	=       num_attention_heads
         SCREAMING_SNAKE_CASE					:			List[str]    	=       hidden_act
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       hidden_dropout_prob
         SCREAMING_SNAKE_CASE					:			Tuple    	=       attention_probs_dropout_prob
         SCREAMING_SNAKE_CASE					:			Tuple    	=       classifier_dropout_prob
         SCREAMING_SNAKE_CASE					:			Any    	=       initializer_range
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       drop_path_rate
         SCREAMING_SNAKE_CASE					:			int    	=       layer_norm_eps
         SCREAMING_SNAKE_CASE					:			Any    	=       decoder_hidden_size
         SCREAMING_SNAKE_CASE					:			List[str]    	=       kwargs.get('reshape_last_stage'    ,							lowercase__ )
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       semantic_loss_ignore_index






class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







    snake_case__							:   Tuple							=						version.parse("1.11" )






    @property
    def       _UpperCamelCase    (       self )							-> Mapping[str, Mapping[int, str]]:
         return OrderedDict(
             [
                 ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
             ] )






    @property
    def       _UpperCamelCase    (       self )							-> float:
         return 1E-4






    @property
    def       _UpperCamelCase    (       self )							-> int:
         return 12



