


import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
 from PIL import Image

 from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
 import torch

if is_tf_available():
 import tensorflow as tf

@require_vision
@require_torchvision
class   A__ (   unittest.TestCase     ):







    """simple docstring"""

    def 		a_      (	self   ):
         snake_case					=    tempfile.mkdtemp()
         snake_case					=    SamImageProcessor()
         snake_case					=    SamProcessor(__snake_case   )
         processor.save_pretrained(self.tmpdirname   )
    def 		a_      (	self      , **__snake_case   ):
         return AutoProcessor.from_pretrained(self.tmpdirname      , **__snake_case   ).image_processor
    def 		a_      (	self   ):
         shutil.rmtree(self.tmpdirname   )
    def 		a_      (	self   ):
         snake_case					=    [np.random.randint(2_5_5      , size=(3, 3_0, 4_0_0)      , dtype=np.uinta   )]

         snake_case					=    [Image.fromarray(np.moveaxis(__snake_case      , 0      , -1   )   ) for x in image_inputs]

         return image_inputs
    def 		a_      (	self   ):
         snake_case					=    SamProcessor(image_processor=self.get_image_processor()   )
         processor.save_pretrained(self.tmpdirname   )

         snake_case					=    self.get_image_processor(do_normalize=__snake_case      , padding_value=1.0   )

         snake_case					=    SamProcessor.from_pretrained(self.tmpdirname      , do_normalize=__snake_case      , padding_value=1.0   )

         self.assertEqual(processor.image_processor.to_json_string()      , image_processor_add_kwargs.to_json_string()   )
         self.assertIsInstance(processor.image_processor      , __snake_case   )
    def 		a_      (	self   ):
         snake_case					=    self.get_image_processor()

         snake_case					=    SamProcessor(image_processor=__snake_case   )

         snake_case					=    self.prepare_image_inputs()

         snake_case					=    image_processor(__snake_case      , return_tensors='''np'''   )
         snake_case					=    processor(images=__snake_case      , return_tensors='''np'''   )

         input_feat_extract.pop('''original_sizes'''   )  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop('''reshaped_input_sizes'''   )  # pop original_sizes as it is popped in the processor

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




    @require_torch
    def 		a_      (	self   ):
         snake_case					=    self.get_image_processor()

         snake_case					=    SamProcessor(image_processor=__snake_case   )
         snake_case					=    [torch.ones((1, 3, 5, 5)   )]

         snake_case					=    [[1_7_6_4, 2_6_4_6]]

         snake_case					=    [[6_8_3, 1_0_2_4]]
         snake_case					=    processor.post_process_masks(__snake_case      , __snake_case      , __snake_case   )
         self.assertEqual(masks[0].shape      , (1, 3, 1_7_6_4, 2_6_4_6)   )

         snake_case					=    processor.post_process_masks(
             __snake_case      , torch.tensor(__snake_case   )      , torch.tensor(__snake_case   )   )
         self.assertEqual(masks[0].shape      , (1, 3, 1_7_6_4, 2_6_4_6)   )

         # should also work with np
         snake_case					=    [np.ones((1, 3, 5, 5)   )]
         snake_case					=    processor.post_process_masks(__snake_case      , np.array(__snake_case   )      , np.array(__snake_case   )   )

         self.assertEqual(masks[0].shape      , (1, 3, 1_7_6_4, 2_6_4_6)   )

         snake_case					=    [[1, 0], [0, 1]]
         with self.assertRaises(__snake_case   ):
              snake_case					=    processor.post_process_masks(__snake_case      , np.array(__snake_case   )      , np.array(__snake_case   )   )






