






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


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import SwinaSRImageProcessor
     from transformers.image_transforms import get_image_size




class  lowercase_     (unittest.TestCase  ):






      def __init__(     self  ,  a_  ,  a_=7  ,  a_=3  ,  a_=1_8  ,  a_=3_0  ,  a_=4_0_0  ,  a_=True  ,  a_=1 / 2_5_5  ,  a_=True  ,  a_=8  ,  )						->Any:







        '''simple docstring'''


        _a					      =						parent
        _a					      =						batch_size
        _a					      =						num_channels
        _a					      =						image_size
        _a					      =						min_resolution
        _a					      =						max_resolution
        _a					      =						do_rescale
        _a					      =						rescale_factor
        _a					      =						do_pad
        _a					      =						pad_size






      def       lowerCamelCase__     (     self						)						->str:







        '''simple docstring'''


        return {
            "do_rescale": self.do_rescale,
            "rescale_factor": self.rescale_factor,
            "do_pad": self.do_pad,
            "pad_size": self.pad_size,
        }






      def       lowerCamelCase__     (     self  ,  a_=False  ,  a_=False  ,  a_=False						)						->int:







        '''simple docstring'''


        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
          _a					      =						[]
          for i in range(self.batch_size						):
            image_inputs.append(
                np.random.randint(
                    2_5_5  ,  size=(self.num_channels, self.max_resolution, self.max_resolution)  ,  dtype=np.uinta						)						)
        else:
          _a					      =						[]
          for i in range(self.batch_size						):
            _a      ,						_a					      =						np.random.choice(np.arange(self.min_resolution  ,  self.max_resolution						)  ,  2						)
            image_inputs.append(np.random.randint(2_5_5  ,  size=(self.num_channels, width, height)  ,  dtype=np.uinta						)						)

        if not numpify and not torchify:
          # PIL expects the channel dimension as last dimension
          _a					      =						[Image.fromarray(np.moveaxis(a_  ,  0  ,  -1						)						) for x in image_inputs]

        if torchify:
          _a					      =						[torch.from_numpy(a_						) for x in image_inputs]

        return image_inputs







@require_torch
@require_vision
class  lowercase_     (_UpperCAmelCase,     unittest.TestCase  ):
      A__						:		str							      =					SwinaSRImageProcessor if is_vision_available() else None






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







        '''simple docstring'''


        _a					      =						SwinaSRImageProcessingTester(self						)






      @property
      def       lowerCamelCase__     (     self						)						->Dict:







        '''simple docstring'''


        return self.image_processor_tester.prepare_image_processor_dict()






      def       lowerCamelCase__     (     self						)						->Optional[int]:







        '''simple docstring'''


        _a					      =						self.image_processing_class(**self.image_processor_dict						)
        self.assertTrue(hasattr(a_  ,  "do_rescale"						)						)
        self.assertTrue(hasattr(a_  ,  "rescale_factor"						)						)
        self.assertTrue(hasattr(a_  ,  "do_pad"						)						)
        self.assertTrue(hasattr(a_  ,  "pad_size"						)						)






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







        '''simple docstring'''


        pass






      def       lowerCamelCase__     (     self  ,  a_						)						->Optional[int]:







        '''simple docstring'''


        _a      ,						_a					      =						get_image_size(a_						)
        _a					      =						self.image_processor_tester.pad_size

        _a					      =						(old_height // size + 1) * size - old_height
        _a					      =						(old_width // size + 1) * size - old_width
        return old_height + pad_height, old_width + pad_width






      def       lowerCamelCase__     (     self						)						->int:







        '''simple docstring'''


        _a					      =						self.image_processing_class(**self.image_processor_dict						)
        # create random PIL images
        _a					      =						self.image_processor_tester.prepare_inputs(equal_resolution=a_						)
        for image in image_inputs:
          self.assertIsInstance(a_  ,  Image.Image						)

        # Test not batched input
        _a					      =						image_processor(image_inputs[0]  ,  return_tensors="pt"						).pixel_values
        _a      ,						_a					      =						self.calculate_expected_size(np.array(image_inputs[0]						)						)
        self.assertEqual(
            encoded_images.shape  ,  (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )  ,  )






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







        '''simple docstring'''


        _a					      =						self.image_processing_class(**self.image_processor_dict						)
        # create random numpy tensors
        _a					      =						self.image_processor_tester.prepare_inputs(equal_resolution=a_  ,  numpify=a_						)
        for image in image_inputs:
          self.assertIsInstance(a_  ,  np.ndarray						)

        # Test not batched input
        _a					      =						image_processor(image_inputs[0]  ,  return_tensors="pt"						).pixel_values
        _a      ,						_a					      =						self.calculate_expected_size(image_inputs[0]						)
        self.assertEqual(
            encoded_images.shape  ,  (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )  ,  )






      def       lowerCamelCase__     (     self						)						->Optional[int]:







        '''simple docstring'''


        _a					      =						self.image_processing_class(**self.image_processor_dict						)
        # create random PyTorch tensors
        _a					      =						self.image_processor_tester.prepare_inputs(equal_resolution=a_  ,  torchify=a_						)
        for image in image_inputs:
          self.assertIsInstance(a_  ,  torch.Tensor						)

        # Test not batched input
        _a					      =						image_processor(image_inputs[0]  ,  return_tensors="pt"						).pixel_values
        _a      ,						_a					      =						self.calculate_expected_size(image_inputs[0]						)
        self.assertEqual(
            encoded_images.shape  ,  (
                1,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )  ,  )










"""simple docstring"""




from typing import TYPE_CHECKING

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


UpperCamelCase											=     {"""configuration_pegasus""": ["""PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PegasusConfig"""]}

try:
     if not is_sentencepiece_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     ["""PegasusTokenizer"""]

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     ["""PegasusTokenizerFast"""]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     [
         """PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST""",
         """PegasusForCausalLM""",
         """PegasusForConditionalGeneration""",
         """PegasusModel""",
         """PegasusPreTrainedModel""",
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     [
         """TFPegasusForConditionalGeneration""",
         """TFPegasusModel""",
         """TFPegasusPreTrainedModel""",
     ]

try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     UpperCamelCase											=     [
         """FlaxPegasusForConditionalGeneration""",
         """FlaxPegasusModel""",
         """FlaxPegasusPreTrainedModel""",
     ]


if TYPE_CHECKING:
     from .configuration_pegasus import PEGASUS_PRETRAINED_CONFIG_ARCHIVE_MAP, PegasusConfig

     try:
          if not is_sentencepiece_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_pegasus import PegasusTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_pegasus_fast import PegasusTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_pegasus import (
              PEGASUS_PRETRAINED_MODEL_ARCHIVE_LIST,
              PegasusForCausalLM,
              PegasusForConditionalGeneration,
              PegasusModel,
              PegasusPreTrainedModel,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_pegasus import TFPegasusForConditionalGeneration, TFPegasusModel, TFPegasusPreTrainedModel

     try:
          if not is_flax_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_flax_pegasus import (
              FlaxPegasusForConditionalGeneration,
              FlaxPegasusModel,
              FlaxPegasusPreTrainedModel,
          )

else:
     import sys

     UpperCamelCase											=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


