




"""simple docstring"""






from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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 (
			    TFConvBertForMaskedLM,
			    TFConvBertForMultipleChoice,
			    TFConvBertForQuestionAnswering,
			    TFConvBertForSequenceClassification,
			    TFConvBertForTokenClassification,
			    TFConvBertModel,
			)






class lowerCAmelCase__			:






			'''simple docstring'''



			def __init__(						self						,   lowercase						,   lowercase=13						,   lowercase=7						,   lowercase=True						,   lowercase=True						,   lowercase=True						,   lowercase=True						,   lowercase=99						,   lowercase=32						,   lowercase=2						,   lowercase=4						,   lowercase=37						,   lowercase="gelu"						,   lowercase=0.1						,   lowercase=0.1						,   lowercase=512						,   lowercase=16						,   lowercase=2						,   lowercase=0.02						,   lowercase=3						,   lowercase=4						,   lowercase=None						,   ):
							_lowerCamelCase  :    List[Any]					=					parent
							_lowerCamelCase  :    Tuple					=					13
							_lowerCamelCase  :    Optional[Any]					=					7
							_lowerCamelCase  :    Dict					=					True
							_lowerCamelCase  :    Optional[int]					=					True
							_lowerCamelCase  :    Optional[Any]					=					True
							_lowerCamelCase  :    Tuple					=					True
							_lowerCamelCase  :    Union[str, Any]					=					99
							_lowerCamelCase  :    int					=					384
							_lowerCamelCase  :    List[Any]					=					2
							_lowerCamelCase  :    Optional[int]					=					4
							_lowerCamelCase  :    Optional[int]					=					37
							_lowerCamelCase  :    Dict					=					'gelu'
							_lowerCamelCase  :    Union[str, Any]					=					0.1
							_lowerCamelCase  :    Union[str, Any]					=					0.1
							_lowerCamelCase  :    Optional[int]					=					512
							_lowerCamelCase  :    List[str]					=					16
							_lowerCamelCase  :    Tuple					=					2
							_lowerCamelCase  :    Dict					=					0.02
							_lowerCamelCase  :    str					=					3
							_lowerCamelCase  :    List[Any]					=					4
							_lowerCamelCase  :    Tuple					=					128
							_lowerCamelCase  :    Optional[Any]					=					2
							_lowerCamelCase  :    Any					=					9
							_lowerCamelCase  :    List[str]					=					1
							_lowerCamelCase  :    Optional[Any]					=					None
			def       A_					(						self						):
							_lowerCamelCase  :    Union[str, Any]					=					ids_tensor([self.batch_size, self.seq_length]						,   self.vocab_size						)

							_lowerCamelCase  :    str					=					None
							if self.use_input_mask:
											_lowerCamelCase  :    Optional[Any]					=					random_attention_mask([self.batch_size, self.seq_length]						)

							_lowerCamelCase  :    Any					=					None
							if self.use_token_type_ids:
											_lowerCamelCase  :    Any					=					ids_tensor([self.batch_size, self.seq_length]						,   self.type_vocab_size						)

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

							_lowerCamelCase  :    int					=					ConvBertConfig(
							    vocab_size=self.vocab_size						,   hidden_size=self.hidden_size						,   num_hidden_layers=self.num_hidden_layers						,   num_attention_heads=self.num_attention_heads						,   intermediate_size=self.intermediate_size						,   hidden_act=self.hidden_act						,   hidden_dropout_prob=self.hidden_dropout_prob						,   attention_probs_dropout_prob=self.attention_probs_dropout_prob						,   max_position_embeddings=self.max_position_embeddings						,   type_vocab_size=self.type_vocab_size						,   initializer_range=self.initializer_range						,   return_dict=lowercase						,   )

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
			def       A_					(						self						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						):
							_lowerCamelCase  :    Dict					=					TFConvBertModel(config=lowercase						)
							_lowerCamelCase  :    List[Any]					=					{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

							_lowerCamelCase  :    List[Any]					=					[input_ids, input_mask]
							_lowerCamelCase  :    Optional[Any]					=					model(lowercase						)

							_lowerCamelCase  :    str					=					model(lowercase						)

							self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, self.seq_length, self.hidden_size)						)
			def       A_					(						self						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						):
							_lowerCamelCase  :    Union[str, Any]					=					TFConvBertForMaskedLM(config=lowercase						)
							_lowerCamelCase  :    Dict					=					{
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							    'token_type_ids': token_type_ids,
							}
							_lowerCamelCase  :    str					=					model(lowercase						)
							self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.seq_length, self.vocab_size)						)
			def       A_					(						self						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						):
							_lowerCamelCase  :    Optional[int]					=					self.num_labels
							_lowerCamelCase  :    List[Any]					=					TFConvBertForSequenceClassification(config=lowercase						)
							_lowerCamelCase  :    Any					=					{
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							    'token_type_ids': token_type_ids,
							}

							_lowerCamelCase  :    List[Any]					=					model(lowercase						)
							self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.num_labels)						)
			def       A_					(						self						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						):
							_lowerCamelCase  :    Optional[Any]					=					self.num_choices
							_lowerCamelCase  :    Tuple					=					TFConvBertForMultipleChoice(config=lowercase						)
							_lowerCamelCase  :    Dict					=					tf.tile(tf.expand_dims(lowercase						,   1						)						,   (1, self.num_choices, 1)						)
							_lowerCamelCase  :    Tuple					=					tf.tile(tf.expand_dims(lowercase						,   1						)						,   (1, self.num_choices, 1)						)
							_lowerCamelCase  :    int					=					tf.tile(tf.expand_dims(lowercase						,   1						)						,   (1, self.num_choices, 1)						)
							_lowerCamelCase  :    Optional[int]					=					{
							    'input_ids': multiple_choice_inputs_ids,
							    'attention_mask': multiple_choice_input_mask,
							    'token_type_ids': multiple_choice_token_type_ids,
							}
							_lowerCamelCase  :    Dict					=					model(lowercase						)
							self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.num_choices)						)
			def       A_					(						self						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						):
							_lowerCamelCase  :    Any					=					self.num_labels
							_lowerCamelCase  :    List[Any]					=					TFConvBertForTokenClassification(config=lowercase						)
							_lowerCamelCase  :    Optional[Any]					=					{
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							    'token_type_ids': token_type_ids,
							}
							_lowerCamelCase  :    Dict					=					model(lowercase						)
							self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.seq_length, self.num_labels)						)
			def       A_					(						self						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						,   lowercase						):
							_lowerCamelCase  :    Dict					=					TFConvBertForQuestionAnswering(config=lowercase						)
							_lowerCamelCase  :    Optional[Any]					=					{
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							    'token_type_ids': token_type_ids,
							}

							_lowerCamelCase  :    Union[str, Any]					=					model(lowercase						)
							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       A_					(						self						):
							_lowerCamelCase  :    Tuple					=					self.prepare_config_and_inputs()
							(
							    (
							    _lowerCamelCase
							),				(
							    _lowerCamelCase
							),				(
							    _lowerCamelCase
							),				(
							    _lowerCamelCase
							),				(
							    _lowerCamelCase
							),				(
							    _lowerCamelCase
							),				(
							    _lowerCamelCase
							),				
							)  :    List[str]					=					config_and_inputs
							_lowerCamelCase  :    Optional[int]					=					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
							return config, inputs_dict






