






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


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import ViltImageProcessor







class 						_a  (		unittest.TestCase       ):





   '''simple docstring'''






   def __init__(       self					, UpperCAmelCase_					, UpperCAmelCase_=7					, UpperCAmelCase_=3					, UpperCAmelCase_=18					, UpperCAmelCase_=30					, UpperCAmelCase_=400					, UpperCAmelCase_=True					, UpperCAmelCase_=None					, UpperCAmelCase_=2					, UpperCAmelCase_=True					, UpperCAmelCase_=[0.5, 0.5, 0.5]					, UpperCAmelCase_=[0.5, 0.5, 0.5]					, )			->   Dict:




          '''simple docstring'''




          lowercase__:							Tuple         =     size if size is not None else {"shortest_edge": 30}
          lowercase__:							Tuple         =     parent
          lowercase__:							Optional[Any]         =     batch_size
          lowercase__:							Optional[Any]         =     num_channels
          lowercase__:							List[str]         =     image_size
          lowercase__:							Union[str, Any]         =     min_resolution
          lowercase__:							Optional[int]         =     max_resolution
          lowercase__:							Optional[Any]         =     do_resize
          lowercase__:							Any         =     size
          lowercase__:							List[Any]         =     size_divisor
          lowercase__:							List[Any]         =     do_normalize
          lowercase__:							Dict         =     image_mean
          lowercase__:							Any         =     image_std





   def __lowercase							(       self)			->   List[Any]:




          '''simple docstring'''




          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 __lowercase							(       self					, UpperCAmelCase_					, UpperCAmelCase_=False)			->   Optional[int]:




          '''simple docstring'''




          if not batched:
                 lowercase__:							int         =     self.size["shortest_edge"]
                 lowercase__:							int         =     image_inputs[0]
                 if isinstance(UpperCAmelCase_					, Image.Image):
                        lowercase__    ,      lowercase__:							Any         =     image.size
                 else:
                        lowercase__    ,      lowercase__:							List[str]         =     image.shape[1], image.shape[2]
                 lowercase__:							Any         =     size / min(UpperCAmelCase_					, UpperCAmelCase_)
                 if h < w:
                        lowercase__    ,      lowercase__:							List[str]         =     size, scale * w
                 else:
                        lowercase__    ,      lowercase__:							Any         =     scale * h, size

                 lowercase__:							List[Any]         =     int((1_333 / 800) * size)
                 if max(UpperCAmelCase_					, UpperCAmelCase_) > max_size:
                        lowercase__:							str         =     max_size / max(UpperCAmelCase_					, UpperCAmelCase_)
                        lowercase__:							Union[str, Any]         =     newh * scale
                        lowercase__:							Optional[Any]         =     neww * scale

                 lowercase__    ,      lowercase__:							Dict         =     int(newh + 0.5), int(neww + 0.5)
                 lowercase__    ,      lowercase__:							int         =     (
                     newh // self.size_divisor * self.size_divisor,
                     neww // self.size_divisor * self.size_divisor,
                 )

          else:
                 lowercase__:							str         =     []
                 for image in image_inputs:
                        lowercase__    ,      lowercase__:							Tuple         =     self.get_expected_values([image])
                        expected_values.append((expected_height, expected_width))
                 lowercase__:							List[str]         =     max(UpperCAmelCase_					, key=lambda UpperCAmelCase_: item[0])[0]
                 lowercase__:							str         =     max(UpperCAmelCase_					, key=lambda UpperCAmelCase_: item[1])[1]

          return expected_height, expected_width







