






'''simple docstring'''


import unittest

import numpy as np
from datasets import load_dataset

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 BeitImageProcessor
class    _SCREAMING_SNAKE_CASE							(		unittest.TestCase	):


			'''simple docstring'''





			def __init__(   self							:						str ,      lowercase							:						Optional[Any] ,      lowercase							:						Any=7 ,      lowercase							:						List[Any]=3 ,      lowercase							:						Dict=1_8 ,      lowercase							:						List[Any]=3_0 ,      lowercase							:						Union[str, Any]=4_0_0 ,      lowercase							:						Optional[int]=True ,      lowercase							:						Tuple=None ,      lowercase							:						Optional[int]=True ,      lowercase							:						List[str]=None ,      lowercase							:						int=True ,      lowercase							:						Dict=[0.5, 0.5, 0.5] ,      lowercase							:						int=[0.5, 0.5, 0.5] ,      lowercase							:						Optional[int]=False ,      )							->    List[str]:

									'''simple docstring'''







									UpperCamelCase__         = size if size is not None else {"""height""": 2_0, """width""": 2_0}
									UpperCamelCase__         = crop_size if crop_size is not None else {"""height""": 1_8, """width""": 1_8}
									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
									UpperCamelCase__         = do_reduce_labels







			def 				A      (   self							:						Tuple			)							->    Any:

									'''simple docstring'''







									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_center_crop": self.do_center_crop,
									    "crop_size": self.crop_size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_reduce_labels": self.do_reduce_labels,
									}







def 	__magic_name__( ):
						'''simple docstring'''





						UpperCamelCase__         = load_dataset("""hf-internal-testing/fixtures_ade20k"""						,      split="""test"""					)

						UpperCamelCase__         = Image.open(dataset[0]["""file"""]					)
						UpperCamelCase__         = Image.open(dataset[1]["""file"""]					)

						return image, map







def 	__magic_name__( ):
						'''simple docstring'''





						UpperCamelCase__         = load_dataset("""hf-internal-testing/fixtures_ade20k"""						,      split="""test"""					)

						UpperCamelCase__         = Image.open(ds[0]["""file"""]					)
						UpperCamelCase__         = Image.open(ds[1]["""file"""]					)
						UpperCamelCase__         = Image.open(ds[2]["""file"""]					)
						UpperCamelCase__         = Image.open(ds[3]["""file"""]					)

						return [imagea, imagea], [mapa, mapa]







