
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   __lowercase						(						unittest.TestCase       ):






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






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

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

							return expected_height, expected_width




@require_torch
@require_vision
class   __lowercase						(						__snake_case   , unittest.TestCase       ):
	_A       =   DeformableDetrImageProcessor if is_vision_available() else None






	def       _a(self				: str    ) ->			str:
							_lowercase       :	Optional[int]           =   DeformableDetrImageProcessingTester(self    )






	@property
	def       _a(self				: Optional[Any]    ) ->			int:
							return self.image_processor_tester.prepare_image_processor_dict()






	def       _a(self				: List[str]    ) ->			List[str]:
							_lowercase       :	Union[str, 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       _a(self				: Any    ) ->			List[str]:
							_lowercase       :	List[Any]           =   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    )

							_lowercase       :	List[Any]           =   self.image_processing_class.from_dict(
							    self.image_processor_dict      ,	size=42      ,	max_size=84      ,	pad_and_return_pixel_mask=snake_case    )
							self.assertEqual(image_processor.size      ,	{"shortest_edge": 42, "longest_edge": 84}    )
							self.assertEqual(image_processor.do_pad      ,	snake_case    )






	def       _a(self				: Tuple    ) ->			Dict:
							pass






	def       _a(self				: Dict    ) ->			Union[str, Any]:
							# Initialize image_processing
							_lowercase       :	Optional[int]           =   self.image_processing_class(**self.image_processor_dict    )
							# create random PIL images
							_lowercase       :	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
							_lowercase       :	Union[str, Any]           =   image_processing(image_inputs[0]      ,	return_tensors="pt"    ).pixel_values

							_lowercase      ,    _lowercase       :	int           =   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
							_lowercase      ,    _lowercase       :	Union[str, Any]           =   self.image_processor_tester.get_expected_values(snake_case      ,	batched=snake_case    )

							_lowercase       :	str           =   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       _a(self				: Optional[int]    ) ->			List[str]:
							# Initialize image_processing
							_lowercase       :	Tuple           =   self.image_processing_class(**self.image_processor_dict    )
							# create random numpy tensors
							_lowercase       :	Tuple           =   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
							_lowercase       :	Optional[int]           =   image_processing(image_inputs[0]      ,	return_tensors="pt"    ).pixel_values

							_lowercase      ,    _lowercase       :	Dict           =   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
							_lowercase       :	Optional[Any]           =   image_processing(snake_case      ,	return_tensors="pt"    ).pixel_values

							_lowercase      ,    _lowercase       :	Optional[Any]           =   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       _a(self				: List[str]    ) ->			List[Any]:
							# Initialize image_processing
							_lowercase       :	Dict           =   self.image_processing_class(**self.image_processor_dict    )
							# create random PyTorch tensors
							_lowercase       :	str           =   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
							_lowercase       :	Any           =   image_processing(image_inputs[0]      ,	return_tensors="pt"    ).pixel_values

							_lowercase      ,    _lowercase       :	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
							_lowercase       :	List[str]           =   image_processing(snake_case      ,	return_tensors="pt"    ).pixel_values

							_lowercase      ,    _lowercase       :	Optional[Any]           =   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       _a(self				: Optional[Any]    ) ->			Optional[Any]:
							# prepare image and target
							_lowercase       :	List[Any]           =   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
							with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt"      ,	"r"    ) as f:
													_lowercase       :	Optional[Any]           =   json.loads(f.read()    )

							_lowercase       :	str           =   {"image_id": 3_9769, "annotations": target}

							# encode them
							_lowercase       :	Optional[int]           =   DeformableDetrImageProcessor()
							_lowercase       :	Any           =   image_processing(images=snake_case      ,	annotations=snake_case      ,	return_tensors="pt"    )

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

							_lowercase       :	Optional[Any]           =   torch.tensor([0.27_96, 0.31_38, 0.34_81]    )
							self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]      ,	snake_case      ,	atol=1e-4    )    )

							# verify area
							_lowercase       :	Optional[int]           =   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"]      ,	snake_case    )    )
							# verify boxes
							_lowercase       :	Optional[Any]           =   torch.Size([6, 4]    )
							self.assertEqual(encoding["labels"][0]["boxes"].shape      ,	snake_case    )
							_lowercase       :	Tuple           =   torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]      ,	snake_case      ,	atol=1e-3    )    )
							# verify image_id
							_lowercase       :	Dict           =   torch.tensor([3_9769]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]      ,	snake_case    )    )
							# verify is_crowd
							_lowercase       :	int           =   torch.tensor([0, 0, 0, 0, 0, 0]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]      ,	snake_case    )    )
							# verify class_labels
							_lowercase       :	Optional[Any]           =   torch.tensor([75, 75, 63, 65, 17, 17]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]      ,	snake_case    )    )
							# verify orig_size
							_lowercase       :	List[str]           =   torch.tensor([480, 640]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]      ,	snake_case    )    )
							# verify size
							_lowercase       :	List[str]           =   torch.tensor([800, 1066]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["size"]      ,	snake_case    )    )






	@slow
	def       _a(self				: List[str]    ) ->			List[Any]:
							# prepare image, target and masks_path
							_lowercase       :	Union[str, Any]           =   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
							with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"      ,	"r"    ) as f:
													_lowercase       :	Tuple           =   json.loads(f.read()    )

							_lowercase       :	str           =   {"file_name": "000000039769.png", "image_id": 3_9769, "segments_info": target}

							_lowercase       :	Union[str, Any]           =   pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic"    )

							# encode them
							_lowercase       :	Tuple           =   DeformableDetrImageProcessor(format="coco_panoptic"    )
							_lowercase       :	Union[str, Any]           =   image_processing(images=snake_case      ,	annotations=snake_case      ,	masks_path=snake_case      ,	return_tensors="pt"    )

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

							_lowercase       :	Union[str, Any]           =   torch.tensor([0.27_96, 0.31_38, 0.34_81]    )
							self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]      ,	snake_case      ,	atol=1e-4    )    )

							# verify area
							_lowercase       :	List[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"]      ,	snake_case    )    )
							# verify boxes
							_lowercase       :	List[Any]           =   torch.Size([6, 4]    )
							self.assertEqual(encoding["labels"][0]["boxes"].shape      ,	snake_case    )
							_lowercase       :	int           =   torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]      ,	snake_case      ,	atol=1e-3    )    )
							# verify image_id
							_lowercase       :	Optional[Any]           =   torch.tensor([3_9769]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]      ,	snake_case    )    )
							# verify is_crowd
							_lowercase       :	Union[str, Any]           =   torch.tensor([0, 0, 0, 0, 0, 0]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]      ,	snake_case    )    )
							# verify class_labels
							_lowercase       :	str           =   torch.tensor([17, 17, 63, 75, 75, 93]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]      ,	snake_case    )    )
							# verify masks
							_lowercase       :	Tuple           =   82_2873
							self.assertEqual(encoding["labels"][0]["masks"].sum().item()      ,	snake_case    )
							# verify orig_size
							_lowercase       :	List[Any]           =   torch.tensor([480, 640]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]      ,	snake_case    )    )
							# verify size
							_lowercase       :	str           =   torch.tensor([800, 1066]    )
							self.assertTrue(torch.allclose(encoding["labels"][0]["size"]      ,	snake_case    )    )






