



import unittest

import numpy as np

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



class    _lowerCAmelCase    (				unittest.TestCase		):





		"""simple docstring"""






		def __init__(					self							,    _lowercase							,    _lowercase=7							,    _lowercase=3							,    _lowercase=1_8							,    _lowercase=3_0							,    _lowercase=4_0_0							,    _lowercase=True							,    _lowercase=None							,    _lowercase=True							,    _lowercase=None							,    _lowercase=True							,    ) ->						List[str]:







								'''simple docstring'''

								snake_case_   : Union[str, Any]						=			size if size is not None else {"""shortest_edge""": 2_0}
								snake_case_   : Optional[int]						=			crop_size if crop_size is not None else {"""height""": 1_8, """width""": 1_8}
								snake_case_   : Union[str, Any]						=			parent
								snake_case_   : Optional[Any]						=			batch_size
								snake_case_   : Optional[int]						=			num_channels
								snake_case_   : Optional[int]						=			image_size
								snake_case_   : List[str]						=			min_resolution
								snake_case_   : List[Any]						=			max_resolution
								snake_case_   : Optional[int]						=			do_resize
								snake_case_   : Dict						=			size
								snake_case_   : int						=			do_center_crop
								snake_case_   : Union[str, Any]						=			crop_size
								snake_case_   : str						=			do_flip_channel_order





		def    UpperCAmelCase__							(					self   ) ->						Optional[Any]:







								'''simple docstring'''

								return {
								    "do_resize": self.do_resize,
								    "size": self.size,
								    "do_center_crop": self.do_center_crop,
								    "crop_size": self.crop_size,
								    "do_flip_channel_order": self.do_flip_channel_order,
								}




@require_torch
@require_vision
class    _lowerCAmelCase    (				UpperCamelCase__  ,				unittest.TestCase		):





		"""simple docstring"""






		_lowerCamelCase			  =       MobileViTImageProcessor if is_vision_available() else None





		def    UpperCAmelCase__							(					self   ) ->						int:







								'''simple docstring'''

								snake_case_   : Dict						=			MobileViTImageProcessingTester(self   )





		@property
		def    UpperCAmelCase__							(					self   ) ->						str:







								'''simple docstring'''

								return self.image_processor_tester.prepare_image_processor_dict()





		def    UpperCAmelCase__							(					self   ) ->						Optional[Any]:







								'''simple docstring'''

								snake_case_   : Dict						=			self.image_processing_class(**self.image_processor_dict   )
								self.assertTrue(hasattr(_lowercase							,    """do_resize"""   )   )
								self.assertTrue(hasattr(_lowercase							,    """size"""   )   )
								self.assertTrue(hasattr(_lowercase							,    """do_center_crop"""   )   )
								self.assertTrue(hasattr(_lowercase							,    """center_crop"""   )   )
								self.assertTrue(hasattr(_lowercase							,    """do_flip_channel_order"""   )   )





		def    UpperCAmelCase__							(					self   ) ->						Tuple:







								'''simple docstring'''

								snake_case_   : Union[str, Any]						=			self.image_processing_class.from_dict(self.image_processor_dict   )
								self.assertEqual(image_processor.size							,    {"""shortest_edge""": 2_0}   )
								self.assertEqual(image_processor.crop_size							,    {"""height""": 1_8, """width""": 1_8}   )

								snake_case_   : Dict						=			self.image_processing_class.from_dict(self.image_processor_dict							,    size=4_2							,    crop_size=8_4   )
								self.assertEqual(image_processor.size							,    {"""shortest_edge""": 4_2}   )
								self.assertEqual(image_processor.crop_size							,    {"""height""": 8_4, """width""": 8_4}   )





		def    UpperCAmelCase__							(					self   ) ->						Optional[int]:







								'''simple docstring'''

								pass





		def    UpperCAmelCase__							(					self   ) ->						List[Any]:







								'''simple docstring'''

								snake_case_   : Optional[Any]						=			self.image_processing_class(**self.image_processor_dict   )
								# create random PIL images
								snake_case_   : Optional[int]						=			prepare_image_inputs(self.image_processor_tester							,    equal_resolution=_lowercase   )
								for image in image_inputs:
														self.assertIsInstance(_lowercase							,    Image.Image   )

								# Test not batched input
								snake_case_   : Tuple						=			image_processing(image_inputs[0]							,    return_tensors="""pt"""   ).pixel_values
								self.assertEqual(
								    encoded_images.shape							,    (
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )							,    )

								# Test batched
								snake_case_   : int						=			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,
								        self.image_processor_tester.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )							,    )





		def    UpperCAmelCase__							(					self   ) ->						str:







								'''simple docstring'''

								snake_case_   : Dict						=			self.image_processing_class(**self.image_processor_dict   )
								# create random numpy tensors
								snake_case_   : Tuple						=			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
								snake_case_   : List[str]						=			image_processing(image_inputs[0]							,    return_tensors="""pt"""   ).pixel_values
								self.assertEqual(
								    encoded_images.shape							,    (
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )							,    )

								# Test batched
								snake_case_   : 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,
								        self.image_processor_tester.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )							,    )





		def    UpperCAmelCase__							(					self   ) ->						Optional[Any]:







								'''simple docstring'''

								snake_case_   : Union[str, Any]						=			self.image_processing_class(**self.image_processor_dict   )
								# create random PyTorch tensors
								snake_case_   : 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
								snake_case_   : Optional[int]						=			image_processing(image_inputs[0]							,    return_tensors="""pt"""   ).pixel_values
								self.assertEqual(
								    encoded_images.shape							,    (
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )							,    )

								# Test batched
								snake_case_   : Dict						=			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,
								        self.image_processor_tester.crop_size["""height"""],
								        self.image_processor_tester.crop_size["""width"""],
								    )							,    )






