






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      UpperCamelCase (				a__ ):







		"""simple docstring"""



		def       A(  self		:       Union[str, Any]		)    ->   Optional[int]:


						'''simple docstring'''
						A	 =      self.config_class(**self.inputs_dict		)
						self.parent.assertTrue(hasattr(lowerCAmelCase__						,'tf_padding'		)		)
						self.parent.assertTrue(hasattr(lowerCAmelCase__						,'depth_multiplier'		)		)


class      UpperCamelCase :







		"""simple docstring"""



		def __init__(  self		:       List[Any]						,_SCREAMING_SNAKE_CASE		:       List[Any]						,_SCREAMING_SNAKE_CASE		:       Optional[int]=1_3						,_SCREAMING_SNAKE_CASE		:       Union[str, Any]=3						,_SCREAMING_SNAKE_CASE		:       Dict=3_2						,_SCREAMING_SNAKE_CASE		:       List[Any]=0.25						,_SCREAMING_SNAKE_CASE		:       str=8						,_SCREAMING_SNAKE_CASE		:       Optional[int]=8						,_SCREAMING_SNAKE_CASE		:       List[Any]=6						,_SCREAMING_SNAKE_CASE		:       List[Any]=3_2						,_SCREAMING_SNAKE_CASE		:       Tuple=True						,_SCREAMING_SNAKE_CASE		:       Tuple=True						,_SCREAMING_SNAKE_CASE		:       Dict=True						,_SCREAMING_SNAKE_CASE		:       List[str]="relu6"						,_SCREAMING_SNAKE_CASE		:       List[str]=1_2_8_0						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=0.1						,_SCREAMING_SNAKE_CASE		:       int=0.02						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=True						,_SCREAMING_SNAKE_CASE		:       List[Any]=True						,_SCREAMING_SNAKE_CASE		:       Dict=1_0						,_SCREAMING_SNAKE_CASE		:       int=None						,)    ->   str:


						'''simple docstring'''
						A	 =      parent
						A	 =      batch_size
						A	 =      num_channels
						A	 =      image_size
						A	 =      depth_multiplier
						A	 =      depth_divisible_by
						A	 =      min_depth
						A	 =      expand_ratio
						A	 =      tf_padding
						A	 =      output_stride
						A	 =      first_layer_is_expansion
						A	 =      finegrained_output
						A	 =      hidden_act
						A	 =      last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier		)
						A	 =      classifier_dropout_prob
						A	 =      use_labels
						A	 =      is_training
						A	 =      num_labels
						A	 =      initializer_range
						A	 =      scope
		def       A(  self		:       Optional[int]		)    ->   str:


						'''simple docstring'''
						A	 =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

						A	 =      self.get_config()

						return config, pixel_values, labels, pixel_labels
		def       A(  self		:       str		)    ->   List[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       A(  self		:       List[str]						,_SCREAMING_SNAKE_CASE		:       List[str]						,_SCREAMING_SNAKE_CASE		:       str						,_SCREAMING_SNAKE_CASE		:       List[str]						,_SCREAMING_SNAKE_CASE		:       int		)    ->   Optional[Any]:


						'''simple docstring'''
						A	 =      MobileNetVaModel(config=lowerCAmelCase__		)
						model.to(lowerCAmelCase__		)
						model.eval()
						A	 =      model(lowerCAmelCase__		)
						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       A(  self		:       Union[str, Any]						,_SCREAMING_SNAKE_CASE		:       List[str]						,_SCREAMING_SNAKE_CASE		:       List[Any]						,_SCREAMING_SNAKE_CASE		:       Any						,_SCREAMING_SNAKE_CASE		:       Tuple		)    ->   List[str]:


						'''simple docstring'''
						A	 =      self.num_labels
						A	 =      MobileNetVaForImageClassification(lowerCAmelCase__		)
						model.to(lowerCAmelCase__		)
						model.eval()
						A	 =      model(lowerCAmelCase__						,labels=lowerCAmelCase__		)
						self.parent.assertEqual(result.logits.shape						,(self.batch_size, self.num_labels)		)
		def       A(  self		:       Tuple						,_SCREAMING_SNAKE_CASE		:       Union[str, Any]						,_SCREAMING_SNAKE_CASE		:       List[Any]						,_SCREAMING_SNAKE_CASE		:       Any						,_SCREAMING_SNAKE_CASE		:       List[Any]		)    ->   List[str]:


						'''simple docstring'''
						A	 =      self.num_labels
						A	 =      MobileNetVaForSemanticSegmentation(lowerCAmelCase__		)
						model.to(lowerCAmelCase__		)
						model.eval()
						A	 =      model(lowerCAmelCase__		)
						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	 =      model(lowerCAmelCase__						,labels=lowerCAmelCase__		)
						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       A(  self		:       Union[str, Any]		)    ->   str:


						'''simple docstring'''
						A	 =      self.prepare_config_and_inputs()
						A	 =      config_and_inputs
						A	 =      {"pixel_values": pixel_values}
						return config, inputs_dict


@require_torch
class      UpperCamelCase (				a__			,       a__			,       unittest.TestCase ):







		"""simple docstring"""



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

		snake_case    =   False
		snake_case    =   False
		snake_case    =   False
		snake_case    =   False
		def       A(  self		:       Optional[Any]		)    ->   List[Any]:


						'''simple docstring'''
						A	 =      MobileNetVaModelTester(self		)
						A	 =      MobileNetVaConfigTester(self						,config_class=lowerCAmelCase__						,has_text_modality=lowerCAmelCase__		)
		def       A(  self		:       Union[str, Any]		)    ->   str:


						'''simple docstring'''
						self.config_tester.run_common_tests()
		@unittest.skip(reason='MobileNetV2 does not use inputs_embeds'		)
		def       A(  self		:       str		)    ->   str:


						'''simple docstring'''
						pass
		@unittest.skip(reason='MobileNetV2 does not support input and output embeddings'		)
		def       A(  self		:       List[str]		)    ->   int:


						'''simple docstring'''
						pass
		@unittest.skip(reason='MobileNetV2 does not output attentions'		)
		def       A(  self		:       str		)    ->   List[Any]:


						'''simple docstring'''
						pass
		def       A(  self		:       List[Any]		)    ->   Optional[int]:


						'''simple docstring'''
						A	 =      self.model_tester.prepare_config_and_inputs_for_common()

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

										A	 =      ["pixel_values"]
										self.assertListEqual(arg_names[:1]						,lowerCAmelCase__		)
		def       A(  self		:       Optional[Any]		)    ->   Optional[int]:


						'''simple docstring'''
						A	 =      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCAmelCase__		)
		def       A(  self		:       Dict		)    ->   Optional[int]:


						'''simple docstring'''
						def check_hidden_states_output(_SCREAMING_SNAKE_CASE		:       List[str]						,_SCREAMING_SNAKE_CASE		:       Dict						,_SCREAMING_SNAKE_CASE		:       Any		):
										A	 =      model_class(lowerCAmelCase__		)
										model.to(lowerCAmelCase__		)
										model.eval()

										with torch.no_grad():
														A	 =      model(**self._prepare_for_class(lowerCAmelCase__						,lowerCAmelCase__		)		)

										A	 =      outputs.hidden_states

										A	 =      1_6
										self.assertEqual(len(lowerCAmelCase__		)						,lowerCAmelCase__		)

						A	 =      self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										A	 =      True
										check_hidden_states_output(lowerCAmelCase__						,lowerCAmelCase__						,lowerCAmelCase__		)

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

										check_hidden_states_output(lowerCAmelCase__						,lowerCAmelCase__						,lowerCAmelCase__		)
		def       A(  self		:       int		)    ->   List[Any]:


						'''simple docstring'''
						A	 =      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__		)
		def       A(  self		:       Any		)    ->   Any:


						'''simple docstring'''
						A	 =      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_semantic_segmentation(*lowerCAmelCase__		)







		@slow
		def       A(  self		:       Optional[int]		)    ->   str:


						'''simple docstring'''
						for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										A	 =      MobileNetVaModel.from_pretrained(lowerCAmelCase__		)
										self.assertIsNotNone(lowerCAmelCase__		)







