





import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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 MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
 from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import MobileNetVaImageProcessor


class     __magic_name__     (							lowerCamelCase__							):





     """simple docstring"""



     def   SCREAMING_SNAKE_CASE						(							self				:List[str]     ):





        '''simple docstring'''




        A_    : Tuple								=	self.config_class(**self.inputs_dict     )
        self.parent.assertTrue(hasattr(snake_case	,					"tf_padding"     )     )
        self.parent.assertTrue(hasattr(snake_case	,					"depth_multiplier"     )     )




class     __magic_name__     :





     """simple docstring"""



     def __init__(							self				:Dict	,					snake_case				:List[str]	,					snake_case				:Dict=13	,					snake_case				:Optional[int]=3	,					snake_case				:Any=32	,					snake_case				:Any=0.25	,					snake_case				:Union[str, Any]=8	,					snake_case				:List[Any]=8	,					snake_case				:List[Any]=6	,					snake_case				:Dict=32	,					snake_case				:Dict=True	,					snake_case				:Optional[Any]=True	,					snake_case				:Tuple=True	,					snake_case				:Tuple="relu6"	,					snake_case				:List[Any]=1_280	,					snake_case				:Optional[Any]=0.1	,					snake_case				:int=0.02	,					snake_case				:Optional[Any]=True	,					snake_case				:List[str]=True	,					snake_case				:List[str]=10	,					snake_case				:Optional[Any]=None	,					):





        '''simple docstring'''




        A_    : Union[str, Any]								=	parent
        A_    : Tuple								=	batch_size
        A_    : str								=	num_channels
        A_    : Tuple								=	image_size
        A_    : List[str]								=	depth_multiplier
        A_    : str								=	depth_divisible_by
        A_    : Union[str, Any]								=	min_depth
        A_    : Optional[int]								=	expand_ratio
        A_    : Dict								=	tf_padding
        A_    : List[str]								=	output_stride
        A_    : Tuple								=	first_layer_is_expansion
        A_    : Optional[Any]								=	finegrained_output
        A_    : Dict								=	hidden_act
        A_    : str								=	last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier     )
        A_    : Tuple								=	classifier_dropout_prob
        A_    : List[Any]								=	use_labels
        A_    : Tuple								=	is_training
        A_    : Optional[int]								=	num_labels
        A_    : Tuple								=	initializer_range
        A_    : Union[str, Any]								=	scope



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





        '''simple docstring'''




        A_    : Any								=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

        A_    : Optional[Any]								=	None
        A_    : Optional[int]								=	None
        if self.use_labels:
           A_    : Any								=	ids_tensor([self.batch_size]	,					self.num_labels     )
           A_    : int								=	ids_tensor([self.batch_size, self.image_size, self.image_size]	,					self.num_labels     )

        A_    : Dict								=	self.get_config()

        return config, pixel_values, labels, pixel_labels



     def   SCREAMING_SNAKE_CASE						(							self				:str     ):





        '''simple docstring'''




        return MobileNetVaConfig(
            num_channels=self.num_channels	,					image_size=self.image_size	,					depth_multiplier=self.depth_multiplier	,					depth_divisible_by=self.depth_divisible_by	,					min_depth=self.min_depth	,					expand_ratio=self.expand_ratio	,					output_stride=self.output_stride	,					first_layer_is_expansion=self.first_layer_is_expansion	,					finegrained_output=self.finegrained_output	,					hidden_act=self.hidden_act	,					tf_padding=self.tf_padding	,					classifier_dropout_prob=self.classifier_dropout_prob	,					initializer_range=self.initializer_range	,					)



     def   SCREAMING_SNAKE_CASE						(							self				:Optional[Any]	,					snake_case				:str	,					snake_case				:List[str]	,					snake_case				:str	,					snake_case				:Dict     ):





        '''simple docstring'''




        A_    : List[Any]								=	MobileNetVaModel(config=snake_case     )
        model.to(snake_case     )
        model.eval()
        A_    : Any								=	model(snake_case     )
        self.parent.assertEqual(
            result.last_hidden_state.shape	,					(
                self.batch_size,
                self.last_hidden_size,
                self.image_size // self.output_stride,
                self.image_size // self.output_stride,
            )	,					)
        self.parent.assertEqual(
            result.pooler_output.shape	,					(self.batch_size, self.last_hidden_size)	,					)



     def   SCREAMING_SNAKE_CASE						(							self				:List[Any]	,					snake_case				:Optional[int]	,					snake_case				:Optional[int]	,					snake_case				:Optional[Any]	,					snake_case				:List[Any]     ):





        '''simple docstring'''




        A_    : Union[str, Any]								=	self.num_labels
        A_    : Dict								=	MobileNetVaForImageClassification(snake_case     )
        model.to(snake_case     )
        model.eval()
        A_    : List[Any]								=	model(snake_case	,					labels=snake_case     )
        self.parent.assertEqual(result.logits.shape	,					(self.batch_size, self.num_labels)     )



     def   SCREAMING_SNAKE_CASE						(							self				:Any	,					snake_case				:int	,					snake_case				:Dict	,					snake_case				:int	,					snake_case				:List[Any]     ):





        '''simple docstring'''




        A_    : Tuple								=	self.num_labels
        A_    : List[str]								=	MobileNetVaForSemanticSegmentation(snake_case     )
        model.to(snake_case     )
        model.eval()
        A_    : Any								=	model(snake_case     )
        self.parent.assertEqual(
            result.logits.shape	,					(
                self.batch_size,
                self.num_labels,
                self.image_size // self.output_stride,
                self.image_size // self.output_stride,
            )	,					)
        A_    : Union[str, Any]								=	model(snake_case	,					labels=snake_case     )
        self.parent.assertEqual(
            result.logits.shape	,					(
                self.batch_size,
                self.num_labels,
                self.image_size // self.output_stride,
                self.image_size // self.output_stride,
            )	,					)





     def   SCREAMING_SNAKE_CASE						(							self				:str     ):





        '''simple docstring'''




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




