




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 EfficientNetImageProcessor




class     a__ (      unittest.TestCase   ):







     def __init__(			self						,	A						,	A=13						,	A=3						,	A=18						,	A=30						,	A=400						,	A=True						,	A=None						,	A=True						,	A=[0.5, 0.5, 0.5]						,	A=[0.5, 0.5, 0.5]						,	)						->					Any:



          '''simple docstring'''



          a											=   size if size is not None else {"height": 18, "width": 18}
          a											=   parent
          a											=   batch_size
          a											=   num_channels
          a											=   image_size
          a											=   min_resolution
          a											=   max_resolution
          a											=   do_resize
          a											=   size
          a											=   do_normalize
          a											=   image_mean
          a											=   image_std







     def     lowerCAmelCase_       (			self				)						->					int:



          '''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,
          }







@require_torch
@require_vision
class     a__ (      UpperCamelCase__     ,      unittest.TestCase   ):
     a	:    int	      =			EfficientNetImageProcessor if is_vision_available() else None







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



          '''simple docstring'''



          a											=   EfficientNetImageProcessorTester(self				)







     @property
     def     lowerCAmelCase_       (			self				)						->					Optional[Any]:



          '''simple docstring'''



          return self.image_processor_tester.prepare_image_processor_dict()







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



          '''simple docstring'''



          a											=   self.image_processing_class(**self.image_processor_dict				)
          self.assertTrue(hasattr(A						,	"image_mean"				)				)
          self.assertTrue(hasattr(A						,	"image_std"				)				)
          self.assertTrue(hasattr(A						,	"do_normalize"				)				)
          self.assertTrue(hasattr(A						,	"do_resize"				)				)
          self.assertTrue(hasattr(A						,	"size"				)				)







     def     lowerCAmelCase_       (			self				)						->					Any:



          '''simple docstring'''



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

          a											=   self.image_processing_class.from_dict(self.image_processor_dict						,	size=42				)
          self.assertEqual(image_processor.size						,	{"height": 42, "width": 42}				)







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



          '''simple docstring'''



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

          # Test not batched input
          a											=   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
          a											=   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"],
              )						,	)







     def     lowerCAmelCase_       (			self				)						->					Tuple:



          '''simple docstring'''



          a											=   self.image_processing_class(**self.image_processor_dict				)
          # create random numpy tensors
          a											=   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
          a											=   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
          a											=   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"],
              )						,	)







     def     lowerCAmelCase_       (			self				)						->					int:



          '''simple docstring'''



          a											=   self.image_processing_class(**self.image_processor_dict				)
          # create random PyTorch tensors
          a											=   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
          a											=   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
          a											=   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"],
              )						,	)







import unittest

from transformers.image_processing_utils import get_size_dict




class     a__ (      unittest.TestCase   ):







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



          '''simple docstring'''



          a											=   {"wrong_key": 224}
          with self.assertRaises(A				):
               get_size_dict(A				)

          a											=   {"height": 224}
          with self.assertRaises(A				):
               get_size_dict(A				)

          a											=   {"width": 224, "shortest_edge": 224}
          with self.assertRaises(A				):
               get_size_dict(A				)

          # Test a dict with the correct keys is returned as is
          a											=   {"height": 224, "width": 224}
          a											=   get_size_dict(A				)
          self.assertEqual(A						,	A				)

          a											=   {"shortest_edge": 224}
          a											=   get_size_dict(A				)
          self.assertEqual(A						,	{"shortest_edge": 224}				)

          a											=   {"longest_edge": 224, "shortest_edge": 224}
          a											=   get_size_dict(A				)
          self.assertEqual(A						,	{"longest_edge": 224, "shortest_edge": 224}				)

          # Test a single int value which  represents (size, size)
          a											=   get_size_dict(224				)
          self.assertEqual(A						,	{"height": 224, "width": 224}				)

          # Test a single int value which represents the shortest edge
          a											=   get_size_dict(224						,	default_to_square=A				)
          self.assertEqual(A						,	{"shortest_edge": 224}				)

          # Test a tuple of ints which represents (height, width)
          a											=   get_size_dict((150, 200)				)
          self.assertEqual(A						,	{"height": 150, "width": 200}				)

          # Test a tuple of ints which represents (width, height)
          a											=   get_size_dict((150, 200)						,	height_width_order=A				)
          self.assertEqual(A						,	{"height": 200, "width": 150}				)

          # Test an int representing the shortest edge and max_size which represents the longest edge
          a											=   get_size_dict(224						,	max_size=256						,	default_to_square=A				)
          self.assertEqual(A						,	{"shortest_edge": 224, "longest_edge": 256}				)

          # Test int with default_to_square=True and max_size fails
          with self.assertRaises(A				):
               get_size_dict(224						,	max_size=256						,	default_to_square=A				)

