


"""simple docstring"""







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







    """simple docstring"""




    def 	__snake_case							(					self				:    List[str]):
      a						:		Dict      							=			tempfile.mkdtemp()

      # fmt: off
      a						:		Any      							=			["", "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						:		List[Any]      							=			dict(zip(__UpperCAmelCase				, range(len(__UpperCAmelCase))))
      a						:		Optional[int]      							=			["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
      a						:		Optional[int]      							=			{"unk_token": "<unk>"}

      a						:		int      							=			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						:		Optional[Any]      							=			{
          "do_resize": True,
          "size": 20,
          "do_center_crop": True,
          "crop_size": 18,
          "do_normalize": True,
          "image_mean": [0.48_145_466, 0.4_578_275, 0.40_821_073],
          "image_std": [0.26_862_954, 0.26_130_258, 0.27_577_711],
      }
      a						:		int      							=			os.path.join(self.tmpdirname				, __UpperCAmelCase)
      with open(self.image_processor_file				, "w"				, encoding="utf-8") as fp:
        json.dump(__UpperCAmelCase				, __UpperCAmelCase)






    def 	__snake_case							(					self				:    int				, **__UpperCAmelCase				:    int):
      return CLIPTokenizer.from_pretrained(self.tmpdirname				, pad_token="!"				, **__UpperCAmelCase)






    def 	__snake_case							(					self				:    Optional[int]				, **__UpperCAmelCase				:    Optional[Any]):
      return CLIPTokenizerFast.from_pretrained(self.tmpdirname				, pad_token="!"				, **__UpperCAmelCase)






    def 	__snake_case							(					self				:    str				, **__UpperCAmelCase				:    List[Any]):
      return OwlViTImageProcessor.from_pretrained(self.tmpdirname				, **__UpperCAmelCase)






    def 	__snake_case							(					self				:    Tuple):
      shutil.rmtree(self.tmpdirname)






    def 	__snake_case							(					self				:    Union[str, Any]):
      a						:		str      							=			[np.random.randint(255				, size=(3, 30, 400)				, dtype=np.uinta)]

      a						:		Optional[int]      							=			[Image.fromarray(np.moveaxis(__UpperCAmelCase				, 0				, -1)) for x in image_inputs]

      return image_inputs






    def 	__snake_case							(					self				:    Optional[Any]):
      a						:		Any      							=			self.get_tokenizer()
      a						:		Any      							=			self.get_rust_tokenizer()
      a						:		str      							=			self.get_image_processor()

      a						:		Dict      							=			OwlViTProcessor(tokenizer=__UpperCAmelCase				, image_processor=__UpperCAmelCase)
      processor_slow.save_pretrained(self.tmpdirname)
      a						:		Optional[Any]      							=			OwlViTProcessor.from_pretrained(self.tmpdirname				, use_fast=__UpperCAmelCase)

      a						:		str      							=			OwlViTProcessor(tokenizer=__UpperCAmelCase				, image_processor=__UpperCAmelCase)
      processor_fast.save_pretrained(self.tmpdirname)
      a						:		Tuple      							=			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 	__snake_case							(					self				:    Optional[Any]):
      a						:		int      							=			OwlViTProcessor(tokenizer=self.get_tokenizer()				, image_processor=self.get_image_processor())
      processor.save_pretrained(self.tmpdirname)

      a						:		Tuple      							=			self.get_tokenizer(bos_token="(BOS)"				, eos_token="(EOS)")
      a						:		Optional[int]      							=			self.get_image_processor(do_normalize=__UpperCAmelCase)

      a						:		Optional[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 	__snake_case							(					self				:    Union[str, Any]):
      a						:		Any      							=			self.get_image_processor()
      a						:		Dict      							=			self.get_tokenizer()

      a						:		Optional[int]      							=			OwlViTProcessor(tokenizer=__UpperCAmelCase				, image_processor=__UpperCAmelCase)

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

      a						:		Dict      							=			image_processor(__UpperCAmelCase				, return_tensors="np")
      a						:		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 	__snake_case							(					self				:    int):
      a						:		int      							=			self.get_image_processor()
      a						:		Dict      							=			self.get_tokenizer()

      a						:		Optional[int]      							=			OwlViTProcessor(tokenizer=__UpperCAmelCase				, image_processor=__UpperCAmelCase)

      a						:		Tuple      							=			"lower newer"

      a						:		Tuple      							=			processor(text=__UpperCAmelCase				, return_tensors="np")

      a						:		Tuple      							=			tokenizer(__UpperCAmelCase				, return_tensors="np")

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






    def 	__snake_case							(					self				:    str):
      a						:		Any      							=			self.get_image_processor()
      a						:		str      							=			self.get_tokenizer()

      a						:		Any      							=			OwlViTProcessor(tokenizer=__UpperCAmelCase				, image_processor=__UpperCAmelCase)

      a						:		int      							=			"lower newer"
      a						:		int      							=			self.prepare_image_inputs()

      a						:		Optional[int]      							=			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 	__snake_case							(					self				:    Optional[Any]):
      a						:		Tuple      							=			"google/owlvit-base-patch32"
      a						:		Optional[Any]      							=			OwlViTProcessor.from_pretrained(__UpperCAmelCase)

      a						:		Dict      							=			["cat", "nasa badge"]
      a						:		Union[str, Any]      							=			processor(text=__UpperCAmelCase)

      a						:		Optional[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 	__snake_case							(					self				:    Optional[int]):
      a						:		Dict      							=			"google/owlvit-base-patch32"
      a						:		Tuple      							=			OwlViTProcessor.from_pretrained(__UpperCAmelCase)

      a						:		Optional[Any]      							=			[["cat", "nasa badge"], ["person"]]
      a						:		Tuple      							=			processor(text=__UpperCAmelCase)

      a						:		Any      							=			16
      a						:		Any      							=			len(__UpperCAmelCase)
      a						:		Optional[Any]      							=			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 	__snake_case							(					self				:    Optional[Any]):
      a						:		Optional[int]      							=			"google/owlvit-base-patch32"
      a						:		Tuple      							=			OwlViTProcessor.from_pretrained(__UpperCAmelCase)

      a						:		str      							=			["cat", "nasa badge"]
      a						:		Union[str, Any]      							=			processor(text=__UpperCAmelCase)

      a						:		Optional[Any]      							=			16
      a						:		Dict      							=			inputs["input_ids"]
      a						:		List[str]      							=			[
          [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 	__snake_case							(					self				:    Any):
      a						:		Tuple      							=			self.get_image_processor()
      a						:		Union[str, Any]      							=			self.get_tokenizer()

      a						:		str      							=			OwlViTProcessor(tokenizer=__UpperCAmelCase				, image_processor=__UpperCAmelCase)

      a						:		str      							=			self.prepare_image_inputs()
      a						:		Optional[int]      							=			self.prepare_image_inputs()

      a						:		Any      							=			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 	__snake_case							(					self				:    Union[str, Any]):
      a						:		int      							=			self.get_image_processor()
      a						:		Optional[Any]      							=			self.get_tokenizer()

      a						:		List[str]      							=			OwlViTProcessor(tokenizer=__UpperCAmelCase				, image_processor=__UpperCAmelCase)

      a						:		Union[str, Any]      							=			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

      a						:		Any      							=			processor.batch_decode(__UpperCAmelCase)
      a						:		List[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  SCREAMING_SNAKE_CASE    :


       @staticmethod
       def 							SCREAMING_SNAKE_CASE  (						*_UpperCAmelCase						,	**_UpperCAmelCase):

            '''simple docstring'''






            pass






def        _lowerCAmelCase			(  __snake_case  : Image			)     ->     str:
     __A						: Dict      =       hashlib.mda(image.tobytes()			)
     return m.hexdigest()[:10]






def        _lowerCAmelCase			(  __snake_case  : Image			)     ->     Dict:
     __A						: Dict      =       np.array(__snake_case			)
     __A						: List[Any]      =       npimg.shape
     return {"hash": hashimage(__snake_case			), "shape": shape}







@is_pipeline_test
@require_vision
@require_torch
class  SCREAMING_SNAKE_CASE    (unittest.TestCase							):
     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 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






          __A						: Any      =       MaskGenerationPipeline(model=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)
          return image_segmenter, [
              "./tests/fixtures/tests_samples/COCO/000000039769.png",
              "./tests/fixtures/tests_samples/COCO/000000039769.png",
          ]


     def 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






          pass


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

          '''simple docstring'''






          pass


     @slow
     @require_torch
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[int]      =       pipeline('mask-generation'						,	model='facebook/sam-vit-huge')

          __A						: Tuple      =       image_segmenter('http://images.cocodataset.org/val2017/000000039769.jpg'						,	points_per_batch=256)

          # Shortening by hashing
          __A						: 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.0444},
                  {'mask': {'hash': '6affa964c6', 'shape': (480, 640)}, 'scores': 1.021},
                  {'mask': {'hash': 'dfe28a0388', 'shape': (480, 640)}, 'scores': 1.0167},
                  {'mask': {'hash': 'c0a5f4a318', 'shape': (480, 640)}, 'scores': 1.0132},
                  {'mask': {'hash': 'fe8065c197', 'shape': (480, 640)}, 'scores': 1.0053},
                  {'mask': {'hash': 'e2d0b7a0b7', 'shape': (480, 640)}, 'scores': 0.9967},
                  {'mask': {'hash': '453c7844bd', 'shape': (480, 640)}, 'scores': 0.993},
                  {'mask': {'hash': '3d44f2926d', 'shape': (480, 640)}, 'scores': 0.9909},
                  {'mask': {'hash': '64033ddc3f', 'shape': (480, 640)}, 'scores': 0.9879},
                  {'mask': {'hash': '801064ff79', 'shape': (480, 640)}, 'scores': 0.9834},
                  {'mask': {'hash': '6172f276ef', 'shape': (480, 640)}, 'scores': 0.9716},
                  {'mask': {'hash': 'b49e60e084', 'shape': (480, 640)}, 'scores': 0.9612},
                  {'mask': {'hash': 'a811e775fd', 'shape': (480, 640)}, 'scores': 0.9599},
                  {'mask': {'hash': 'a6a8ebcf4b', 'shape': (480, 640)}, 'scores': 0.9552},
                  {'mask': {'hash': '9d8257e080', 'shape': (480, 640)}, 'scores': 0.9532},
                  {'mask': {'hash': '32de6454a8', 'shape': (480, 640)}, 'scores': 0.9516},
                  {'mask': {'hash': 'af3d4af2c8', 'shape': (480, 640)}, 'scores': 0.9499},
                  {'mask': {'hash': '3c6db475fb', 'shape': (480, 640)}, 'scores': 0.9483},
                  {'mask': {'hash': 'c290813fb9', 'shape': (480, 640)}, 'scores': 0.9464},
                  {'mask': {'hash': 'b6f0b8f606', 'shape': (480, 640)}, 'scores': 0.943},
                  {'mask': {'hash': '92ce16bfdf', 'shape': (480, 640)}, 'scores': 0.943},
                  {'mask': {'hash': 'c749b25868', 'shape': (480, 640)}, 'scores': 0.9408},
                  {'mask': {'hash': 'efb6cab859', 'shape': (480, 640)}, 'scores': 0.9335},
                  {'mask': {'hash': '1ff2eafb30', 'shape': (480, 640)}, 'scores': 0.9326},
                  {'mask': {'hash': '788b798e24', 'shape': (480, 640)}, 'scores': 0.9262},
                  {'mask': {'hash': 'abea804f0e', 'shape': (480, 640)}, 'scores': 0.8999},
                  {'mask': {'hash': '7b9e8ddb73', 'shape': (480, 640)}, 'scores': 0.8986},
                  {'mask': {'hash': 'cd24047c8a', 'shape': (480, 640)}, 'scores': 0.8984},
                  {'mask': {'hash': '6943e6bcbd', 'shape': (480, 640)}, 'scores': 0.8873},
                  {'mask': {'hash': 'b5f47c9191', 'shape': (480, 640)}, 'scores': 0.8871}
              ]						,	)
          # fmt: on


     @require_torch
     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: int      =       'facebook/sam-vit-huge'
          __A						: Optional[Any]      =       pipeline('mask-generation'						,	model=_UpperCAmelCase)

          __A						: int      =       image_segmenter(
              'http://images.cocodataset.org/val2017/000000039769.jpg'						,	pred_iou_thresh=1						,	points_per_batch=256)

          # Shortening by hashing
          __A						: 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.0444},
                  {'mask': {'hash': '6affa964c6', 'shape': (480, 640)}, 'scores': 1.0210},
                  {'mask': {'hash': 'dfe28a0388', 'shape': (480, 640)}, 'scores': 1.0167},
                  {'mask': {'hash': 'c0a5f4a318', 'shape': (480, 640)}, 'scores': 1.0132},
                  {'mask': {'hash': 'fe8065c197', 'shape': (480, 640)}, 'scores': 1.0053},
              ]						,	)