






from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
     from transformers import (
         TFBertModel,
         TFCLIPVisionModel,
         TFDeiTModel,
         TFRobertaModel,
         TFVisionTextDualEncoderModel,
         TFViTModel,
         VisionTextDualEncoderConfig,
     )

if is_vision_available():
     from PIL import Image

     from transformers import VisionTextDualEncoderProcessor







def 		_UpperCamelCase				(      UpperCamelCase_    :   Tuple						)					->				List[str]:

      """simple docstring"""



      if isinstance(UpperCamelCase_      ,    collections.abc.Iterable						):
            return x
      return (x, x)


@require_tf
class       __SCREAMING_SNAKE_CASE   :






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase				):






            """simple docstring"""
            pass






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            pass






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            pass






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase=None       ,  **_UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE				)

            lowerCAmelCase__        =  TFVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE				)

            lowerCAmelCase__        =  model(input_ids=_SCREAMING_SNAKE_CASE       ,  pixel_values=_SCREAMING_SNAKE_CASE       ,  attention_mask=_SCREAMING_SNAKE_CASE				)

            self.assertEqual(output['text_embeds'].shape       ,  (input_ids.shape[0], config.projection_dim)				)
            self.assertEqual(output['image_embeds'].shape       ,  (pixel_values.shape[0], config.projection_dim)				)






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase=None       ,  **_UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  self.get_vision_text_model(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE				)
            lowerCAmelCase__        =  TFVisionTextDualEncoderModel(vision_model=_SCREAMING_SNAKE_CASE       ,  text_model=_SCREAMING_SNAKE_CASE				)

            lowerCAmelCase__        =  model(input_ids=_SCREAMING_SNAKE_CASE       ,  pixel_values=_SCREAMING_SNAKE_CASE       ,  attention_mask=_SCREAMING_SNAKE_CASE				)

            self.assertEqual(output['text_embeds'].shape       ,  (input_ids.shape[0], model.config.projection_dim)				)
            self.assertEqual(output['image_embeds'].shape       ,  (pixel_values.shape[0], model.config.projection_dim)				)






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase=None       ,  **_UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  self.get_vision_text_model(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE				)
            lowerCAmelCase__        =  {"""vision_model""": vision_model, """text_model""": text_model}
            lowerCAmelCase__        =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE				)

            lowerCAmelCase__        =  model(input_ids=_SCREAMING_SNAKE_CASE       ,  pixel_values=_SCREAMING_SNAKE_CASE       ,  attention_mask=_SCREAMING_SNAKE_CASE				)

            self.assertEqual(output['text_embeds'].shape       ,  (input_ids.shape[0], model.config.projection_dim)				)
            self.assertEqual(output['image_embeds'].shape       ,  (pixel_values.shape[0], model.config.projection_dim)				)






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase=None       ,  **_UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  self.get_vision_text_model(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE				)
            lowerCAmelCase__        =  TFVisionTextDualEncoderModel(vision_model=_SCREAMING_SNAKE_CASE       ,  text_model=_SCREAMING_SNAKE_CASE				)

            lowerCAmelCase__        =  model(input_ids=_SCREAMING_SNAKE_CASE       ,  pixel_values=_SCREAMING_SNAKE_CASE       ,  attention_mask=_SCREAMING_SNAKE_CASE				)
            lowerCAmelCase__        =  output[0].numpy()

            with tempfile.TemporaryDirectory() as tmpdirname:
                  model.save_pretrained(_SCREAMING_SNAKE_CASE				)
                  lowerCAmelCase__        =  TFVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE				)

                  lowerCAmelCase__        =  model(input_ids=_SCREAMING_SNAKE_CASE       ,  pixel_values=_SCREAMING_SNAKE_CASE       ,  attention_mask=_SCREAMING_SNAKE_CASE				)
                  lowerCAmelCase__        =  after_output[0].numpy()
                  lowerCAmelCase__        =  np.amax(np.abs(out_a - out_a				)				)
                  self.assertLessEqual(_SCREAMING_SNAKE_CASE       ,  1E-5				)






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase=None       ,  **_UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  self.get_vision_text_model(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE				)
            lowerCAmelCase__        =  TFVisionTextDualEncoderModel(vision_model=_SCREAMING_SNAKE_CASE       ,  text_model=_SCREAMING_SNAKE_CASE				)

            lowerCAmelCase__        =  model(
                input_ids=_SCREAMING_SNAKE_CASE       ,  pixel_values=_SCREAMING_SNAKE_CASE       ,  attention_mask=_SCREAMING_SNAKE_CASE       ,  output_attentions=_SCREAMING_SNAKE_CASE				)

            lowerCAmelCase__        =  output.vision_model_output.attentions
            self.assertEqual(len(_SCREAMING_SNAKE_CASE				)       ,  vision_config.num_hidden_layers				)

            # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
            lowerCAmelCase__        =  to_atuple(vision_model.config.image_size				)
            lowerCAmelCase__        =  to_atuple(vision_model.config.patch_size				)
            lowerCAmelCase__        =  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
            lowerCAmelCase__        =  num_patches + 1
            self.assertEqual(vision_attentions[0].shape[-3:]       ,  (vision_config.num_attention_heads, seq_len, seq_len)				)

            lowerCAmelCase__        =  output.text_model_output.attentions
            self.assertEqual(len(_SCREAMING_SNAKE_CASE				)       ,  text_config.num_hidden_layers				)

            self.assertEqual(
                text_attentions[0].shape[-3:]       ,  (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])       ,  )






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  np.abs((a - b)				).max()
            self.assertLessEqual(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE       ,  F"Difference between torch and flax is {diff} (>= {tol})."				)






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  self.prepare_config_and_inputs()
            self.check_vision_text_dual_encoder_model(**_SCREAMING_SNAKE_CASE				)






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  self.prepare_config_and_inputs()
            self.check_model_from_pretrained_configs(**_SCREAMING_SNAKE_CASE				)






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  self.prepare_config_and_inputs()
            self.check_vision_text_dual_encoder_from_pretrained(**_SCREAMING_SNAKE_CASE				)






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  self.prepare_config_and_inputs()
            self.check_save_load(**_SCREAMING_SNAKE_CASE				)






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  self.prepare_config_and_inputs()
            self.check_vision_text_output_attention(**_SCREAMING_SNAKE_CASE				)







      @slow
      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  self.get_pretrained_model_and_inputs()

            lowerCAmelCase__        =  model_a(**_SCREAMING_SNAKE_CASE				)
            lowerCAmelCase__        =  outputs[0].numpy()

            with tempfile.TemporaryDirectory() as tmp_dirname:
                  model_a.save_pretrained(_SCREAMING_SNAKE_CASE				)
                  lowerCAmelCase__        =  TFVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE				)

                  lowerCAmelCase__        =  model_a(**_SCREAMING_SNAKE_CASE				)
                  lowerCAmelCase__        =  after_outputs[0].numpy()
                  lowerCAmelCase__        =  np.amax(np.abs(out_a - out_a				)				)
                  self.assertLessEqual(_SCREAMING_SNAKE_CASE       ,  1E-5				)




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






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
                'hf-internal-testing/tiny-random-vit'       ,  'hf-internal-testing/tiny-random-bert'				)
            lowerCAmelCase__        =  13
            lowerCAmelCase__        =  floats_tensor(
                [
                    batch_size,
                    model.vision_model.config.num_channels,
                    model.vision_model.config.image_size,
                    model.vision_model.config.image_size,
                ]				)
            lowerCAmelCase__        =  ids_tensor([batch_size, 4]       ,  model.text_model.config.vocab_size				)
            lowerCAmelCase__        =  random_attention_mask([batch_size, 4]				)
            lowerCAmelCase__        =  {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

            return model, inputs






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  TFViTModel(_SCREAMING_SNAKE_CASE       ,  name='vision_model'				)
            lowerCAmelCase__        =  TFBertModel(_SCREAMING_SNAKE_CASE       ,  name='text_model'				)
            return vision_model, text_model







      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  TFViTModelTester(self				)
            lowerCAmelCase__        =  TFBertModelTester(self				)
            lowerCAmelCase__        =  vit_model_tester.prepare_config_and_inputs()
            lowerCAmelCase__        =  bert_model_tester.prepare_config_and_inputs()

            lowerCAmelCase__        =  vision_config_and_inputs

            (
                lowerCAmelCase__		
            )        =  text_config_and_inputs

            return {
                "text_config": text_config,
                "vision_config": vision_config,
                "pixel_values": pixel_values,
                "attention_mask": input_mask,
                "input_ids": input_ids,
                "text_token_type_ids": token_type_ids,
                "text_sequence_labels": sequence_labels,
                "text_token_labels": token_labels,
                "text_choice_labels": choice_labels,
            }




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






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
                'Rocketknight1/tiny-random-deit-tf'       ,  'hf-internal-testing/tiny-random-roberta'				)
            lowerCAmelCase__        =  13
            lowerCAmelCase__        =  floats_tensor(
                [
                    batch_size,
                    model.vision_model.config.num_channels,
                    model.vision_model.config.image_size,
                    model.vision_model.config.image_size,
                ]				)
            lowerCAmelCase__        =  ids_tensor([batch_size, 4]       ,  model.text_model.config.vocab_size				)
            lowerCAmelCase__        =  random_attention_mask([batch_size, 4]				)
            lowerCAmelCase__        =  {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

            return model, inputs






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase=None       ,  **_UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  self.get_vision_text_model(_SCREAMING_SNAKE_CASE       ,  _SCREAMING_SNAKE_CASE				)
            lowerCAmelCase__        =  TFVisionTextDualEncoderModel(vision_model=_SCREAMING_SNAKE_CASE       ,  text_model=_SCREAMING_SNAKE_CASE				)

            lowerCAmelCase__        =  model(
                input_ids=_SCREAMING_SNAKE_CASE       ,  pixel_values=_SCREAMING_SNAKE_CASE       ,  attention_mask=_SCREAMING_SNAKE_CASE       ,  output_attentions=_SCREAMING_SNAKE_CASE				)

            lowerCAmelCase__        =  output.vision_model_output.attentions
            self.assertEqual(len(_SCREAMING_SNAKE_CASE				)       ,  vision_config.num_hidden_layers				)

            # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
            lowerCAmelCase__        =  to_atuple(vision_model.config.image_size				)
            lowerCAmelCase__        =  to_atuple(vision_model.config.patch_size				)
            lowerCAmelCase__        =  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
            lowerCAmelCase__        =  num_patches + 2
            self.assertEqual(vision_attentions[0].shape[-3:]       ,  (vision_config.num_attention_heads, seq_len, seq_len)				)

            lowerCAmelCase__        =  output.text_model_output.attentions
            self.assertEqual(len(_SCREAMING_SNAKE_CASE				)       ,  text_config.num_hidden_layers				)

            self.assertEqual(
                text_attentions[0].shape[-3:]       ,  (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])       ,  )






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  TFDeiTModel(_SCREAMING_SNAKE_CASE       ,  name='vision_model'				)
            lowerCAmelCase__        =  TFRobertaModel(_SCREAMING_SNAKE_CASE       ,  name='text_model'				)
            return vision_model, text_model







      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  TFDeiTModelTester(self				)
            lowerCAmelCase__        =  TFRobertaModelTester(self				)
            lowerCAmelCase__        =  vit_model_tester.prepare_config_and_inputs()
            lowerCAmelCase__        =  bert_model_tester.prepare_config_and_inputs()

            lowerCAmelCase__        =  vision_config_and_inputs

            (
                lowerCAmelCase__		
            )        =  text_config_and_inputs

            return {
                "text_config": text_config,
                "vision_config": vision_config,
                "pixel_values": pixel_values,
                "attention_mask": input_mask,
                "input_ids": input_ids,
                "text_token_type_ids": token_type_ids,
                "text_sequence_labels": sequence_labels,
                "text_token_labels": token_labels,
                "text_choice_labels": choice_labels,
            }




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






      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
                'Rocketknight1/tiny-random-clip-tf'       ,  'hf-internal-testing/tiny-random-bert'				)
            lowerCAmelCase__        =  13
            lowerCAmelCase__        =  floats_tensor(
                [
                    batch_size,
                    model.vision_model.config.num_channels,
                    model.vision_model.config.image_size,
                    model.vision_model.config.image_size,
                ]				)
            lowerCAmelCase__        =  ids_tensor([batch_size, 4]       ,  model.text_model.config.vocab_size				)
            lowerCAmelCase__        =  random_attention_mask([batch_size, 4]				)
            lowerCAmelCase__        =  {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

            return model, inputs






      def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase				):






            """simple docstring"""
            lowerCAmelCase__        =  TFCLIPVisionModel(_SCREAMING_SNAKE_CASE       ,  name='vision_model'				)
            lowerCAmelCase__        =  TFBertModel(_SCREAMING_SNAKE_CASE       ,  name='text_model'				)
            return vision_model, text_model







      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  TFCLIPVisionModelTester(self				)
            lowerCAmelCase__        =  TFBertModelTester(self				)
            lowerCAmelCase__        =  clip_model_tester.prepare_config_and_inputs()
            lowerCAmelCase__        =  bert_model_tester.prepare_config_and_inputs()

            lowerCAmelCase__        =  vision_config_and_inputs

            (
                lowerCAmelCase__		
            )        =  text_config_and_inputs

            return {
                "text_config": text_config,
                "vision_config": vision_config,
                "pixel_values": pixel_values,
                "attention_mask": input_mask,
                "input_ids": input_ids,
                "text_token_type_ids": token_type_ids,
                "text_sequence_labels": sequence_labels,
                "text_token_labels": token_labels,
                "text_choice_labels": choice_labels,
            }




