




"""simple docstring"""


import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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 DonutImageProcessor






class  lowercase(					unittest.TestCase			):







 '''simple docstring'''





 def __init__(    self:		Optional[Any],				a_:		str,				a_:		int=7,				a_:		List[Any]=3,				a_:		Any=18,				a_:		List[str]=30,				a_:		Union[str, Any]=400,				a_:		Dict=True,				a_:		Any=None,				a_:		Dict=True,				a_:		Dict=False,				a_:		List[str]=True,				a_:		str=True,				a_:		Any=[0.5, 0.5, 0.5],				a_:		Any=[0.5, 0.5, 0.5],				):




   '''simple docstring'''



   _snake_case							:		Dict								=  parent
   _snake_case							:		int								=  batch_size
   _snake_case							:		Optional[Any]								=  num_channels
   _snake_case							:		Dict								=  image_size
   _snake_case							:		Dict								=  min_resolution
   _snake_case							:		List[str]								=  max_resolution
   _snake_case							:		List[str]								=  do_resize
   _snake_case							:		Optional[Any]								=  size if size is not None else {"""height""": 18, """width""": 20}
   _snake_case							:		List[Any]								=  do_thumbnail
   _snake_case							:		Tuple								=  do_align_axis
   _snake_case							:		List[str]								=  do_pad
   _snake_case							:		Optional[Any]								=  do_normalize
   _snake_case							:		List[str]								=  image_mean
   _snake_case							:		int								=  image_std


 def 			UpperCamelCase_       (    self:		Tuple		):




   '''simple docstring'''



   return {
       "do_resize": self.do_resize,
       "size": self.size,
       "do_thumbnail": self.do_thumbnail,
       "do_align_long_axis": self.do_align_axis,
       "do_pad": self.do_pad,
       "do_normalize": self.do_normalize,
       "image_mean": self.image_mean,
       "image_std": self.image_std,
   }







