






'''simple docstring'''
import unittest

import numpy as np
import requests

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

 from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
 UpperCamelCase__ =			False

if is_vision_available():
 from PIL import Image

 from transformers import PixaStructImageProcessor


class        _UpperCAmelCase						(		unittest.TestCase			):




    def __init__(			self  :       Tuple  ,     a  :       List[str]  ,     a  :       Optional[int]=7  ,     a  :       List[str]=3  ,     a  :       Dict=1_8  ,     a  :       Tuple=3_0  ,     a  :       List[Any]=4_0_0  ,     a  :       Any=None  ,     a  :       Any=True  ,     a  :       Tuple=True  ,     a  :       List[str]=None  ,     ):






       '''simple docstring'''






       lowercase_	:    str           =     size if size is not None else {"height": 2_0, "width": 2_0}
       lowercase_	:    Any           =     parent
       lowercase_	:    Union[str, Any]           =     batch_size
       lowercase_	:    List[Any]           =     num_channels
       lowercase_	:    int           =     image_size
       lowercase_	:    Optional[Any]           =     min_resolution
       lowercase_	:    str           =     max_resolution
       lowercase_	:    str           =     size
       lowercase_	:    Tuple           =     do_normalize
       lowercase_	:    Optional[Any]           =     do_convert_rgb
       lowercase_	:    Optional[Any]           =     [5_1_2, 1_0_2_4, 2_0_4_8, 4_0_9_6]
       lowercase_	:    Dict           =     patch_size if patch_size is not None else {"height": 1_6, "width": 1_6}




    def 				lowerCAmelCase__				(			self  :       Any			):






       '''simple docstring'''






       return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}





    def 				lowerCAmelCase__				(			self  :       Optional[int]			):






       '''simple docstring'''






       lowercase_	:    Dict           =     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"
       lowercase_	:    int           =     Image.open(requests.get(UpperCAmelCase__  ,     stream=UpperCAmelCase__			).raw			).convert("RGB"			)
       return raw_image


@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11				,					reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'				,					)
@require_torch
@require_vision
class        _UpperCAmelCase						(		snake_case				,					unittest.TestCase			):
    __lowerCamelCase:   Any		       =    PixaStructImageProcessor if is_vision_available() else None




    def 				lowerCAmelCase__				(			self  :       Optional[int]			):






       '''simple docstring'''






       lowercase_	:    Optional[Any]           =     PixaStructImageProcessingTester(self			)




    @property
    def 				lowerCAmelCase__				(			self  :       Dict			):






       '''simple docstring'''






       return self.image_processor_tester.prepare_image_processor_dict()




    def 				lowerCAmelCase__				(			self  :       Union[str, Any]			):






       '''simple docstring'''






       lowercase_	:    str           =     self.image_processing_class(**self.image_processor_dict			)
       self.assertTrue(hasattr(UpperCAmelCase__  ,     "do_normalize"			)			)
       self.assertTrue(hasattr(UpperCAmelCase__  ,     "do_convert_rgb"			)			)




    def 				lowerCAmelCase__				(			self  :       Tuple			):






       '''simple docstring'''






       lowercase_	:    List[Any]           =     self.image_processor_tester.prepare_dummy_image()

       lowercase_	:    Optional[Any]           =     self.image_processing_class(**self.image_processor_dict			)
       lowercase_	:    Tuple           =     2_0_4_8

       lowercase_	:    Optional[int]           =     image_processor(UpperCAmelCase__  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			)
       self.assertTrue(torch.allclose(inputs.flattened_patches.mean()  ,     torch.tensor(0.0606			)  ,     atol=1e-3  ,     rtol=1e-3			)			)




    def 				lowerCAmelCase__				(			self  :       List[Any]			):






       '''simple docstring'''






       lowercase_	:    List[str]           =     self.image_processing_class(**self.image_processor_dict			)
       # create random PIL images
       lowercase_	:    List[Any]           =     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_	:    Dict           =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
          # Test not batched input
          lowercase_	:    List[Any]           =     image_processor(
              image_inputs[0]  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (1, max_patch, expected_hidden_dim)  ,     )

          # Test batched
          lowercase_	:    int           =     image_processor(
              UpperCAmelCase__  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)  ,     )




    def 				lowerCAmelCase__				(			self  :       List[Any]			):






       '''simple docstring'''






       lowercase_	:    List[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[Any]           =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * self.image_processor_tester.num_channels
       ) + 2

       lowercase_	:    int           =     True

       for max_patch in self.image_processor_tester.max_patches:
          # Test not batched input
          with self.assertRaises(UpperCAmelCase__			):
             lowercase_	:    List[Any]           =     image_processor(
                 image_inputs[0]  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			).flattened_patches

          lowercase_	:    List[Any]           =     "Hello"

          lowercase_	:    Dict           =     image_processor(
              image_inputs[0]  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__  ,     header_text=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (1, max_patch, expected_hidden_dim)  ,     )

          # Test batched
          lowercase_	:    Dict           =     image_processor(
              UpperCAmelCase__  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__  ,     header_text=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)  ,     )




    def 				lowerCAmelCase__				(			self  :       int			):






       '''simple docstring'''






       lowercase_	:    Union[str, Any]           =     self.image_processing_class(**self.image_processor_dict			)
       # create random numpy tensors
       lowercase_	:    Optional[int]           =     prepare_image_inputs(self.image_processor_tester  ,     equal_resolution=UpperCAmelCase__  ,     numpify=UpperCAmelCase__			)
       for image in image_inputs:
          self.assertIsInstance(UpperCAmelCase__  ,     np.ndarray			)

       lowercase_	:    int           =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
          # Test not batched input
          lowercase_	:    List[str]           =     image_processor(
              image_inputs[0]  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (1, max_patch, expected_hidden_dim)  ,     )

          # Test batched
          lowercase_	:    Tuple           =     image_processor(
              UpperCAmelCase__  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)  ,     )





    def 				lowerCAmelCase__				(			self  :       Optional[Any]			):






       '''simple docstring'''






       lowercase_	:    List[Any]           =     self.image_processing_class(**self.image_processor_dict			)
       # create random PyTorch tensors
       lowercase_	:    Optional[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_	:    List[Any]           =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
          # Test not batched input
          lowercase_	:    Optional[Any]           =     image_processor(
              image_inputs[0]  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (1, max_patch, expected_hidden_dim)  ,     )

          # Test batched
          lowercase_	:    List[Any]           =     image_processor(
              UpperCAmelCase__  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)  ,     )


