





import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
		from PIL import Image

		from transformers import OwlViTImageProcessor, OwlViTProcessor




@require_vision
class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					tempfile.mkdtemp()

									# fmt: off
									__a	:  Optional[int]         =					['''''', '''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>''']
									# fmt: on
									__a	:  int         =					dict(zip(_UpperCAmelCase		,     range(len(_UpperCAmelCase       )       )       )       )
									__a	:  Tuple         =					['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', '''''']
									__a	:  Any         =					{'''unk_token''': '''<unk>'''}

									__a	:  str         =					os.path.join(self.tmpdirname		,     VOCAB_FILES_NAMES['''vocab_file''']       )
									__a	:  Optional[int]         =					os.path.join(self.tmpdirname		,     VOCAB_FILES_NAMES['''merges_file''']       )
									with open(self.vocab_file		,     '''w'''		,     encoding='''utf-8'''       ) as fp:
													fp.write(json.dumps(_UpperCAmelCase       ) + '''\n'''       )
									with open(self.merges_file		,     '''w'''		,     encoding='''utf-8'''       ) as fp:
													fp.write('''\n'''.join(_UpperCAmelCase       )       )

									__a	:  Union[str, Any]         =					{
									    '''do_resize''': True,
									    '''size''': 20,
									    '''do_center_crop''': True,
									    '''crop_size''': 18,
									    '''do_normalize''': True,
									    '''image_mean''': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
									    '''image_std''': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
									}
									__a	:  List[Any]         =					os.path.join(self.tmpdirname		,     _UpperCAmelCase       )
									with open(self.image_processor_file		,     '''w'''		,     encoding='''utf-8'''       ) as fp:
													json.dump(_UpperCAmelCase		,     _UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     **_UpperCAmelCase       ):
									return CLIPTokenizer.from_pretrained(self.tmpdirname		,     pad_token='''!'''		,     **_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     **_UpperCAmelCase       ):
									return CLIPTokenizerFast.from_pretrained(self.tmpdirname		,     pad_token='''!'''		,     **_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     **_UpperCAmelCase       ):
									return OwlViTImageProcessor.from_pretrained(self.tmpdirname		,     **_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									shutil.rmtree(self.tmpdirname       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					[np.random.randint(255		,     size=(3, 30, 400)		,     dtype=np.uinta       )]

									__a	:  Optional[Any]         =					[Image.fromarray(np.moveaxis(_UpperCAmelCase		,     0		,     -1       )       ) for x in image_inputs]

									return image_inputs







					def 	_lowerCamelCase						(  self       ):
									__a	:  Any         =					self.get_tokenizer()
									__a	:  Union[str, Any]         =					self.get_rust_tokenizer()
									__a	:  int         =					self.get_image_processor()

									__a	:  Optional[Any]         =					OwlViTProcessor(tokenizer=_UpperCAmelCase		,     image_processor=_UpperCAmelCase       )
									processor_slow.save_pretrained(self.tmpdirname       )
									__a	:  Optional[int]         =					OwlViTProcessor.from_pretrained(self.tmpdirname		,     use_fast=_UpperCAmelCase       )

									__a	:  Dict         =					OwlViTProcessor(tokenizer=_UpperCAmelCase		,     image_processor=_UpperCAmelCase       )
									processor_fast.save_pretrained(self.tmpdirname       )
									__a	:  str         =					OwlViTProcessor.from_pretrained(self.tmpdirname       )

									self.assertEqual(processor_slow.tokenizer.get_vocab()		,     tokenizer_slow.get_vocab()       )
									self.assertEqual(processor_fast.tokenizer.get_vocab()		,     tokenizer_fast.get_vocab()       )
									self.assertEqual(tokenizer_slow.get_vocab()		,     tokenizer_fast.get_vocab()       )
									self.assertIsInstance(processor_slow.tokenizer		,     _UpperCAmelCase       )
									self.assertIsInstance(processor_fast.tokenizer		,     _UpperCAmelCase       )

									self.assertEqual(processor_slow.image_processor.to_json_string()		,     image_processor.to_json_string()       )
									self.assertEqual(processor_fast.image_processor.to_json_string()		,     image_processor.to_json_string()       )
									self.assertIsInstance(processor_slow.image_processor		,     _UpperCAmelCase       )
									self.assertIsInstance(processor_fast.image_processor		,     _UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					OwlViTProcessor(tokenizer=self.get_tokenizer()		,     image_processor=self.get_image_processor()       )
									processor.save_pretrained(self.tmpdirname       )

									__a	:  Dict         =					self.get_tokenizer(bos_token='''(BOS)'''		,     eos_token='''(EOS)'''       )
									__a	:  int         =					self.get_image_processor(do_normalize=_UpperCAmelCase       )

									__a	:  List[Any]         =					OwlViTProcessor.from_pretrained(
									    self.tmpdirname		,     bos_token='''(BOS)'''		,     eos_token='''(EOS)'''		,     do_normalize=_UpperCAmelCase       )

									self.assertEqual(processor.tokenizer.get_vocab()		,     tokenizer_add_kwargs.get_vocab()       )
									self.assertIsInstance(processor.tokenizer		,     _UpperCAmelCase       )

									self.assertEqual(processor.image_processor.to_json_string()		,     image_processor_add_kwargs.to_json_string()       )
									self.assertIsInstance(processor.image_processor		,     _UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					self.get_image_processor()
									__a	:  Dict         =					self.get_tokenizer()

									__a	:  Dict         =					OwlViTProcessor(tokenizer=_UpperCAmelCase		,     image_processor=_UpperCAmelCase       )

									__a	:  Optional[Any]         =					self.prepare_image_inputs()

									__a	:  Tuple         =					image_processor(_UpperCAmelCase		,     return_tensors='''np'''       )
									__a	:  List[str]         =					processor(images=_UpperCAmelCase		,     return_tensors='''np'''       )

									for key in input_image_proc.keys():
													self.assertAlmostEqual(input_image_proc[key].sum()		,     input_processor[key].sum()		,     delta=1e-2       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					self.get_image_processor()
									__a	:  Any         =					self.get_tokenizer()

									__a	:  Union[str, Any]         =					OwlViTProcessor(tokenizer=_UpperCAmelCase		,     image_processor=_UpperCAmelCase       )

									__a	:  List[Any]         =					'''lower newer'''

									__a	:  List[str]         =					processor(text=_UpperCAmelCase		,     return_tensors='''np'''       )

									__a	:  List[str]         =					tokenizer(_UpperCAmelCase		,     return_tensors='''np'''       )

									for key in encoded_tok.keys():
													self.assertListEqual(encoded_tok[key][0].tolist()		,     encoded_processor[key][0].tolist()       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  str         =					self.get_image_processor()
									__a	:  Optional[Any]         =					self.get_tokenizer()

									__a	:  Union[str, Any]         =					OwlViTProcessor(tokenizer=_UpperCAmelCase		,     image_processor=_UpperCAmelCase       )

									__a	:  Dict         =					'''lower newer'''
									__a	:  List[str]         =					self.prepare_image_inputs()

									__a	:  Any         =					processor(text=_UpperCAmelCase		,     images=_UpperCAmelCase       )

									self.assertListEqual(list(inputs.keys()       )		,     ['''input_ids''', '''attention_mask''', '''pixel_values''']       )

									# test if it raises when no input is passed
									with pytest.raises(_UpperCAmelCase       ):
													processor()







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[Any]         =					'''google/owlvit-base-patch32'''
									__a	:  List[Any]         =					OwlViTProcessor.from_pretrained(_UpperCAmelCase       )

									__a	:  int         =					['''cat''', '''nasa badge''']
									__a	:  Tuple         =					processor(text=_UpperCAmelCase       )

									__a	:  Any         =					16
									self.assertListEqual(list(inputs.keys()       )		,     ['''input_ids''', '''attention_mask''']       )
									self.assertEqual(inputs['''input_ids'''].shape		,     (2, seq_length)       )

									# test if it raises when no input is passed
									with pytest.raises(_UpperCAmelCase       ):
													processor()







					def 	_lowerCamelCase						(  self       ):
									__a	:  Any         =					'''google/owlvit-base-patch32'''
									__a	:  Any         =					OwlViTProcessor.from_pretrained(_UpperCAmelCase       )

									__a	:  Optional[int]         =					[['''cat''', '''nasa badge'''], ['''person''']]
									__a	:  List[Any]         =					processor(text=_UpperCAmelCase       )

									__a	:  Optional[int]         =					16
									__a	:  List[str]         =					len(_UpperCAmelCase       )
									__a	:  int         =					max([len(_UpperCAmelCase       ) for texts in input_texts]       )

									self.assertListEqual(list(inputs.keys()       )		,     ['''input_ids''', '''attention_mask''']       )
									self.assertEqual(inputs['''input_ids'''].shape		,     (batch_size * num_max_text_queries, seq_length)       )

									# test if it raises when no input is passed
									with pytest.raises(_UpperCAmelCase       ):
													processor()







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					'''google/owlvit-base-patch32'''
									__a	:  Any         =					OwlViTProcessor.from_pretrained(_UpperCAmelCase       )

									__a	:  int         =					['''cat''', '''nasa badge''']
									__a	:  List[Any]         =					processor(text=_UpperCAmelCase       )

									__a	:  str         =					16
									__a	:  List[Any]         =					inputs['''input_ids''']
									__a	:  Union[str, Any]         =					[
									    [49406, 2368, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
									    [49406, 6841, 11301, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
									]

									self.assertListEqual(list(inputs.keys()       )		,     ['''input_ids''', '''attention_mask''']       )
									self.assertEqual(inputs['''input_ids'''].shape		,     (2, seq_length)       )
									self.assertListEqual(list(input_ids[0]       )		,     predicted_ids[0]       )
									self.assertListEqual(list(input_ids[1]       )		,     predicted_ids[1]       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					self.get_image_processor()
									__a	:  Tuple         =					self.get_tokenizer()

									__a	:  Tuple         =					OwlViTProcessor(tokenizer=_UpperCAmelCase		,     image_processor=_UpperCAmelCase       )

									__a	:  Dict         =					self.prepare_image_inputs()
									__a	:  Union[str, Any]         =					self.prepare_image_inputs()

									__a	:  Tuple         =					processor(images=_UpperCAmelCase		,     query_images=_UpperCAmelCase       )

									self.assertListEqual(list(inputs.keys()       )		,     ['''query_pixel_values''', '''pixel_values''']       )

									# test if it raises when no input is passed
									with pytest.raises(_UpperCAmelCase       ):
													processor()







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					self.get_image_processor()
									__a	:  int         =					self.get_tokenizer()

									__a	:  str         =					OwlViTProcessor(tokenizer=_UpperCAmelCase		,     image_processor=_UpperCAmelCase       )

									__a	:  List[str]         =					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

									__a	:  Optional[int]         =					processor.batch_decode(_UpperCAmelCase       )
									__a	:  str         =					tokenizer.batch_decode(_UpperCAmelCase       )

									self.assertListEqual(_UpperCAmelCase		,     _UpperCAmelCase       )






"""simple docstring"""






import hashlib
import unittest
from typing import Dict

import numpy as np

from transformers import (
    MODEL_FOR_MASK_GENERATION_MAPPING,
    TF_MODEL_FOR_MASK_GENERATION_MAPPING,
    is_vision_available,
    pipeline,
)
from transformers.pipelines import MaskGenerationPipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)


if is_vision_available():
		from PIL import Image
else:




		class 			__lowercase     :







							'''simple docstring'''




							@staticmethod
							def 	_lowerCamelCase						(  *_UpperCAmelCase		,     **_UpperCAmelCase       ):
											pass

def       __A (     a_  :Image)						->						str:
				__a	:  List[str]         =					hashlib.mda(image.tobytes())
				return m.hexdigest()[:10]

def       __A (     a_  :Image)						->						Dict:
				__a	:  Any         =					np.array(a_)
				__a	:  Tuple         =					npimg.shape
				return {"hash": hashimage(a_), "shape": shape}






@is_pipeline_test
@require_vision
@require_torch
class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					dict(
					    (list(MODEL_FOR_MASK_GENERATION_MAPPING.items()							) if MODEL_FOR_MASK_GENERATION_MAPPING else [])							)
					__lowerCAmelCase					=					dict(
					    (list(TF_MODEL_FOR_MASK_GENERATION_MAPPING.items()							) if TF_MODEL_FOR_MASK_GENERATION_MAPPING else [])							)







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  List[str]         =					MaskGenerationPipeline(model=_UpperCAmelCase		,     image_processor=_UpperCAmelCase       )
									return image_segmenter, [
									    "./tests/fixtures/tests_samples/COCO/000000039769.png",
									    "./tests/fixtures/tests_samples/COCO/000000039769.png",
									]







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									pass







					@require_tf
					@unittest.skip('''Image segmentation not implemented in TF'''       )
					def 	_lowerCamelCase						(  self       ):
									pass







					@slow
					@require_torch
					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					pipeline('''mask-generation'''		,     model='''facebook/sam-vit-huge'''       )

									__a	:  Optional[Any]         =					image_segmenter('''http://images.cocodataset.org/val2017/000000039769.jpg'''		,     points_per_batch=256       )

									# Shortening by hashing
									__a	:  Optional[int]         =					[]
									for i, o in enumerate(outputs['''masks''']       ):
													new_outupt += [{"mask": mask_to_test_readable(_UpperCAmelCase       ), "scores": outputs["scores"][i]}]

									# fmt: off
									self.assertEqual(
									    nested_simplify(_UpperCAmelCase		,     decimals=4       )		,     [
									        {'''mask''': {'''hash''': '''115ad19f5f''', '''shape''': (480, 640)}, '''scores''': 1.0_4_4_4},
									        {'''mask''': {'''hash''': '''6affa964c6''', '''shape''': (480, 640)}, '''scores''': 1.0_2_1},
									        {'''mask''': {'''hash''': '''dfe28a0388''', '''shape''': (480, 640)}, '''scores''': 1.0_1_6_7},
									        {'''mask''': {'''hash''': '''c0a5f4a318''', '''shape''': (480, 640)}, '''scores''': 1.0_1_3_2},
									        {'''mask''': {'''hash''': '''fe8065c197''', '''shape''': (480, 640)}, '''scores''': 1.0_0_5_3},
									        {'''mask''': {'''hash''': '''e2d0b7a0b7''', '''shape''': (480, 640)}, '''scores''': 0.9_9_6_7},
									        {'''mask''': {'''hash''': '''453c7844bd''', '''shape''': (480, 640)}, '''scores''': 0.9_9_3},
									        {'''mask''': {'''hash''': '''3d44f2926d''', '''shape''': (480, 640)}, '''scores''': 0.9_9_0_9},
									        {'''mask''': {'''hash''': '''64033ddc3f''', '''shape''': (480, 640)}, '''scores''': 0.9_8_7_9},
									        {'''mask''': {'''hash''': '''801064ff79''', '''shape''': (480, 640)}, '''scores''': 0.9_8_3_4},
									        {'''mask''': {'''hash''': '''6172f276ef''', '''shape''': (480, 640)}, '''scores''': 0.9_7_1_6},
									        {'''mask''': {'''hash''': '''b49e60e084''', '''shape''': (480, 640)}, '''scores''': 0.9_6_1_2},
									        {'''mask''': {'''hash''': '''a811e775fd''', '''shape''': (480, 640)}, '''scores''': 0.9_5_9_9},
									        {'''mask''': {'''hash''': '''a6a8ebcf4b''', '''shape''': (480, 640)}, '''scores''': 0.9_5_5_2},
									        {'''mask''': {'''hash''': '''9d8257e080''', '''shape''': (480, 640)}, '''scores''': 0.9_5_3_2},
									        {'''mask''': {'''hash''': '''32de6454a8''', '''shape''': (480, 640)}, '''scores''': 0.9_5_1_6},
									        {'''mask''': {'''hash''': '''af3d4af2c8''', '''shape''': (480, 640)}, '''scores''': 0.9_4_9_9},
									        {'''mask''': {'''hash''': '''3c6db475fb''', '''shape''': (480, 640)}, '''scores''': 0.9_4_8_3},
									        {'''mask''': {'''hash''': '''c290813fb9''', '''shape''': (480, 640)}, '''scores''': 0.9_4_6_4},
									        {'''mask''': {'''hash''': '''b6f0b8f606''', '''shape''': (480, 640)}, '''scores''': 0.9_4_3},
									        {'''mask''': {'''hash''': '''92ce16bfdf''', '''shape''': (480, 640)}, '''scores''': 0.9_4_3},
									        {'''mask''': {'''hash''': '''c749b25868''', '''shape''': (480, 640)}, '''scores''': 0.9_4_0_8},
									        {'''mask''': {'''hash''': '''efb6cab859''', '''shape''': (480, 640)}, '''scores''': 0.9_3_3_5},
									        {'''mask''': {'''hash''': '''1ff2eafb30''', '''shape''': (480, 640)}, '''scores''': 0.9_3_2_6},
									        {'''mask''': {'''hash''': '''788b798e24''', '''shape''': (480, 640)}, '''scores''': 0.9_2_6_2},
									        {'''mask''': {'''hash''': '''abea804f0e''', '''shape''': (480, 640)}, '''scores''': 0.8_9_9_9},
									        {'''mask''': {'''hash''': '''7b9e8ddb73''', '''shape''': (480, 640)}, '''scores''': 0.8_9_8_6},
									        {'''mask''': {'''hash''': '''cd24047c8a''', '''shape''': (480, 640)}, '''scores''': 0.8_9_8_4},
									        {'''mask''': {'''hash''': '''6943e6bcbd''', '''shape''': (480, 640)}, '''scores''': 0.8_8_7_3},
									        {'''mask''': {'''hash''': '''b5f47c9191''', '''shape''': (480, 640)}, '''scores''': 0.8_8_7_1}
									    ]		,     )
									# fmt: on







					@require_torch
					@slow
					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					'''facebook/sam-vit-huge'''
									__a	:  Tuple         =					pipeline('''mask-generation'''		,     model=_UpperCAmelCase       )

									__a	:  List[Any]         =					image_segmenter(
									    '''http://images.cocodataset.org/val2017/000000039769.jpg'''		,     pred_iou_thresh=1		,     points_per_batch=256       )

									# Shortening by hashing
									__a	:  Optional[int]         =					[]
									for i, o in enumerate(outputs['''masks''']       ):
													new_outupt += [{"mask": mask_to_test_readable(_UpperCAmelCase       ), "scores": outputs["scores"][i]}]

									self.assertEqual(
									    nested_simplify(_UpperCAmelCase		,     decimals=4       )		,     [
									        {'''mask''': {'''hash''': '''115ad19f5f''', '''shape''': (480, 640)}, '''scores''': 1.0_4_4_4},
									        {'''mask''': {'''hash''': '''6affa964c6''', '''shape''': (480, 640)}, '''scores''': 1.0_2_1_0},
									        {'''mask''': {'''hash''': '''dfe28a0388''', '''shape''': (480, 640)}, '''scores''': 1.0_1_6_7},
									        {'''mask''': {'''hash''': '''c0a5f4a318''', '''shape''': (480, 640)}, '''scores''': 1.0_1_3_2},
									        {'''mask''': {'''hash''': '''fe8065c197''', '''shape''': (480, 640)}, '''scores''': 1.0_0_5_3},
									    ]		,     )