




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 DetaImageProcessor







class 				lowerCAmelCase_       (					unittest.TestCase							):






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






				def 		snake_case	(			self ):
							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 		snake_case	(			self      ,snake_case__      ,snake_case__=False ):
							if not batched:
										SCREAMING_SNAKE_CASE_   : List[str]					  =      image_inputs[0]
										if isinstance(snake_case__      ,Image.Image ):
													SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : str					  =      image.size
										else:
													SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Tuple					  =      image.shape[1], image.shape[2]
										if w < h:
													SCREAMING_SNAKE_CASE_   : Dict					  =      int(self.size['shortest_edge'] * h / w )
													SCREAMING_SNAKE_CASE_   : Optional[int]					  =      self.size['shortest_edge']
										elif w > h:
													SCREAMING_SNAKE_CASE_   : str					  =      self.size['shortest_edge']
													SCREAMING_SNAKE_CASE_   : Optional[int]					  =      int(self.size['shortest_edge'] * w / h )
										else:
													SCREAMING_SNAKE_CASE_   : int					  =      self.size['shortest_edge']
													SCREAMING_SNAKE_CASE_   : List[Any]					  =      self.size['shortest_edge']

							else:
										SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      []
										for image in image_inputs:
													SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : int					  =      self.get_expected_values([image] )
													expected_values.append((expected_height, expected_width) )
										SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      max(snake_case__      ,key=lambda snake_case__ : item[0] )[0]
										SCREAMING_SNAKE_CASE_   : Optional[int]					  =      max(snake_case__      ,key=lambda snake_case__ : item[1] )[1]

							return expected_height, expected_width







