


import random
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():
       import PIL

       from transformers import FlavaImageProcessor
       from transformers.image_utils import PILImageResampling
       from transformers.models.flava.image_processing_flava import (
           FLAVA_CODEBOOK_MEAN,
           FLAVA_CODEBOOK_STD,
           FLAVA_IMAGE_MEAN,
           FLAVA_IMAGE_STD,
       )
else:
       lowercase_           =	lowercase_           =	lowercase_           =	lowercase_           =	None


class        A_					(       unittest.TestCase    ):




      '''simple docstring'''


      def __init__(				self:       str						,      a:       Tuple						,      a:       List[Any]=7						,      a:       int=3						,      a:       Optional[Any]=30						,      a:       List[Any]=400						,      a:       List[str]=True						,      a:       Tuple=None						,      a:       List[str]=True						,      a:       int=None						,      a:       List[str]=None						,      a:       Dict=True						,      a:       Optional[Any]=1 / 255						,      a:       Optional[int]=True						,      a:       Any=FLAVA_IMAGE_MEAN						,      a:       Optional[int]=FLAVA_IMAGE_STD						,      a:       Optional[Any]=14						,      a:       List[str]=75						,      a:       Optional[int]=None						,      a:       Tuple=16						,      a:       Optional[Any]=0.3						,      a:       Dict=None						,      a:       List[Any]=True						,      a:       Dict=None						,      a:       Dict=None						,      a:       Optional[Any]=True						,      a:       Tuple=None						,      a:       Tuple=True						,      a:       List[Any]=True						,      a:       Optional[Any]=FLAVA_CODEBOOK_MEAN						,      a:       Any=FLAVA_CODEBOOK_STD						,      ):
       __lowerCamelCase		:							List[str]							=						size if size is not None else {'height': 224, 'width': 224}
       __lowerCamelCase		:							Optional[int]							=						crop_size if crop_size is not None else {'height': 224, 'width': 224}
       __lowerCamelCase		:							str							=						codebook_size if codebook_size is not None else {'height': 112, 'width': 112}
       __lowerCamelCase		:							List[Any]							=						codebook_crop_size if codebook_crop_size is not None else {'height': 112, 'width': 112}

       __lowerCamelCase		:							Union[str, Any]							=						parent
       __lowerCamelCase		:							Dict							=						batch_size
       __lowerCamelCase		:							Union[str, Any]							=						num_channels
       __lowerCamelCase		:							List[Any]							=						do_resize
       __lowerCamelCase		:							Any							=						do_rescale
       __lowerCamelCase		:							Tuple							=						rescale_factor
       __lowerCamelCase		:							Dict							=						min_resolution
       __lowerCamelCase		:							Optional[int]							=						max_resolution
       __lowerCamelCase		:							Optional[Any]							=						size
       __lowerCamelCase		:							Union[str, Any]							=						resample if resample is not None else PILImageResampling.BICUBIC
       __lowerCamelCase		:							List[Any]							=						do_normalize
       __lowerCamelCase		:							int							=						image_mean
       __lowerCamelCase		:							List[str]							=						image_std
       __lowerCamelCase		:							Any							=						do_center_crop
       __lowerCamelCase		:							Union[str, Any]							=						crop_size

       __lowerCamelCase		:							str							=						input_size_patches
       __lowerCamelCase		:							List[str]							=						total_mask_patches
       __lowerCamelCase		:							List[Any]							=						mask_group_max_patches
       __lowerCamelCase		:							str							=						mask_group_min_patches
       __lowerCamelCase		:							Any							=						mask_group_min_aspect_ratio
       __lowerCamelCase		:							Tuple							=						mask_group_max_aspect_ratio

       __lowerCamelCase		:							Optional[Any]							=						codebook_do_resize
       __lowerCamelCase		:							Tuple							=						codebook_size
       __lowerCamelCase		:							int							=						codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
       __lowerCamelCase		:							str							=						codebook_do_center_crop
       __lowerCamelCase		:							Any							=						codebook_crop_size
       __lowerCamelCase		:							Union[str, Any]							=						codebook_do_map_pixels
       __lowerCamelCase		:							int							=						codebook_do_normalize
       __lowerCamelCase		:							Tuple							=						codebook_image_mean
       __lowerCamelCase		:							str							=						codebook_image_std




      def      _snake_case	(				self:       Union[str, Any]				):
       return {
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_normalize": self.do_normalize,
           "do_resize": self.do_resize,
           "size": self.size,
           "resample": self.resample,
           "do_rescale": self.do_rescale,
           "rescale_factor": self.rescale_factor,
           "do_center_crop": self.do_center_crop,
           "crop_size": self.crop_size,
           "input_size_patches": self.input_size_patches,
           "total_mask_patches": self.total_mask_patches,
           "mask_group_max_patches": self.mask_group_max_patches,
           "mask_group_min_patches": self.mask_group_min_patches,
           "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
           "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
           "codebook_do_resize": self.codebook_do_resize,
           "codebook_size": self.codebook_size,
           "codebook_resample": self.codebook_resample,
           "codebook_do_center_crop": self.codebook_do_center_crop,
           "codebook_crop_size": self.codebook_crop_size,
           "codebook_do_map_pixels": self.codebook_do_map_pixels,
           "codebook_do_normalize": self.codebook_do_normalize,
           "codebook_image_mean": self.codebook_image_mean,
           "codebook_image_std": self.codebook_image_std,
       }




      def      _snake_case	(				self:       List[str]				):
       return (self.size["height"], self.size["width"])




      def      _snake_case	(				self:       Dict				):
       return (
           (self.input_size_patches, self.input_size_patches)
           if not isinstance(self.input_size_patches						,      a				)
           else self.input_size_patches
       )




      def      _snake_case	(				self:       Tuple				):
       return (self.codebook_size["height"], self.codebook_size["width"])