@require_tf
class lowerCAmelCase__			(  lowercase,			lowercase,			unittest.TestCase		):






			'''simple docstring'''



			lowerCamelCase__   =    (
			    (
			        TFConvBertModel,
			        TFConvBertForMaskedLM,
			        TFConvBertForQuestionAnswering,
			        TFConvBertForSequenceClassification,
			        TFConvBertForTokenClassification,
			        TFConvBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			lowerCamelCase__   =    (
			    {
			        """feature-extraction""": TFConvBertModel,
			        """fill-mask""": TFConvBertForMaskedLM,
			        """question-answering""": TFConvBertForQuestionAnswering,
			        """text-classification""": TFConvBertForSequenceClassification,
			        """token-classification""": TFConvBertForTokenClassification,
			        """zero-shot""": TFConvBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			lowerCamelCase__   =    False
			lowerCamelCase__   =    False
			lowerCamelCase__   =    False
			def       A_					(						self						):
							_lowerCamelCase  :    Tuple					=					TFConvBertModelTester(self						)
							_lowerCamelCase  :    Dict					=					ConfigTester(self						,   config_class=lowercase						,   hidden_size=37						)
			def       A_					(						self						):
							self.config_tester.run_common_tests()
			def       A_					(						self						):
							_lowerCamelCase  :    Optional[int]					=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase						)
			def       A_					(						self						):
							_lowerCamelCase  :    Tuple					=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*lowercase						)
			def       A_					(						self						):
							_lowerCamelCase  :    Tuple					=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*lowercase						)
			def       A_					(						self						):
							_lowerCamelCase  :    int					=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowercase						)
			def       A_					(						self						):
							_lowerCamelCase  :    Optional[Any]					=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowercase						)
			def       A_					(						self						):
							_lowerCamelCase  :    str					=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowercase						)
			@slow
			def       A_					(						self						):
							_lowerCamelCase,				_lowerCamelCase  :    Any					=					self.model_tester.prepare_config_and_inputs_for_common()
							_lowerCamelCase  :    List[Any]					=					True
							_lowerCamelCase  :    Dict					=					True

							if hasattr(lowercase						,   'use_cache'						):
											_lowerCamelCase  :    Optional[Any]					=					True

							_lowerCamelCase  :    List[Any]					=					getattr(self.model_tester						,   'encoder_seq_length'						,   self.model_tester.seq_length						)
							_lowerCamelCase  :    str					=					getattr(self.model_tester						,   'key_length'						,   lowercase						)

							for model_class in self.all_model_classes:
											_lowerCamelCase  :    Union[str, Any]					=					self._prepare_for_class(lowercase						,   lowercase						)
											_lowerCamelCase  :    Optional[Any]					=					model_class(lowercase						)
											_lowerCamelCase  :    Dict					=					len(model(lowercase						)						)

											with tempfile.TemporaryDirectory() as tmpdirname:
															model.save_pretrained(lowercase						,   saved_model=lowercase						)
															_lowerCamelCase  :    Optional[Any]					=					os.path.join(lowercase						,   'saved_model'						,   '1'						)
															_lowerCamelCase  :    int					=					tf.keras.models.load_model(lowercase						)
															_lowerCamelCase  :    Optional[int]					=					model(lowercase						)

															if self.is_encoder_decoder:
																			_lowerCamelCase  :    List[Any]					=					outputs['encoder_hidden_states']
																			_lowerCamelCase  :    Any					=					outputs['encoder_attentions']
															else:
																			_lowerCamelCase  :    Tuple					=					outputs['hidden_states']
																			_lowerCamelCase  :    Any					=					outputs['attentions']

															self.assertEqual(len(lowercase						)						,   lowercase						)

															_lowerCamelCase  :    Tuple					=					getattr(
															    self.model_tester						,   'expected_num_hidden_layers'						,   self.model_tester.num_hidden_layers + 1						)

															self.assertEqual(len(lowercase						)						,   lowercase						)
															self.assertListEqual(
															    list(output_hidden_states[0].shape[-2:]						)						,   [self.model_tester.seq_length, self.model_tester.hidden_size]						,   )

															self.assertEqual(len(lowercase						)						,   self.model_tester.num_hidden_layers						)
															self.assertListEqual(
															    list(output_attentions[0].shape[-3:]						)						,   [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]						,   )
			@slow
			def       A_					(						self						):
							_lowerCamelCase  :    List[Any]					=					TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
							self.assertIsNotNone(lowercase						)






			def       A_					(						self						):
							_lowerCamelCase,				_lowerCamelCase  :    str					=					self.model_tester.prepare_config_and_inputs_for_common()
							_lowerCamelCase  :    Any					=					True
							_lowerCamelCase  :    List[str]					=					getattr(self.model_tester						,   'decoder_seq_length'						,   self.model_tester.seq_length						)
							_lowerCamelCase  :    Optional[Any]					=					getattr(self.model_tester						,   'encoder_seq_length'						,   self.model_tester.seq_length						)
							_lowerCamelCase  :    Optional[Any]					=					getattr(self.model_tester						,   'key_length'						,   lowercase						)
							_lowerCamelCase  :    Dict					=					getattr(self.model_tester						,   'key_length'						,   lowercase						)

							def check_decoder_attentions_output(lowercase						):
											_lowerCamelCase  :    Dict					=					len(lowercase						)
											self.assertEqual(out_len % 2						,   0						)
											_lowerCamelCase  :    List[Any]					=					outputs.decoder_attentions
											self.assertEqual(len(lowercase						)						,   self.model_tester.num_hidden_layers						)
											self.assertListEqual(
											    list(decoder_attentions[0].shape[-3:]						)						,   [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]						,   )

							def check_encoder_attentions_output(lowercase						):
											_lowerCamelCase  :    Optional[Any]					=					[
											    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
											]
											self.assertEqual(len(lowercase						)						,   self.model_tester.num_hidden_layers						)
											self.assertListEqual(
											    list(attentions[0].shape[-3:]						)						,   [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]						,   )

							for model_class in self.all_model_classes:
											_lowerCamelCase  :    Optional[Any]					=					True
											_lowerCamelCase  :    str					=					False
											_lowerCamelCase  :    Union[str, Any]					=					model_class(lowercase						)
											_lowerCamelCase  :    Dict					=					model(self._prepare_for_class(lowercase						,   lowercase						)						)
											_lowerCamelCase  :    int					=					len(lowercase						)
											self.assertEqual(config.output_hidden_states						,   lowercase						)
											check_encoder_attentions_output(lowercase						)

											if self.is_encoder_decoder:
															_lowerCamelCase  :    Optional[int]					=					model_class(lowercase						)
															_lowerCamelCase  :    List[Any]					=					model(self._prepare_for_class(lowercase						,   lowercase						)						)
															self.assertEqual(config.output_hidden_states						,   lowercase						)
															check_decoder_attentions_output(lowercase						)

											# Check that output attentions can also be changed via the config
											del inputs_dict["output_attentions"]
											_lowerCamelCase  :    List[Any]					=					True
											_lowerCamelCase  :    List[Any]					=					model_class(lowercase						)
											_lowerCamelCase  :    List[str]					=					model(self._prepare_for_class(lowercase						,   lowercase						)						)
											self.assertEqual(config.output_hidden_states						,   lowercase						)
											check_encoder_attentions_output(lowercase						)

											# Check attention is always last and order is fine
											_lowerCamelCase  :    Dict					=					True
											_lowerCamelCase  :    str					=					True
											_lowerCamelCase  :    int					=					model_class(lowercase						)
											_lowerCamelCase  :    str					=					model(self._prepare_for_class(lowercase						,   lowercase						)						)

											self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)						,   len(lowercase						)						)
											self.assertEqual(model.config.output_hidden_states						,   lowercase						)
											check_encoder_attentions_output(lowercase						)