@require_vision
@require_tf
class       __SCREAMING_SNAKE_CASE   (				unittest.TestCase):






      @slow
      def  UpperCamelCase__ (		self				):






            """simple docstring"""
            lowerCAmelCase__        =  TFVisionTextDualEncoderModel.from_pretrained(
                'clip-italian/clip-italian'       ,  logit_scale_init_value=1.0       ,  from_pt=_SCREAMING_SNAKE_CASE				)
            lowerCAmelCase__        =  VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'				)

            lowerCAmelCase__        =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
            lowerCAmelCase__        =  processor(
                text=['una foto di un gatto', 'una foto di un cane']       ,  images=_SCREAMING_SNAKE_CASE       ,  padding=_SCREAMING_SNAKE_CASE       ,  return_tensors='np'				)

            lowerCAmelCase__        =  model(**_SCREAMING_SNAKE_CASE				)

            # verify the logits
            self.assertEqual(outputs.logits_per_image.shape       ,  (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])				)
            self.assertEqual(
                outputs.logits_per_text.shape       ,  (inputs.input_ids.shape[0], inputs.pixel_values.shape[0])       ,  )

            lowerCAmelCase__        =  np.array([[1.2_28_47_27, 0.3_10_41_22]]				)

            self.assertTrue(np.allclose(outputs.logits_per_image.numpy()       ,  _SCREAMING_SNAKE_CASE       ,  atol=1E-3				)				)













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 DeformableDetrImageProcessor


