

"""simple docstring"""




from typing import Dict, Iterable, 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, logging


A_				:      str           =			logging.get_logger(__name__)





class  lowerCamelCase    (A__      ):
			lowerCamelCase__ :		Any       		=				['pixel_values']

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

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

			def        SCREAMING_SNAKE_CASE					(       self					:    Union[str, Any]     ,					__UpperCAmelCase					:    np.ndarray     ,					__UpperCAmelCase					:    Dict[str, int]     ,					__UpperCAmelCase					:    PILImageResampling = PILImageResampling.BICUBIC     ,					__UpperCAmelCase					:    Optional[Union[str, ChannelDimension]] = None     ,					**__UpperCAmelCase					:    List[str]     ,					) ->     np.ndarray:
						SCREAMING_SNAKE_CASE__										=   get_size_dict(__UpperCAmelCase     ,					default_to_square=__UpperCAmelCase							)
						# size_dict is a dict with either keys "height" and "width" or "shortest_edge"
						if "shortest_edge" in size:
									SCREAMING_SNAKE_CASE__										=   int((2_5_6 / 2_2_4) * size["""shortest_edge"""]							)
									SCREAMING_SNAKE_CASE__										=   get_resize_output_image_size(__UpperCAmelCase     ,					size=__UpperCAmelCase     ,					default_to_square=__UpperCAmelCase							)
									SCREAMING_SNAKE_CASE__										=   {"""height""": output_size[0], """width""": output_size[1]}
						if "height" not in size_dict or "width" not in size_dict:
									raise ValueError(
									    F"""Size dict must have keys 'height' and 'width' or 'shortest_edge'. Got {size_dict.keys()}"""							)
						return resize(
						    __UpperCAmelCase     ,					size=(size_dict["""height"""], size_dict["""width"""])     ,					resample=__UpperCAmelCase     ,					data_format=__UpperCAmelCase     ,					**__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]     ,					__UpperCAmelCase					:    np.ndarray     ,					__UpperCAmelCase					:    Dict[str, int]     ,					__UpperCAmelCase					:    Optional[Union[str, ChannelDimension]] = None     ,					**__UpperCAmelCase					:    Any     ,					) ->     np.ndarray:
						SCREAMING_SNAKE_CASE__										=   get_size_dict(__UpperCAmelCase							)
						if "height" not in size or "width" not in size:
									raise ValueError(F"""Size dict must have keys 'height' and 'width'. Got {size.keys()}"""							)
						return center_crop(__UpperCAmelCase     ,					size=(size["""height"""], size["""width"""])     ,					data_format=__UpperCAmelCase     ,					**__UpperCAmelCase							)

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

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





			def        SCREAMING_SNAKE_CASE					(       self					:    Union[str, Any]     ,					__UpperCAmelCase					:    ImageInput     ,					__UpperCAmelCase					:    Optional[bool] = None     ,					__UpperCAmelCase					:    Optional[Dict[str, int]] = None     ,					__UpperCAmelCase					:    PILImageResampling = None     ,					__UpperCAmelCase					:    Optional[bool] = None     ,					__UpperCAmelCase					:    Optional[Dict[str, int]] = None     ,					__UpperCAmelCase					:    Optional[bool] = None     ,					__UpperCAmelCase					:    Optional[float] = None     ,					__UpperCAmelCase					:    Optional[bool] = None     ,					__UpperCAmelCase					:    Optional[Union[float, Iterable[float]]] = None     ,					__UpperCAmelCase					:    Optional[Union[float, Iterable[float]]] = None     ,					__UpperCAmelCase					:    Optional[TensorType] = None     ,					__UpperCAmelCase					:    ChannelDimension = ChannelDimension.FIRST     ,					**__UpperCAmelCase					:    Optional[Any]     ,					) ->     BatchFeature:
						SCREAMING_SNAKE_CASE__										=   do_resize if do_resize is not None else self.do_resize
						SCREAMING_SNAKE_CASE__										=   resample if resample is not None else self.resample
						SCREAMING_SNAKE_CASE__										=   do_center_crop if do_center_crop is not None else self.do_center_crop
						SCREAMING_SNAKE_CASE__										=   do_rescale if do_rescale is not None else self.do_rescale
						SCREAMING_SNAKE_CASE__										=   rescale_factor if rescale_factor is not None else self.rescale_factor
						SCREAMING_SNAKE_CASE__										=   do_normalize if do_normalize is not None else self.do_normalize
						SCREAMING_SNAKE_CASE__										=   image_mean if image_mean is not None else self.image_mean
						SCREAMING_SNAKE_CASE__										=   image_std if image_std is not None else self.image_std

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

						SCREAMING_SNAKE_CASE__										=   make_list_of_images(__UpperCAmelCase							)

						if not valid_images(__UpperCAmelCase							):
									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_center_crop and crop_size is None:
									raise ValueError("""Crop size 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.
						SCREAMING_SNAKE_CASE__										=   [to_numpy_array(__UpperCAmelCase							) for image in images]

						if do_resize:
									SCREAMING_SNAKE_CASE__										=   [self.resize(__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase							) for image in images]

						if do_center_crop:
									SCREAMING_SNAKE_CASE__										=   [self.center_crop(__UpperCAmelCase     ,					__UpperCAmelCase							) for image in images]

						if do_rescale:
									SCREAMING_SNAKE_CASE__										=   [self.rescale(__UpperCAmelCase     ,					__UpperCAmelCase							) for image in images]

						if do_normalize:
									SCREAMING_SNAKE_CASE__										=   [self.normalize(__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase							) for image in images]

						SCREAMING_SNAKE_CASE__										=   [to_channel_dimension_format(__UpperCAmelCase     ,					__UpperCAmelCase							) for image in images]

						SCREAMING_SNAKE_CASE__										=   {"""pixel_values""": images}
						return BatchFeature(data=__UpperCAmelCase     ,					tensor_type=__UpperCAmelCase							)





"""simple docstring"""




A_				:      Any           =			"0.21.0"

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
						from .utils import rich


