





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





class 			snake_case		(   unittest.TestCase   ):

      def __init__(			self   :Optional[int]						,							_lowerCamelCase   :str						,							_lowerCamelCase   :Tuple=7						,							_lowerCamelCase   :str=3						,							_lowerCamelCase   :Dict=1_8						,							_lowerCamelCase   :int=3_0						,							_lowerCamelCase   :int=4_0_0						,							_lowerCamelCase   :Dict=True						,							_lowerCamelCase   :int=None						,							_lowerCamelCase   :List[Any]=True						,							_lowerCamelCase   :int=False						,							_lowerCamelCase   :Dict=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]						,							_lowerCamelCase   :Union[str, Any]=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]						,							_lowerCamelCase   :Optional[Any]=True						,							):
       __SCREAMING_SNAKE_CASE :			Tuple												=       size if size is not None else {'''height''': 2_0, '''width''': 2_0}
       __SCREAMING_SNAKE_CASE :			Tuple												=       parent
       __SCREAMING_SNAKE_CASE :			Tuple												=       batch_size
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       num_channels
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       image_size
       __SCREAMING_SNAKE_CASE :			int												=       min_resolution
       __SCREAMING_SNAKE_CASE :			List[Any]												=       max_resolution
       __SCREAMING_SNAKE_CASE :			Any												=       do_resize
       __SCREAMING_SNAKE_CASE :			Dict												=       size
       __SCREAMING_SNAKE_CASE :			Dict												=       do_normalize
       __SCREAMING_SNAKE_CASE :			List[str]												=       image_mean
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       image_std
       __SCREAMING_SNAKE_CASE :			List[Any]												=       do_pad
       __SCREAMING_SNAKE_CASE :			List[Any]												=       do_convert_rgb

      def 						SCREAMING_SNAKE_CASE_			(			self   :int						):
       return {
           "do_resize": self.do_resize,
           "size": self.size,
           "do_normalize": self.do_normalize,
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_convert_rgb": self.do_convert_rgb,
           "do_pad": self.do_pad,
       }



      def 						SCREAMING_SNAKE_CASE_			(			self   :Tuple						,							_lowerCamelCase   :Dict=False						,							_lowerCamelCase   :Union[str, Any]=False						,							_lowerCamelCase   :Dict=False						):
       assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

       if equal_resolution:
        __SCREAMING_SNAKE_CASE :			Any												=       []
        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:
        __SCREAMING_SNAKE_CASE :			List[Any]												=       []
        for i in range(self.batch_size						):
         __SCREAMING_SNAKE_CASE							,      __SCREAMING_SNAKE_CASE :			Optional[Any]												=       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
        __SCREAMING_SNAKE_CASE :			Dict												=       [Image.fromarray(np.moveaxis(_lowercase						,							0						,							-1						)						) for x in image_inputs]

       if torchify:
        __SCREAMING_SNAKE_CASE :			List[Any]												=       [torch.from_numpy(_lowercase						) for x in image_inputs]

       return image_inputs







