


import unittest
from typing import Dict, List, Optional, Union

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, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import BridgeTowerImageProcessor
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				def __init__(       self			:Any			,			lowerCAmelCase__			:Tuple			,			lowerCAmelCase__			:bool = True			,			lowerCAmelCase__			:Dict[str, int] = None			,			lowerCAmelCase__			:int = 32			,			lowerCAmelCase__			:bool = True			,			lowerCAmelCase__			:Union[int, float] = 1 / 255			,			lowerCAmelCase__			:bool = True			,			lowerCAmelCase__			:bool = True			,			lowerCAmelCase__			:Optional[Union[float, List[float]]] = [0.4814_5466, 0.457_8275, 0.4082_1073]			,			lowerCAmelCase__			:Optional[Union[float, List[float]]] = [0.2686_2954, 0.2613_0258, 0.2757_7711]			,			lowerCAmelCase__			:bool = True			,			lowerCAmelCase__			:Tuple=7			,			lowerCAmelCase__			:Any=30			,			lowerCAmelCase__			:Optional[Any]=400			,			lowerCAmelCase__			:List[str]=3			,			)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		parent
										__SCREAMING_SNAKE_CASE						:     List[str]					=		do_resize
										__SCREAMING_SNAKE_CASE						:     List[str]					=		size if size is not None else {'''shortest_edge''': 288}
										__SCREAMING_SNAKE_CASE						:     str					=		size_divisor
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		do_rescale
										__SCREAMING_SNAKE_CASE						:     Dict					=		rescale_factor
										__SCREAMING_SNAKE_CASE						:     int					=		do_normalize
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		do_center_crop
										__SCREAMING_SNAKE_CASE						:     int					=		image_mean
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		image_std
										__SCREAMING_SNAKE_CASE						:     Any					=		do_pad
										__SCREAMING_SNAKE_CASE						:     str					=		batch_size
										__SCREAMING_SNAKE_CASE						:     Dict					=		num_channels
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		min_resolution
										__SCREAMING_SNAKE_CASE						:     Tuple					=		max_resolution


				def 		__magic_name__(       self			:str	)		->			Any:
										return {
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_normalize": self.do_normalize,
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "size_divisor": self.size_divisor,
										}



				def 		__magic_name__(       self			:Optional[Any]			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:Tuple=False	)		->			List[Any]:
										if not batched:
																__SCREAMING_SNAKE_CASE						:     Dict					=		self.size['''shortest_edge''']
																__SCREAMING_SNAKE_CASE						:     int					=		image_inputs[0]
																if isinstance(lowerCAmelCase__			,			Image.Image	):
																						__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     List[str]					=		image.size
																else:
																						__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     int					=		image.shape[1], image.shape[2]
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		size / min(lowerCAmelCase__			,			lowerCAmelCase__	)
																if h < w:
																						__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     int					=		size, scale * w
																else:
																						__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     int					=		scale * h, size

																__SCREAMING_SNAKE_CASE						:     Tuple					=		int((1_333 / 800) * size	)
																if max(lowerCAmelCase__			,			lowerCAmelCase__	) > max_size:
																						__SCREAMING_SNAKE_CASE						:     Tuple					=		max_size / max(lowerCAmelCase__			,			lowerCAmelCase__	)
																						__SCREAMING_SNAKE_CASE						:     Any					=		newh * scale
																						__SCREAMING_SNAKE_CASE						:     List[Any]					=		neww * scale

																__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Any					=		int(newh + 0.5	), int(neww + 0.5	)
																__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     int					=		(
																    newh // self.size_divisor * self.size_divisor,
																    neww // self.size_divisor * self.size_divisor,
																)

										else:
																__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		[]
																for image in image_inputs:
																						__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     str					=		self.get_expected_values([image]	)
																						expected_values.append((expected_height, expected_width)	)
																__SCREAMING_SNAKE_CASE						:     int					=		max(lowerCAmelCase__			,			key=lambda lowerCAmelCase__	: item[0]	)[0]
																__SCREAMING_SNAKE_CASE						:     str					=		max(lowerCAmelCase__			,			key=lambda lowerCAmelCase__	: item[1]	)[1]

										return expected_height, expected_width




