

from typing import Dict, List, Optional, Tuple, Union

import numpy as np

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


if is_vision_available():
    import PIL

if is_torch_available():
    import torch


lowerCAmelCase__       = logging.get_logger(__name__)






class 						__magic_name__    (       __a    ):
 UpperCAmelCase							    =						['''pixel_values''']


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

   UpperCAmelCase					      =  do_resize
   UpperCAmelCase					      =  size
   UpperCAmelCase					      =  resample
   UpperCAmelCase					      =  do_rescale
   UpperCAmelCase					      =  rescale_factor
   UpperCAmelCase					      =  do_center_crop
   UpperCAmelCase					      =  crop_size
   UpperCAmelCase					      =  do_flip_channel_order


 def _UpperCamelCase	(						self		: Any			,						lowerCAmelCase__		: np.ndarray			,						lowerCAmelCase__		: Dict[str, int]			,						lowerCAmelCase__		: PILImageResampling = PIL.Image.BILINEAR			,						lowerCAmelCase__		: Optional[Union[str, ChannelDimension]] = None			,						**lowerCAmelCase__		: List[Any]			,						)    ->      Optional[Any]:
   UpperCAmelCase					      =  get_size_dict(snake_case__			,						default_to_square=snake_case__    )
   if "shortest_edge" not in size:
     raise ValueError(f"The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}"    )
   UpperCAmelCase					      =  get_resize_output_image_size(snake_case__			,						size=size["shortest_edge"]			,						default_to_square=snake_case__    )
   return resize(snake_case__			,						size=snake_case__			,						resample=snake_case__			,						data_format=snake_case__			,						**snake_case__    )


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


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


 def _UpperCamelCase	(						self		: Dict			,						lowerCAmelCase__		: np.ndarray			,						lowerCAmelCase__		: Optional[Union[str, ChannelDimension]] = None    )    ->      Dict:
   return flip_channel_order(snake_case__			,						data_format=snake_case__    )


 def _UpperCamelCase	(						self		: Tuple			,						lowerCAmelCase__		: ImageInput			,						lowerCAmelCase__		: bool = None			,						lowerCAmelCase__		: Dict[str, int] = None			,						lowerCAmelCase__		: PILImageResampling = None			,						lowerCAmelCase__		: bool = None			,						lowerCAmelCase__		: float = None			,						lowerCAmelCase__		: bool = None			,						lowerCAmelCase__		: Dict[str, int] = None			,						lowerCAmelCase__		: bool = None			,						lowerCAmelCase__		: Optional[Union[str, TensorType]] = None			,						lowerCAmelCase__		: ChannelDimension = ChannelDimension.FIRST			,						**lowerCAmelCase__		: Dict			,						)    ->      Tuple:
   UpperCAmelCase					      =  do_resize if do_resize is not None else self.do_resize
   UpperCAmelCase					      =  resample if resample is not None else self.resample
   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_center_crop if do_center_crop is not None else self.do_center_crop
   UpperCAmelCase					      =  (
       do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order
   )

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

   UpperCAmelCase					      =  make_list_of_images(snake_case__    )

   if not valid_images(snake_case__    ):
     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:
     raise ValueError("Size must be specified if do_resize is True."    )

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

   if do_center_crop and crop_size is None:
     raise ValueError("Crop size must be specified if do_center_crop is True."    )

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

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

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

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

   # the pretrained checkpoints assume images are BGR, not RGB
   if do_flip_channel_order:
     UpperCAmelCase					      =  [self.flip_channel_order(image=snake_case__    ) for image in images]

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

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


 def _UpperCamelCase	(						self		: Tuple			,						lowerCAmelCase__		: str			,						lowerCAmelCase__		: List[Tuple] = None    )    ->      Any:
   UpperCAmelCase					      =  outputs.logits

   # Resize logits and compute semantic segmentation maps
   if target_sizes is not None:
     if len(snake_case__    ) != len(snake_case__    ):
       raise ValueError(
           "Make sure that you pass in as many target sizes as the batch dimension of the logits"    )

     if is_torch_tensor(snake_case__    ):
       UpperCAmelCase					      =  target_sizes.numpy()

     UpperCAmelCase					      =  []

     for idx in range(len(snake_case__    )    ):
       UpperCAmelCase					      =  torch.nn.functional.interpolate(
           logits[idx].unsqueeze(dim=0    )			,						size=target_sizes[idx]			,						mode="bilinear"			,						align_corners=snake_case__    )
       UpperCAmelCase					      =  resized_logits[0].argmax(dim=0    )
       semantic_segmentation.append(snake_case__    )
   else:
     UpperCAmelCase					      =  logits.argmax(dim=1    )
     UpperCAmelCase					      =  [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]    )]

   return semantic_segmentation






from . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)



