



from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    TFFunnelBaseModel,
				    TFFunnelForMaskedLM,
				    TFFunnelForMultipleChoice,
				    TFFunnelForPreTraining,
				    TFFunnelForQuestionAnswering,
				    TFFunnelForSequenceClassification,
				    TFFunnelForTokenClassification,
				    TFFunnelModel,
				)

class 		UpperCAmelCase__			:




			'''simple docstring'''






			def __init__(   self							: List[Any]       ,       a_							: int       ,       a_							: Tuple=13       ,       a_							: int=7       ,       a_							: Any=True       ,       a_							: List[str]=True       ,       a_							: Union[str, Any]=True       ,       a_							: List[str]=True       ,       a_							: int=99       ,       a_							: int=[1, 1, 2]       ,       a_							: Optional[int]=1       ,       a_							: Optional[Any]=32       ,       a_							: int=4       ,       a_							: int=8       ,       a_							: Any=37       ,       a_							: Tuple="gelu_new"       ,       a_							: Dict=0.1       ,       a_							: List[Any]=0.1       ,       a_							: str=0.0       ,       a_							: Dict=5_12       ,       a_							: Optional[int]=3       ,       a_							: Optional[Any]=0.0_2       ,       a_							: Optional[Any]=3       ,       a_							: str=4       ,       a_							: List[str]=None       ,       a_							: Tuple=False       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Dict													=	parent
					__UpperCAmelCase    :     Dict													=	batch_size
					__UpperCAmelCase    :     Union[str, Any]													=	seq_length
					__UpperCAmelCase    :     Dict													=	is_training
					__UpperCAmelCase    :     Dict													=	use_input_mask
					__UpperCAmelCase    :     List[str]													=	use_token_type_ids
					__UpperCAmelCase    :     List[Any]													=	use_labels
					__UpperCAmelCase    :     Any													=	vocab_size
					__UpperCAmelCase    :     str													=	block_sizes
					__UpperCAmelCase    :     Optional[Any]													=	num_decoder_layers
					__UpperCAmelCase    :     List[str]													=	d_model
					__UpperCAmelCase    :     List[str]													=	n_head
					__UpperCAmelCase    :     str													=	d_head
					__UpperCAmelCase    :     str													=	d_inner
					__UpperCAmelCase    :     Any													=	hidden_act
					__UpperCAmelCase    :     Dict													=	hidden_dropout
					__UpperCAmelCase    :     List[str]													=	attention_dropout
					__UpperCAmelCase    :     Optional[int]													=	activation_dropout
					__UpperCAmelCase    :     Union[str, Any]													=	max_position_embeddings
					__UpperCAmelCase    :     Tuple													=	type_vocab_size
					__UpperCAmelCase    :     List[Any]													=	2
					__UpperCAmelCase    :     Dict													=	num_labels
					__UpperCAmelCase    :     Optional[Any]													=	num_choices
					__UpperCAmelCase    :     str													=	scope
					__UpperCAmelCase    :     Dict													=	initializer_std

					# Used in the tests to check the size of the first attention layer
					__UpperCAmelCase    :     Optional[Any]													=	n_head
					# Used in the tests to check the size of the first hidden state
					__UpperCAmelCase    :     Optional[Any]													=	self.d_model
					# Used in the tests to check the number of output hidden states/attentions
					__UpperCAmelCase    :     Optional[Any]													=	sum(self.block_sizes ) + (0 if base else self.num_decoder_layers)
					# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
					# the last hidden state of the first block (which is the first hidden state of the decoder).
					if not base:
							__UpperCAmelCase    :     Tuple													=	self.num_hidden_layers + 2




			def  snake_case__     (   self							: List[Any] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[Any]													=	ids_tensor([self.batch_size, self.seq_length]       ,       self.vocab_size )

					__UpperCAmelCase    :     Union[str, Any]													=	None
					if self.use_input_mask:
							__UpperCAmelCase    :     List[str]													=	random_attention_mask([self.batch_size, self.seq_length] )

					__UpperCAmelCase    :     Union[str, Any]													=	None
					if self.use_token_type_ids:
							__UpperCAmelCase    :     Dict													=	ids_tensor([self.batch_size, self.seq_length]       ,       self.type_vocab_size )

					__UpperCAmelCase    :     Optional[int]													=	None
					__UpperCAmelCase    :     Optional[Any]													=	None
					__UpperCAmelCase    :     List[str]													=	None
					if self.use_labels:
							__UpperCAmelCase    :     str													=	ids_tensor([self.batch_size]       ,       self.type_sequence_label_size )
							__UpperCAmelCase    :     Union[str, Any]													=	ids_tensor([self.batch_size, self.seq_length]       ,       self.num_labels )
							__UpperCAmelCase    :     Any													=	ids_tensor([self.batch_size]       ,       self.num_choices )

					__UpperCAmelCase    :     str													=	FunnelConfig(
					    vocab_size=self.vocab_size       ,       block_sizes=self.block_sizes       ,       num_decoder_layers=self.num_decoder_layers       ,       d_model=self.d_model       ,       n_head=self.n_head       ,       d_head=self.d_head       ,       d_inner=self.d_inner       ,       hidden_act=self.hidden_act       ,       hidden_dropout=self.hidden_dropout       ,       attention_dropout=self.attention_dropout       ,       activation_dropout=self.activation_dropout       ,       max_position_embeddings=self.max_position_embeddings       ,       type_vocab_size=self.type_vocab_size       ,       initializer_std=self.initializer_std       ,       )

					return (
					    config,
					    input_ids,
					    token_type_ids,
					    input_mask,
					    sequence_labels,
					    token_labels,
					    choice_labels,
					)




			def  snake_case__     (   self							: Tuple       ,       a_							: List[str]       ,       a_							: Union[str, Any]       ,       a_							: Tuple       ,       a_							: Dict       ,       a_							: int       ,       a_							: Optional[Any]       ,       a_							: List[Any]       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Dict													=	TFFunnelModel(config=a_ )
					__UpperCAmelCase    :     Optional[Any]													=	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					__UpperCAmelCase    :     Tuple													=	model(a_ )

					__UpperCAmelCase    :     Optional[int]													=	[input_ids, input_mask]
					__UpperCAmelCase    :     Dict													=	model(a_ )

					__UpperCAmelCase    :     Dict													=	model(a_ )
					self.parent.assertEqual(result.last_hidden_state.shape       ,       (self.batch_size, self.seq_length, self.d_model) )

					__UpperCAmelCase    :     List[Any]													=	False
					__UpperCAmelCase    :     int													=	TFFunnelModel(config=a_ )
					__UpperCAmelCase    :     Union[str, Any]													=	model(a_ )
					self.parent.assertEqual(result.last_hidden_state.shape       ,       (self.batch_size, self.seq_length, self.d_model) )

					__UpperCAmelCase    :     List[str]													=	False
					__UpperCAmelCase    :     str													=	TFFunnelModel(config=a_ )
					__UpperCAmelCase    :     Optional[int]													=	model(a_ )
					self.parent.assertEqual(result.last_hidden_state.shape       ,       (self.batch_size, self.seq_length, self.d_model) )




			def  snake_case__     (   self							: Dict       ,       a_							: Optional[int]       ,       a_							: Tuple       ,       a_							: List[str]       ,       a_							: str       ,       a_							: Tuple       ,       a_							: Any       ,       a_							: Tuple       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Any													=	TFFunnelBaseModel(config=a_ )
					__UpperCAmelCase    :     int													=	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					__UpperCAmelCase    :     List[str]													=	model(a_ )

					__UpperCAmelCase    :     Optional[Any]													=	[input_ids, input_mask]
					__UpperCAmelCase    :     str													=	model(a_ )

					__UpperCAmelCase    :     Optional[int]													=	model(a_ )
					self.parent.assertEqual(result.last_hidden_state.shape       ,       (self.batch_size, 2, self.d_model) )

					__UpperCAmelCase    :     int													=	False
					__UpperCAmelCase    :     Optional[Any]													=	TFFunnelBaseModel(config=a_ )
					__UpperCAmelCase    :     List[Any]													=	model(a_ )
					self.parent.assertEqual(result.last_hidden_state.shape       ,       (self.batch_size, 3, self.d_model) )

					__UpperCAmelCase    :     Union[str, Any]													=	False
					__UpperCAmelCase    :     str													=	TFFunnelBaseModel(config=a_ )
					__UpperCAmelCase    :     int													=	model(a_ )
					self.parent.assertEqual(result.last_hidden_state.shape       ,       (self.batch_size, 2, self.d_model) )




			def  snake_case__     (   self							: Any       ,       a_							: Optional[int]       ,       a_							: Dict       ,       a_							: List[str]       ,       a_							: Optional[int]       ,       a_							: Any       ,       a_							: str       ,       a_							: List[str]       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[int]													=	TFFunnelForPreTraining(config=a_ )
					__UpperCAmelCase    :     Dict													=	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					__UpperCAmelCase    :     List[str]													=	model(a_ )
					self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.seq_length) )




			def  snake_case__     (   self							: Tuple       ,       a_							: List[str]       ,       a_							: Optional[int]       ,       a_							: Tuple       ,       a_							: Optional[Any]       ,       a_							: Any       ,       a_							: Any       ,       a_							: Union[str, Any]       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     int													=	TFFunnelForMaskedLM(config=a_ )
					__UpperCAmelCase    :     List[Any]													=	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					__UpperCAmelCase    :     Dict													=	model(a_ )
					self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.seq_length, self.vocab_size) )




			def  snake_case__     (   self							: str       ,       a_							: Tuple       ,       a_							: Union[str, Any]       ,       a_							: List[str]       ,       a_							: int       ,       a_							: List[Any]       ,       a_							: Tuple       ,       a_							: Tuple       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Dict													=	self.num_labels
					__UpperCAmelCase    :     Any													=	TFFunnelForSequenceClassification(config=a_ )
					__UpperCAmelCase    :     Tuple													=	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					__UpperCAmelCase    :     List[Any]													=	model(a_ )
					self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.num_labels) )




			def  snake_case__     (   self							: int       ,       a_							: Union[str, Any]       ,       a_							: Any       ,       a_							: Dict       ,       a_							: Any       ,       a_							: Any       ,       a_							: List[str]       ,       a_							: List[Any]       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Union[str, Any]													=	self.num_choices
					__UpperCAmelCase    :     List[str]													=	TFFunnelForMultipleChoice(config=a_ )
					__UpperCAmelCase    :     Any													=	tf.tile(tf.expand_dims(a_       ,       1 )       ,       (1, self.num_choices, 1) )
					__UpperCAmelCase    :     List[Any]													=	tf.tile(tf.expand_dims(a_       ,       1 )       ,       (1, self.num_choices, 1) )
					__UpperCAmelCase    :     str													=	tf.tile(tf.expand_dims(a_       ,       1 )       ,       (1, self.num_choices, 1) )
					__UpperCAmelCase    :     Optional[int]													=	{
					    '''input_ids''': multiple_choice_inputs_ids,
					    '''attention_mask''': multiple_choice_input_mask,
					    '''token_type_ids''': multiple_choice_token_type_ids,
					}
					__UpperCAmelCase    :     Dict													=	model(a_ )
					self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.num_choices) )




			def  snake_case__     (   self							: str       ,       a_							: Any       ,       a_							: List[Any]       ,       a_							: Any       ,       a_							: Optional[int]       ,       a_							: Optional[int]       ,       a_							: int       ,       a_							: int       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[Any]													=	self.num_labels
					__UpperCAmelCase    :     int													=	TFFunnelForTokenClassification(config=a_ )
					__UpperCAmelCase    :     str													=	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					__UpperCAmelCase    :     Dict													=	model(a_ )
					self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.seq_length, self.num_labels) )




			def  snake_case__     (   self							: Optional[Any]       ,       a_							: List[Any]       ,       a_							: Optional[int]       ,       a_							: Dict       ,       a_							: Optional[int]       ,       a_							: Any       ,       a_							: Dict       ,       a_							: List[Any]       ,       ):
					'''simple docstring'''





					__UpperCAmelCase    :     int													=	TFFunnelForQuestionAnswering(config=a_ )
					__UpperCAmelCase    :     Optional[Any]													=	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					__UpperCAmelCase    :     List[str]													=	model(a_ )
					self.parent.assertEqual(result.start_logits.shape       ,       (self.batch_size, self.seq_length) )
					self.parent.assertEqual(result.end_logits.shape       ,       (self.batch_size, self.seq_length) )




			def  snake_case__     (   self							: Union[str, Any] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[int]													=	self.prepare_config_and_inputs()
					(
					    (
					    __UpperCAmelCase
					)							,					(
					    __UpperCAmelCase
					)							,					(
					    __UpperCAmelCase
					)							,					(
					    __UpperCAmelCase
					)							,					(
					    __UpperCAmelCase
					)							,					(
					    __UpperCAmelCase
					)							,					(
					    __UpperCAmelCase
					)							,					
					)    :     List[str]													=	config_and_inputs
					__UpperCAmelCase    :     Dict													=	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
					return config, inputs_dict

@require_tf
class 		UpperCAmelCase__			(			__UpperCamelCase						,__UpperCamelCase						,unittest.TestCase   ):




			'''simple docstring'''






			UpperCamelCase									=       (
			    (
			        TFFunnelModel,
			        TFFunnelForMaskedLM,
			        TFFunnelForPreTraining,
			        TFFunnelForQuestionAnswering,
			        TFFunnelForTokenClassification,
			    )
			    if is_tf_available()
			    else ()
			)
			UpperCamelCase									=       (
			    {
			        """feature-extraction""": (TFFunnelBaseModel, TFFunnelModel),
			        """fill-mask""": TFFunnelForMaskedLM,
			        """question-answering""": TFFunnelForQuestionAnswering,
			        """text-classification""": TFFunnelForSequenceClassification,
			        """token-classification""": TFFunnelForTokenClassification,
			        """zero-shot""": TFFunnelForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			UpperCamelCase									=       False
			UpperCamelCase									=       False




			def  snake_case__     (   self							: Union[str, Any] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[int]													=	TFFunnelModelTester(self )
					__UpperCAmelCase    :     Dict													=	ConfigTester(self       ,       config_class=a_ )




			def  snake_case__     (   self							: Any ):
					'''simple docstring'''





					self.config_tester.run_common_tests()




			def  snake_case__     (   self							: List[str] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Any													=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*a_ )




			def  snake_case__     (   self							: Dict ):
					'''simple docstring'''





					__UpperCAmelCase    :     str													=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*a_ )




			def  snake_case__     (   self							: Optional[int] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[int]													=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*a_ )




			def  snake_case__     (   self							: Optional[Any] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Any													=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*a_ )




			def  snake_case__     (   self							: Dict ):
					'''simple docstring'''





					__UpperCAmelCase    :     Union[str, Any]													=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*a_ )

@require_tf
class 		UpperCAmelCase__			(			__UpperCamelCase						,unittest.TestCase   ):




			'''simple docstring'''






			UpperCamelCase									=       (
			    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
			)
			UpperCamelCase									=       False
			UpperCamelCase									=       False




			def  snake_case__     (   self							: str ):
					'''simple docstring'''





					__UpperCAmelCase    :     Any													=	TFFunnelModelTester(self       ,       base=a_ )
					__UpperCAmelCase    :     str													=	ConfigTester(self       ,       config_class=a_ )




			def  snake_case__     (   self							: Tuple ):
					'''simple docstring'''





					self.config_tester.run_common_tests()




			def  snake_case__     (   self							: Union[str, Any] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Union[str, Any]													=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_base_model(*a_ )




			def  snake_case__     (   self							: Any ):
					'''simple docstring'''





					__UpperCAmelCase    :     Any													=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*a_ )




			def  snake_case__     (   self							: Any ):
					'''simple docstring'''





					__UpperCAmelCase    :     List[str]													=	self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*a_ )










import glob
import os
import random
from string import ascii_lowercase, digits

import cva
__A   			=""
__A   			=""
__A   			=""
__A   			=1  # (0 is vertical, 1 is horizontal)

def        a						(      ):







		'''simple docstring'''





		__UpperCAmelCase							,					__UpperCAmelCase    :     Union[str, Any]													=	get_dataset(_UpperCAmelCase ,   _UpperCAmelCase						)
		print('''Processing...'''						)
		__UpperCAmelCase							,					__UpperCAmelCase							,					__UpperCAmelCase    :     List[Any]													=	update_image_and_anno(_UpperCAmelCase ,   _UpperCAmelCase ,   _UpperCAmelCase						)

		for index, image in enumerate(_UpperCAmelCase						):
				# Get random string code: '7b7ad245cdff75241935e4dd860f3bad'
				__UpperCAmelCase    :     Any													=	random_chars(32						)
				__UpperCAmelCase    :     List[str]													=	paths[index].split(os.sep						)[-1].rsplit('''.''' ,   1						)[0]
				__UpperCAmelCase    :     Optional[Any]													=	f'{OUTPUT_DIR}/{file_name}_FLIP_{letter_code}'
				cva.imwrite(f'/{file_root}.jpg' ,   _UpperCAmelCase ,   [cva.IMWRITE_JPEG_QUALITY, 85]						)
				print(f'Success {index+1}/{len(_UpperCAmelCase						)} with {file_name}'						)
				__UpperCAmelCase    :     Optional[Any]													=	[]
				for anno in new_annos[index]:
						__UpperCAmelCase    :     Union[str, Any]													=	f'{anno[0]} {anno[1]} {anno[2]} {anno[3]} {anno[4]}'
						annos_list.append(_UpperCAmelCase						)
				with open(f'/{file_root}.txt' ,   '''w'''						) as outfile:
						outfile.write('''\n'''.join(line for line in annos_list						)						)

def        a						(      _UpperCAmelCase							:					str ,   _UpperCAmelCase							:					str						):







		'''simple docstring'''





		__UpperCAmelCase    :     Any													=	[]
		__UpperCAmelCase    :     Any													=	[]
		for label_file in glob.glob(os.path.join(_UpperCAmelCase ,   '''*.txt'''						)						):
				__UpperCAmelCase    :     Optional[Any]													=	label_file.split(os.sep						)[-1].rsplit('''.''' ,   1						)[0]
				with open(_UpperCAmelCase						) as in_file:
						__UpperCAmelCase    :     List[str]													=	in_file.readlines()
				__UpperCAmelCase    :     Optional[Any]													=	os.path.join(_UpperCAmelCase ,   f'{label_name}.jpg'						)

				__UpperCAmelCase    :     str													=	[]
				for obj_list in obj_lists:
						__UpperCAmelCase    :     str													=	obj_list.rstrip('''\n'''						).split(''' '''						)
						boxes.append(
						    [
						        int(obj[0]						),
						        float(obj[1]						),
						        float(obj[2]						),
						        float(obj[3]						),
						        float(obj[4]						),
						    ]						)
				if not boxes:
						continue
				img_paths.append(_UpperCAmelCase						)
				labels.append(_UpperCAmelCase						)
		return img_paths, labels

def        a						(      _UpperCAmelCase							:					list ,   _UpperCAmelCase							:					list ,   _UpperCAmelCase							:					int = 1						):







		'''simple docstring'''





		__UpperCAmelCase    :     Dict													=	[]
		__UpperCAmelCase    :     Optional[Any]													=	[]
		__UpperCAmelCase    :     Any													=	[]
		for idx in range(len(_UpperCAmelCase						)						):
				__UpperCAmelCase    :     Tuple													=	[]
				__UpperCAmelCase    :     List[Any]													=	img_list[idx]
				path_list.append(_UpperCAmelCase						)
				__UpperCAmelCase    :     str													=	anno_list[idx]
				__UpperCAmelCase    :     str													=	cva.imread(_UpperCAmelCase						)
				if flip_type == 1:
						__UpperCAmelCase    :     Any													=	cva.flip(_UpperCAmelCase ,   _UpperCAmelCase						)
						for bbox in img_annos:
								__UpperCAmelCase    :     List[str]													=	1 - bbox[1]
								new_annos.append([bbox[0], x_center_new, bbox[2], bbox[3], bbox[4]]						)
				elif flip_type == 0:
						__UpperCAmelCase    :     Any													=	cva.flip(_UpperCAmelCase ,   _UpperCAmelCase						)
						for bbox in img_annos:
								__UpperCAmelCase    :     Union[str, Any]													=	1 - bbox[2]
								new_annos.append([bbox[0], bbox[1], y_center_new, bbox[3], bbox[4]]						)
				new_annos_lists.append(_UpperCAmelCase						)
				new_imgs_list.append(_UpperCAmelCase						)
		return new_imgs_list, new_annos_lists, path_list

def        a						(      _UpperCAmelCase							:					int = 32						):







		'''simple docstring'''





		assert number_char > 1, "The number of character should greater than 1"
		__UpperCAmelCase    :     Union[str, Any]													=	ascii_lowercase + digits
		return "".join(random.choice(_UpperCAmelCase						) for _ in range(_UpperCAmelCase						)						)


if __name__ == "__main__":
				main()
				print("DONE ✅")





