


import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
	import tensorflow as tf

if is_tensorflow_text_available():
	from transformers.models.bert import TFBertTokenizer


_lowerCAmelCase:						List[str]   				=       ['bert-base-uncased', 'bert-base-cased']
_lowerCAmelCase:						List[str]   				=       'hf-internal-testing/tiny-bert-tf-only'

if is_tf_available():




	class       lowercase_					(tf.keras.Model   ):





					def __init__(				self       ,  lowercase_)  -> List[str]:
												super().__init__()
												a__      =tokenizer
												a__      =AutoConfig.from_pretrained(lowercase_)
												a__      =TFAutoModel.from_config(lowercase_)





					def 						__UpperCamelCase    (				self       ,  lowercase_)  -> Optional[int]:
												a__      =self.tokenizer(lowercase_)
												a__      =self.bert(**lowercase_)
												return out["pooler_output"]




@require_tf
@require_tensorflow_text
class       lowercase_					(unittest.TestCase   ):





				def 						__UpperCamelCase    (				self)  -> Dict:
											super().setUp()

											a__      =[
											    BertTokenizer.from_pretrained(lowercase_) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
											]  # repeat for when fast_bert_tokenizer=false
											a__      =[TFBertTokenizer.from_pretrained(lowercase_) for checkpoint in TOKENIZER_CHECKPOINTS] + [
											    TFBertTokenizer.from_pretrained(lowercase_       ,  use_fast_bert_tokenizer=lowercase_)
											    for checkpoint in TOKENIZER_CHECKPOINTS
											]
											assert len(self.tokenizers) == len(self.tf_tokenizers)

											a__      =[
											    'This is a straightforward English test sentence.',
											    'This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.',
											    'Now we\'re going to add some Chinese: 一 二 三 一二三',
											    'And some much more rare Chinese: 齉 堃 齉堃',
											    'Je vais aussi écrire en français pour tester les accents',
											    'Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ',
											]
											a__      =list(zip(self.test_sentences       ,  self.test_sentences[::-1]))





				def 						__UpperCamelCase    (				self)  -> List[str]:
											for tokenizer, tf_tokenizer in zip(self.tokenizers       ,  self.tf_tokenizers):
																		for test_inputs in (self.test_sentences, self.paired_sentences):
																									a__      =tokenizer(lowercase_       ,  return_tensors='tf'       ,  padding='longest')
																									a__      =tf_tokenizer(lowercase_)

																									for key in python_outputs.keys():
																																self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape))
																																self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]       ,  tf.intaa) == tf_outputs[key]))





				@slow
				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											for tf_tokenizer in self.tf_tokenizers:
																		a__      =tf_tokenizer(self.paired_sentences)
																		a__      =tf_tokenizer(
																		    text=[sentence[0] for sentence in self.paired_sentences]       ,  text_pair=[sentence[1] for sentence in self.paired_sentences]       ,  )
																		for key in merged_outputs.keys():
																									self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]       ,  tf.intaa) == separated_outputs[key]))





				@slow
				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											for tf_tokenizer in self.tf_tokenizers:
																		a__      =tf.function(lowercase_)
																		for test_inputs in (self.test_sentences, self.paired_sentences):
																									a__      =tf.constant(lowercase_)
																									a__      =compiled_tokenizer(lowercase_)
																									a__      =tf_tokenizer(lowercase_)

																									for key in eager_outputs.keys():
																																self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]))





				@slow
				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											for tf_tokenizer in self.tf_tokenizers:
																		a__      =ModelToSave(tokenizer=lowercase_)
																		a__      =tf.convert_to_tensor(self.test_sentences)
																		a__      =model(lowercase_)  # Build model with some sample inputs
																		with TemporaryDirectory() as tempdir:
																									a__      =Path(lowercase_) / 'saved.model'
																									model.save(lowercase_)
																									a__      =tf.keras.models.load_model(lowercase_)
																		a__      =loaded_model(lowercase_)
																		# We may see small differences because the loaded model is compiled, so we need an epsilon for the test
																		self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output))       ,  1e-5)





from typing import Dict, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, logging


if is_torch_available():
	import torch


_lowerCAmelCase:						List[Any]   				=       logging.get_logger(__name__)