class       __SCREAMING_SNAKE_CASE   (				unittest.TestCase):






						def __init__(		self       ,  _UpperCamelCase       ,  _UpperCamelCase=7       ,  _UpperCamelCase=3       ,  _UpperCamelCase=30       ,  _UpperCamelCase=4_00       ,  _UpperCamelCase=True       ,  _UpperCamelCase=None       ,  _UpperCamelCase=True       ,  _UpperCamelCase=[0.5, 0.5, 0.5]       ,  _UpperCamelCase=[0.5, 0.5, 0.5]       ,  _UpperCamelCase=True       ,  _UpperCamelCase=1 / 2_55       ,  _UpperCamelCase=True       ,  ):






												"""simple docstring"""
												# by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
												lowerCAmelCase__        =  size if size is not None else {'shortest_edge': 18, 'longest_edge': 13_33}
												lowerCAmelCase__        =  parent
												lowerCAmelCase__        =  batch_size
												lowerCAmelCase__        =  num_channels
												lowerCAmelCase__        =  min_resolution
												lowerCAmelCase__        =  max_resolution
												lowerCAmelCase__        =  do_resize
												lowerCAmelCase__        =  size
												lowerCAmelCase__        =  do_normalize
												lowerCAmelCase__        =  image_mean
												lowerCAmelCase__        =  image_std
												lowerCAmelCase__        =  do_rescale
												lowerCAmelCase__        =  rescale_factor
												lowerCAmelCase__        =  do_pad






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												return {
												    "do_resize": self.do_resize,
												    "size": self.size,
												    "do_normalize": self.do_normalize,
												    "image_mean": self.image_mean,
												    "image_std": self.image_std,
												    "do_rescale": self.do_rescale,
												    "rescale_factor": self.rescale_factor,
												    "do_pad": self.do_pad,
												}







						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase=False				):






												"""simple docstring"""
												if not batched:
																		lowerCAmelCase__        =  image_inputs[0]
																		if isinstance(_UpperCamelCase       ,  Image.Image				):
																								lowerCAmelCase__  ,    lowerCAmelCase__        =  image.size
																		else:
																								lowerCAmelCase__  ,    lowerCAmelCase__        =  image.shape[1], image.shape[2]
																		if w < h:
																								lowerCAmelCase__        =  int(self.size['shortest_edge'] * h / w				)
																								lowerCAmelCase__        =  self.size['shortest_edge']
																		elif w > h:
																								lowerCAmelCase__        =  self.size['shortest_edge']
																								lowerCAmelCase__        =  int(self.size['shortest_edge'] * w / h				)
																		else:
																								lowerCAmelCase__        =  self.size['shortest_edge']
																								lowerCAmelCase__        =  self.size['shortest_edge']

												else:
																		lowerCAmelCase__        =  []
																		for image in image_inputs:
																								lowerCAmelCase__  ,    lowerCAmelCase__        =  self.get_expected_values([image]				)
																								expected_values.append((expected_height, expected_width)				)
																		lowerCAmelCase__        =  max(_UpperCamelCase       ,  key=lambda _UpperCamelCase				: item[0]				)[0]
																		lowerCAmelCase__        =  max(_UpperCamelCase       ,  key=lambda _UpperCamelCase				: item[1]				)[1]

												return expected_height, expected_width