@require_torch
@require_vision
class        A_					(       __UpperCamelCase					,      unittest.TestCase    ):




      '''simple docstring'''


      __snake_case					       =   FlavaImageProcessor if is_vision_available() else None
      __snake_case					       =   None




      def      _snake_case	(				self:       Optional[int]				):
       __lowerCamelCase		:							List[Any]							=						FlavaImageProcessingTester(self				)




      @property
      def      _snake_case	(				self:       Optional[int]				):
       return self.image_processor_tester.prepare_image_processor_dict()




      def      _snake_case	(				self:       Union[str, Any]				):
       __lowerCamelCase		:							List[str]							=						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						,      'resample'				)				)
       self.assertTrue(hasattr(a						,      'crop_size'				)				)
       self.assertTrue(hasattr(a						,      'do_center_crop'				)				)
       self.assertTrue(hasattr(a						,      'do_rescale'				)				)
       self.assertTrue(hasattr(a						,      'rescale_factor'				)				)
       self.assertTrue(hasattr(a						,      'masking_generator'				)				)
       self.assertTrue(hasattr(a						,      'codebook_do_resize'				)				)
       self.assertTrue(hasattr(a						,      'codebook_size'				)				)
       self.assertTrue(hasattr(a						,      'codebook_resample'				)				)
       self.assertTrue(hasattr(a						,      'codebook_do_center_crop'				)				)
       self.assertTrue(hasattr(a						,      'codebook_crop_size'				)				)
       self.assertTrue(hasattr(a						,      'codebook_do_map_pixels'				)				)
       self.assertTrue(hasattr(a						,      'codebook_do_normalize'				)				)
       self.assertTrue(hasattr(a						,      'codebook_image_mean'				)				)
       self.assertTrue(hasattr(a						,      'codebook_image_std'				)				)




      def      _snake_case	(				self:       int				):
       __lowerCamelCase		:							Tuple							=						self.image_processing_class.from_dict(self.image_processor_dict				)
       self.assertEqual(image_processor.size						,      {'height': 224, 'width': 224}				)
       self.assertEqual(image_processor.crop_size						,      {'height': 224, 'width': 224}				)
       self.assertEqual(image_processor.codebook_size						,      {'height': 112, 'width': 112}				)
       self.assertEqual(image_processor.codebook_crop_size						,      {'height': 112, 'width': 112}				)

       __lowerCamelCase		:							Any							=						self.image_processing_class.from_dict(
           self.image_processor_dict						,      size=42						,      crop_size=84						,      codebook_size=33						,      codebook_crop_size=66				)
       self.assertEqual(image_processor.size						,      {'height': 42, 'width': 42}				)
       self.assertEqual(image_processor.crop_size						,      {'height': 84, 'width': 84}				)
       self.assertEqual(image_processor.codebook_size						,      {'height': 33, 'width': 33}				)
       self.assertEqual(image_processor.codebook_crop_size						,      {'height': 66, 'width': 66}				)




      def      _snake_case	(				self:       int				):
       pass




      def      _snake_case	(				self:       Union[str, Any]				):
       # Initialize image_processing
       __lowerCamelCase		:							List[str]							=						self.image_processing_class(**self.image_processor_dict				)
       # create random PIL images
       __lowerCamelCase		:							Any							=						prepare_image_inputs(self.image_processor_tester						,      equal_resolution=a				)
       for image in image_inputs:
        self.assertIsInstance(a						,      PIL.Image.Image				)

       # Test not batched input
       __lowerCamelCase		:							Tuple							=						image_processing(image_inputs[0]						,      return_tensors='pt'				)

       # Test no bool masked pos
       self.assertFalse('bool_masked_pos' in encoded_images				)

       __lowerCamelCase					,     __lowerCamelCase		:							str							=						self.image_processor_tester.get_expected_image_size()

       self.assertEqual(
           encoded_images.pixel_values.shape						,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)						,      )

       # Test batched
       __lowerCamelCase		:							Tuple							=						image_processing(a						,      return_tensors='pt'				)
       __lowerCamelCase					,     __lowerCamelCase		:							Dict							=						self.image_processor_tester.get_expected_image_size()

       # Test no bool masked pos
       self.assertFalse('bool_masked_pos' in encoded_images				)

       self.assertEqual(
           encoded_images.pixel_values.shape						,      (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )						,      )




      def      _snake_case	(				self:       Optional[Any]						,      a:       List[Any]						,      a:       int				):
       # Initialize image_processing
       __lowerCamelCase		:							Any							=						self.image_processing_class(**self.image_processor_dict				)
       # create random tensors
       __lowerCamelCase		:							Dict							=						prepare_image_inputs(self.image_processor_tester						,      equal_resolution=a						,      **a				)
       for image in image_inputs:
        self.assertIsInstance(a						,      a				)

       # Test not batched input
       __lowerCamelCase		:							List[Any]							=						image_processing(image_inputs[0]						,      return_tensors='pt'				)

       __lowerCamelCase					,     __lowerCamelCase		:							int							=						self.image_processor_tester.get_expected_image_size()
       self.assertEqual(
           encoded_images.pixel_values.shape						,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)						,      )

       __lowerCamelCase		:							int							=						image_processing(a						,      return_image_mask=a						,      return_tensors='pt'				)

       __lowerCamelCase					,     __lowerCamelCase		:							Any							=						self.image_processor_tester.get_expected_image_size()
       self.assertEqual(
           encoded_images.pixel_values.shape						,      (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )						,      )

       __lowerCamelCase					,     __lowerCamelCase		:							Optional[int]							=						self.image_processor_tester.get_expected_mask_size()
       self.assertEqual(
           encoded_images.bool_masked_pos.shape						,      (
               self.image_processor_tester.batch_size,
               expected_height,
               expected_width,
           )						,      )

       # Test batched
       __lowerCamelCase		:							Tuple							=						image_processing(a						,      return_tensors='pt'				).pixel_values

       __lowerCamelCase					,     __lowerCamelCase		:							Optional[Any]							=						self.image_processor_tester.get_expected_image_size()
       self.assertEqual(
           encoded_images.shape						,      (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )						,      )

       # Test masking
       __lowerCamelCase		:							Tuple							=						image_processing(a						,      return_image_mask=a						,      return_tensors='pt'				)

       __lowerCamelCase					,     __lowerCamelCase		:							int							=						self.image_processor_tester.get_expected_image_size()
       self.assertEqual(
           encoded_images.pixel_values.shape						,      (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )						,      )

       __lowerCamelCase					,     __lowerCamelCase		:							Dict							=						self.image_processor_tester.get_expected_mask_size()
       self.assertEqual(
           encoded_images.bool_masked_pos.shape						,      (
               self.image_processor_tester.batch_size,
               expected_height,
               expected_width,
           )						,      )




      def      _snake_case	(				self:       Dict				):
       self._test_call_framework(np.ndarray						,      prepare_kwargs={'numpify': True}				)




      def      _snake_case	(				self:       int				):
       self._test_call_framework(torch.Tensor						,      prepare_kwargs={'torchify': True}				)




      def      _snake_case	(				self:       str				):
       # Initialize image_processing
       random.seed(1234				)
       __lowerCamelCase		:							Optional[Any]							=						self.image_processing_class(**self.image_processor_dict				)
       __lowerCamelCase		:							List[Any]							=						prepare_image_inputs(self.image_processor_tester						,      equal_resolution=a						,      torchify=a				)

       # Test not batched input
       __lowerCamelCase		:							List[str]							=						image_processing(image_inputs[0]						,      return_image_mask=a						,      return_tensors='pt'				)
       self.assertEqual(encoded_images.bool_masked_pos.sum().item()						,      75				)




      def      _snake_case	(				self:       Optional[Any]				):
       # Initialize image_processing
       __lowerCamelCase		:							Optional[int]							=						self.image_processing_class(**self.image_processor_dict				)
       # create random PIL images
       __lowerCamelCase		:							str							=						prepare_image_inputs(self.image_processor_tester						,      equal_resolution=a				)
       for image in image_inputs:
        self.assertIsInstance(a						,      PIL.Image.Image				)

       # Test not batched input
       __lowerCamelCase		:							int							=						image_processing(image_inputs[0]						,      return_codebook_pixels=a						,      return_tensors='pt'				)
       __lowerCamelCase					,     __lowerCamelCase		:							Any							=						self.image_processor_tester.get_expected_codebook_image_size()
       self.assertEqual(
           encoded_images.codebook_pixel_values.shape						,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)						,      )

       # Test batched
       __lowerCamelCase		:							int							=						image_processing(a						,      return_codebook_pixels=a						,      return_tensors='pt'				)
       __lowerCamelCase					,     __lowerCamelCase		:							Any							=						self.image_processor_tester.get_expected_codebook_image_size()
       self.assertEqual(
           encoded_images.codebook_pixel_values.shape						,      (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               expected_height,
               expected_width,
           )						,      )