@require_torch
class     __magic_name__     (							lowerCamelCase__			, lowerCamelCase__			, unittest.TestCase							):





     """simple docstring"""



     __UpperCamelCase    			=   (
         (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
         if is_torch_available()
         else ()
     )
     __UpperCamelCase    			=   (
         {
             "feature-extraction": MobileNetVaModel,
             "image-classification": MobileNetVaForImageClassification,
             "image-segmentation": MobileNetVaForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     __UpperCamelCase    			=   False
     __UpperCamelCase    			=   False
     __UpperCamelCase    			=   False
     __UpperCamelCase    			=   False



     def   SCREAMING_SNAKE_CASE						(							self				:Any     ):





        '''simple docstring'''




        A_    : Any								=	MobileNetVaModelTester(self     )
        A_    : List[str]								=	MobileNetVaConfigTester(self	,					config_class=snake_case	,					has_text_modality=snake_case     )



     def   SCREAMING_SNAKE_CASE						(							self				:Any     ):





        '''simple docstring'''




        self.config_tester.run_common_tests()



     @unittest.skip(reason="MobileNetV2 does not use inputs_embeds"     )
     def   SCREAMING_SNAKE_CASE						(							self				:List[str]     ):





        '''simple docstring'''




        pass



     @unittest.skip(reason="MobileNetV2 does not support input and output embeddings"     )
     def   SCREAMING_SNAKE_CASE						(							self				:int     ):





        '''simple docstring'''




        pass



     @unittest.skip(reason="MobileNetV2 does not output attentions"     )
     def   SCREAMING_SNAKE_CASE						(							self				:Any     ):





        '''simple docstring'''




        pass



     def   SCREAMING_SNAKE_CASE						(							self				:Optional[int]     ):





        '''simple docstring'''




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

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

           A_    : Any								=	["pixel_values"]
           self.assertListEqual(arg_names[:1]	,					snake_case     )



     def   SCREAMING_SNAKE_CASE						(							self				:List[str]     ):





        '''simple docstring'''




        A_    : str								=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case     )



     def   SCREAMING_SNAKE_CASE						(							self				:List[Any]     ):





        '''simple docstring'''




        def check_hidden_states_output(snake_case				:List[Any]	,					snake_case				:Union[str, Any]	,					snake_case				:str     ):
           A_    : int								=	model_class(snake_case     )
           model.to(snake_case     )
           model.eval()

           with torch.no_grad():
              A_    : Dict								=	model(**self._prepare_for_class(snake_case	,					snake_case     )     )

           A_    : Union[str, Any]								=	outputs.hidden_states

           A_    : List[str]								=	16
           self.assertEqual(len(snake_case     )	,					snake_case     )

        A_					,  A_    : str								=	self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           A_    : Any								=	True
           check_hidden_states_output(snake_case	,					snake_case	,					snake_case     )

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

           check_hidden_states_output(snake_case	,					snake_case	,					snake_case     )



     def   SCREAMING_SNAKE_CASE						(							self				:Tuple     ):





        '''simple docstring'''




        A_    : List[Any]								=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*snake_case     )



     def   SCREAMING_SNAKE_CASE						(							self				:Dict     ):





        '''simple docstring'''




        A_    : Tuple								=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_semantic_segmentation(*snake_case     )





     @slow
     def   SCREAMING_SNAKE_CASE						(							self				:List[Any]     ):





        '''simple docstring'''




        for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           A_    : List[str]								=	MobileNetVaModel.from_pretrained(snake_case     )
           self.assertIsNotNone(snake_case     )




def    __snake_case  (    )       ->  Union[str, Any]:
   A_    : List[str]								=	Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
   return image





@require_torch
@require_vision
class     __magic_name__     (							unittest.TestCase							):





     """simple docstring"""



     @cached_property
     def   SCREAMING_SNAKE_CASE						(							self				:Optional[Any]     ):





        '''simple docstring'''




        return (
            MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v2_1.0_224"     ) if is_vision_available() else None
        )



     @slow
     def   SCREAMING_SNAKE_CASE						(							self				:List[Any]     ):





        '''simple docstring'''




        A_    : Dict								=	MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v2_1.0_224"     ).to(snake_case     )

        A_    : int								=	self.default_image_processor
        A_    : Optional[Any]								=	prepare_img()
        A_    : Union[str, Any]								=	image_processor(images=snake_case	,					return_tensors="pt"     ).to(snake_case     )

        # forward pass
        with torch.no_grad():
           A_    : Union[str, Any]								=	model(**snake_case     )

        # verify the logits
        A_    : Optional[int]								=	torch.Size((1, 1_001)     )
        self.assertEqual(outputs.logits.shape	,					snake_case     )

        A_    : str								=	torch.tensor([0.2445, -1.1993, 0.1905]     ).to(snake_case     )

        self.assertTrue(torch.allclose(outputs.logits[0, :3]	,					snake_case	,					atol=1e-4     )     )





     @slow
     def   SCREAMING_SNAKE_CASE						(							self				:Dict     ):





        '''simple docstring'''




        A_    : Optional[Any]								=	MobileNetVaForSemanticSegmentation.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513"     )
        A_    : Tuple								=	model.to(snake_case     )

        A_    : str								=	MobileNetVaImageProcessor.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513"     )

        A_    : Tuple								=	prepare_img()
        A_    : str								=	image_processor(images=snake_case	,					return_tensors="pt"     ).to(snake_case     )

        # forward pass
        with torch.no_grad():
           A_    : Optional[Any]								=	model(**snake_case     )
        A_    : Any								=	outputs.logits

        # verify the logits
        A_    : Any								=	torch.Size((1, 21, 65, 65)     )
        self.assertEqual(logits.shape	,					snake_case     )

        A_    : Tuple								=	torch.tensor(
            [
                [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
                [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
                [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
            ]	,					device=snake_case	,					)

        self.assertTrue(torch.allclose(logits[0, :3, :3, :3]	,					snake_case	,					atol=1e-4     )     )


import os
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import torch
from torch import nn

from ...models.controlnet import ControlNetModel, ControlNetOutput
from ...models.modeling_utils import ModelMixin
from ...utils import logging


_lowerCamelCase       :				List[str]       =			logging.get_logger(__name__)




class    SCREAMING_SNAKE_CASE__     (	UpperCAmelCase					):





	'''simple docstring'''



	def __init__(  self	:    Dict			,  lowercase	:    Union[List[ControlNetModel], Tuple[ControlNetModel]]	):


				'''simple docstring'''


				super().__init__()
				_snake_case		=			nn.ModuleList(lowercase	)



	def  A							(  self	:    Optional[int]			,  lowercase	:    torch.FloatTensor			,  lowercase	:    Union[torch.Tensor, float, int]			,  lowercase	:    torch.Tensor			,  lowercase	:    List[torch.tensor]			,  lowercase	:    List[float]			,  lowercase	:    Optional[torch.Tensor] = None			,  lowercase	:    Optional[torch.Tensor] = None			,  lowercase	:    Optional[torch.Tensor] = None			,  lowercase	:    Optional[Dict[str, Any]] = None			,  lowercase	:    bool = False			,  lowercase	:    bool = True			,  ):


				'''simple docstring'''


				for i, (image, scale, controlnet) in enumerate(zip(lowercase			,  lowercase			,  self.nets	)	):
							_snake_case	,					_snake_case		=			controlnet(
							    lowercase			,  lowercase			,  lowercase			,  lowercase			,  lowercase			,  lowercase			,  lowercase			,  lowercase			,  lowercase			,  lowercase			,  lowercase			,  )

							# merge samples
							if i == 0:
										_snake_case	,					_snake_case		=			down_samples, mid_sample
							else:
										_snake_case		=			[
										    samples_prev + samples_curr
										    for samples_prev, samples_curr in zip(lowercase			,  lowercase	)
										]
										mid_block_res_sample += mid_sample

				return down_block_res_samples, mid_block_res_sample



	def  A							(  self	:    Dict			,  lowercase	:    Union[str, os.PathLike]			,  lowercase	:    bool = True			,  lowercase	:    Callable = None			,  lowercase	:    bool = False			,  lowercase	:    Optional[str] = None			,  ):


				'''simple docstring'''


				_snake_case		=			0
				_snake_case		=			save_directory
				for controlnet in self.nets:
							controlnet.save_pretrained(
							    lowercase			,  is_main_process=lowercase			,  save_function=lowercase			,  safe_serialization=lowercase			,  variant=lowercase			,  )

							idx += 1
							_snake_case		=			model_path_to_save + f'''_{idx}'''





	@classmethod
	def  A							(  cls	:    Any			,  lowercase	:    Optional[Union[str, os.PathLike]]			,  **lowercase	:    List[str]	):


				'''simple docstring'''


				_snake_case		=			0
				_snake_case		=			[]

				# load controlnet and append to list until no controlnet directory exists anymore
				# first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained`
				# second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ...
				_snake_case		=			pretrained_model_path
				while os.path.isdir(lowercase	):
							_snake_case		=			ControlNetModel.from_pretrained(lowercase			,  **lowercase	)
							controlnets.append(lowercase	)

							idx += 1
							_snake_case		=			pretrained_model_path + f'''_{idx}'''

				logger.info(f'''{len(lowercase	)} controlnets loaded from {pretrained_model_path}.'''	)

				if len(lowercase	) == 0:
							raise ValueError(
							    f'''No ControlNets found under {os.path.dirname(lowercase	)}. Expected at least {pretrained_model_path + '_0'}.'''	)

				return cls(lowercase	)