def      snake_case		(							):
				A	 =      Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
				return image



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







		"""simple docstring"""



		@cached_property
		def       A(  self		:       List[str]		)    ->   Optional[Any]:


						'''simple docstring'''
						return (
						    MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224'		) if is_vision_available() else None
						)
		@slow
		def       A(  self		:       Optional[int]		)    ->   List[str]:


						'''simple docstring'''
						A	 =      MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'		).to(lowerCAmelCase__		)

						A	 =      self.default_image_processor
						A	 =      prepare_img()
						A	 =      image_processor(images=lowerCAmelCase__						,return_tensors='pt'		).to(lowerCAmelCase__		)

						# forward pass
						with torch.no_grad():
										A	 =      model(**lowerCAmelCase__		)

						# verify the logits
						A	 =      torch.Size((1, 1_0_0_1)		)
						self.assertEqual(outputs.logits.shape						,lowerCAmelCase__		)

						A	 =      torch.tensor([0.2445, -1.1993, 0.1905]		).to(lowerCAmelCase__		)

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







		@slow
		def       A(  self		:       int		)    ->   int:


						'''simple docstring'''
						A	 =      MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'		)
						A	 =      model.to(lowerCAmelCase__		)

						A	 =      MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'		)

						A	 =      prepare_img()
						A	 =      image_processor(images=lowerCAmelCase__						,return_tensors='pt'		).to(lowerCAmelCase__		)

						# forward pass
						with torch.no_grad():
										A	 =      model(**lowerCAmelCase__		)
						A	 =      outputs.logits

						# verify the logits
						A	 =      torch.Size((1, 2_1, 6_5, 6_5)		)
						self.assertEqual(logits.shape						,lowerCAmelCase__		)

						A	 =      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=lowerCAmelCase__						,)

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