@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11				,					reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'				,					)
@require_torch
@require_vision
class        _UpperCAmelCase						(		snake_case				,					unittest.TestCase			):
    __lowerCamelCase:   List[Any]		       =    PixaStructImageProcessor if is_vision_available() else None




    def 				lowerCAmelCase__				(			self  :       List[Any]			):






       '''simple docstring'''






       lowercase_	:    str           =     PixaStructImageProcessingTester(self  ,     num_channels=4			)
       lowercase_	:    str           =     3




    @property
    def 				lowerCAmelCase__				(			self  :       int			):






       '''simple docstring'''






       return self.image_processor_tester.prepare_image_processor_dict()




    def 				lowerCAmelCase__				(			self  :       List[str]			):






       '''simple docstring'''






       lowercase_	:    int           =     self.image_processing_class(**self.image_processor_dict			)
       self.assertTrue(hasattr(UpperCAmelCase__  ,     "do_normalize"			)			)
       self.assertTrue(hasattr(UpperCAmelCase__  ,     "do_convert_rgb"			)			)





    def 				lowerCAmelCase__				(			self  :       Optional[int]			):






       '''simple docstring'''






       lowercase_	:    Tuple           =     self.image_processing_class(**self.image_processor_dict			)
       # create random PIL images
       lowercase_	:    Any           =     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[Any]           =     (
           (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
           * (self.image_processor_tester.num_channels - 1)
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
          # Test not batched input
          lowercase_	:    Any           =     image_processor(
              image_inputs[0]  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (1, max_patch, expected_hidden_dim)  ,     )

          # Test batched
          lowercase_	:    str           =     image_processor(
              UpperCAmelCase__  ,     return_tensors="pt"  ,     max_patches=UpperCAmelCase__			).flattened_patches
          self.assertEqual(
              encoded_images.shape  ,     (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)  ,     )












'''simple docstring'''
from typing import TYPE_CHECKING

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


UpperCamelCase__ =			{
    'configuration_encodec': [
        'ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'EncodecConfig',
    ],
    'feature_extraction_encodec': ['EncodecFeatureExtractor'],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__ =			[
     'ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST',
     'EncodecModel',
     'EncodecPreTrainedModel',
 ]

if TYPE_CHECKING:
 from .configuration_encodec import (
     ENCODEC_PRETRAINED_CONFIG_ARCHIVE_MAP,
     EncodecConfig,
 )
 from .feature_extraction_encodec import EncodecFeatureExtractor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_encodec import (
      ENCODEC_PRETRAINED_MODEL_ARCHIVE_LIST,
      EncodecModel,
      EncodecPreTrainedModel,
  )

else:
 import sys

 UpperCamelCase__ =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