@require_torch
@require_vision
class 				lowerCAmelCase_       (					lowerCamelCase_						,     unittest.TestCase							):
				__a     :       Dict						=     DetaImageProcessor if is_vision_available() else None






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      DetaImageProcessingTester(self )






				@property
				def 		snake_case	(			self ):
							return self.image_processor_tester.prepare_image_processor_dict()






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Any					  =      self.image_processing_class(**self.image_processor_dict )
							self.assertTrue(hasattr(snake_case__      ,'image_mean' ) )
							self.assertTrue(hasattr(snake_case__      ,'image_std' ) )
							self.assertTrue(hasattr(snake_case__      ,'do_normalize' ) )
							self.assertTrue(hasattr(snake_case__      ,'do_resize' ) )
							self.assertTrue(hasattr(snake_case__      ,'do_rescale' ) )
							self.assertTrue(hasattr(snake_case__      ,'do_pad' ) )
							self.assertTrue(hasattr(snake_case__      ,'size' ) )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : int					  =      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      ,snake_case__ )






				def 		snake_case	(			self ):
							pass






				def 		snake_case	(			self ):
							# Initialize image_processing
							SCREAMING_SNAKE_CASE_   : Tuple					  =      self.image_processing_class(**self.image_processor_dict )
							# create random PIL images
							SCREAMING_SNAKE_CASE_   : List[str]					  =      prepare_image_inputs(self.image_processor_tester      ,equal_resolution=snake_case__ )
							for image in image_inputs:
										self.assertIsInstance(snake_case__      ,Image.Image )

							# Test not batched input
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      image_processing(image_inputs[0]      ,return_tensors='pt' ).pixel_values

							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : str					  =      self.image_processor_tester.get_expected_values(snake_case__ )

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

							# Test batched
							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Any					  =      self.image_processor_tester.get_expected_values(snake_case__      ,batched=snake_case__ )

							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      image_processing(snake_case__      ,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 		snake_case	(			self ):
							# Initialize image_processing
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      self.image_processing_class(**self.image_processor_dict )
							# create random numpy tensors
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      prepare_image_inputs(self.image_processor_tester      ,equal_resolution=snake_case__      ,numpify=snake_case__ )
							for image in image_inputs:
										self.assertIsInstance(snake_case__      ,np.ndarray )

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

							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      self.image_processor_tester.get_expected_values(snake_case__ )

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

							# Test batched
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      image_processing(snake_case__      ,return_tensors='pt' ).pixel_values

							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Tuple					  =      self.image_processor_tester.get_expected_values(snake_case__      ,batched=snake_case__ )

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






				def 		snake_case	(			self ):
							# Initialize image_processing
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      self.image_processing_class(**self.image_processor_dict )
							# create random PyTorch tensors
							SCREAMING_SNAKE_CASE_   : int					  =      prepare_image_inputs(self.image_processor_tester      ,equal_resolution=snake_case__      ,torchify=snake_case__ )
							for image in image_inputs:
										self.assertIsInstance(snake_case__      ,torch.Tensor )

							# Test not batched input
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      image_processing(image_inputs[0]      ,return_tensors='pt' ).pixel_values

							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Tuple					  =      self.image_processor_tester.get_expected_values(snake_case__ )

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

							# Test batched
							SCREAMING_SNAKE_CASE_   : List[str]					  =      image_processing(snake_case__      ,return_tensors='pt' ).pixel_values

							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Dict					  =      self.image_processor_tester.get_expected_values(snake_case__      ,batched=snake_case__ )

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






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

							SCREAMING_SNAKE_CASE_   : Any					  =      {'image_id': 39769, 'annotations': target}

							# encode them
							SCREAMING_SNAKE_CASE_   : int					  =      DetaImageProcessor()
							SCREAMING_SNAKE_CASE_   : int					  =      image_processing(images=snake_case__      ,annotations=snake_case__      ,return_tensors='pt' )

							# verify pixel values
							SCREAMING_SNAKE_CASE_   : Tuple					  =      torch.Size([1, 3, 800, 1066] )
							self.assertEqual(encoding['pixel_values'].shape      ,snake_case__ )

							SCREAMING_SNAKE_CASE_   : int					  =      torch.tensor([0.2796, 0.3138, 0.3481] )
							self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3]      ,snake_case__      ,atol=1E-4 ) )

							# verify area
							SCREAMING_SNAKE_CASE_   : Dict					  =      torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['area']      ,snake_case__ ) )
							# verify boxes
							SCREAMING_SNAKE_CASE_   : int					  =      torch.Size([6, 4] )
							self.assertEqual(encoding['labels'][0]['boxes'].shape      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : str					  =      torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]      ,snake_case__      ,atol=1E-3 ) )
							# verify image_id
							SCREAMING_SNAKE_CASE_   : int					  =      torch.tensor([39769] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']      ,snake_case__ ) )
							# verify is_crowd
							SCREAMING_SNAKE_CASE_   : List[str]					  =      torch.tensor([0, 0, 0, 0, 0, 0] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']      ,snake_case__ ) )
							# verify class_labels
							SCREAMING_SNAKE_CASE_   : Tuple					  =      torch.tensor([75, 75, 63, 65, 17, 17] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']      ,snake_case__ ) )
							# verify orig_size
							SCREAMING_SNAKE_CASE_   : int					  =      torch.tensor([480, 640] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']      ,snake_case__ ) )
							# verify size
							SCREAMING_SNAKE_CASE_   : Any					  =      torch.tensor([800, 1066] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['size']      ,snake_case__ ) )






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

							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      {'file_name': '000000039769.png', 'image_id': 39769, 'segments_info': target}

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

							# encode them
							SCREAMING_SNAKE_CASE_   : Any					  =      DetaImageProcessor(format='coco_panoptic' )
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      image_processing(images=snake_case__      ,annotations=snake_case__      ,masks_path=snake_case__      ,return_tensors='pt' )

							# verify pixel values
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      torch.Size([1, 3, 800, 1066] )
							self.assertEqual(encoding['pixel_values'].shape      ,snake_case__ )

							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      torch.tensor([0.2796, 0.3138, 0.3481] )
							self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3]      ,snake_case__      ,atol=1E-4 ) )

							# verify area
							SCREAMING_SNAKE_CASE_   : List[str]					  =      torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['area']      ,snake_case__ ) )
							# verify boxes
							SCREAMING_SNAKE_CASE_   : Dict					  =      torch.Size([6, 4] )
							self.assertEqual(encoding['labels'][0]['boxes'].shape      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : Any					  =      torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]      ,snake_case__      ,atol=1E-3 ) )
							# verify image_id
							SCREAMING_SNAKE_CASE_   : List[str]					  =      torch.tensor([39769] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']      ,snake_case__ ) )
							# verify is_crowd
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      torch.tensor([0, 0, 0, 0, 0, 0] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']      ,snake_case__ ) )
							# verify class_labels
							SCREAMING_SNAKE_CASE_   : Tuple					  =      torch.tensor([17, 17, 63, 75, 75, 93] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']      ,snake_case__ ) )
							# verify masks
							SCREAMING_SNAKE_CASE_   : Any					  =      822873
							self.assertEqual(encoding['labels'][0]['masks'].sum().item()      ,snake_case__ )
							# verify orig_size
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      torch.tensor([480, 640] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']      ,snake_case__ ) )
							# verify size
							SCREAMING_SNAKE_CASE_   : Tuple					  =      torch.tensor([800, 1066] )
							self.assertTrue(torch.allclose(encoding['labels'][0]['size']      ,snake_case__ ) )








from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowercase    	=		logging.get_logger(__name__)

_lowercase    	=		{
    "xlm-roberta-base": "https://huggingface.co/xlm-roberta-base/resolve/main/config.json",
    "xlm-roberta-large": "https://huggingface.co/xlm-roberta-large/resolve/main/config.json",
    "xlm-roberta-large-finetuned-conll02-dutch": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json"
    ),
    "xlm-roberta-large-finetuned-conll02-spanish": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json"
    ),
    "xlm-roberta-large-finetuned-conll03-english": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json"
    ),
    "xlm-roberta-large-finetuned-conll03-german": (
        "https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json"
    ),
}



class      _UpperCAmelCase      (							A__  ):
     UpperCamelCase__		     =					'''xlm-roberta'''




     def __init__(					self			,    a__=3_0_5_2_2			,    a__=7_6_8			,    a__=1_2			,    a__=1_2			,    a__=3_0_7_2			,    a__="gelu"			,    a__=0.1			,    a__=0.1			,    a__=5_1_2			,    a__=2			,    a__=0.0_2			,    a__=1e-12			,    a__=1			,    a__=0			,    a__=2			,    a__="absolute"			,    a__=True			,    a__=None			,    **a__			,    ):
         super().__init__(pad_token_id=a__			,    bos_token_id=a__			,    eos_token_id=a__			,    **a__)

         A__					=       vocab_size
         A__					=       hidden_size
         A__					=       num_hidden_layers
         A__					=       num_attention_heads
         A__					=       hidden_act
         A__					=       intermediate_size
         A__					=       hidden_dropout_prob
         A__					=       attention_probs_dropout_prob
         A__					=       max_position_embeddings
         A__					=       type_vocab_size
         A__					=       initializer_range
         A__					=       layer_norm_eps
         A__					=       position_embedding_type
         A__					=       use_cache
         A__					=       classifier_dropout







class      _UpperCAmelCase      (							A__  ):




     @property
     def 		snake_case_		(					self):
         if self.task == "multiple-choice":
             A__					=       {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
         else:
             A__					=       {0: '''batch''', 1: '''sequence'''}
         return OrderedDict(
             [
                 ('''input_ids''', dynamic_axis),
                 ('''attention_mask''', dynamic_axis),
             ])