from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
					from PIL import Image

					from ..image_utils import load_image

if is_tf_available():
					from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING

if is_torch_available():
					import torch

					from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING

lowerCAmelCase_           = logging.get_logger(__name__)
@add_end_docstrings(snake_case__ )
class      UpperCamelCase (				snake_case__ ):







		"""simple docstring"""



		def __init__(  self		:       str						,*_SCREAMING_SNAKE_CASE		:       List[Any]						,**_SCREAMING_SNAKE_CASE		:       List[Any]		)    ->   Dict:


						'''simple docstring'''
						super().__init__(*_SCREAMING_SNAKE_CASE						,**_SCREAMING_SNAKE_CASE		)
						requires_backends(self						,'vision'		)
						self.check_model_type(
						    TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == 'tf' else MODEL_FOR_VISION_2_SEQ_MAPPING		)
		def       A(  self		:       Union[str, Any]						,_SCREAMING_SNAKE_CASE		:       List[Any]=None						,_SCREAMING_SNAKE_CASE		:       List[str]=None						,_SCREAMING_SNAKE_CASE		:       int=None		)    ->   Any:


						'''simple docstring'''
						A	 =      {}
						A	 =      {}

						if prompt is not None:
										A	 =      prompt

						if generate_kwargs is not None:
										A	 =      generate_kwargs
						if max_new_tokens is not None:
										if "generate_kwargs" not in forward_kwargs:
														A	 =      {}
										if "max_new_tokens" in forward_kwargs["generate_kwargs"]:
														raise ValueError(
														    '\'max_new_tokens\' is defined twice, once in \'generate_kwargs\' and once as a direct parameter,'
														    ' please use only one'		)
										A	 =      max_new_tokens
						return preprocess_params, forward_kwargs, {}
		def __call__(  self		:       int						,_SCREAMING_SNAKE_CASE		:       Union[str, List[str], "Image.Image", List["Image.Image"]]						,**_SCREAMING_SNAKE_CASE		:       List[str]		)    ->   Union[str, Any]:


						'''simple docstring'''
						return super().__call__(_SCREAMING_SNAKE_CASE						,**_SCREAMING_SNAKE_CASE		)
		def       A(  self		:       Dict						,_SCREAMING_SNAKE_CASE		:       List[Any]						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=None		)    ->   int:


						'''simple docstring'''
						A	 =      load_image(_SCREAMING_SNAKE_CASE		)

						if prompt is not None:
										if not isinstance(_SCREAMING_SNAKE_CASE						,_SCREAMING_SNAKE_CASE		):
														raise ValueError(
														    f'Received an invalid text input, got - {type(_SCREAMING_SNAKE_CASE		)} - but expected a single string. '
														    'Note also that one single text can be provided for conditional image to text generation.'		)

										A	 =      self.model.config.model_type

										if model_type == "git":
														A	 =      self.image_processor(images=_SCREAMING_SNAKE_CASE						,return_tensors=self.framework		)
														A	 =      self.tokenizer(text=_SCREAMING_SNAKE_CASE						,add_special_tokens=_SCREAMING_SNAKE_CASE		).input_ids
														A	 =      [self.tokenizer.cls_token_id] + input_ids
														A	 =      torch.tensor(_SCREAMING_SNAKE_CASE		).unsqueeze(0		)
														model_inputs.update({'input_ids': input_ids}		)

										elif model_type == "pix2struct":
														A	 =      self.image_processor(images=_SCREAMING_SNAKE_CASE						,header_text=_SCREAMING_SNAKE_CASE						,return_tensors=self.framework		)

										elif model_type != "vision-encoder-decoder":
														# vision-encoder-decoder does not support conditional generation
														A	 =      self.image_processor(images=_SCREAMING_SNAKE_CASE						,return_tensors=self.framework		)
														A	 =      self.tokenizer(_SCREAMING_SNAKE_CASE						,return_tensors=self.framework		)
														model_inputs.update(_SCREAMING_SNAKE_CASE		)

										else:
														raise ValueError(f'Model type {model_type} does not support conditional text generation'		)

						else:
										A	 =      self.image_processor(images=_SCREAMING_SNAKE_CASE						,return_tensors=self.framework		)

						if self.model.config.model_type == "git" and prompt is None:
										A	 =      None

						return model_inputs
		def       A(  self		:       str						,_SCREAMING_SNAKE_CASE		:       str						,_SCREAMING_SNAKE_CASE		:       Dict=None		)    ->   List[str]:


						'''simple docstring'''
						# Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the
						# pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first.
						if (
						    "input_ids" in model_inputs
						    and isinstance(model_inputs['input_ids']						,_SCREAMING_SNAKE_CASE		)
						    and all(x is None for x in model_inputs['input_ids']		)
						):
										A	 =      None

						if generate_kwargs is None:
										A	 =      {}
						# FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py`
						#  parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas
						#  the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name`
						#  in the `_prepare_model_inputs` method.
						A	 =      model_inputs.pop(self.model.main_input_name		)
						A	 =      self.model.generate(_SCREAMING_SNAKE_CASE						,**_SCREAMING_SNAKE_CASE						,**_SCREAMING_SNAKE_CASE		)
						return model_outputs







		def       A(  self		:       Dict						,_SCREAMING_SNAKE_CASE		:       Dict		)    ->   Optional[int]:


						'''simple docstring'''
						A	 =      []
						for output_ids in model_outputs:
										A	 =      {
										    'generated_text': self.tokenizer.decode(
										        _SCREAMING_SNAKE_CASE						,skip_special_tokens=_SCREAMING_SNAKE_CASE						,)
										}
										records.append(_SCREAMING_SNAKE_CASE		)
						return records