@require_tf
class lowerCAmelCase__			(  unittest.TestCase		):






			'''simple docstring'''



			@slow
			def       A_					(						self						):
							_lowerCamelCase  :    Any					=					TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
							_lowerCamelCase  :    List[Any]					=					tf.constant([[0, 1, 2, 3, 4, 5]]						)
							_lowerCamelCase  :    str					=					model(lowercase						)[0]

							_lowerCamelCase  :    Tuple					=					[1, 6, 768]
							self.assertEqual(output.shape						,   lowercase						)

							_lowerCamelCase  :    Optional[Any]					=					tf.constant(
							    [
							        [
							            [-0.03_47_54_93, -0.4_68_60_34, -0.30_63_88_32],
							            [0.22_63_72_48, -0.26_98_86_46, -0.7_42_34_24],
							            [0.10_32_48_68, -0.45_01_35_08, -0.58_28_07_84],
							        ]
							    ]						)
							tf.debugging.assert_near(output[:, :3, :3]						,   lowercase						,   atol=1E-4						)





"""simple docstring"""






import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
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 DetrImageProcessor






class lowerCAmelCase__			(  unittest.TestCase		):






			'''simple docstring'''



			def __init__(						self						,   lowercase						,   lowercase=7						,   lowercase=3						,   lowercase=30						,   lowercase=400						,   lowercase=True						,   lowercase=None						,   lowercase=True						,   lowercase=1 / 255						,   lowercase=True						,   lowercase=[0.5, 0.5, 0.5]						,   lowercase=[0.5, 0.5, 0.5]						,   lowercase=True						,   ):
							# by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
							_lowerCamelCase  :    Any					=					size if size is not None else {'shortest_edge': 18, 'longest_edge': 1333}
							_lowerCamelCase  :    Optional[int]					=					parent
							_lowerCamelCase  :    Any					=					batch_size
							_lowerCamelCase  :    int					=					num_channels
							_lowerCamelCase  :    Union[str, Any]					=					min_resolution
							_lowerCamelCase  :    List[Any]					=					max_resolution
							_lowerCamelCase  :    Union[str, Any]					=					do_resize
							_lowerCamelCase  :    str					=					size
							_lowerCamelCase  :    List[str]					=					do_rescale
							_lowerCamelCase  :    Optional[int]					=					rescale_factor
							_lowerCamelCase  :    str					=					do_normalize
							_lowerCamelCase  :    Optional[int]					=					image_mean
							_lowerCamelCase  :    Tuple					=					image_std
							_lowerCamelCase  :    Tuple					=					do_pad
			def       A_					(						self						):
							return {
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "do_rescale": self.do_rescale,
							    "rescale_factor": self.rescale_factor,
							    "do_normalize": self.do_normalize,
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_pad": self.do_pad,
							}






			def       A_					(						self						,   lowercase						,   lowercase=False						):
							if not batched:
											_lowerCamelCase  :    Union[str, Any]					=					image_inputs[0]
											if isinstance(lowercase						,   Image.Image						):
															_lowerCamelCase,				_lowerCamelCase  :    Optional[Any]					=					image.size
											else:
															_lowerCamelCase,				_lowerCamelCase  :    List[str]					=					image.shape[1], image.shape[2]
											if w < h:
															_lowerCamelCase  :    List[Any]					=					int(self.size['shortest_edge'] * h / w						)
															_lowerCamelCase  :    Union[str, Any]					=					self.size['shortest_edge']
											elif w > h:
															_lowerCamelCase  :    List[str]					=					self.size['shortest_edge']
															_lowerCamelCase  :    Optional[Any]					=					int(self.size['shortest_edge'] * w / h						)
											else:
															_lowerCamelCase  :    int					=					self.size['shortest_edge']
															_lowerCamelCase  :    Optional[Any]					=					self.size['shortest_edge']

							else:
											_lowerCamelCase  :    Union[str, Any]					=					[]
											for image in image_inputs:
															_lowerCamelCase,				_lowerCamelCase  :    Tuple					=					self.get_expected_values([image]						)
															expected_values.append((expected_height, expected_width)						)
											_lowerCamelCase  :    Tuple					=					max(lowercase						,   key=lambda lowercase						: item[0]						)[0]
											_lowerCamelCase  :    Any					=					max(lowercase						,   key=lambda lowercase						: item[1]						)[1]

							return expected_height, expected_width