class       lowercase_					(lowercase__   ):
				snake_case	    =['pixel_values']





				def __init__(				self       ,  lowercase_ = True       ,  lowercase_ = None       ,  lowercase_ = PILImageResampling.BILINEAR       ,  lowercase_ = True       ,  lowercase_ = None       ,  lowercase_ = True       ,  lowercase_ = 1 / 255       ,  lowercase_ = True       ,  lowercase_ = None       ,  lowercase_ = None       ,  **lowercase_       ,  )  -> None:
											super().__init__(**lowercase_)
											a__      =size if size is not None else {'shortest_edge': 256}
											a__      =get_size_dict(lowercase_       ,  default_to_square=lowercase_)
											a__      =crop_size if crop_size is not None else {'height': 224, 'width': 224}
											a__      =get_size_dict(lowercase_       ,  param_name='crop_size')
											a__      =do_resize
											a__      =size
											a__      =resample
											a__      =do_center_crop
											a__      =crop_size
											a__      =do_rescale
											a__      =rescale_factor
											a__      =do_normalize
											a__      =image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
											a__      =image_std if image_std is not None else IMAGENET_STANDARD_STD





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_ = PILImageResampling.BICUBIC       ,  lowercase_ = None       ,  **lowercase_       ,  )  -> np.ndarray:
											a__      =get_size_dict(lowercase_       ,  default_to_square=lowercase_)
											if "shortest_edge" not in size:
																		raise ValueError(F"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}""")
											a__      =get_resize_output_image_size(lowercase_       ,  size=size['shortest_edge']       ,  default_to_square=lowercase_)
											return resize(lowercase_       ,  size=lowercase_       ,  resample=lowercase_       ,  data_format=lowercase_       ,  **lowercase_)





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_ = None       ,  **lowercase_       ,  )  -> np.ndarray:
											a__      =get_size_dict(lowercase_)
											if "height" not in size or "width" not in size:
																		raise ValueError(F"""The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}""")
											return center_crop(lowercase_       ,  size=(size['height'], size['width'])       ,  data_format=lowercase_       ,  **lowercase_)





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_ = None       ,  **lowercase_)  -> np.ndarray:
											return rescale(lowercase_       ,  scale=lowercase_       ,  data_format=lowercase_       ,  **lowercase_)





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_ = None       ,  **lowercase_       ,  )  -> np.ndarray:
											return normalize(lowercase_       ,  mean=lowercase_       ,  std=lowercase_       ,  data_format=lowercase_       ,  **lowercase_)





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = None       ,  lowercase_ = ChannelDimension.FIRST       ,  **lowercase_       ,  )  -> Tuple:
											a__      =do_resize if do_resize is not None else self.do_resize
											a__      =size if size is not None else self.size
											a__      =get_size_dict(lowercase_       ,  default_to_square=lowercase_)
											a__      =resample if resample is not None else self.resample
											a__      =do_center_crop if do_center_crop is not None else self.do_center_crop
											a__      =crop_size if crop_size is not None else self.crop_size
											a__      =get_size_dict(lowercase_       ,  param_name='crop_size')
											a__      =do_rescale if do_rescale is not None else self.do_rescale
											a__      =rescale_factor if rescale_factor is not None else self.rescale_factor
											a__      =do_normalize if do_normalize is not None else self.do_normalize
											a__      =image_mean if image_mean is not None else self.image_mean
											a__      =image_std if image_std is not None else self.image_std

											a__      =make_list_of_images(lowercase_)

											if not valid_images(lowercase_):
																		raise ValueError(
																		    'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
																		    'torch.Tensor, tf.Tensor or jax.ndarray.')

											if do_resize and size is None:
																		raise ValueError('Size must be specified if do_resize is True.')

											if do_center_crop and crop_size is None:
																		raise ValueError('Crop size must be specified if do_center_crop is True.')

											if do_rescale and rescale_factor is None:
																		raise ValueError('Rescale factor must be specified if do_rescale is True.')

											if do_normalize and (image_mean is None or image_std is None):
																		raise ValueError('Image mean and std must be specified if do_normalize is True.')

											# All transformations expect numpy arrays.
											a__      =[to_numpy_array(lowercase_) for image in images]

											if do_resize:
																		a__      =[self.resize(image=lowercase_       ,  size=lowercase_       ,  resample=lowercase_) for image in images]

											if do_center_crop:
																		a__      =[self.center_crop(image=lowercase_       ,  size=lowercase_) for image in images]

											if do_rescale:
																		a__      =[self.rescale(image=lowercase_       ,  scale=lowercase_) for image in images]

											if do_normalize:
																		a__      =[self.normalize(image=lowercase_       ,  mean=lowercase_       ,  std=lowercase_) for image in images]

											a__      =[to_channel_dimension_format(lowercase_       ,  lowercase_) for image in images]

											a__      ={'pixel_values': images}
											return BatchFeature(data=lowercase_       ,  tensor_type=lowercase_)





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_ = None)  -> str:
											a__      =outputs.logits

											# Resize logits and compute semantic segmentation maps
											if target_sizes is not None:
																		if len(lowercase_) != len(lowercase_):
																									raise ValueError(
																									    'Make sure that you pass in as many target sizes as the batch dimension of the logits')

																		if is_torch_tensor(lowercase_):
																									a__      =target_sizes.numpy()

																		a__      =[]

																		for idx in range(len(lowercase_)):
																									a__      =torch.nn.functional.interpolate(
																									    logits[idx].unsqueeze(dim=0)       ,  size=target_sizes[idx]       ,  mode='bilinear'       ,  align_corners=lowercase_)
																									a__      =resized_logits[0].argmax(dim=0)
																									semantic_segmentation.append(lowercase_)
											else:
																		a__      =logits.argmax(dim=1)
																		a__      =[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])]

											return semantic_segmentation