@require_torch
@require_vision
class 			snake_case		(   _snake_case ,     unittest.TestCase   ):
      lowerCamelCase__						= BlipImageProcessor if is_vision_available() else None

      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[int]						):
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       BlipImageProcessingTester(self						)

      @property
      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       return self.image_processor_tester.prepare_image_processor_dict()

      def 						SCREAMING_SNAKE_CASE_			(			self   :Union[str, Any]						):
       __SCREAMING_SNAKE_CASE :			int												=       self.image_processing_class(**self.image_processor_dict						)
       self.assertTrue(hasattr(_lowercase						,							'''do_resize'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''size'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''do_normalize'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''image_mean'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''image_std'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''do_convert_rgb'''						)						)

      def 						SCREAMING_SNAKE_CASE_			(			self   :Tuple						):
       pass

      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       # Initialize image_processor
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       self.image_processing_class(**self.image_processor_dict						)
       # create random PIL images
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase						)
       for image in image_inputs:
        self.assertIsInstance(_lowercase						,							Image.Image						)

       # Test not batched input
       __SCREAMING_SNAKE_CASE :			Tuple												=       image_processor(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
       __SCREAMING_SNAKE_CASE :			List[Any]												=       image_processor(_lowercase						,							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 						SCREAMING_SNAKE_CASE_			(			self   :str						):
       # Initialize image_processor
       __SCREAMING_SNAKE_CASE :			Optional[Any]												=       self.image_processing_class(**self.image_processor_dict						)
       # create random numpy tensors
       __SCREAMING_SNAKE_CASE :			Tuple												=       self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase						,							numpify=_lowercase						)
       for image in image_inputs:
        self.assertIsInstance(_lowercase						,							np.ndarray						)

       # Test not batched input
       __SCREAMING_SNAKE_CASE :			str												=       image_processor(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
       __SCREAMING_SNAKE_CASE :			List[str]												=       image_processor(_lowercase						,							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 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       # Initialize image_processor
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       self.image_processing_class(**self.image_processor_dict						)
       # create random PyTorch tensors
       __SCREAMING_SNAKE_CASE :			str												=       self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase						,							torchify=_lowercase						)
       for image in image_inputs:
        self.assertIsInstance(_lowercase						,							torch.Tensor						)

       # Test not batched input
       __SCREAMING_SNAKE_CASE :			Any												=       image_processor(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
       __SCREAMING_SNAKE_CASE :			Tuple												=       image_processor(_lowercase						,							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'''],
           )						,							)







@require_torch
@require_vision
class 			snake_case		(   _snake_case ,     unittest.TestCase   ):
      lowerCamelCase__						= BlipImageProcessor if is_vision_available() else None

      def 						SCREAMING_SNAKE_CASE_			(			self   :Tuple						):
       __SCREAMING_SNAKE_CASE :			Tuple												=       BlipImageProcessingTester(self						,							num_channels=4						)
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       3

      @property
      def 						SCREAMING_SNAKE_CASE_			(			self   :str						):
       return self.image_processor_tester.prepare_image_processor_dict()

      def 						SCREAMING_SNAKE_CASE_			(			self   :Any						):
       __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       self.image_processing_class(**self.image_processor_dict						)
       self.assertTrue(hasattr(_lowercase						,							'''do_resize'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''size'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''do_normalize'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''image_mean'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''image_std'''						)						)
       self.assertTrue(hasattr(_lowercase						,							'''do_convert_rgb'''						)						)

      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[Any]						):
       pass



      def 						SCREAMING_SNAKE_CASE_			(			self   :Dict						):
       # Initialize image_processor
       __SCREAMING_SNAKE_CASE :			int												=       self.image_processing_class(**self.image_processor_dict						)
       # create random PIL images
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       self.image_processor_tester.prepare_inputs(equal_resolution=_lowercase						)
       for image in image_inputs:
        self.assertIsInstance(_lowercase						,							Image.Image						)

       # Test not batched input
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       image_processor(image_inputs[0]						,							return_tensors='''pt'''						).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               1,
               self.expected_encoded_image_num_channels,
               self.image_processor_tester.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )						,							)

       # Test batched
       __SCREAMING_SNAKE_CASE :			Optional[int]												=       image_processor(_lowercase						,							return_tensors='''pt'''						).pixel_values
       self.assertEqual(
           encoded_images.shape						,							(
               self.image_processor_tester.batch_size,
               self.expected_encoded_image_num_channels,
               self.image_processor_tester.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )						,							)






from dataclasses import dataclass
from typing import Callable, Optional

import datasets







@dataclass
class 							a_  (		datasets.BuilderConfig				):
						UpperCamelCase__      :						Optional[Callable]				       =None
						UpperCamelCase__      :						Optional[dict]				       =None
						UpperCamelCase__      :						Optional[datasets.Features]				       =None

						def 	__a       (    self	:List[str])		->					List[str]:
												assert self.generator is not None, "generator must be specified"

												if self.gen_kwargs is None:
																		UpperCAmelCase_     							=     {}







class 							a_  (		datasets.GeneratorBasedBuilder				):
						UpperCamelCase__      :						List[str]				       =GeneratorConfig

						def 	__a       (    self	:List[str])		->					List[Any]:
												return datasets.DatasetInfo(features=self.config.features)

						def 	__a       (    self	:Optional[Any]   ,						_lowercase	:List[str])		->					Optional[Any]:
												return [datasets.SplitGenerator(name=datasets.Split.TRAIN   ,						gen_kwargs=self.config.gen_kwargs)]




						def 	__a       (    self	:Optional[int]   ,						**_lowercase	:str)		->					Union[str, Any]:
												for idx, ex in enumerate(self.config.generator(**_lowercase)):
																		yield idx, ex





