

from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
      import PIL


lowerCAmelCase__       = logging.get_logger(__name__)






class 						__magic_name__    (       _snake_case    ):
 UpperCAmelCase							    =						["""pixel_values"""]


 def __init__(						self		: List[str]			,						lowerCAmelCase__		: bool = True			,						lowerCAmelCase__		: Dict[str, int] = None			,						lowerCAmelCase__		: int = 0.9			,						lowerCAmelCase__		: PILImageResampling = PILImageResampling.BICUBIC			,						lowerCAmelCase__		: bool = True			,						lowerCAmelCase__		: Dict[str, int] = None			,						lowerCAmelCase__		: Union[int, float] = 1 / 2_5_5			,						lowerCAmelCase__		: bool = True			,						lowerCAmelCase__		: bool = True			,						lowerCAmelCase__		: Optional[Union[float, List[float]]] = None			,						lowerCAmelCase__		: Optional[Union[float, List[float]]] = None			,						**lowerCAmelCase__		: Dict			,						)    ->      None:
   super().__init__(**lowerCAmelCase__    )
   UpperCAmelCase					      =  size if size is not None else {"shortest_edge": 2_2_4}
   UpperCAmelCase					      =  get_size_dict(lowerCAmelCase__			,						default_to_square=lowerCAmelCase__    )
   UpperCAmelCase					      =  crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
   UpperCAmelCase					      =  get_size_dict(lowerCAmelCase__			,						param_name="crop_size"    )

   UpperCAmelCase					      =  do_resize
   UpperCAmelCase					      =  size
   UpperCAmelCase					      =  crop_pct
   UpperCAmelCase					      =  resample
   UpperCAmelCase					      =  do_center_crop
   UpperCAmelCase					      =  crop_size
   UpperCAmelCase					      =  do_rescale
   UpperCAmelCase					      =  rescale_factor
   UpperCAmelCase					      =  do_normalize
   UpperCAmelCase					      =  image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
   UpperCAmelCase					      =  image_std if image_std is not None else IMAGENET_DEFAULT_STD


 def _UpperCamelCase	(						self		: Union[str, Any]			,						lowerCAmelCase__		: np.ndarray			,						lowerCAmelCase__		: Dict[str, int]			,						lowerCAmelCase__		: Optional[float] = None			,						lowerCAmelCase__		: PILImageResampling = PILImageResampling.BICUBIC			,						lowerCAmelCase__		: Optional[Union[str, ChannelDimension]] = None			,						**lowerCAmelCase__		: Tuple			,						)    ->      np.ndarray:
   UpperCAmelCase					      =  get_size_dict(lowerCAmelCase__			,						default_to_square=lowerCAmelCase__    )
   if "shortest_edge" not in size and ("height" not in size or "width" not in size):
     raise ValueError(f"size must contain 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}"    )
   if crop_pct is not None:
     if "shortest_edge" in size:
       UpperCAmelCase					      =  int(size["shortest_edge"] / crop_pct    )
     elif "height" in size and "width" in size:
       if size["height"] == size["width"]:
         UpperCAmelCase					      =  int(size["height"] / crop_pct    )
       else:
         UpperCAmelCase					      =  (int(size["height"] / crop_pct    ), int(size["width"] / crop_pct    ))
     else:
       raise ValueError("Invalid size for resize: {}".format(lowerCAmelCase__    )    )

     UpperCAmelCase					      =  get_resize_output_image_size(lowerCAmelCase__			,						size=lowerCAmelCase__			,						default_to_square=lowerCAmelCase__    )
   else:
     if "shortest_edge" in size:
       UpperCAmelCase					      =  get_resize_output_image_size(lowerCAmelCase__			,						size=size["shortest_edge"]			,						default_to_square=lowerCAmelCase__    )
     elif "height" in size and "width" in size:
       UpperCAmelCase					      =  (size["height"], size["width"])
     else:
       raise ValueError("Invalid size for resize: {}".format(lowerCAmelCase__    )    )

   return resize(lowerCAmelCase__			,						size=lowerCAmelCase__			,						resample=lowerCAmelCase__			,						data_format=lowerCAmelCase__			,						**lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: List[Any]			,						lowerCAmelCase__		: np.ndarray			,						lowerCAmelCase__		: Dict[str, int]			,						lowerCAmelCase__		: Optional[Union[str, ChannelDimension]] = None			,						**lowerCAmelCase__		: Dict			,						)    ->      np.ndarray:
   UpperCAmelCase					      =  get_size_dict(lowerCAmelCase__    )
   if "height" not in size or "width" not in size:
     raise ValueError(f"size must contain 'height' and 'width' as keys. Got {size.keys()}"    )
   return center_crop(lowerCAmelCase__			,						size=(size["height"], size["width"])			,						data_format=lowerCAmelCase__			,						**lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: List[str]			,						lowerCAmelCase__		: np.ndarray			,						lowerCAmelCase__		: Union[int, float]			,						lowerCAmelCase__		: Optional[Union[str, ChannelDimension]] = None			,						**lowerCAmelCase__		: Optional[int]			,						)    ->      Any:
   return rescale(lowerCAmelCase__			,						scale=lowerCAmelCase__			,						data_format=lowerCAmelCase__			,						**lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: List[str]			,						lowerCAmelCase__		: np.ndarray			,						lowerCAmelCase__		: Union[float, List[float]]			,						lowerCAmelCase__		: Union[float, List[float]]			,						lowerCAmelCase__		: Optional[Union[str, ChannelDimension]] = None			,						**lowerCAmelCase__		: Optional[int]			,						)    ->      np.ndarray:
   return normalize(lowerCAmelCase__			,						mean=lowerCAmelCase__			,						std=lowerCAmelCase__			,						data_format=lowerCAmelCase__			,						**lowerCAmelCase__    )


 def _UpperCamelCase	(						self		: Any			,						lowerCAmelCase__		: ImageInput			,						lowerCAmelCase__		: bool = None			,						lowerCAmelCase__		: Dict[str, int] = None			,						lowerCAmelCase__		: int = None			,						lowerCAmelCase__		: PILImageResampling = None			,						lowerCAmelCase__		: bool = None			,						lowerCAmelCase__		: Dict[str, int] = None			,						lowerCAmelCase__		: bool = None			,						lowerCAmelCase__		: float = None			,						lowerCAmelCase__		: bool = None			,						lowerCAmelCase__		: Optional[Union[float, List[float]]] = None			,						lowerCAmelCase__		: Optional[Union[float, List[float]]] = None			,						lowerCAmelCase__		: Optional[Union[str, TensorType]] = None			,						lowerCAmelCase__		: ChannelDimension = ChannelDimension.FIRST			,						**lowerCAmelCase__		: str			,						)    ->      PIL.Image.Image:
   UpperCAmelCase					      =  do_resize if do_resize is not None else self.do_resize
   UpperCAmelCase					      =  crop_pct if crop_pct is not None else self.crop_pct
   UpperCAmelCase					      =  resample if resample is not None else self.resample
   UpperCAmelCase					      =  do_center_crop if do_center_crop is not None else self.do_center_crop
   UpperCAmelCase					      =  do_rescale if do_rescale is not None else self.do_rescale
   UpperCAmelCase					      =  rescale_factor if rescale_factor is not None else self.rescale_factor
   UpperCAmelCase					      =  do_normalize if do_normalize is not None else self.do_normalize
   UpperCAmelCase					      =  image_mean if image_mean is not None else self.image_mean
   UpperCAmelCase					      =  image_std if image_std is not None else self.image_std

   UpperCAmelCase					      =  size if size is not None else self.size
   UpperCAmelCase					      =  get_size_dict(lowerCAmelCase__			,						default_to_square=lowerCAmelCase__    )
   UpperCAmelCase					      =  crop_size if crop_size is not None else self.crop_size
   UpperCAmelCase					      =  get_size_dict(lowerCAmelCase__			,						param_name="crop_size"    )

   UpperCAmelCase					      =  make_list_of_images(lowerCAmelCase__    )

   if not valid_images(lowerCAmelCase__    ):
     raise ValueError(
         "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
         "torch.Tensor, tf.Tensor or jax.ndarray."    )

   if do_resize and size is None or resample is None:
     raise ValueError("Size and resample must be specified if do_resize is True."    )

   if do_center_crop and crop_pct is None:
     raise ValueError("Crop_pct must be specified if do_center_crop is True."    )

   if do_rescale and rescale_factor is None:
     raise ValueError("Rescale factor must be specified if do_rescale is True."    )

   if do_normalize and (image_mean is None or image_std is None):
     raise ValueError("Image mean and std must be specified if do_normalize is True."    )

   # All transformations expect numpy arrays.
   UpperCAmelCase					      =  [to_numpy_array(lowerCAmelCase__    ) for image in images]

   if do_resize:
     UpperCAmelCase					      =  [self.resize(image=lowerCAmelCase__			,						size=lowerCAmelCase__			,						crop_pct=lowerCAmelCase__			,						resample=lowerCAmelCase__    ) for image in images]

   if do_center_crop:
     UpperCAmelCase					      =  [self.center_crop(image=lowerCAmelCase__			,						size=lowerCAmelCase__    ) for image in images]

   if do_rescale:
     UpperCAmelCase					      =  [self.rescale(image=lowerCAmelCase__			,						scale=lowerCAmelCase__    ) for image in images]

   if do_normalize:
     UpperCAmelCase					      =  [self.normalize(image=lowerCAmelCase__			,						mean=lowerCAmelCase__			,						std=lowerCAmelCase__    ) for image in images]

   UpperCAmelCase					      =  [to_channel_dimension_format(lowerCAmelCase__			,						lowerCAmelCase__    ) for image in images]

   UpperCAmelCase					      =  {"pixel_values": images}
   return BatchFeature(data=lowerCAmelCase__			,						tensor_type=lowerCAmelCase__    )






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


