


'''simple docstring'''






import copy
import inspect
import unittest

from transformers import PretrainedConfig, SwiftFormerConfig
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 torch import nn

		from transformers import SwiftFormerForImageClassification, SwiftFormerModel
		from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import ViTImageProcessor




class 						a__							:





							'''simple docstring'''






							def __init__(						self   ,       lowerCamelCase_   ,       lowerCamelCase_=13   ,       lowerCamelCase_=3   ,       lowerCamelCase_=True   ,       lowerCamelCase_=True   ,       lowerCamelCase_=0.1   ,       lowerCamelCase_=0.1   ,       lowerCamelCase_=2_24   ,       lowerCamelCase_=10_00   ,       lowerCamelCase_=[3, 3, 6, 4]   ,       lowerCamelCase_=[48, 56, 1_12, 2_20]   ,       )			->				Union[str, Any]:
														lowerCAmelCase__			  =      parent
														lowerCAmelCase__			  =      batch_size
														lowerCAmelCase__			  =      num_channels
														lowerCAmelCase__			  =      is_training
														lowerCAmelCase__			  =      use_labels
														lowerCAmelCase__			  =      hidden_dropout_prob
														lowerCAmelCase__			  =      attention_probs_dropout_prob
														lowerCAmelCase__			  =      num_labels
														lowerCAmelCase__			  =      image_size
														lowerCAmelCase__			  =      layer_depths
														lowerCAmelCase__			  =      embed_dims







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

														lowerCAmelCase__			  =      self.get_config()

														return config, pixel_values, labels







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														return SwiftFormerConfig(
														    depths=self.layer_depths   ,       embed_dims=self.embed_dims   ,       mlp_ratio=4   ,       downsamples=[True, True, True, True]   ,       hidden_act='''gelu'''   ,       num_labels=self.num_labels   ,       down_patch_size=3   ,       down_stride=2   ,       down_pad=1   ,       drop_rate=0.0   ,       drop_path_rate=0.0   ,       use_layer_scale=lowerCamelCase_   ,       layer_scale_init_value=1e-5   ,       )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				int:
														lowerCAmelCase__			  =      SwiftFormerModel(config=lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.embed_dims[-1], 7, 7)  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				List[Any]:
														lowerCAmelCase__			  =      self.num_labels
														lowerCAmelCase__			  =      SwiftFormerForImageClassification(lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()
														lowerCAmelCase__			  =      model(lowerCamelCase_   ,       labels=lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.num_labels)  )

														lowerCAmelCase__			  =      SwiftFormerForImageClassification(lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()

														lowerCAmelCase__			  =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.num_labels)  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														((lowerCAmelCase__)  ,   (lowerCAmelCase__)  ,   (lowerCAmelCase__))			  =      self.prepare_config_and_inputs()
														lowerCAmelCase__			  =      {'''pixel_values''': pixel_values}
														return config, inputs_dict




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





							'''simple docstring'''






							lowercase__     :  Union[str, Any]      		= (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
							lowercase__     :  Tuple      		= (
							    {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification}
							    if is_torch_available()
							    else {}
							)

							lowercase__     :  Dict      		= False
							lowercase__     :  Dict      		= False
							lowercase__     :  List[Any]      		= False
							lowercase__     :  List[str]      		= False
							lowercase__     :  Tuple      		= False







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[Any]:
														lowerCAmelCase__			  =      SwiftFormerModelTester(self  )
														lowerCAmelCase__			  =      ConfigTester(
														    self   ,       config_class=lowerCamelCase_   ,       has_text_modality=lowerCamelCase_   ,       hidden_size=37   ,       num_attention_heads=12   ,       num_hidden_layers=12   ,       )







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







							@unittest.skip(reason='''SwiftFormer does not use inputs_embeds'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														pass







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCAmelCase__			  =      model_class(lowerCamelCase_  )
																					lowerCAmelCase__			  =      model.get_output_embeddings()
																					self.assertTrue(x is None or isinstance(lowerCamelCase_   ,       nn.Linear  )  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Any:
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()

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

																					lowerCAmelCase__			  =      ['''pixel_values''']
																					self.assertListEqual(arg_names[:1]   ,       lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_  )







							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCAmelCase__			  =      SwiftFormerModel.from_pretrained(lowerCamelCase_  )
																					self.assertIsNotNone(lowerCamelCase_  )







							@unittest.skip(reason='''SwiftFormer does not output attentions'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														pass







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														def check_hidden_states_output(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  ):
																					lowerCAmelCase__			  =      model_class(lowerCamelCase_  )
																					model.to(lowerCamelCase_  )
																					model.eval()

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

																					lowerCAmelCase__			  =      outputs.hidden_states

																					lowerCAmelCase__			  =      8
																					self.assertEqual(len(lowerCamelCase_  )   ,       lowerCamelCase_  )  # TODO

																					# SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width)
																					# with the width and height being successively divided by 2, after every 2 blocks
																					for i in range(len(lowerCamelCase_  )  ):
																												self.assertEqual(
																												    hidden_states[i].shape   ,       torch.Size(
																												        [
																												            self.model_tester.batch_size,
																												            self.model_tester.embed_dims[i // 2],
																												            (self.model_tester.image_size // 4) // 2 ** (i // 2),
																												            (self.model_tester.image_size // 4) // 2 ** (i // 2),
																												        ]  )   ,       )

														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()

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

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

																					check_hidden_states_output(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[Any]:
														def _config_zero_init(lowerCamelCase_  ):
																					lowerCAmelCase__			  =      copy.deepcopy(lowerCamelCase_  )
																					for key in configs_no_init.__dict__.keys():
																												if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key:
																																			setattr(lowerCamelCase_   ,       lowerCamelCase_   ,       1e-10  )
																												if isinstance(getattr(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )   ,       lowerCamelCase_  ):
																																			lowerCAmelCase__			  =      _config_zero_init(getattr(lowerCamelCase_   ,       lowerCamelCase_  )  )
																																			setattr(lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )
																					return configs_no_init

														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()

														lowerCAmelCase__			  =      _config_zero_init(lowerCamelCase_  )
														for model_class in self.all_model_classes:
																					lowerCAmelCase__			  =      model_class(config=lowerCamelCase_  )
																					for name, param in model.named_parameters():
																												if param.requires_grad:
																																			self.assertIn(
																																			    ((param.data.mean() * 1e9) / 1e9).round().item()   ,       [0.0, 1.0]   ,       msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""   ,       )







							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														pass


def    _snake_case  (	)		->       List[Any]:
							lowerCAmelCase__			  =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
							return image






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





							'''simple docstring'''






							@cached_property
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Any:
														return ViTImageProcessor.from_pretrained('''MBZUAI/swiftformer-xs'''  ) if is_vision_available() else None







							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														lowerCAmelCase__			  =      SwiftFormerForImageClassification.from_pretrained('''MBZUAI/swiftformer-xs'''  ).to(lowerCamelCase_  )

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

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

														# verify the logits
														lowerCAmelCase__			  =      torch.Size((1, 10_00)  )
														self.assertEqual(outputs.logits.shape   ,       lowerCamelCase_  )

														lowerCAmelCase__			  =      torch.tensor([[-2.17_03e00, 2.11_07e00, -2.08_11e00]]  ).to(lowerCamelCase_  )
														self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,       lowerCamelCase_   ,       atol=1e-4  )  )

from typing import Any, Dict, List, Union

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


if is_vision_available():
							from ..image_utils import load_image


if is_torch_available():
							import torch

							from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING

UpperCamelCase_						:     int	       =							logging.get_logger(__name__)


UpperCamelCase_						:     List[Any]	       =							Dict[str, Any]
UpperCamelCase_						:     Optional[int]	       =							List[Prediction]




@add_end_docstrings(__snake_case       )
class   __lowercase						(						__snake_case       ):






	def __init__(self				: Dict      ,	*snake_case				: List[Any]      ,	**snake_case				: Tuple    ) ->			Any:
							super().__init__(*snake_case      ,	**snake_case    )

							if self.framework == "tf":
													raise ValueError(F"""The {self.__class__} is only available in PyTorch."""    )

							requires_backends(self      ,	"vision"    )
							self.check_model_type(
							    dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items()    )    )






	def       _a(self				: Optional[int]      ,	**snake_case				: Union[str, Any]    ) ->			Any:
							_lowercase       :	List[Any]           =   {}
							if "threshold" in kwargs:
													_lowercase       :	Optional[Any]           =   kwargs["threshold"]
							return {}, {}, postprocess_kwargs






	def __call__(self				: str      ,	*snake_case				: Dict      ,	**snake_case				: Optional[int]    ) ->			Union[Predictions, List[Prediction]]:
							return super().__call__(*snake_case      ,	**snake_case    )






	def       _a(self				: Tuple      ,	snake_case				: str    ) ->			Dict:
							_lowercase       :	List[str]           =   load_image(snake_case    )
							_lowercase       :	str           =   torch.IntTensor([[image.height, image.width]]    )
							_lowercase       :	int           =   self.image_processor(images=[image]      ,	return_tensors="pt"    )
							if self.tokenizer is not None:
													_lowercase       :	List[Any]           =   self.tokenizer(text=inputs["words"]      ,	boxes=inputs["boxes"]      ,	return_tensors="pt"    )
							_lowercase       :	str           =   target_size
							return inputs






	def       _a(self				: List[Any]      ,	snake_case				: Any    ) ->			str:
							_lowercase       :	Dict           =   model_inputs.pop("target_size"    )
							_lowercase       :	str           =   self.model(**snake_case    )
							_lowercase       :	Union[str, Any]           =   outputs.__class__({"target_size": target_size, **outputs}    )
							if self.tokenizer is not None:
													_lowercase       :	str           =   model_inputs["bbox"]
							return model_outputs






	def       _a(self				: str      ,	snake_case				: Tuple      ,	snake_case				: Dict=0.9    ) ->			List[str]:
							_lowercase       :	Optional[Any]           =   model_outputs["target_size"]
							if self.tokenizer is not None:
													# This is a LayoutLMForTokenClassification variant.
													# The OCR got the boxes and the model classified the words.
													_lowercase      ,    _lowercase       :	str           =   target_size[0].tolist()

													def unnormalize(snake_case				: str    ):
																			return self._get_bounding_box(
																			    torch.Tensor(
																			        [
																			            (width * bbox[0] / 1000),
																			            (height * bbox[1] / 1000),
																			            (width * bbox[2] / 1000),
																			            (height * bbox[3] / 1000),
																			        ]    )    )

													_lowercase      ,    _lowercase       :	Any           =   model_outputs["logits"].squeeze(0    ).softmax(dim=-1    ).max(dim=-1    )
													_lowercase       :	Optional[Any]           =   [self.model.config.idalabel[prediction] for prediction in classes.tolist()]
													_lowercase       :	Dict           =   [unnormalize(snake_case    ) for bbox in model_outputs["bbox"].squeeze(0    )]
													_lowercase       :	Tuple           =   ["score", "label", "box"]
													_lowercase       :	int           =   [dict(zip(snake_case      ,	snake_case    )    ) for vals in zip(scores.tolist()      ,	snake_case      ,	snake_case    ) if vals[0] > threshold]
							else:
													# This is a regular ForObjectDetectionModel
													_lowercase       :	str           =   self.image_processor.post_process_object_detection(snake_case      ,	snake_case      ,	snake_case    )
													_lowercase       :	List[Any]           =   raw_annotations[0]
													_lowercase       :	str           =   raw_annotation["scores"]
													_lowercase       :	Optional[int]           =   raw_annotation["labels"]
													_lowercase       :	Dict           =   raw_annotation["boxes"]

													_lowercase       :	int           =   scores.tolist()
													_lowercase       :	List[str]           =   [self.model.config.idalabel[label.item()] for label in labels]
													_lowercase       :	List[Any]           =   [self._get_bounding_box(snake_case    ) for box in boxes]

													# {"scores": [...], ...} --> [{"score":x, ...}, ...]
													_lowercase       :	Union[str, Any]           =   ["score", "label", "box"]
													_lowercase       :	Tuple           =   [
													    dict(zip(snake_case      ,	snake_case    )    )
													    for vals in zip(raw_annotation["scores"]      ,	raw_annotation["labels"]      ,	raw_annotation["boxes"]    )
													]

							return annotation






	def       _a(self				: int      ,	snake_case				: "torch.Tensor"    ) ->			Dict[str, int]:
							if self.framework != "pt":
													raise ValueError("The ObjectDetectionPipeline is only available in PyTorch."    )
							_lowercase      ,    _lowercase      ,    _lowercase      ,    _lowercase       :	Optional[int]           =   box.int().tolist()
							_lowercase       :	Tuple           =   {
							    "xmin": xmin,
							    "ymin": ymin,
							    "xmax": xmax,
							    "ymax": ymax,
							}
							return bbox




