






import numpy as np
from PIL import Image







def 		SCREAMING_SNAKE_CASE							( __lowerCAmelCase ,   __lowerCAmelCase ,   __lowerCAmelCase      )			->     np.ndarray:
     UpperCamelCase__      :       List[Any]	    =  np.array(__lowerCAmelCase      )
     if arr.shape[0] != arr.shape[1]:
          raise ValueError("The input array is not a square matrix"      )
     UpperCamelCase__      :       Any	    =  0
     UpperCamelCase__      :       List[str]	    =  0
     UpperCamelCase__      :       Union[str, Any]	    =  0
     UpperCamelCase__      :       List[Any]	    =  0

     # compute the shape of the output matrix
     UpperCamelCase__      :       Any	    =  (arr.shape[0] - size) // stride + 1
     # initialize the output matrix with zeros of shape maxpool_shape
     UpperCamelCase__      :       List[Any]	    =  np.zeros((maxpool_shape, maxpool_shape)      )

     while i < arr.shape[0]:
          if i + size > arr.shape[0]:
               # if the end of the matrix is reached, break
               break
          while j < arr.shape[1]:
               # if the end of the matrix is reached, break
               if j + size > arr.shape[1]:
                    break
               # compute the maximum of the pooling matrix
               UpperCamelCase__      :       str	    =  np.max(arr[i : i + size, j : j + size]      )
               # shift the pooling matrix by stride of column pixels
               j += stride
               mat_j += 1

          # shift the pooling matrix by stride of row pixels
          i += stride
          mat_i += 1

          # reset the column index to 0
          UpperCamelCase__      :       int	    =  0
          UpperCamelCase__      :       Dict	    =  0

     return updated_arr







def 		SCREAMING_SNAKE_CASE							( __lowerCAmelCase ,   __lowerCAmelCase ,   __lowerCAmelCase      )			->     np.ndarray:
     UpperCamelCase__      :       List[Any]	    =  np.array(__lowerCAmelCase      )
     if arr.shape[0] != arr.shape[1]:
          raise ValueError("The input array is not a square matrix"      )
     UpperCamelCase__      :       Any	    =  0
     UpperCamelCase__      :       str	    =  0
     UpperCamelCase__      :       Any	    =  0
     UpperCamelCase__      :       int	    =  0

     # compute the shape of the output matrix
     UpperCamelCase__      :       Union[str, Any]	    =  (arr.shape[0] - size) // stride + 1
     # initialize the output matrix with zeros of shape avgpool_shape
     UpperCamelCase__      :       int	    =  np.zeros((avgpool_shape, avgpool_shape)      )

     while i < arr.shape[0]:
          # if the end of the matrix is reached, break
          if i + size > arr.shape[0]:
               break
          while j < arr.shape[1]:
               # if the end of the matrix is reached, break
               if j + size > arr.shape[1]:
                    break
               # compute the average of the pooling matrix
               UpperCamelCase__      :       int	    =  int(np.average(arr[i : i + size, j : j + size]      )      )
               # shift the pooling matrix by stride of column pixels
               j += stride
               mat_j += 1

          # shift the pooling matrix by stride of row pixels
          i += stride
          mat_i += 1
          # reset the column index to 0
          UpperCamelCase__      :       Optional[Any]	    =  0
          UpperCamelCase__      :       str	    =  0

     return updated_arr


# Main Function
if __name__ == "__main__":
 from doctest import testmod

 testmod(name='''avgpooling''', verbose=True)

 # Loading the image
 lowerCamelCase     :							Optional[int]    =Image.open('''path_to_image''')

 # Converting the image to numpy array and maxpooling, displaying the result
 # Ensure that the image is a square matrix

 Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

 # Converting the image to numpy array and averagepooling, displaying the result
 # Ensure that the image is a square matrix

 Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()







from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCamelCase     :							List[Any]    =logging.get_logger(__name__)

lowerCamelCase     :							Optional[Any]    ={
    '''google/bit-50''': '''https://huggingface.co/google/bit-50/resolve/main/config.json''',
}





class       __a  (   A__	,       A__	):
 _lowerCAmelCase	:	Union[str, Any]				=			'''bit'''
 _lowerCAmelCase	:	List[str]				=			['''preactivation''', '''bottleneck''']
 _lowerCAmelCase	:	Any				=			['''SAME''', '''VALID''']







 def __init__(		self :       str							,       SCREAMING_SNAKE_CASE :       Tuple=3							,       SCREAMING_SNAKE_CASE :       str=64							,       SCREAMING_SNAKE_CASE :       List[Any]=[2_56, 5_12, 10_24, 20_48]							,       SCREAMING_SNAKE_CASE :       Union[str, Any]=[3, 4, 6, 3]							,       SCREAMING_SNAKE_CASE :       str="preactivation"							,       SCREAMING_SNAKE_CASE :       Any="relu"							,       SCREAMING_SNAKE_CASE :       Dict=None							,       SCREAMING_SNAKE_CASE :       List[Any]=32							,       SCREAMING_SNAKE_CASE :       Tuple=0.0							,       SCREAMING_SNAKE_CASE :       Dict=False							,       SCREAMING_SNAKE_CASE :       int=32							,       SCREAMING_SNAKE_CASE :       str=1							,       SCREAMING_SNAKE_CASE :       List[str]=None							,       SCREAMING_SNAKE_CASE :       Optional[int]=None							,       **SCREAMING_SNAKE_CASE :       int							,       ):


      '''simple docstring'''






      super().__init__(**SCREAMING_SNAKE_CASE						)
      if layer_type not in self.layer_types:
           raise ValueError(F'layer_type={layer_type} is not one of {",".join(self.layer_types						)}'						)
      if global_padding is not None:
           if global_padding.upper() in self.supported_padding:
                UpperCamelCase__      :       Any	    =  global_padding.upper()
           else:
                raise ValueError(F'Padding strategy {global_padding} not supported'						)
      UpperCamelCase__      :       Dict	    =  num_channels
      UpperCamelCase__      :       Dict	    =  embedding_size
      UpperCamelCase__      :       Tuple	    =  hidden_sizes
      UpperCamelCase__      :       Any	    =  depths
      UpperCamelCase__      :       Optional[int]	    =  layer_type
      UpperCamelCase__      :       int	    =  hidden_act
      UpperCamelCase__      :       str	    =  global_padding
      UpperCamelCase__      :       Any	    =  num_groups
      UpperCamelCase__      :       str	    =  drop_path_rate
      UpperCamelCase__      :       Optional[Any]	    =  embedding_dynamic_padding
      UpperCamelCase__      :       Tuple	    =  output_stride
      UpperCamelCase__      :       List[str]	    =  width_factor

      UpperCamelCase__      :       Any	    =  ["stem"] + [F'stage{idx}' for idx in range(1							,       len(SCREAMING_SNAKE_CASE						) + 1						)]
      UpperCamelCase__    ,					UpperCamelCase__      :       Union[str, Any]	    =  get_aligned_output_features_output_indices(
          out_features=SCREAMING_SNAKE_CASE							,       out_indices=SCREAMING_SNAKE_CASE							,       stage_names=self.stage_names						)