lowerCAmelCase__       = logging.get_logger(__name__)

lowerCAmelCase__       = {
    "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json",
}






class 						__magic_name__    (       _snake_case	,						_snake_case    ):
 UpperCAmelCase							    =						"""convnextv2"""


 def __init__(						self		: Optional[Any]			,						lowerCAmelCase__		: List[Any]=3			,						lowerCAmelCase__		: str=4			,						lowerCAmelCase__		: Dict=4			,						lowerCAmelCase__		: Optional[Any]=None			,						lowerCAmelCase__		: Optional[Any]=None			,						lowerCAmelCase__		: str="gelu"			,						lowerCAmelCase__		: Optional[int]=0.02			,						lowerCAmelCase__		: Dict=1e-1_2			,						lowerCAmelCase__		: str=0.0			,						lowerCAmelCase__		: str=2_2_4			,						lowerCAmelCase__		: int=None			,						lowerCAmelCase__		: List[Any]=None			,						**lowerCAmelCase__		: List[Any]			,						)    ->      List[Any]:
   super().__init__(**lowerCAmelCase__    )

   UpperCAmelCase					      =  num_channels
   UpperCAmelCase					      =  patch_size
   UpperCAmelCase					      =  num_stages
   UpperCAmelCase					      =  [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes
   UpperCAmelCase					      =  [3, 3, 9, 3] if depths is None else depths
   UpperCAmelCase					      =  hidden_act
   UpperCAmelCase					      =  initializer_range
   UpperCAmelCase					      =  layer_norm_eps
   UpperCAmelCase					      =  drop_path_rate
   UpperCAmelCase					      =  image_size
   UpperCAmelCase					      =  ["stem"] + [f"stage{idx}" for idx in range(1			,						len(self.depths    ) + 1    )]
   UpperCAmelCase      ,	UpperCAmelCase					      =  get_aligned_output_features_output_indices(
       out_features=lowerCAmelCase__			,						out_indices=lowerCAmelCase__			,						stage_names=self.stage_names    )