import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


UpperCamelCase_						:     int	       =							logging.get_logger(__name__)



def 		UpperCamelCase     (       _UpperCAmelCase  :   Optional[int] ,			_UpperCAmelCase  :   Dict  )   ->			Any:






						'''simple docstring'''

						_lowercase       :	List[Any]           =   nn.functional.normalize(_UpperCAmelCase  )
						_lowercase       :	List[str]           =   nn.functional.normalize(_UpperCAmelCase  )
						return torch.mm(_UpperCAmelCase ,			normalized_text_embeds.t()  )




class   __lowercase						(						__snake_case       ):
	_A       =   CLIPConfig

	_A       =   ["CLIPEncoderLayer"]






	def __init__(self				: List[str]      ,	snake_case				: CLIPConfig    ) ->			Optional[int]:
							super().__init__(snake_case    )

							_lowercase       :	Tuple           =   CLIPVisionModel(config.vision_config    )
							_lowercase       :	Any           =   nn.Linear(config.vision_config.hidden_size      ,	config.projection_dim      ,	bias=snake_case    )

							_lowercase       :	int           =   nn.Parameter(torch.ones(17      ,	config.projection_dim    )      ,	requires_grad=snake_case    )
							_lowercase       :	str           =   nn.Parameter(torch.ones(3      ,	config.projection_dim    )      ,	requires_grad=snake_case    )

							_lowercase       :	Any           =   nn.Parameter(torch.ones(17    )      ,	requires_grad=snake_case    )
							_lowercase       :	Dict           =   nn.Parameter(torch.ones(3    )      ,	requires_grad=snake_case    )






	@torch.no_grad()
	def       _a(self				: int      ,	snake_case				: Tuple      ,	snake_case				: Optional[Any]    ) ->			Union[str, Any]:
							_lowercase       :	str           =   self.vision_model(snake_case    )[1]  # pooled_output
							_lowercase       :	List[Any]           =   self.visual_projection(snake_case    )

							# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
							_lowercase       :	str           =   cosine_distance(snake_case      ,	self.special_care_embeds    ).cpu().float().numpy()
							_lowercase       :	Optional[int]           =   cosine_distance(snake_case      ,	self.concept_embeds    ).cpu().float().numpy()

							_lowercase       :	List[str]           =   []
							_lowercase       :	int           =   image_embeds.shape[0]
							for i in range(snake_case    ):
													_lowercase       :	int           =   {"special_scores": {}, "special_care": [], "concept_scores": {}, "bad_concepts": []}

													# increase this value to create a stronger `nfsw` filter
													# at the cost of increasing the possibility of filtering benign images
													_lowercase       :	Optional[int]           =   0.0

													for concept_idx in range(len(special_cos_dist[0]    )    ):
																			_lowercase       :	Union[str, Any]           =   special_cos_dist[i][concept_idx]
																			_lowercase       :	Optional[int]           =   self.special_care_embeds_weights[concept_idx].item()
																			_lowercase       :	Optional[int]           =   round(concept_cos - concept_threshold + adjustment      ,	3    )
																			if result_img["special_scores"][concept_idx] > 0:
																									result_img["special_care"].append({concept_idx, result_img["special_scores"][concept_idx]}    )
																									_lowercase       :	Union[str, Any]           =   0.01

													for concept_idx in range(len(cos_dist[0]    )    ):
																			_lowercase       :	Tuple           =   cos_dist[i][concept_idx]
																			_lowercase       :	Union[str, Any]           =   self.concept_embeds_weights[concept_idx].item()
																			_lowercase       :	str           =   round(concept_cos - concept_threshold + adjustment      ,	3    )
																			if result_img["concept_scores"][concept_idx] > 0:
																									result_img["bad_concepts"].append(snake_case    )

													result.append(snake_case    )

							_lowercase       :	Dict           =   [len(res["bad_concepts"]    ) > 0 for res in result]

							return images, has_nsfw_concepts






	@torch.no_grad()
	def       _a(self				: str      ,	snake_case				: torch.FloatTensor      ,	snake_case				: torch.FloatTensor    ) ->			List[str]:
							_lowercase       :	Dict           =   self.vision_model(snake_case    )[1]  # pooled_output
							_lowercase       :	Any           =   self.visual_projection(snake_case    )

							_lowercase       :	Optional[Any]           =   cosine_distance(snake_case      ,	self.special_care_embeds    )
							_lowercase       :	Optional[Any]           =   cosine_distance(snake_case      ,	self.concept_embeds    )

							# increase this value to create a stronger `nsfw` filter
							# at the cost of increasing the possibility of filtering benign images
							_lowercase       :	Optional[int]           =   0.0

							_lowercase       :	Optional[int]           =   special_cos_dist - self.special_care_embeds_weights + adjustment
							# special_scores = special_scores.round(decimals=3)
							_lowercase       :	List[str]           =   torch.any(special_scores > 0      ,	dim=1    )
							_lowercase       :	List[Any]           =   special_care * 0.01
							_lowercase       :	Optional[Any]           =   special_adjustment.unsqueeze(1    ).expand(-1      ,	cos_dist.shape[1]    )

							_lowercase       :	Any           =   (cos_dist - self.concept_embeds_weights) + special_adjustment
							# concept_scores = concept_scores.round(decimals=3)
							_lowercase       :	List[Any]           =   torch.any(concept_scores > 0      ,	dim=1    )

							return images, has_nsfw_concepts