from typing import List

import datasets
from datasets.tasks import ImageClassification

from ..folder_based_builder import folder_based_builder


lowercase_           =	datasets.utils.logging.get_logger(__name__)


class        A_					(       folder_based_builder.FolderBasedBuilderConfig    ):




      '''simple docstring'''


      __snake_case					       =   None
      __snake_case					       =   None



class        A_					(       folder_based_builder.FolderBasedBuilder    ):




      '''simple docstring'''


      __snake_case					       =   datasets.Image()
      __snake_case					       =   """image"""
      __snake_case					       =   ImageFolderConfig
      __snake_case					       =   42  # definition at the bottom of the script
      __snake_case					       =   ImageClassification(image_column="""image"""					,      label_column="""label"""    )



lowercase_           =	[
    '.blp',
    '.bmp',
    '.dib',
    '.bufr',
    '.cur',
    '.pcx',
    '.dcx',
    '.dds',
    '.ps',
    '.eps',
    '.fit',
    '.fits',
    '.fli',
    '.flc',
    '.ftc',
    '.ftu',
    '.gbr',
    '.gif',
    '.grib',
    '.h5',
    '.hdf',
    '.png',
    '.apng',
    '.jp2',
    '.j2k',
    '.jpc',
    '.jpf',
    '.jpx',
    '.j2c',
    '.icns',
    '.ico',
    '.im',
    '.iim',
    '.tif',
    '.tiff',
    '.jfif',
    '.jpe',
    '.jpg',
    '.jpeg',
    '.mpg',
    '.mpeg',
    '.msp',
    '.pcd',
    '.pxr',
    '.pbm',
    '.pgm',
    '.ppm',
    '.pnm',
    '.psd',
    '.bw',
    '.rgb',
    '.rgba',
    '.sgi',
    '.ras',
    '.tga',
    '.icb',
    '.vda',
    '.vst',
    '.webp',
    '.wmf',
    '.emf',
    '.xbm',
    '.xpm',
]
lowercase_           =	IMAGE_EXTENSIONS