@require_torch
@require_vision
class  lowercase(					__a	,      unittest.TestCase			):







 '''simple docstring'''





 lowercase__			 = DonutImageProcessor if is_vision_available() else None


 def 			UpperCamelCase_       (    self:		Dict		):




   '''simple docstring'''



   _snake_case							:		List[str]								=  DonutImageProcessingTester(self		)


 @property
 def 			UpperCamelCase_       (    self:		List[str]		):




   '''simple docstring'''



   return self.image_processor_tester.prepare_image_processor_dict()


 def 			UpperCamelCase_       (    self:		str		):




   '''simple docstring'''



   _snake_case							:		Optional[Any]								=  self.image_processing_class(**self.image_processor_dict		)
   self.assertTrue(hasattr(a_,				"""do_resize"""		)		)
   self.assertTrue(hasattr(a_,				"""size"""		)		)
   self.assertTrue(hasattr(a_,				"""do_thumbnail"""		)		)
   self.assertTrue(hasattr(a_,				"""do_align_long_axis"""		)		)
   self.assertTrue(hasattr(a_,				"""do_pad"""		)		)
   self.assertTrue(hasattr(a_,				"""do_normalize"""		)		)
   self.assertTrue(hasattr(a_,				"""image_mean"""		)		)
   self.assertTrue(hasattr(a_,				"""image_std"""		)		)


 def 			UpperCamelCase_       (    self:		List[str]		):




   '''simple docstring'''



   _snake_case							:		Dict								=  self.image_processing_class.from_dict(self.image_processor_dict		)
   self.assertEqual(image_processor.size,				{"""height""": 18, """width""": 20}		)

   _snake_case							:		Optional[Any]								=  self.image_processing_class.from_dict(self.image_processor_dict,				size=42		)
   self.assertEqual(image_processor.size,				{"""height""": 42, """width""": 42}		)

   # Previous config had dimensions in (width, height) order
   _snake_case							:		str								=  self.image_processing_class.from_dict(self.image_processor_dict,				size=(42, 84)		)
   self.assertEqual(image_processor.size,				{"""height""": 84, """width""": 42}		)


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




   '''simple docstring'''



   pass


 @is_flaky()
 def 			UpperCamelCase_       (    self:		Union[str, Any]		):




   '''simple docstring'''



   _snake_case							:		Dict								=  self.image_processing_class(**self.image_processor_dict		)
   # create random PIL images
   _snake_case							:		Optional[Any]								=  prepare_image_inputs(self.image_processor_tester,				equal_resolution=a_		)
   for image in image_inputs:
     self.assertIsInstance(a_,				Image.Image		)

   # Test not batched input
   _snake_case							:		Union[str, Any]								=  image_processing(image_inputs[0],				return_tensors="""pt"""		).pixel_values
   self.assertEqual(
       encoded_images.shape,				(
           1,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.size["""height"""],
           self.image_processor_tester.size["""width"""],
       ),				)

   # Test batched
   _snake_case							:		Optional[Any]								=  image_processing(a_,				return_tensors="""pt"""		).pixel_values
   self.assertEqual(
       encoded_images.shape,				(
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.size["""height"""],
           self.image_processor_tester.size["""width"""],
       ),				)


 @is_flaky()
 def 			UpperCamelCase_       (    self:		Dict		):




   '''simple docstring'''



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

   # Test not batched input
   _snake_case							:		str								=  image_processing(image_inputs[0],				return_tensors="""pt"""		).pixel_values
   self.assertEqual(
       encoded_images.shape,				(
           1,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.size["""height"""],
           self.image_processor_tester.size["""width"""],
       ),				)

   # Test batched
   _snake_case							:		Any								=  image_processing(a_,				return_tensors="""pt"""		).pixel_values
   self.assertEqual(
       encoded_images.shape,				(
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.size["""height"""],
           self.image_processor_tester.size["""width"""],
       ),				)


 @is_flaky()
 def 			UpperCamelCase_       (    self:		List[Any]		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  self.image_processing_class(**self.image_processor_dict		)
   # create random PyTorch tensors
   _snake_case							:		Any								=  prepare_image_inputs(self.image_processor_tester,				equal_resolution=a_,				torchify=a_		)
   for image in image_inputs:
     self.assertIsInstance(a_,				torch.Tensor		)

   # Test not batched input
   _snake_case							:		Tuple								=  image_processing(image_inputs[0],				return_tensors="""pt"""		).pixel_values
   self.assertEqual(
       encoded_images.shape,				(
           1,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.size["""height"""],
           self.image_processor_tester.size["""width"""],
       ),				)

   # Test batched
   _snake_case							:		Optional[int]								=  image_processing(a_,				return_tensors="""pt"""		).pixel_values
   self.assertEqual(
       encoded_images.shape,				(
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.size["""height"""],
           self.image_processor_tester.size["""width"""],
       ),				)







"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


A_      =    {
    '''configuration_maskformer''': ['''MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MaskFormerConfig'''],
    '''configuration_maskformer_swin''': ['''MaskFormerSwinConfig'''],
}

try:
     if not is_vision_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     A_      =    ['''MaskFormerFeatureExtractor''']
     A_      =    ['''MaskFormerImageProcessor''']


try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     A_      =    [
         '''MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''MaskFormerForInstanceSegmentation''',
         '''MaskFormerModel''',
         '''MaskFormerPreTrainedModel''',
     ]
     A_      =    [
         '''MaskFormerSwinBackbone''',
         '''MaskFormerSwinModel''',
         '''MaskFormerSwinPreTrainedModel''',
     ]

if TYPE_CHECKING:
     from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig
     from .configuration_maskformer_swin import MaskFormerSwinConfig

     try:
          if not is_vision_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .feature_extraction_maskformer import MaskFormerFeatureExtractor
          from .image_processing_maskformer import MaskFormerImageProcessor
     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_maskformer import (
              MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
              MaskFormerForInstanceSegmentation,
              MaskFormerModel,
              MaskFormerPreTrainedModel,
          )
          from .modeling_maskformer_swin import (
              MaskFormerSwinBackbone,
              MaskFormerSwinModel,
              MaskFormerSwinPreTrainedModel,
          )


else:
     import sys

     A_      =    _LazyModule(__name__, globals()['''__file__'''], _import_structure)


