






'''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 LevitImageProcessor






class     a_						(      unittest.TestCase    ):

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



           """simple docstring"""






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


     def   A__						(				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,
               "do_center_crop": self.do_center_crop,
               "size": self.size,
               "crop_size": self.crop_size,
           }






@require_torch
@require_vision
class     a_						(      lowerCamelCase     ,	unittest.TestCase    ):
     lowercase							=				LevitImageProcessor if is_vision_available() else None

     def   A__						(				self				)  ->				Any:



           """simple docstring"""






           UpperCamelCase      = LevitImageProcessingTester(self				)

     @property
     def   A__						(				self				)  ->				int:



           """simple docstring"""






           return self.image_processor_tester.prepare_image_processor_dict()

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



           """simple docstring"""






           UpperCamelCase      = self.image_processing_class(**self.image_processor_dict				)
           self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""image_mean"""				)				)
           self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""image_std"""				)				)
           self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""do_normalize"""				)				)
           self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""do_resize"""				)				)
           self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""do_center_crop"""				)				)
           self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      ,		"""size"""				)				)

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



           """simple docstring"""






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

           UpperCamelCase      = self.image_processing_class.from_dict(self.image_processor_dict      ,		size=42      ,		crop_size=84				)
           self.assertEqual(image_processor.size      ,		{"""shortest_edge""": 42}				)
           self.assertEqual(image_processor.crop_size      ,		{"""height""": 84, """width""": 84}				)

     def   A__						(				self				)  ->				Dict:



           """simple docstring"""






           pass

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



           """simple docstring"""






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

           # Test not batched input
           UpperCamelCase      = 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.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )      ,		)

           # Test batched
           UpperCamelCase      = image_processing(_SCREAMING_SNAKE_CASE      ,		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.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )      ,		)

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



           """simple docstring"""






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

           # Test not batched input
           UpperCamelCase      = 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.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )      ,		)

           # Test batched
           UpperCamelCase      = image_processing(_SCREAMING_SNAKE_CASE      ,		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.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )      ,		)


     def   A__						(				self				)  ->				str:



           """simple docstring"""






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

           # Test not batched input
           UpperCamelCase      = 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.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )      ,		)

           # Test batched
           UpperCamelCase      = image_processing(_SCREAMING_SNAKE_CASE      ,		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.crop_size["""height"""],
                   self.image_processor_tester.crop_size["""width"""],
               )      ,		)








'''simple docstring'''

import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)






class     a_						(      unittest.TestCase    ):

  def   A__						(				self				)  ->				Tuple:



        """simple docstring"""






        UpperCamelCase      = ["""a""", """b""", """c"""]

        # Defaults to last layer if both are None
        UpperCamelCase  ,UpperCamelCase      = get_aligned_output_features_output_indices(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)
        self.assertEqual(_SCREAMING_SNAKE_CASE      ,		["""c"""]				)
        self.assertEqual(_SCREAMING_SNAKE_CASE      ,		[2]				)

        # Out indices set to match out features
        UpperCamelCase  ,UpperCamelCase      = get_aligned_output_features_output_indices(["""a""", """c"""]      ,		_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE				)
        self.assertEqual(_SCREAMING_SNAKE_CASE      ,		["""a""", """c"""]				)
        self.assertEqual(_SCREAMING_SNAKE_CASE      ,		[0, 2]				)

        # Out features set to match out indices
        UpperCamelCase  ,UpperCamelCase      = get_aligned_output_features_output_indices(_SCREAMING_SNAKE_CASE      ,		[0, 2]      ,		_SCREAMING_SNAKE_CASE				)
        self.assertEqual(_SCREAMING_SNAKE_CASE      ,		["""a""", """c"""]				)
        self.assertEqual(_SCREAMING_SNAKE_CASE      ,		[0, 2]				)

        # Out features selected from negative indices
        UpperCamelCase  ,UpperCamelCase      = get_aligned_output_features_output_indices(_SCREAMING_SNAKE_CASE      ,		[-3, -1]      ,		_SCREAMING_SNAKE_CASE				)
        self.assertEqual(_SCREAMING_SNAKE_CASE      ,		["""a""", """c"""]				)
        self.assertEqual(_SCREAMING_SNAKE_CASE      ,		[-3, -1]				)

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        with self.assertRaises(_SCREAMING_SNAKE_CASE				):
              verify_out_features_out_indices(["""a""", """b"""]      ,		(0, 1)      ,		_SCREAMING_SNAKE_CASE				)

        # Out features must be a list
        with self.assertRaises(_SCREAMING_SNAKE_CASE				):
              verify_out_features_out_indices(("""a""", """b""")      ,		(0, 1)      ,		["""a""", """b"""]				)

        # Out features must be a subset of stage names
        with self.assertRaises(_SCREAMING_SNAKE_CASE				):
              verify_out_features_out_indices(["""a""", """b"""]      ,		(0, 1)      ,		["""a"""]				)

        # Out indices must be a list or tuple
        with self.assertRaises(_SCREAMING_SNAKE_CASE				):
              verify_out_features_out_indices(_SCREAMING_SNAKE_CASE      ,		0      ,		["""a""", """b"""]				)

        # Out indices must be a subset of stage names
        with self.assertRaises(_SCREAMING_SNAKE_CASE				):
              verify_out_features_out_indices(_SCREAMING_SNAKE_CASE      ,		(0, 1)      ,		["""a"""]				)

        # Out features and out indices must be the same length
        with self.assertRaises(_SCREAMING_SNAKE_CASE				):
              verify_out_features_out_indices(["""a""", """b"""]      ,		(0,)      ,		["""a""", """b""", """c"""]				)

        # Out features should match out indices
        with self.assertRaises(_SCREAMING_SNAKE_CASE				):
              verify_out_features_out_indices(["""a""", """b"""]      ,		(0, 2)      ,		["""a""", """b""", """c"""]				)

        # Out features and out indices should be in order
        with self.assertRaises(_SCREAMING_SNAKE_CASE				):
              verify_out_features_out_indices(["""b""", """a"""]      ,		(0, 1)      ,		["""a""", """b"""]				)

        # Check passes with valid inputs
        verify_out_features_out_indices(["""a""", """b""", """d"""]      ,		(0, 1, -1)      ,		["""a""", """b""", """c""", """d"""]				)


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



        """simple docstring"""






        UpperCamelCase      = BackboneMixin()

        UpperCamelCase      = ["""a""", """b""", """c"""]
        UpperCamelCase      = ["""a""", """c"""]
        UpperCamelCase      = [0, 2]

        # Check that the output features and indices are set correctly
        self.assertEqual(backbone.out_features      ,		["""a""", """c"""]				)
        self.assertEqual(backbone.out_indices      ,		[0, 2]				)

        # Check out features and indices are updated correctly
        UpperCamelCase      = ["""a""", """b"""]
        self.assertEqual(backbone.out_features      ,		["""a""", """b"""]				)
        self.assertEqual(backbone.out_indices      ,		[0, 1]				)

        UpperCamelCase      = [-3, -1]
        self.assertEqual(backbone.out_features      ,		["""a""", """c"""]				)
        self.assertEqual(backbone.out_indices      ,		[-3, -1]				)