@require_torch
@require_vision
class 						_a  (		lowercase_							,	unittest.TestCase       ):





   '''simple docstring'''






   UpperCamelCase__     	=							ViltImageProcessor if is_vision_available() else None





   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          lowercase__:							List[str]         =     ViltImageProcessingTester(self)





   @property
   def __lowercase							(       self)			->   Optional[int]:




          '''simple docstring'''




          return self.image_processor_tester.prepare_image_processor_dict()





   def __lowercase							(       self)			->   List[Any]:




          '''simple docstring'''




          lowercase__:							Optional[Any]         =     self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(UpperCAmelCase_					, "image_mean"))
          self.assertTrue(hasattr(UpperCAmelCase_					, "image_std"))
          self.assertTrue(hasattr(UpperCAmelCase_					, "do_normalize"))
          self.assertTrue(hasattr(UpperCAmelCase_					, "do_resize"))
          self.assertTrue(hasattr(UpperCAmelCase_					, "size"))
          self.assertTrue(hasattr(UpperCAmelCase_					, "size_divisor"))





   def __lowercase							(       self)			->   List[str]:




          '''simple docstring'''




          lowercase__:							str         =     self.image_processing_class.from_dict(self.image_processor_dict)
          self.assertEqual(image_processor.size					, {"shortest_edge": 30})

          lowercase__:							Dict         =     self.image_processing_class.from_dict(self.image_processor_dict					, size=42)
          self.assertEqual(image_processor.size					, {"shortest_edge": 42})





   def __lowercase							(       self)			->   Any:




          '''simple docstring'''




          pass





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




          '''simple docstring'''




          lowercase__:							str         =     self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          lowercase__:							Tuple         =     prepare_image_inputs(self.image_processor_tester					, equal_resolution=UpperCAmelCase_)
          for image in image_inputs:
                 self.assertIsInstance(UpperCAmelCase_					, Image.Image)

          # Test not batched input
          lowercase__:							List[str]         =     image_processing(image_inputs[0]					, return_tensors="pt").pixel_values

          lowercase__    ,      lowercase__:							List[Any]         =     self.image_processor_tester.get_expected_values(UpperCAmelCase_)
          self.assertEqual(
              encoded_images.shape					, (1, self.image_processor_tester.num_channels, expected_height, expected_width)					, )

          # Test batched
          lowercase__:							Optional[int]         =     image_processing(UpperCAmelCase_					, return_tensors="pt").pixel_values

          lowercase__    ,      lowercase__:							Optional[int]         =     self.image_processor_tester.get_expected_values(UpperCAmelCase_					, batched=UpperCAmelCase_)
          self.assertEqual(
              encoded_images.shape					, (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )					, )





   def __lowercase							(       self)			->   str:




          '''simple docstring'''




          lowercase__:							List[Any]         =     self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          lowercase__:							List[str]         =     prepare_image_inputs(self.image_processor_tester					, equal_resolution=UpperCAmelCase_					, numpify=UpperCAmelCase_)
          for image in image_inputs:
                 self.assertIsInstance(UpperCAmelCase_					, np.ndarray)

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

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

          # Test batched
          lowercase__:							List[Any]         =     image_processing(UpperCAmelCase_					, return_tensors="pt").pixel_values

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





   def __lowercase							(       self)			->   Any:




          '''simple docstring'''




          lowercase__:							Dict         =     self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          lowercase__:							int         =     prepare_image_inputs(self.image_processor_tester					, equal_resolution=UpperCAmelCase_					, torchify=UpperCAmelCase_)
          for image in image_inputs:
                 self.assertIsInstance(UpperCAmelCase_					, torch.Tensor)

          # Test not batched input
          lowercase__:							int         =     image_processing(image_inputs[0]					, return_tensors="pt").pixel_values

          lowercase__    ,      lowercase__:							int         =     self.image_processor_tester.get_expected_values(UpperCAmelCase_)
          self.assertEqual(
              encoded_images.shape					, (1, self.image_processor_tester.num_channels, expected_height, expected_width)					, )

          # Test batched
          lowercase__:							Any         =     image_processing(UpperCAmelCase_					, return_tensors="pt").pixel_values

          lowercase__    ,      lowercase__:							Optional[int]         =     self.image_processor_tester.get_expected_values(UpperCAmelCase_					, batched=UpperCAmelCase_)
          self.assertEqual(
              encoded_images.shape					, (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              )					, )







"""simple docstring"""


from typing import TYPE_CHECKING

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


__UpperCamelCase       :    Any         =		{
    "configuration_openai": ["OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "OpenAIGPTConfig"],
    "tokenization_openai": ["OpenAIGPTTokenizer"],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCamelCase       :    Optional[int]         =		["OpenAIGPTTokenizerFast"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCamelCase       :    List[Any]         =		[
      "OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "OpenAIGPTDoubleHeadsModel",
      "OpenAIGPTForSequenceClassification",
      "OpenAIGPTLMHeadModel",
      "OpenAIGPTModel",
      "OpenAIGPTPreTrainedModel",
      "load_tf_weights_in_openai_gpt",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCamelCase       :    Tuple         =		[
      "TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "TFOpenAIGPTDoubleHeadsModel",
      "TFOpenAIGPTForSequenceClassification",
      "TFOpenAIGPTLMHeadModel",
      "TFOpenAIGPTMainLayer",
      "TFOpenAIGPTModel",
      "TFOpenAIGPTPreTrainedModel",
  ]


if TYPE_CHECKING:
  from .configuration_openai import OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OpenAIGPTConfig
  from .tokenization_openai import OpenAIGPTTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_openai_fast import OpenAIGPTTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_openai import (
        OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
        OpenAIGPTDoubleHeadsModel,
        OpenAIGPTForSequenceClassification,
        OpenAIGPTLMHeadModel,
        OpenAIGPTModel,
        OpenAIGPTPreTrainedModel,
        load_tf_weights_in_openai_gpt,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_openai import (
        TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFOpenAIGPTDoubleHeadsModel,
        TFOpenAIGPTForSequenceClassification,
        TFOpenAIGPTLMHeadModel,
        TFOpenAIGPTMainLayer,
        TFOpenAIGPTModel,
        TFOpenAIGPTPreTrainedModel,
    )

else:
  import sys

  __UpperCamelCase       :    str         =		_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)