@require_torch
@require_vision
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE       ,unittest.TestCase	):


			'''simple docstring'''





			__a							:							Tuple					     =					BeitImageProcessor if is_vision_available() else None






			def 				A      (   self							:						Optional[int]			)							->    List[Any]:

									'''simple docstring'''







									UpperCamelCase__         = BeitImageProcessingTester(self			)






			@property
			def 				A      (   self							:						Tuple			)							->    Any:

									'''simple docstring'''







									return self.image_processor_tester.prepare_image_processor_dict()






			def 				A      (   self							:						Optional[Any]			)							->    Tuple:

									'''simple docstring'''







									UpperCamelCase__         = self.image_processing_class(**self.image_processor_dict			)
									self.assertTrue(hasattr(lowercase ,      """do_resize"""			)			)
									self.assertTrue(hasattr(lowercase ,      """size"""			)			)
									self.assertTrue(hasattr(lowercase ,      """do_center_crop"""			)			)
									self.assertTrue(hasattr(lowercase ,      """center_crop"""			)			)
									self.assertTrue(hasattr(lowercase ,      """do_normalize"""			)			)
									self.assertTrue(hasattr(lowercase ,      """image_mean"""			)			)
									self.assertTrue(hasattr(lowercase ,      """image_std"""			)			)






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

									'''simple docstring'''







									UpperCamelCase__         = self.image_processing_class.from_dict(self.image_processor_dict			)
									self.assertEqual(image_processor.size ,      {"""height""": 2_0, """width""": 2_0}			)
									self.assertEqual(image_processor.crop_size ,      {"""height""": 1_8, """width""": 1_8}			)
									self.assertEqual(image_processor.do_reduce_labels ,      lowercase			)

									UpperCamelCase__         = self.image_processing_class.from_dict(
									    self.image_processor_dict ,      size=4_2 ,      crop_size=8_4 ,      reduce_labels=lowercase			)
									self.assertEqual(image_processor.size ,      {"""height""": 4_2, """width""": 4_2}			)
									self.assertEqual(image_processor.crop_size ,      {"""height""": 8_4, """width""": 8_4}			)
									self.assertEqual(image_processor.do_reduce_labels ,      lowercase			)






			def 				A      (   self							:						Any			)							->    Optional[int]:

									'''simple docstring'''







									pass






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

									'''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=lowercase			)
									for image in image_inputs:
															self.assertIsInstance(lowercase ,      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(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.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )






			def 				A      (   self							:						Tuple			)							->    Tuple:

									'''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=lowercase ,      numpify=lowercase			)
									for image in image_inputs:
															self.assertIsInstance(lowercase ,      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(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.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )






			def 				A      (   self							:						str			)							->    int:

									'''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=lowercase ,      torchify=lowercase			)
									for image in image_inputs:
															self.assertIsInstance(lowercase ,      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(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.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )






			def 				A      (   self							:						int			)							->    Any:

									'''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=lowercase ,      torchify=lowercase			)
									UpperCamelCase__         = []
									for image in image_inputs:
															self.assertIsInstance(lowercase ,      torch.Tensor			)
															maps.append(torch.zeros(image.shape[-2:]			).long()			)

									# Test not batched input
									UpperCamelCase__         = image_processing(image_inputs[0] ,      maps[0] ,      return_tensors="""pt"""			)
									self.assertEqual(
									    encoding["""pixel_values"""].shape ,      (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )
									self.assertEqual(
									    encoding["""labels"""].shape ,      (
									        1,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )
									self.assertEqual(encoding["""labels"""].dtype ,      torch.long			)
									self.assertTrue(encoding["""labels"""].min().item() >= 0			)
									self.assertTrue(encoding["""labels"""].max().item() <= 2_5_5			)

									# Test batched
									UpperCamelCase__         = image_processing(lowercase ,      lowercase ,      return_tensors="""pt"""			)
									self.assertEqual(
									    encoding["""pixel_values"""].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"""],
									    ) ,      )
									self.assertEqual(
									    encoding["""labels"""].shape ,      (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )
									self.assertEqual(encoding["""labels"""].dtype ,      torch.long			)
									self.assertTrue(encoding["""labels"""].min().item() >= 0			)
									self.assertTrue(encoding["""labels"""].max().item() <= 2_5_5			)

									# Test not batched input (PIL images)
									UpperCamelCase__    ,   UpperCamelCase__         = prepare_semantic_single_inputs()

									UpperCamelCase__         = image_processing(lowercase ,      lowercase ,      return_tensors="""pt"""			)
									self.assertEqual(
									    encoding["""pixel_values"""].shape ,      (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )
									self.assertEqual(
									    encoding["""labels"""].shape ,      (
									        1,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )
									self.assertEqual(encoding["""labels"""].dtype ,      torch.long			)
									self.assertTrue(encoding["""labels"""].min().item() >= 0			)
									self.assertTrue(encoding["""labels"""].max().item() <= 2_5_5			)

									# Test batched input (PIL images)
									UpperCamelCase__    ,   UpperCamelCase__         = prepare_semantic_batch_inputs()

									UpperCamelCase__         = image_processing(lowercase ,      lowercase ,      return_tensors="""pt"""			)
									self.assertEqual(
									    encoding["""pixel_values"""].shape ,      (
									        2,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )
									self.assertEqual(
									    encoding["""labels"""].shape ,      (
									        2,
									        self.image_processor_tester.crop_size["""height"""],
									        self.image_processor_tester.crop_size["""width"""],
									    ) ,      )
									self.assertEqual(encoding["""labels"""].dtype ,      torch.long			)
									self.assertTrue(encoding["""labels"""].min().item() >= 0			)
									self.assertTrue(encoding["""labels"""].max().item() <= 2_5_5			)







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

									'''simple docstring'''







									UpperCamelCase__         = self.image_processing_class(**self.image_processor_dict			)

									# ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
									UpperCamelCase__    ,   UpperCamelCase__         = prepare_semantic_single_inputs()
									UpperCamelCase__         = image_processing(lowercase ,      lowercase ,      return_tensors="""pt"""			)
									self.assertTrue(encoding["""labels"""].min().item() >= 0			)
									self.assertTrue(encoding["""labels"""].max().item() <= 1_5_0			)

									UpperCamelCase__         = True
									UpperCamelCase__         = image_processing(lowercase ,      lowercase ,      return_tensors="""pt"""			)
									self.assertTrue(encoding["""labels"""].min().item() >= 0			)
									self.assertTrue(encoding["""labels"""].max().item() <= 2_5_5			)








'''simple docstring'''


import enum
import os
from hashlib import shaaaa
from typing import Optional

from .. import config
from .logging import get_logger


lowerCamelCase_				: Any    	=							get_logger(__name__)
class    _SCREAMING_SNAKE_CASE							(		enum.Enum	):


			'''simple docstring'''





			__a							:							Tuple					     =					"all_checks"
			__a							:							List[Any]					     =					"basic_checks"
			__a							:							Dict					     =					"no_checks"
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''












def 	__magic_name__( _A						,      _A						,      _A=None					):
						'''simple docstring'''





						if expected_checksums is None:
												logger.info("""Unable to verify checksums."""					)
												return
						if len(set(_A					) - set(_A					)					) > 0:
												raise ExpectedMoreDownloadedFiles(str(set(_A					) - set(_A					)					)					)
						if len(set(_A					) - set(_A					)					) > 0:
												raise UnexpectedDownloadedFile(str(set(_A					) - set(_A					)					)					)
						UpperCamelCase__         = [url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]]
						UpperCamelCase__         = """ for """ + verification_name if verification_name is not None else """"""
						if len(_A					) > 0:
												raise NonMatchingChecksumError(
												    f"Checksums didn't match{for_verification_name}:\n"
												    f"{bad_urls}\n"
												    """Set `verification_mode='no_checks'` to skip checksums verification and ignore this error"""					)
						logger.info("""All the checksums matched successfully""" + for_verification_name					)
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''












def 	__magic_name__( _A						,      _A					):
						'''simple docstring'''





						if expected_splits is None:
												logger.info("""Unable to verify splits sizes."""					)
												return
						if len(set(_A					) - set(_A					)					) > 0:
												raise ExpectedMoreSplits(str(set(_A					) - set(_A					)					)					)
						if len(set(_A					) - set(_A					)					) > 0:
												raise UnexpectedSplits(str(set(_A					) - set(_A					)					)					)
						UpperCamelCase__         = [
						    {"""expected""": expected_splits[name], """recorded""": recorded_splits[name]}
						    for name in expected_splits
						    if expected_splits[name].num_examples != recorded_splits[name].num_examples
						]
						if len(_A					) > 0:
												raise NonMatchingSplitsSizesError(str(_A					)					)
						logger.info("""All the splits matched successfully."""					)







def 	__magic_name__( _A						,      _A = True					):
						'''simple docstring'''





						if record_checksum:
												UpperCamelCase__         = shaaaa()
												with open(_A						,      """rb"""					) as f:
																		for chunk in iter(lambda: f.read(1 << 20					)						,      B""""""					):
																								m.update(_A					)
																		UpperCamelCase__         = m.hexdigest()
						else:
												UpperCamelCase__         = None
						return {"num_bytes": os.path.getsize(_A					), "checksum": checksum}







def 	__magic_name__( _A					):
						'''simple docstring'''





						if dataset_size and config.IN_MEMORY_MAX_SIZE:
												return dataset_size < config.IN_MEMORY_MAX_SIZE
						else:
												return False