@require_torch
@require_vision
class       __SCREAMING_SNAKE_CASE   (				__lowercase						,   unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:    Dict						=   DeformableDetrImageProcessor if is_vision_available() else None






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  DeformableDetrImageProcessingTester(self				)






						@property
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												return self.image_processor_tester.prepare_image_processor_dict()






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.image_processing_class(**self.image_processor_dict				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'image_mean'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'image_std'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'do_normalize'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'do_resize'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'do_rescale'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'do_pad'				)				)
												self.assertTrue(hasattr(_UpperCamelCase       ,  'size'				)				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  self.image_processing_class.from_dict(self.image_processor_dict				)
												self.assertEqual(image_processor.size       ,  {'shortest_edge': 18, 'longest_edge': 13_33}				)
												self.assertEqual(image_processor.do_pad       ,  _UpperCamelCase				)

												lowerCAmelCase__        =  self.image_processing_class.from_dict(
												    self.image_processor_dict       ,  size=42       ,  max_size=84       ,  pad_and_return_pixel_mask=_UpperCamelCase				)
												self.assertEqual(image_processor.size       ,  {'shortest_edge': 42, 'longest_edge': 84}				)
												self.assertEqual(image_processor.do_pad       ,  _UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												pass






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												# 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=_UpperCamelCase				)
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase       ,  Image.Image				)

												# Test not batched input
												lowerCAmelCase__        =  image_processing(image_inputs[0]       ,  return_tensors='pt'				).pixel_values

												lowerCAmelCase__  ,    lowerCAmelCase__        =  self.image_processor_tester.get_expected_values(_UpperCamelCase				)

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

												# Test batched
												lowerCAmelCase__  ,    lowerCAmelCase__        =  self.image_processor_tester.get_expected_values(_UpperCamelCase       ,  batched=_UpperCamelCase				)

												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  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  UpperCamelCase__ (		self				):






												"""simple docstring"""
												# 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=_UpperCamelCase       ,  numpify=_UpperCamelCase				)
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase       ,  np.ndarray				)

												# Test not batched input
												lowerCAmelCase__        =  image_processing(image_inputs[0]       ,  return_tensors='pt'				).pixel_values

												lowerCAmelCase__  ,    lowerCAmelCase__        =  self.image_processor_tester.get_expected_values(_UpperCamelCase				)

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

												# Test batched
												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  return_tensors='pt'				).pixel_values

												lowerCAmelCase__  ,    lowerCAmelCase__        =  self.image_processor_tester.get_expected_values(_UpperCamelCase       ,  batched=_UpperCamelCase				)

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






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												# 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=_UpperCamelCase       ,  torchify=_UpperCamelCase				)
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase       ,  torch.Tensor				)

												# Test not batched input
												lowerCAmelCase__        =  image_processing(image_inputs[0]       ,  return_tensors='pt'				).pixel_values

												lowerCAmelCase__  ,    lowerCAmelCase__        =  self.image_processor_tester.get_expected_values(_UpperCamelCase				)

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

												# Test batched
												lowerCAmelCase__        =  image_processing(_UpperCamelCase       ,  return_tensors='pt'				).pixel_values

												lowerCAmelCase__  ,    lowerCAmelCase__        =  self.image_processor_tester.get_expected_values(_UpperCamelCase       ,  batched=_UpperCamelCase				)

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






						@slow
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												# prepare image and target
												lowerCAmelCase__        =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
												with open('./tests/fixtures/tests_samples/COCO/coco_annotations.txt'       ,  'r'				) as f:
																		lowerCAmelCase__        =  json.loads(f.read()				)

												lowerCAmelCase__        =  {'image_id': 3_97_69, 'annotations': target}

												# encode them
												lowerCAmelCase__        =  DeformableDetrImageProcessor()
												lowerCAmelCase__        =  image_processing(images=_UpperCamelCase       ,  annotations=_UpperCamelCase       ,  return_tensors='pt'				)

												# verify pixel values
												lowerCAmelCase__        =  torch.Size([1, 3, 8_00, 10_66]				)
												self.assertEqual(encoding['pixel_values'].shape       ,  _UpperCamelCase				)

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

												# verify area
												lowerCAmelCase__        =  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']       ,  _UpperCamelCase				)				)
												# verify boxes
												lowerCAmelCase__        =  torch.Size([6, 4]				)
												self.assertEqual(encoding['labels'][0]['boxes'].shape       ,  _UpperCamelCase				)
												lowerCAmelCase__        =  torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]       ,  _UpperCamelCase       ,  atol=1E-3				)				)
												# verify image_id
												lowerCAmelCase__        =  torch.tensor([3_97_69]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']       ,  _UpperCamelCase				)				)
												# verify is_crowd
												lowerCAmelCase__        =  torch.tensor([0, 0, 0, 0, 0, 0]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']       ,  _UpperCamelCase				)				)
												# verify class_labels
												lowerCAmelCase__        =  torch.tensor([75, 75, 63, 65, 17, 17]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']       ,  _UpperCamelCase				)				)
												# verify orig_size
												lowerCAmelCase__        =  torch.tensor([4_80, 6_40]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']       ,  _UpperCamelCase				)				)
												# verify size
												lowerCAmelCase__        =  torch.tensor([8_00, 10_66]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['size']       ,  _UpperCamelCase				)				)







						@slow
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												# prepare image, target and masks_path
												lowerCAmelCase__        =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
												with open('./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'       ,  'r'				) as f:
																		lowerCAmelCase__        =  json.loads(f.read()				)

												lowerCAmelCase__        =  {'file_name': '000000039769.png', 'image_id': 3_97_69, 'segments_info': target}

												lowerCAmelCase__        =  pathlib.Path('./tests/fixtures/tests_samples/COCO/coco_panoptic'				)

												# encode them
												lowerCAmelCase__        =  DeformableDetrImageProcessor(format='coco_panoptic'				)
												lowerCAmelCase__        =  image_processing(images=_UpperCamelCase       ,  annotations=_UpperCamelCase       ,  masks_path=_UpperCamelCase       ,  return_tensors='pt'				)

												# verify pixel values
												lowerCAmelCase__        =  torch.Size([1, 3, 8_00, 10_66]				)
												self.assertEqual(encoding['pixel_values'].shape       ,  _UpperCamelCase				)

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

												# verify area
												lowerCAmelCase__        =  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']       ,  _UpperCamelCase				)				)
												# verify boxes
												lowerCAmelCase__        =  torch.Size([6, 4]				)
												self.assertEqual(encoding['labels'][0]['boxes'].shape       ,  _UpperCamelCase				)
												lowerCAmelCase__        =  torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]       ,  _UpperCamelCase       ,  atol=1E-3				)				)
												# verify image_id
												lowerCAmelCase__        =  torch.tensor([3_97_69]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']       ,  _UpperCamelCase				)				)
												# verify is_crowd
												lowerCAmelCase__        =  torch.tensor([0, 0, 0, 0, 0, 0]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']       ,  _UpperCamelCase				)				)
												# verify class_labels
												lowerCAmelCase__        =  torch.tensor([17, 17, 63, 75, 75, 93]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']       ,  _UpperCamelCase				)				)
												# verify masks
												lowerCAmelCase__        =  82_28_73
												self.assertEqual(encoding['labels'][0]['masks'].sum().item()       ,  _UpperCamelCase				)
												# verify orig_size
												lowerCAmelCase__        =  torch.tensor([4_80, 6_40]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']       ,  _UpperCamelCase				)				)
												# verify size
												lowerCAmelCase__        =  torch.tensor([8_00, 10_66]				)
												self.assertTrue(torch.allclose(encoding['labels'][0]['size']       ,  _UpperCamelCase				)				)