@require_torch
@require_vision
class        _lowercase      (			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Optional[Any]						=		BridgeTowerImageProcessor if is_vision_available() else None


				def 		__magic_name__(       self			:Optional[int]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		BridgeTowerImageProcessingTester(self	)


				@property
				def 		__magic_name__(       self			:Dict	)		->			Optional[int]:
										return self.image_processor_tester.prepare_image_processor_dict()


				def 		__magic_name__(       self			:List[str]	)		->			List[str]:
										__SCREAMING_SNAKE_CASE						:     str					=		self.image_processing_class(**self.image_processor_dict	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''image_mean'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''image_std'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''do_normalize'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''do_resize'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''size'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''size_divisor'''	)	)


				def 		__magic_name__(       self			:int	)		->			List[str]:
										pass


				def 		__magic_name__(       self			:Optional[int]	)		->			int:
										# Initialize image processor
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		self.image_processing_class(**self.image_processor_dict	)
										# create random PIL images
										__SCREAMING_SNAKE_CASE						:     Dict					=		prepare_image_inputs(self.image_processor_tester			,			equal_resolution=lowerCAmelCase__	)
										for image in image_inputs:
																self.assertIsInstance(lowerCAmelCase__			,			Image.Image	)

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     Dict					=		image_processing(image_inputs[0]			,			return_tensors='''pt'''	).pixel_values

										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Dict					=		self.image_processor_tester.get_expected_values(lowerCAmelCase__	)
										self.assertEqual(
										    encoded_images.shape			,			(1, self.image_processor_tester.num_channels, expected_height, expected_width)			,			)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     Tuple					=		image_processing(lowerCAmelCase__			,			return_tensors='''pt'''	).pixel_values

										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     str					=		self.image_processor_tester.get_expected_values(lowerCAmelCase__			,			batched=lowerCAmelCase__	)
										self.assertEqual(
										    encoded_images.shape			,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )			,			)


				def 		__magic_name__(       self			:str	)		->			Any:
										# Initialize image processor
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		self.image_processing_class(**self.image_processor_dict	)
										# create random numpy tensors
										__SCREAMING_SNAKE_CASE						:     str					=		prepare_image_inputs(self.image_processor_tester			,			equal_resolution=lowerCAmelCase__			,			numpify=lowerCAmelCase__	)
										for image in image_inputs:
																self.assertIsInstance(lowerCAmelCase__			,			np.ndarray	)

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     Any					=		image_processing(image_inputs[0]			,			return_tensors='''pt'''	).pixel_values

										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     List[str]					=		self.image_processor_tester.get_expected_values(lowerCAmelCase__	)
										self.assertEqual(
										    encoded_images.shape			,			(1, self.image_processor_tester.num_channels, expected_height, expected_width)			,			)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     int					=		image_processing(lowerCAmelCase__			,			return_tensors='''pt'''	).pixel_values

										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.image_processor_tester.get_expected_values(lowerCAmelCase__			,			batched=lowerCAmelCase__	)
										self.assertEqual(
										    encoded_images.shape			,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )			,			)



				def 		__magic_name__(       self			:List[str]	)		->			Any:
										# Initialize image processor
										__SCREAMING_SNAKE_CASE						:     Dict					=		self.image_processing_class(**self.image_processor_dict	)
										# create random PyTorch tensors
										__SCREAMING_SNAKE_CASE						:     Any					=		prepare_image_inputs(self.image_processor_tester			,			equal_resolution=lowerCAmelCase__			,			torchify=lowerCAmelCase__	)
										for image in image_inputs:
																self.assertIsInstance(lowerCAmelCase__			,			torch.Tensor	)

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     Tuple					=		image_processing(image_inputs[0]			,			return_tensors='''pt'''	).pixel_values

										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.image_processor_tester.get_expected_values(lowerCAmelCase__	)
										self.assertEqual(
										    encoded_images.shape			,			(1, self.image_processor_tester.num_channels, expected_height, expected_width)			,			)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     Dict					=		image_processing(lowerCAmelCase__			,			return_tensors='''pt'''	).pixel_values

										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     int					=		self.image_processor_tester.get_expected_values(lowerCAmelCase__			,			batched=lowerCAmelCase__	)
										self.assertEqual(
										    encoded_images.shape			,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )			,			)


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowercase  :					str						    =							{
    """configuration_funnel""": ["""FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FunnelConfig"""],
    """convert_funnel_original_tf_checkpoint_to_pytorch""": [],
    """tokenization_funnel""": ["""FunnelTokenizer"""],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase  :					Tuple						    =							["""FunnelTokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase  :					List[str]						    =							[
      """FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """FunnelBaseModel""",
      """FunnelForMaskedLM""",
      """FunnelForMultipleChoice""",
      """FunnelForPreTraining""",
      """FunnelForQuestionAnswering""",
      """FunnelForSequenceClassification""",
      """FunnelForTokenClassification""",
      """FunnelModel""",
      """FunnelPreTrainedModel""",
      """load_tf_weights_in_funnel""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase  :					Optional[Any]						    =							[
      """TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFFunnelBaseModel""",
      """TFFunnelForMaskedLM""",
      """TFFunnelForMultipleChoice""",
      """TFFunnelForPreTraining""",
      """TFFunnelForQuestionAnswering""",
      """TFFunnelForSequenceClassification""",
      """TFFunnelForTokenClassification""",
      """TFFunnelModel""",
      """TFFunnelPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
  from .tokenization_funnel import FunnelTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_funnel_fast import FunnelTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_funnel import (
        FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
        FunnelBaseModel,
        FunnelForMaskedLM,
        FunnelForMultipleChoice,
        FunnelForPreTraining,
        FunnelForQuestionAnswering,
        FunnelForSequenceClassification,
        FunnelForTokenClassification,
        FunnelModel,
        FunnelPreTrainedModel,
        load_tf_weights_in_funnel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_funnel import (
        TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFFunnelBaseModel,
        TFFunnelForMaskedLM,
        TFFunnelForMultipleChoice,
        TFFunnelForPreTraining,
        TFFunnelForQuestionAnswering,
        TFFunnelForSequenceClassification,
        TFFunnelForTokenClassification,
        TFFunnelModel,
        TFFunnelPreTrainedModel,
    )

else:
  import sys

  lowercase  :					Any						    =							_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