@require_torch
@require_vision
class lowerCAmelCase__			(  lowercase,			unittest.TestCase		):






			'''simple docstring'''



			lowerCamelCase__   =    DetrImageProcessor if is_vision_available() else None
			def       A_					(						self						):
							_lowerCamelCase  :    int					=					DetrImageProcessingTester(self						)
			@property
			def       A_					(						self						):
							return self.image_processor_tester.prepare_image_processor_dict()
			def       A_					(						self						):
							_lowerCamelCase  :    str					=					self.image_processing_class(**self.image_processor_dict						)
							self.assertTrue(hasattr(lowercase						,   'image_mean'						)						)
							self.assertTrue(hasattr(lowercase						,   'image_std'						)						)
							self.assertTrue(hasattr(lowercase						,   'do_normalize'						)						)
							self.assertTrue(hasattr(lowercase						,   'do_rescale'						)						)
							self.assertTrue(hasattr(lowercase						,   'rescale_factor'						)						)
							self.assertTrue(hasattr(lowercase						,   'do_resize'						)						)
							self.assertTrue(hasattr(lowercase						,   'size'						)						)
							self.assertTrue(hasattr(lowercase						,   'do_pad'						)						)
			def       A_					(						self						):
							_lowerCamelCase  :    List[str]					=					self.image_processing_class.from_dict(self.image_processor_dict						)
							self.assertEqual(image_processor.size						,   {'shortest_edge': 18, 'longest_edge': 1333}						)
							self.assertEqual(image_processor.do_pad						,   lowercase						)

							_lowerCamelCase  :    Union[str, Any]					=					self.image_processing_class.from_dict(
							    self.image_processor_dict						,   size=42						,   max_size=84						,   pad_and_return_pixel_mask=lowercase						)
							self.assertEqual(image_processor.size						,   {'shortest_edge': 42, 'longest_edge': 84}						)
							self.assertEqual(image_processor.do_pad						,   lowercase						)
			def       A_					(						self						):
							pass
			def       A_					(						self						):
							# Initialize image_processing
							_lowerCamelCase  :    Optional[int]					=					self.image_processing_class(**self.image_processor_dict						)
							# create random PIL images
							_lowerCamelCase  :    List[str]					=					prepare_image_inputs(self.image_processor_tester						,   equal_resolution=lowercase						)
							for image in image_inputs:
											self.assertIsInstance(lowercase						,   Image.Image						)

							# Test not batched input
							_lowerCamelCase  :    Optional[int]					=					image_processing(image_inputs[0]						,   return_tensors='pt'						).pixel_values

							_lowerCamelCase,				_lowerCamelCase  :    List[Any]					=					self.image_processor_tester.get_expected_values(lowercase						)

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

							# Test batched
							_lowerCamelCase,				_lowerCamelCase  :    str					=					self.image_processor_tester.get_expected_values(lowercase						,   batched=lowercase						)

							_lowerCamelCase  :    Union[str, Any]					=					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,
							        expected_height,
							        expected_width,
							    )						,   )
			def       A_					(						self						):
							# Initialize image_processing
							_lowerCamelCase  :    Optional[int]					=					self.image_processing_class(**self.image_processor_dict						)
							# create random numpy tensors
							_lowerCamelCase  :    Union[str, Any]					=					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
							_lowerCamelCase  :    Optional[int]					=					image_processing(image_inputs[0]						,   return_tensors='pt'						).pixel_values

							_lowerCamelCase,				_lowerCamelCase  :    List[Any]					=					self.image_processor_tester.get_expected_values(lowercase						)

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

							# Test batched
							_lowerCamelCase  :    str					=					image_processing(lowercase						,   return_tensors='pt'						).pixel_values

							_lowerCamelCase,				_lowerCamelCase  :    Union[str, Any]					=					self.image_processor_tester.get_expected_values(lowercase						,   batched=lowercase						)

							self.assertEqual(
							    encoded_images.shape						,   (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    )						,   )
			def       A_					(						self						):
							# Initialize image_processing
							_lowerCamelCase  :    List[str]					=					self.image_processing_class(**self.image_processor_dict						)
							# create random PyTorch tensors
							_lowerCamelCase  :    Optional[Any]					=					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
							_lowerCamelCase  :    int					=					image_processing(image_inputs[0]						,   return_tensors='pt'						).pixel_values

							_lowerCamelCase,				_lowerCamelCase  :    Optional[Any]					=					self.image_processor_tester.get_expected_values(lowercase						)

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

							# Test batched
							_lowerCamelCase  :    Dict					=					image_processing(lowercase						,   return_tensors='pt'						).pixel_values

							_lowerCamelCase,				_lowerCamelCase  :    List[str]					=					self.image_processor_tester.get_expected_values(lowercase						,   batched=lowercase						)

							self.assertEqual(
							    encoded_images.shape						,   (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    )						,   )
			@slow
			def       A_					(						self						):
							# prepare image and target
							_lowerCamelCase  :    Tuple					=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
							with open('./tests/fixtures/tests_samples/COCO/coco_annotations.txt'						,   'r'						) as f:
											_lowerCamelCase  :    int					=					json.loads(f.read()						)

							_lowerCamelCase  :    str					=					{'image_id': 39769, 'annotations': target}

							# encode them
							_lowerCamelCase  :    str					=					DetrImageProcessor.from_pretrained('facebook/detr-resnet-50'						)
							_lowerCamelCase  :    int					=					image_processing(images=lowercase						,   annotations=lowercase						,   return_tensors='pt'						)

							# verify pixel values
							_lowerCamelCase  :    Optional[Any]					=					torch.Size([1, 3, 800, 1066]						)
							self.assertEqual(encoding['pixel_values'].shape						,   lowercase						)

							_lowerCamelCase  :    List[str]					=					torch.tensor([0.27_96, 0.31_38, 0.34_81]						)
							self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3]						,   lowercase						,   atol=1E-4						)						)

							# verify area
							_lowerCamelCase  :    Dict					=					torch.tensor([58_87.96_00, 1_12_50.20_61, 48_93_53.84_38, 83_71_22.75_00, 14_79_67.51_56, 16_57_32.34_38]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['area']						,   lowercase						)						)
							# verify boxes
							_lowerCamelCase  :    int					=					torch.Size([6, 4]						)
							self.assertEqual(encoding['labels'][0]['boxes'].shape						,   lowercase						)
							_lowerCamelCase  :    Tuple					=					torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]						,   lowercase						,   atol=1E-3						)						)
							# verify image_id
							_lowerCamelCase  :    List[Any]					=					torch.tensor([39769]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']						,   lowercase						)						)
							# verify is_crowd
							_lowerCamelCase  :    str					=					torch.tensor([0, 0, 0, 0, 0, 0]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']						,   lowercase						)						)
							# verify class_labels
							_lowerCamelCase  :    Dict					=					torch.tensor([75, 75, 63, 65, 17, 17]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']						,   lowercase						)						)
							# verify orig_size
							_lowerCamelCase  :    Optional[int]					=					torch.tensor([480, 640]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']						,   lowercase						)						)
							# verify size
							_lowerCamelCase  :    List[str]					=					torch.tensor([800, 1066]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['size']						,   lowercase						)						)






			@slow
			def       A_					(						self						):
							# prepare image, target and masks_path
							_lowerCamelCase  :    Optional[int]					=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
							with open('./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'						,   'r'						) as f:
											_lowerCamelCase  :    List[Any]					=					json.loads(f.read()						)

							_lowerCamelCase  :    str					=					{'file_name': '000000039769.png', 'image_id': 39769, 'segments_info': target}

							_lowerCamelCase  :    List[Any]					=					pathlib.Path('./tests/fixtures/tests_samples/COCO/coco_panoptic'						)

							# encode them
							_lowerCamelCase  :    List[Any]					=					DetrImageProcessor.from_pretrained('facebook/detr-resnet-50-panoptic'						)
							_lowerCamelCase  :    Dict					=					image_processing(images=lowercase						,   annotations=lowercase						,   masks_path=lowercase						,   return_tensors='pt'						)

							# verify pixel values
							_lowerCamelCase  :    Optional[int]					=					torch.Size([1, 3, 800, 1066]						)
							self.assertEqual(encoding['pixel_values'].shape						,   lowercase						)

							_lowerCamelCase  :    List[Any]					=					torch.tensor([0.27_96, 0.31_38, 0.34_81]						)
							self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3]						,   lowercase						,   atol=1E-4						)						)

							# verify area
							_lowerCamelCase  :    Optional[Any]					=					torch.tensor([14_79_79.68_75, 16_55_27.04_69, 48_46_38.59_38, 1_12_92.93_75, 58_79.65_62, 76_34.11_47]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['area']						,   lowercase						)						)
							# verify boxes
							_lowerCamelCase  :    Dict					=					torch.Size([6, 4]						)
							self.assertEqual(encoding['labels'][0]['boxes'].shape						,   lowercase						)
							_lowerCamelCase  :    Union[str, Any]					=					torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]						,   lowercase						,   atol=1E-3						)						)
							# verify image_id
							_lowerCamelCase  :    List[str]					=					torch.tensor([39769]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']						,   lowercase						)						)
							# verify is_crowd
							_lowerCamelCase  :    Optional[Any]					=					torch.tensor([0, 0, 0, 0, 0, 0]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']						,   lowercase						)						)
							# verify class_labels
							_lowerCamelCase  :    List[str]					=					torch.tensor([17, 17, 63, 75, 75, 93]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']						,   lowercase						)						)
							# verify masks
							_lowerCamelCase  :    List[str]					=					822873
							self.assertEqual(encoding['labels'][0]['masks'].sum().item()						,   lowercase						)
							# verify orig_size
							_lowerCamelCase  :    Optional[Any]					=					torch.tensor([480, 640]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']						,   lowercase						)						)
							# verify size
							_lowerCamelCase  :    List[str]					=					torch.tensor([800, 1066]						)
							self.assertTrue(torch.allclose(encoding['labels'][0]['size']						,   lowercase						)						)