


'''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 	a__       (							unittest.TestCase						):





							'''simple docstring'''






							def __init__(	self      ,					lowerCamelCase_      ,					lowerCamelCase_=7      ,					lowerCamelCase_=3      ,					lowerCamelCase_=18      ,					lowerCamelCase_=30      ,					lowerCamelCase_=4_00      ,					lowerCamelCase_=True      ,					lowerCamelCase_=None      ,					lowerCamelCase_=True      ,					lowerCamelCase_=None      ,					lowerCamelCase_=True      ,					lowerCamelCase_=[0.5, 0.5, 0.5]      ,					lowerCamelCase_=[0.5, 0.5, 0.5]      ,					lowerCamelCase_=False      ,					) ->							Optional[Any]:
														lowerCAmelCase__      =			size if size is not None else {'''height''': 20, '''width''': 20}
														lowerCAmelCase__      =			crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
														lowerCAmelCase__      =			parent
														lowerCAmelCase__      =			batch_size
														lowerCAmelCase__      =			num_channels
														lowerCAmelCase__      =			image_size
														lowerCAmelCase__      =			min_resolution
														lowerCAmelCase__      =			max_resolution
														lowerCAmelCase__      =			do_resize
														lowerCAmelCase__      =			size
														lowerCAmelCase__      =			do_center_crop
														lowerCAmelCase__      =			crop_size
														lowerCAmelCase__      =			do_normalize
														lowerCAmelCase__      =			image_mean
														lowerCAmelCase__      =			image_std
														lowerCAmelCase__      =			do_reduce_labels







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														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       _snake_case     (  )		->					int:
							lowerCAmelCase__      =			load_dataset('''hf-internal-testing/fixtures_ade20k'''				,					split='''test''' )

							lowerCAmelCase__      =			Image.open(dataset[0]['''file'''] )
							lowerCAmelCase__      =			Image.open(dataset[1]['''file'''] )

							return image, map


def       _snake_case     (  )		->					List[Any]:
							lowerCAmelCase__      =			load_dataset('''hf-internal-testing/fixtures_ade20k'''				,					split='''test''' )

							lowerCAmelCase__      =			Image.open(ds[0]['''file'''] )
							lowerCAmelCase__      =			Image.open(ds[1]['''file'''] )
							lowerCAmelCase__      =			Image.open(ds[2]['''file'''] )
							lowerCAmelCase__      =			Image.open(ds[3]['''file'''] )

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






@require_torch
@require_vision
class 	a__       (							a__       ,       unittest.TestCase						):





							'''simple docstring'''






							lowercase__    :     Optional[int]					=   BeitImageProcessor if is_vision_available() else None







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														lowerCAmelCase__      =			BeitImageProcessingTester(self     )







							@property
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														return self.image_processor_tester.prepare_image_processor_dict()







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__      =			self.image_processing_class(**self.image_processor_dict     )
														self.assertTrue(hasattr(lowerCamelCase_      ,					'''do_resize'''     )     )
														self.assertTrue(hasattr(lowerCamelCase_      ,					'''size'''     )     )
														self.assertTrue(hasattr(lowerCamelCase_      ,					'''do_center_crop'''     )     )
														self.assertTrue(hasattr(lowerCamelCase_      ,					'''center_crop'''     )     )
														self.assertTrue(hasattr(lowerCamelCase_      ,					'''do_normalize'''     )     )
														self.assertTrue(hasattr(lowerCamelCase_      ,					'''image_mean'''     )     )
														self.assertTrue(hasattr(lowerCamelCase_      ,					'''image_std'''     )     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														lowerCAmelCase__      =			self.image_processing_class.from_dict(self.image_processor_dict     )
														self.assertEqual(image_processor.size      ,					{'''height''': 20, '''width''': 20}     )
														self.assertEqual(image_processor.crop_size      ,					{'''height''': 18, '''width''': 18}     )
														self.assertEqual(image_processor.do_reduce_labels      ,					lowerCamelCase_     )

														lowerCAmelCase__      =			self.image_processing_class.from_dict(
														    self.image_processor_dict      ,					size=42      ,					crop_size=84      ,					reduce_labels=lowerCamelCase_     )
														self.assertEqual(image_processor.size      ,					{'''height''': 42, '''width''': 42}     )
														self.assertEqual(image_processor.crop_size      ,					{'''height''': 84, '''width''': 84}     )
														self.assertEqual(image_processor.do_reduce_labels      ,					lowerCamelCase_     )







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







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														# Initialize image_processing
														lowerCAmelCase__      =			self.image_processing_class(**self.image_processor_dict     )
														# create random PIL images
														lowerCAmelCase__      =			prepare_image_inputs(self.image_processor_tester      ,					equal_resolution=lowerCamelCase_     )
														for image in image_inputs:
																					self.assertIsInstance(lowerCamelCase_      ,					Image.Image     )

														# Test not batched input
														lowerCAmelCase__      =			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
														lowerCAmelCase__      =			image_processing(lowerCamelCase_      ,					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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														# Initialize image_processing
														lowerCAmelCase__      =			self.image_processing_class(**self.image_processor_dict     )
														# create random numpy tensors
														lowerCAmelCase__      =			prepare_image_inputs(self.image_processor_tester      ,					equal_resolution=lowerCamelCase_      ,					numpify=lowerCamelCase_     )
														for image in image_inputs:
																					self.assertIsInstance(lowerCamelCase_      ,					np.ndarray     )

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

														# Test not batched input
														lowerCAmelCase__      =			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
														lowerCAmelCase__      =			image_processing(lowerCamelCase_      ,					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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														# Initialize image_processing
														lowerCAmelCase__      =			self.image_processing_class(**self.image_processor_dict     )
														# create random PyTorch tensors
														lowerCAmelCase__      =			prepare_image_inputs(self.image_processor_tester      ,					equal_resolution=lowerCamelCase_      ,					torchify=lowerCamelCase_     )
														lowerCAmelCase__      =			[]
														for image in image_inputs:
																					self.assertIsInstance(lowerCamelCase_      ,					torch.Tensor     )
																					maps.append(torch.zeros(image.shape[-2:]     ).long()     )

														# Test not batched input
														lowerCAmelCase__      =			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_55     )

														# Test batched
														lowerCAmelCase__      =			image_processing(lowerCamelCase_      ,					lowerCamelCase_      ,					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_55     )

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

														lowerCAmelCase__      =			image_processing(lowerCamelCase_      ,					lowerCamelCase_      ,					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_55     )

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

														lowerCAmelCase__      =			image_processing(lowerCamelCase_      ,					lowerCamelCase_      ,					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_55     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Any:
														# Initialize image_processing
														lowerCAmelCase__      =			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
														lowerCAmelCase__     ,      lowerCAmelCase__      =			prepare_semantic_single_inputs()
														lowerCAmelCase__      =			image_processing(lowerCamelCase_      ,					lowerCamelCase_      ,					return_tensors='''pt'''     )
														self.assertTrue(encoding['''labels'''].min().item() >= 0     )
														self.assertTrue(encoding['''labels'''].max().item() <= 1_50     )

														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			image_processing(lowerCamelCase_      ,					lowerCamelCase_      ,					return_tensors='''pt'''     )
														self.assertTrue(encoding['''labels'''].min().item() >= 0     )
														self.assertTrue(encoding['''labels'''].max().item() <= 2_55     )



'''simple docstring'''






import tempfile
import unittest

import numpy as np
from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax


if is_flax_available():
		import os

		from flax.core.frozen_dict import unfreeze
		from flax.traverse_util import flatten_dict

		from transformers import FlaxBertModel

		__UpperCAmelCase					       =							'''0.12'''  # assumed parallelism: 8




@require_flax
@is_staging_test
class 	a__       (							unittest.TestCase						):





							'''simple docstring'''






							@classmethod
							def 						__SCREAMING_SNAKE_CASE							(	cls     ) ->							Any:
														lowerCAmelCase__      =			TOKEN
														HfFolder.save_token(lowerCamelCase_     )







							@classmethod
							def 						__SCREAMING_SNAKE_CASE							(	cls     ) ->							Tuple:
														try:
																					delete_repo(token=cls._token      ,					repo_id='''test-model-flax'''     )
														except HTTPError:
																					pass

														try:
																					delete_repo(token=cls._token      ,					repo_id='''valid_org/test-model-flax-org'''     )
														except HTTPError:
																					pass







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__      =			BertConfig(
														    vocab_size=99      ,					hidden_size=32      ,					num_hidden_layers=5      ,					num_attention_heads=4      ,					intermediate_size=37     )
														lowerCAmelCase__      =			FlaxBertModel(lowerCamelCase_     )
														model.push_to_hub('''test-model-flax'''      ,					use_auth_token=self._token     )

														lowerCAmelCase__      =			FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax"""     )

														lowerCAmelCase__      =			flatten_dict(unfreeze(model.params     )     )
														lowerCAmelCase__      =			flatten_dict(unfreeze(new_model.params     )     )

														for key in base_params.keys():
																					lowerCAmelCase__      =			(base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCamelCase_      ,					1e-3      ,					msg=F"""{key} not identical"""     )

														# Reset repo
														delete_repo(token=self._token      ,					repo_id='''test-model-flax'''     )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(lowerCamelCase_      ,					repo_id='''test-model-flax'''      ,					push_to_hub=lowerCamelCase_      ,					use_auth_token=self._token     )

														lowerCAmelCase__      =			FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax"""     )

														lowerCAmelCase__      =			flatten_dict(unfreeze(model.params     )     )
														lowerCAmelCase__      =			flatten_dict(unfreeze(new_model.params     )     )

														for key in base_params.keys():
																					lowerCAmelCase__      =			(base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCamelCase_      ,					1e-3      ,					msg=F"""{key} not identical"""     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														lowerCAmelCase__      =			BertConfig(
														    vocab_size=99      ,					hidden_size=32      ,					num_hidden_layers=5      ,					num_attention_heads=4      ,					intermediate_size=37     )
														lowerCAmelCase__      =			FlaxBertModel(lowerCamelCase_     )
														model.push_to_hub('''valid_org/test-model-flax-org'''      ,					use_auth_token=self._token     )

														lowerCAmelCase__      =			FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org'''     )

														lowerCAmelCase__      =			flatten_dict(unfreeze(model.params     )     )
														lowerCAmelCase__      =			flatten_dict(unfreeze(new_model.params     )     )

														for key in base_params.keys():
																					lowerCAmelCase__      =			(base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCamelCase_      ,					1e-3      ,					msg=F"""{key} not identical"""     )

														# Reset repo
														delete_repo(token=self._token      ,					repo_id='''valid_org/test-model-flax-org'''     )

														# Push to hub via save_pretrained
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(
																					    lowerCamelCase_      ,					repo_id='''valid_org/test-model-flax-org'''      ,					push_to_hub=lowerCamelCase_      ,					use_auth_token=self._token     )

														lowerCAmelCase__      =			FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org'''     )

														lowerCAmelCase__      =			flatten_dict(unfreeze(model.params     )     )
														lowerCAmelCase__      =			flatten_dict(unfreeze(new_model.params     )     )

														for key in base_params.keys():
																					lowerCAmelCase__      =			(base_params[key] - new_params[key]).sum().item()
																					self.assertLessEqual(lowerCamelCase_      ,					1e-3      ,					msg=F"""{key} not identical"""     )


def       _snake_case     (  A				,					A )		->					Optional[int]:
							lowerCAmelCase__      =			True
							lowerCAmelCase__      =			flatten_dict(modela.params )
							lowerCAmelCase__      =			flatten_dict(modela.params )
							for key in flat_params_a.keys():
														if np.sum(np.abs(flat_params_a[key] - flat_params_a[key] ) ) > 1E-4:
																					lowerCAmelCase__      =			False

							return models_are_equal






@require_flax
class 	a__       (							unittest.TestCase						):





							'''simple docstring'''






							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														lowerCAmelCase__      =			BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only'''     )
														lowerCAmelCase__      =			FlaxBertModel(lowerCamelCase_     )

														lowerCAmelCase__      =			'''bert'''
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(os.path.join(lowerCamelCase_      ,					lowerCamelCase_     )     )

																					with self.assertRaises(lowerCamelCase_     ):
																												lowerCAmelCase__      =			FlaxBertModel.from_pretrained(lowerCamelCase_     )

																					lowerCAmelCase__      =			FlaxBertModel.from_pretrained(lowerCamelCase_      ,					subfolder=lowerCamelCase_     )

														self.assertTrue(check_models_equal(lowerCamelCase_      ,					lowerCamelCase_     )     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__      =			BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only'''     )
														lowerCAmelCase__      =			FlaxBertModel(lowerCamelCase_     )

														lowerCAmelCase__      =			'''bert'''
														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(os.path.join(lowerCamelCase_      ,					lowerCamelCase_     )      ,					max_shard_size='''10KB'''     )

																					with self.assertRaises(lowerCamelCase_     ):
																												lowerCAmelCase__      =			FlaxBertModel.from_pretrained(lowerCamelCase_     )

																					lowerCAmelCase__      =			FlaxBertModel.from_pretrained(lowerCamelCase_      ,					subfolder=lowerCamelCase_     )

														self.assertTrue(check_models_equal(lowerCamelCase_      ,					lowerCamelCase_     )     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														lowerCAmelCase__      =			'''bert'''
														lowerCAmelCase__      =			'''hf-internal-testing/tiny-random-bert-subfolder'''

														with self.assertRaises(lowerCamelCase_     ):
																					lowerCAmelCase__      =			FlaxBertModel.from_pretrained(lowerCamelCase_     )

														lowerCAmelCase__      =			FlaxBertModel.from_pretrained(lowerCamelCase_      ,					subfolder=lowerCamelCase_     )

														self.assertIsNotNone(lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__      =			'''bert'''
														lowerCAmelCase__      =			'''hf-internal-testing/tiny-random-bert-sharded-subfolder'''
														with self.assertRaises(lowerCamelCase_     ):
																					lowerCAmelCase__      =			FlaxBertModel.from_pretrained(lowerCamelCase_     )

														lowerCAmelCase__      =			FlaxBertModel.from_pretrained(lowerCamelCase_      ,					subfolder=lowerCamelCase_     )

														self.assertIsNotNone(lowerCamelCase_     )