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







   def 						lowerCamelCase_	(     self						:						Optional[Any]				):





    """simple docstring"""


    UpperCamelCase			  =      tempfile.mkdtemp()
    UpperCamelCase			  =      SamImageProcessor()
    UpperCamelCase			  =      SamProcessor(lowerCamelCase_				)
    processor.save_pretrained(self.tmpdirname				)







   def 						lowerCamelCase_	(     self						:						List[Any]   ,   **lowerCamelCase_						:						Optional[Any]				):





    """simple docstring"""


    return AutoProcessor.from_pretrained(self.tmpdirname   ,   **lowerCamelCase_				).image_processor







   def 						lowerCamelCase_	(     self						:						Optional[Any]				):





    """simple docstring"""


    shutil.rmtree(self.tmpdirname				)







   def 						lowerCamelCase_	(     self						:						Union[str, Any]				):





    """simple docstring"""


    UpperCamelCase			  =      [np.random.randint(255   ,   size=(3, 30, 400)   ,   dtype=np.uinta				)]

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

    return image_inputs







   def 						lowerCamelCase_	(     self						:						Union[str, Any]				):





    """simple docstring"""


    UpperCamelCase			  =      SamProcessor(image_processor=self.get_image_processor()				)
    processor.save_pretrained(self.tmpdirname				)

    UpperCamelCase			  =      self.get_image_processor(do_normalize=lowerCamelCase_   ,   padding_value=1.0				)

    UpperCamelCase			  =      SamProcessor.from_pretrained(self.tmpdirname   ,   do_normalize=lowerCamelCase_   ,   padding_value=1.0				)

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







   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    UpperCamelCase			  =      self.get_image_processor()

    UpperCamelCase			  =      SamProcessor(image_processor=lowerCamelCase_				)

    UpperCamelCase			  =      self.prepare_image_inputs()

    UpperCamelCase			  =      image_processor(lowerCamelCase_   ,   return_tensors="""np"""				)
    UpperCamelCase			  =      processor(images=lowerCamelCase_   ,   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 						lowerCamelCase_	(     self						:						Optional[Any]				):





    """simple docstring"""


    UpperCamelCase			  =      self.get_image_processor()

    UpperCamelCase			  =      SamProcessor(image_processor=lowerCamelCase_				)
    UpperCamelCase			  =      [torch.ones((1, 3, 5, 5)				)]

    UpperCamelCase			  =      [[1764, 2646]]

    UpperCamelCase			  =      [[683, 1024]]
    UpperCamelCase			  =      processor.post_process_masks(lowerCamelCase_   ,   lowerCamelCase_   ,   lowerCamelCase_				)
    self.assertEqual(masks[0].shape   ,   (1, 3, 1764, 2646)				)

    UpperCamelCase			  =      processor.post_process_masks(
        lowerCamelCase_   ,   torch.tensor(lowerCamelCase_				)   ,   torch.tensor(lowerCamelCase_				)				)
    self.assertEqual(masks[0].shape   ,   (1, 3, 1764, 2646)				)

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

    self.assertEqual(masks[0].shape   ,   (1, 3, 1764, 2646)				)

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





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







   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


    UpperCamelCase			  =      tempfile.mkdtemp()
    UpperCamelCase			  =      SamImageProcessor()
    UpperCamelCase			  =      SamProcessor(lowerCamelCase_				)
    processor.save_pretrained(self.tmpdirname				)







   def 						lowerCamelCase_	(     self						:						Optional[Any]   ,   **lowerCamelCase_						:						List[Any]				):





    """simple docstring"""


    return AutoProcessor.from_pretrained(self.tmpdirname   ,   **lowerCamelCase_				).image_processor







   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    shutil.rmtree(self.tmpdirname				)







   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


    UpperCamelCase			  =      [np.random.randint(255   ,   size=(3, 30, 400)   ,   dtype=np.uinta				)]

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

    return image_inputs







   def 						lowerCamelCase_	(     self						:						Union[str, Any]				):





    """simple docstring"""


    UpperCamelCase			  =      SamProcessor(image_processor=self.get_image_processor()				)
    processor.save_pretrained(self.tmpdirname				)

    UpperCamelCase			  =      self.get_image_processor(do_normalize=lowerCamelCase_   ,   padding_value=1.0				)

    UpperCamelCase			  =      SamProcessor.from_pretrained(self.tmpdirname   ,   do_normalize=lowerCamelCase_   ,   padding_value=1.0				)

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







   def 						lowerCamelCase_	(     self						:						Optional[int]				):





    """simple docstring"""


    UpperCamelCase			  =      self.get_image_processor()

    UpperCamelCase			  =      SamProcessor(image_processor=lowerCamelCase_				)

    UpperCamelCase			  =      self.prepare_image_inputs()

    UpperCamelCase			  =      image_processor(lowerCamelCase_   ,   return_tensors="""np"""				)
    UpperCamelCase			  =      processor(images=lowerCamelCase_   ,   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 						lowerCamelCase_	(     self						:						List[str]				):





    """simple docstring"""


    UpperCamelCase			  =      self.get_image_processor()

    UpperCamelCase			  =      SamProcessor(image_processor=lowerCamelCase_				)
    UpperCamelCase			  =      [tf.ones((1, 3, 5, 5)				)]

    UpperCamelCase			  =      [[1764, 2646]]

    UpperCamelCase			  =      [[683, 1024]]
    UpperCamelCase			  =      processor.post_process_masks(lowerCamelCase_   ,   lowerCamelCase_   ,   lowerCamelCase_   ,   return_tensors="""tf"""				)
    self.assertEqual(masks[0].shape   ,   (1, 3, 1764, 2646)				)

    UpperCamelCase			  =      processor.post_process_masks(
        lowerCamelCase_   ,   tf.convert_to_tensor(lowerCamelCase_				)   ,   tf.convert_to_tensor(lowerCamelCase_				)   ,   return_tensors="""tf"""   ,   )
    self.assertEqual(masks[0].shape   ,   (1, 3, 1764, 2646)				)

    # should also work with np
    UpperCamelCase			  =      [np.ones((1, 3, 5, 5)				)]
    UpperCamelCase			  =      processor.post_process_masks(
        lowerCamelCase_   ,   np.array(lowerCamelCase_				)   ,   np.array(lowerCamelCase_				)   ,   return_tensors="""tf"""				)

    self.assertEqual(masks[0].shape   ,   (1, 3, 1764, 2646)				)

    UpperCamelCase			  =      [[1, 0], [0, 1]]
    with self.assertRaises(tf.errors.InvalidArgumentError				):
     UpperCamelCase			  =      processor.post_process_masks(
         lowerCamelCase_   ,   np.array(lowerCamelCase_				)   ,   np.array(lowerCamelCase_				)   ,   return_tensors="""tf"""				)





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







   def 						lowerCamelCase_	(     self						:						Optional[Any]				):





    """simple docstring"""


    UpperCamelCase			  =      tempfile.mkdtemp()
    UpperCamelCase			  =      SamImageProcessor()
    UpperCamelCase			  =      SamProcessor(lowerCamelCase_				)
    processor.save_pretrained(self.tmpdirname				)







   def 						lowerCamelCase_	(     self						:						Tuple   ,   **lowerCamelCase_						:						Union[str, Any]				):





    """simple docstring"""


    return AutoProcessor.from_pretrained(self.tmpdirname   ,   **lowerCamelCase_				).image_processor







   def 						lowerCamelCase_	(     self						:						Union[str, Any]				):





    """simple docstring"""


    shutil.rmtree(self.tmpdirname				)







   def 						lowerCamelCase_	(     self						:						List[str]				):





    """simple docstring"""


    UpperCamelCase			  =      [np.random.randint(255   ,   size=(3, 30, 400)   ,   dtype=np.uinta				)]

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

    return image_inputs







   @is_pt_tf_cross_test
   def 						lowerCamelCase_	(     self						:						List[Any]				):





    """simple docstring"""


    UpperCamelCase			  =      self.get_image_processor()

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

    UpperCamelCase			  =      [[1764, 2646]]

    UpperCamelCase			  =      [[683, 1024]]
    UpperCamelCase			  =      processor.post_process_masks(
        lowerCamelCase_   ,   lowerCamelCase_   ,   lowerCamelCase_   ,   return_tensors="""tf"""				)
    UpperCamelCase			  =      processor.post_process_masks(
        lowerCamelCase_   ,   lowerCamelCase_   ,   lowerCamelCase_   ,   return_tensors="""pt"""				)

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







   @is_pt_tf_cross_test
   def 						lowerCamelCase_	(     self						:						Dict				):





    """simple docstring"""


    UpperCamelCase			  =      self.get_image_processor()

    UpperCamelCase			  =      SamProcessor(image_processor=lowerCamelCase_				)

    UpperCamelCase			  =      self.prepare_image_inputs()

    UpperCamelCase			  =      image_processor(lowerCamelCase_   ,   return_tensors="""pt"""				)["""pixel_values"""].numpy()
    UpperCamelCase			  =      processor(images=lowerCamelCase_   ,   return_tensors="""pt"""				)["""pixel_values"""].numpy()

    UpperCamelCase			  =      image_processor(lowerCamelCase_   ,   return_tensors="""tf"""				)["""pixel_values"""].numpy()
    UpperCamelCase			  =      processor(images=lowerCamelCase_   ,   return_tensors="""tf"""				)["""pixel_values"""].numpy()

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

from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE 		=      logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE 		=      {
    """sail/poolformer_s12""": """https://huggingface.co/sail/poolformer_s12/resolve/main/config.json""",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
}
class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):
   __lowerCAmelCase           =				"""poolformer"""







   def __init__(     self						:						str   ,   lowerCamelCase_						:						Any=3   ,   lowerCamelCase_						:						Any=16   ,   lowerCamelCase_						:						Any=16   ,   lowerCamelCase_						:						Optional[Any]=3   ,   lowerCamelCase_						:						List[str]=4.0   ,   lowerCamelCase_						:						Tuple=[2, 2, 6, 2]   ,   lowerCamelCase_						:						Optional[Any]=[64, 128, 320, 512]   ,   lowerCamelCase_						:						Tuple=[7, 3, 3, 3]   ,   lowerCamelCase_						:						Optional[Any]=[4, 2, 2, 2]   ,   lowerCamelCase_						:						Dict=[2, 1, 1, 1]   ,   lowerCamelCase_						:						Tuple=4   ,   lowerCamelCase_						:						Optional[int]=0.0   ,   lowerCamelCase_						:						Union[str, Any]="gelu"   ,   lowerCamelCase_						:						Union[str, Any]=True   ,   lowerCamelCase_						:						Union[str, Any]=1E-5   ,   lowerCamelCase_						:						int=0.0_2   ,   **lowerCamelCase_						:						str   ,   ):





    """simple docstring"""


    UpperCamelCase			  =      num_channels
    UpperCamelCase			  =      patch_size
    UpperCamelCase			  =      stride
    UpperCamelCase			  =      padding
    UpperCamelCase			  =      pool_size
    UpperCamelCase			  =      hidden_sizes
    UpperCamelCase			  =      mlp_ratio
    UpperCamelCase			  =      depths
    UpperCamelCase			  =      patch_sizes
    UpperCamelCase			  =      strides
    UpperCamelCase			  =      num_encoder_blocks
    UpperCamelCase			  =      drop_path_rate
    UpperCamelCase			  =      hidden_act
    UpperCamelCase			  =      use_layer_scale
    UpperCamelCase			  =      layer_scale_init_value
    UpperCamelCase			  =      initializer_range
    super().__init__(**lowerCamelCase_				)





class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):
   __lowerCAmelCase           =				version.parse("""1.11"""		)







   @property
   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    return OrderedDict(
        [
            ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
        ]				)







   @property
   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    return 2E-3