"""simple docstring"""





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

from ...utils import logging


__lowerCAmelCase       :					Optional[int]      					=					logging.get_logger(__name__)
def 	__lowerCAmelCase						(		__UpperCamelCase      :			Tuple					, __UpperCamelCase      :			Optional[Any]    ):


						'''simple docstring'''






						snake_case_   : Union[str, Any]						=			nn.functional.normalize(__UpperCamelCase    )
						snake_case_   : Tuple						=			nn.functional.normalize(__UpperCamelCase    )
						return torch.mm(__UpperCamelCase					, normalized_text_embeds.t()    )






class    _lowerCAmelCase    (				SCREAMING_SNAKE_CASE__		):





		"""simple docstring"""






		_lowerCamelCase			  =       CLIPConfig

		_lowerCamelCase			  =       ['''CLIPEncoderLayer''']





		def __init__(					self							,    _lowercase   ) ->						Union[str, Any]:







								'''simple docstring'''

								super().__init__(_lowercase   )

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

								snake_case_   : Optional[Any]						=			nn.Parameter(torch.ones(1_7							,    config.projection_dim   )							,    requires_grad=_lowercase   )
								snake_case_   : Dict						=			nn.Parameter(torch.ones(3							,    config.projection_dim   )							,    requires_grad=_lowercase   )

								snake_case_   : Any						=			nn.Parameter(torch.ones(1_7   )							,    requires_grad=_lowercase   )
								snake_case_   : List[str]						=			nn.Parameter(torch.ones(3   )							,    requires_grad=_lowercase   )





		@torch.no_grad()
		def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase   ) ->						Any:







								'''simple docstring'''

								snake_case_   : int						=			self.vision_model(_lowercase   )[1]  # pooled_output
								snake_case_   : str						=			self.visual_projection(_lowercase   )

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

								snake_case_   : Any						=			[]
								snake_case_   : Any						=			image_embeds.shape[0]
								for i in range(_lowercase   ):
														snake_case_   : List[Any]						=			{"""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
														snake_case_   : int						=			0.0

														for concept_idx in range(len(special_cos_dist[0]   )   ):
																				snake_case_   : List[str]						=			special_cos_dist[i][concept_idx]
																				snake_case_   : Union[str, Any]						=			self.special_care_embeds_weights[concept_idx].item()
																				snake_case_   : Tuple						=			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]}   )
																										snake_case_   : Dict						=			0.01

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

														result.append(_lowercase   )

								snake_case_   : Union[str, Any]						=			[len(res["""bad_concepts"""]   ) > 0 for res in result]

								return images, has_nsfw_concepts





		@torch.no_grad()
		def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase   ) ->						Tuple:







								'''simple docstring'''

								snake_case_   : Optional[Any]						=			self.vision_model(_lowercase   )[1]  # pooled_output
								snake_case_   : List[str]						=			self.visual_projection(_lowercase   )

								snake_case_   : str						=			cosine_distance(_lowercase							,    self.special_care_embeds   )
								snake_case_   : Optional[int]						=			cosine_distance(_lowercase							,    self.concept_embeds   )

								# increase this value to create a stronger `nsfw` filter
								# at the cost of increasing the possibility of filtering benign images
								snake_case_   : Tuple						=			0.0

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

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

								return images, has_nsfw_concepts