@require_vision
@require_tf
class   A__ (   unittest.TestCase     ):







    """simple docstring"""

    def 		a_      (	self   ):
         snake_case					=    tempfile.mkdtemp()
         snake_case					=    SamImageProcessor()
         snake_case					=    SamProcessor(__snake_case   )
         processor.save_pretrained(self.tmpdirname   )
    def 		a_      (	self      , **__snake_case   ):
         return AutoProcessor.from_pretrained(self.tmpdirname      , **__snake_case   ).image_processor
    def 		a_      (	self   ):
         shutil.rmtree(self.tmpdirname   )
    def 		a_      (	self   ):
         snake_case					=    [np.random.randint(2_5_5      , size=(3, 3_0, 4_0_0)      , dtype=np.uinta   )]

         snake_case					=    [Image.fromarray(np.moveaxis(__snake_case      , 0      , -1   )   ) for x in image_inputs]

         return image_inputs
    def 		a_      (	self   ):
         snake_case					=    SamProcessor(image_processor=self.get_image_processor()   )
         processor.save_pretrained(self.tmpdirname   )

         snake_case					=    self.get_image_processor(do_normalize=__snake_case      , padding_value=1.0   )

         snake_case					=    SamProcessor.from_pretrained(self.tmpdirname      , do_normalize=__snake_case      , padding_value=1.0   )

         self.assertEqual(processor.image_processor.to_json_string()      , image_processor_add_kwargs.to_json_string()   )
         self.assertIsInstance(processor.image_processor      , __snake_case   )
    def 		a_      (	self   ):
         snake_case					=    self.get_image_processor()

         snake_case					=    SamProcessor(image_processor=__snake_case   )

         snake_case					=    self.prepare_image_inputs()

         snake_case					=    image_processor(__snake_case      , return_tensors='''np'''   )
         snake_case					=    processor(images=__snake_case      , return_tensors='''np'''   )

         input_feat_extract.pop('''original_sizes'''   )  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop('''reshaped_input_sizes'''   )  # pop reshaped_input_sizes as it is popped in the processor

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




    @require_tf
    def 		a_      (	self   ):
         snake_case					=    self.get_image_processor()

         snake_case					=    SamProcessor(image_processor=__snake_case   )
         snake_case					=    [tf.ones((1, 3, 5, 5)   )]

         snake_case					=    [[1_7_6_4, 2_6_4_6]]

         snake_case					=    [[6_8_3, 1_0_2_4]]
         snake_case					=    processor.post_process_masks(__snake_case      , __snake_case      , __snake_case      , return_tensors='''tf'''   )
         self.assertEqual(masks[0].shape      , (1, 3, 1_7_6_4, 2_6_4_6)   )

         snake_case					=    processor.post_process_masks(
             __snake_case      , tf.convert_to_tensor(__snake_case   )      , tf.convert_to_tensor(__snake_case   )      , return_tensors='''tf'''      , )
         self.assertEqual(masks[0].shape      , (1, 3, 1_7_6_4, 2_6_4_6)   )

         # should also work with np
         snake_case					=    [np.ones((1, 3, 5, 5)   )]
         snake_case					=    processor.post_process_masks(
             __snake_case      , np.array(__snake_case   )      , np.array(__snake_case   )      , return_tensors='''tf'''   )

         self.assertEqual(masks[0].shape      , (1, 3, 1_7_6_4, 2_6_4_6)   )

         snake_case					=    [[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError   ):
              snake_case					=    processor.post_process_masks(
                  __snake_case      , np.array(__snake_case   )      , np.array(__snake_case   )      , return_tensors='''tf'''   )






@require_vision
@require_torchvision
class   A__ (   unittest.TestCase     ):







    """simple docstring"""

    def 		a_      (	self   ):
         snake_case					=    tempfile.mkdtemp()
         snake_case					=    SamImageProcessor()
         snake_case					=    SamProcessor(__snake_case   )
         processor.save_pretrained(self.tmpdirname   )
    def 		a_      (	self      , **__snake_case   ):
         return AutoProcessor.from_pretrained(self.tmpdirname      , **__snake_case   ).image_processor
    def 		a_      (	self   ):
         shutil.rmtree(self.tmpdirname   )
    def 		a_      (	self   ):
         snake_case					=    [np.random.randint(2_5_5      , size=(3, 3_0, 4_0_0)      , dtype=np.uinta   )]

         snake_case					=    [Image.fromarray(np.moveaxis(__snake_case      , 0      , -1   )   ) for x in image_inputs]

         return image_inputs
    @is_pt_tf_cross_test
    def 		a_      (	self   ):
         snake_case					=    self.get_image_processor()

         snake_case					=    SamProcessor(image_processor=__snake_case   )
         snake_case					=    np.random.randint(0      , 2      , size=(1, 3, 5, 5)   ).astype(np.floataa   )
         snake_case					=    [tf.convert_to_tensor(__snake_case   )]
         snake_case					=    [torch.tensor(__snake_case   )]

         snake_case					=    [[1_7_6_4, 2_6_4_6]]

         snake_case					=    [[6_8_3, 1_0_2_4]]
         snake_case					=    processor.post_process_masks(
             __snake_case      , __snake_case      , __snake_case      , return_tensors='''tf'''   )
         snake_case					=    processor.post_process_masks(
             __snake_case      , __snake_case      , __snake_case      , return_tensors='''pt'''   )

         self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()   )   )




    @is_pt_tf_cross_test
    def 		a_      (	self   ):
         snake_case					=    self.get_image_processor()

         snake_case					=    SamProcessor(image_processor=__snake_case   )

         snake_case					=    self.prepare_image_inputs()

         snake_case					=    image_processor(__snake_case      , return_tensors='''pt'''   )['''pixel_values'''].numpy()
         snake_case					=    processor(images=__snake_case      , return_tensors='''pt'''   )['''pixel_values'''].numpy()

         snake_case					=    image_processor(__snake_case      , return_tensors='''tf'''   )['''pixel_values'''].numpy()
         snake_case					=    processor(images=__snake_case      , return_tensors='''tf'''   )['''pixel_values'''].numpy()

         self.assertTrue(np.allclose(__snake_case      , __snake_case   )   )
         self.assertTrue(np.allclose(__snake_case      , __snake_case   )   )
         self.assertTrue(np.allclose(__snake_case      , __snake_case   )   )








import warnings

from ...utils import logging
from .image_processing_beit import BeitImageProcessor


_SCREAMING_SNAKE_CASE      :			List[str]      				=   logging.get_logger(__name__)

class   A__ (   snake_case__     ):







    """simple docstring"""

    def __init__(	self      , *__snake_case      , **__snake_case   ):
         warnings.warn(
             '''The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'''
             ''' use BeitImageProcessor instead.'''      , __snake_case      , )
         super().__init__(*__snake_case      , **__snake_case   )




