






"""simple docstring"""







import gc
import unittest

from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline
from transformers.pipelines import PipelineException
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_gpu,
    slow,
)

from .test_pipelines_common import ANY


@is_pipeline_test
class      UpperCamelCase		(unittest.TestCase     ):
						_SCREAMING_SNAKE_CASE  : Optional[int]												=		MODEL_FOR_MASKED_LM_MAPPING
						_SCREAMING_SNAKE_CASE  : Any												=		TF_MODEL_FOR_MASKED_LM_MAPPING






						def      __snake_case      (		self  :List[str]				)      ->Any:
										super().tearDown()
										# clean-up as much as possible GPU memory occupied by PyTorch
										gc.collect()
										if is_torch_available():
														import torch

														torch.cuda.empty_cache()






						@require_tf
						def      __snake_case      (		self  :List[Any]				)      ->Optional[Any]:
										lowercase    :      Tuple       = pipeline(task="""fill-mask"""			, model="""sshleifer/tiny-distilroberta-base"""			, top_k=2			, framework="""tf"""				)
										lowercase    :      List[str]       = unmasker("""My name is <mask>"""				)
										self.assertEqual(
										    nested_simplify(__magic_name__			, decimals=6				)			, [
										        {"""sequence""": """My name is grouped""", """score""": 2.1E-05, """token""": 38_015, """token_str""": """ grouped"""},
										        {"""sequence""": """My name is accuser""", """score""": 2.1E-05, """token""": 25_506, """token_str""": """ accuser"""},
										    ]			, )

										lowercase    :      Union[str, Any]       = unmasker("""The largest city in France is <mask>"""				)
										self.assertEqual(
										    nested_simplify(__magic_name__			, decimals=6				)			, [
										        {
										            """sequence""": """The largest city in France is grouped""",
										            """score""": 2.1E-05,
										            """token""": 38_015,
										            """token_str""": """ grouped""",
										        },
										        {
										            """sequence""": """The largest city in France is accuser""",
										            """score""": 2.1E-05,
										            """token""": 25_506,
										            """token_str""": """ accuser""",
										        },
										    ]			, )

										lowercase    :      Optional[Any]       = unmasker("""My name is <mask>"""			, targets=[""" Patrick""", """ Clara""", """ Teven"""]			, top_k=3				)
										self.assertEqual(
										    nested_simplify(__magic_name__			, decimals=6				)			, [
										        {"""sequence""": """My name is Clara""", """score""": 2E-05, """token""": 13_606, """token_str""": """ Clara"""},
										        {"""sequence""": """My name is Patrick""", """score""": 2E-05, """token""": 3_499, """token_str""": """ Patrick"""},
										        {"""sequence""": """My name is Te""", """score""": 1.9E-05, """token""": 2_941, """token_str""": """ Te"""},
										    ]			, )






						@require_torch
						def      __snake_case      (		self  :int				)      ->Tuple:
										lowercase    :      str       = pipeline(task="""fill-mask"""			, model="""sshleifer/tiny-distilroberta-base"""			, top_k=2			, framework="""pt"""				)

										lowercase    :      int       = unmasker("""My name is <mask>"""				)
										self.assertEqual(
										    nested_simplify(__magic_name__			, decimals=6				)			, [
										        {"""sequence""": """My name is Maul""", """score""": 2.2E-05, """token""": 35_676, """token_str""": """ Maul"""},
										        {"""sequence""": """My name isELS""", """score""": 2.2E-05, """token""": 16_416, """token_str""": """ELS"""},
										    ]			, )

										lowercase    :      Tuple       = unmasker("""The largest city in France is <mask>"""				)
										self.assertEqual(
										    nested_simplify(__magic_name__			, decimals=6				)			, [
										        {
										            """sequence""": """The largest city in France is Maul""",
										            """score""": 2.2E-05,
										            """token""": 35_676,
										            """token_str""": """ Maul""",
										        },
										        {"""sequence""": """The largest city in France isELS""", """score""": 2.2E-05, """token""": 16_416, """token_str""": """ELS"""},
										    ]			, )

										lowercase    :      str       = unmasker("""My name is <mask>"""			, targets=[""" Patrick""", """ Clara""", """ Teven"""]			, top_k=3				)
										self.assertEqual(
										    nested_simplify(__magic_name__			, decimals=6				)			, [
										        {"""sequence""": """My name is Patrick""", """score""": 2.1E-05, """token""": 3_499, """token_str""": """ Patrick"""},
										        {"""sequence""": """My name is Te""", """score""": 2E-05, """token""": 2_941, """token_str""": """ Te"""},
										        {"""sequence""": """My name is Clara""", """score""": 2E-05, """token""": 13_606, """token_str""": """ Clara"""},
										    ]			, )

										lowercase    :      str       = unmasker("""My name is <mask> <mask>"""			, top_k=2				)

										self.assertEqual(
										    nested_simplify(__magic_name__			, decimals=6				)			, [
										        [
										            {
										                """score""": 2.2E-05,
										                """token""": 35_676,
										                """token_str""": """ Maul""",
										                """sequence""": """<s>My name is Maul<mask></s>""",
										            },
										            {"""score""": 2.2E-05, """token""": 16_416, """token_str""": """ELS""", """sequence""": """<s>My name isELS<mask></s>"""},
										        ],
										        [
										            {
										                """score""": 2.2E-05,
										                """token""": 35_676,
										                """token_str""": """ Maul""",
										                """sequence""": """<s>My name is<mask> Maul</s>""",
										            },
										            {"""score""": 2.2E-05, """token""": 16_416, """token_str""": """ELS""", """sequence""": """<s>My name is<mask>ELS</s>"""},
										        ],
										    ]			, )






						@require_torch_gpu
						def      __snake_case      (		self  :Optional[int]				)      ->List[Any]:
										lowercase    :      Dict       = pipeline("""fill-mask"""			, model="""hf-internal-testing/tiny-random-distilbert"""			, device=0			, framework="""pt"""				)

										# convert model to fp16
										pipe.model.half()

										lowercase    :      List[str]       = pipe("""Paris is the [MASK] of France."""				)
										# We actually don't care about the result, we just want to make sure
										# it works, meaning the float16 tensor got casted back to float32
										# for postprocessing.
										self.assertIsInstance(__magic_name__			, __magic_name__				)






						@slow
						@require_torch
						def      __snake_case      (		self  :Optional[int]				)      ->Dict:
										lowercase    :      Optional[Any]       = pipeline(task="""fill-mask"""			, model="""distilroberta-base"""			, top_k=2			, framework="""pt"""				)
										self.run_large_test(__magic_name__				)






						@slow
						@require_tf
						def      __snake_case      (		self  :List[str]				)      ->Union[str, Any]:
										lowercase    :      List[str]       = pipeline(task="""fill-mask"""			, model="""distilroberta-base"""			, top_k=2			, framework="""tf"""				)
										self.run_large_test(__magic_name__				)






						def      __snake_case      (		self  :str			, __magic_name__  :Dict				)      ->str:
										lowercase    :      List[Any]       = unmasker("""My name is <mask>"""				)
										self.assertEqual(
										    nested_simplify(__magic_name__				)			, [
										        {"""sequence""": """My name is John""", """score""": 0.008, """token""": 610, """token_str""": """ John"""},
										        {"""sequence""": """My name is Chris""", """score""": 0.007, """token""": 1_573, """token_str""": """ Chris"""},
										    ]			, )
										lowercase    :      int       = unmasker("""The largest city in France is <mask>"""				)
										self.assertEqual(
										    nested_simplify(__magic_name__				)			, [
										        {
										            """sequence""": """The largest city in France is Paris""",
										            """score""": 0.251,
										            """token""": 2_201,
										            """token_str""": """ Paris""",
										        },
										        {
										            """sequence""": """The largest city in France is Lyon""",
										            """score""": 0.214,
										            """token""": 12_790,
										            """token_str""": """ Lyon""",
										        },
										    ]			, )

										lowercase    :      str       = unmasker("""My name is <mask>"""			, targets=[""" Patrick""", """ Clara""", """ Teven"""]			, top_k=3				)
										self.assertEqual(
										    nested_simplify(__magic_name__				)			, [
										        {"""sequence""": """My name is Patrick""", """score""": 0.005, """token""": 3_499, """token_str""": """ Patrick"""},
										        {"""sequence""": """My name is Clara""", """score""": 0.000, """token""": 13_606, """token_str""": """ Clara"""},
										        {"""sequence""": """My name is Te""", """score""": 0.000, """token""": 2_941, """token_str""": """ Te"""},
										    ]			, )






						@require_torch
						def      __snake_case      (		self  :Optional[int]				)      ->str:
										lowercase    :      Any       = pipeline(task="""fill-mask"""			, model="""sshleifer/tiny-distilroberta-base"""			, framework="""pt"""				)
										lowercase    :      List[str]       = None
										lowercase    :      Union[str, Any]       = None
										self.run_pipeline_test(__magic_name__			, []				)






						@require_tf
						def      __snake_case      (		self  :Optional[int]				)      ->Optional[int]:
										lowercase    :      Optional[int]       = pipeline(task="""fill-mask"""			, model="""sshleifer/tiny-distilroberta-base"""			, framework="""tf"""				)
										lowercase    :      Any       = None
										lowercase    :      Union[str, Any]       = None
										self.run_pipeline_test(__magic_name__			, []				)






						def      __snake_case      (		self  :int			, __magic_name__  :Union[str, Any]			, __magic_name__  :Any			, __magic_name__  :List[str]				)      ->Dict:
										if tokenizer is None or tokenizer.mask_token_id is None:
														self.skipTest("""The provided tokenizer has no mask token, (probably reformer or wav2vec2)"""				)

										lowercase    :      Any       = FillMaskPipeline(model=__magic_name__			, tokenizer=__magic_name__				)
										lowercase    :      Dict       = [
										    f"""This is another {tokenizer.mask_token} test""",
										]
										return fill_masker, examples






						def      __snake_case      (		self  :Any			, __magic_name__  :Any			, __magic_name__  :Optional[Any]				)      ->List[Any]:
										lowercase    :      List[Any]       = fill_masker.tokenizer
										lowercase    :      Optional[Any]       = fill_masker.model

										lowercase    :      Optional[int]       = fill_masker(
										    f"""This is a {tokenizer.mask_token}"""			, )
										self.assertEqual(
										    __magic_name__			, [
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										    ]			, )

										lowercase    :      Optional[int]       = fill_masker([f"""This is a {tokenizer.mask_token}"""]				)
										self.assertEqual(
										    __magic_name__			, [
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										    ]			, )

										lowercase    :      List[Any]       = fill_masker([f"""This is a {tokenizer.mask_token}""", f"""Another {tokenizer.mask_token} great test."""]				)
										self.assertEqual(
										    __magic_name__			, [
										        [
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        ],
										        [
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        ],
										    ]			, )

										with self.assertRaises(__magic_name__				):
														fill_masker([None]				)
										# No mask_token is not supported
										with self.assertRaises(__magic_name__				):
														fill_masker("""This is"""				)

										self.run_test_top_k(__magic_name__			, __magic_name__				)
										self.run_test_targets(__magic_name__			, __magic_name__				)
										self.run_test_top_k_targets(__magic_name__			, __magic_name__				)
										self.fill_mask_with_duplicate_targets_and_top_k(__magic_name__			, __magic_name__				)
										self.fill_mask_with_multiple_masks(__magic_name__			, __magic_name__				)






						def      __snake_case      (		self  :Any			, __magic_name__  :Dict			, __magic_name__  :Tuple				)      ->str:
										lowercase    :      List[Any]       = tokenizer.get_vocab()
										lowercase    :      List[Any]       = sorted(vocab.keys()				)[:2]
										# Pipeline argument
										lowercase    :      List[Any]       = FillMaskPipeline(model=__magic_name__			, tokenizer=__magic_name__			, targets=__magic_name__				)
										lowercase    :      List[Any]       = fill_masker(f"""This is a {tokenizer.mask_token}"""				)
										self.assertEqual(
										    __magic_name__			, [
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										    ]			, )
										lowercase    :      str       = {vocab[el] for el in targets}
										self.assertEqual({el["""token"""] for el in outputs}			, __magic_name__				)
										lowercase    :      Dict       = [tokenizer.decode([x]				) for x in target_ids]
										self.assertEqual({el["""token_str"""] for el in outputs}			, set(__magic_name__				)				)

										# Call argument
										lowercase    :      str       = FillMaskPipeline(model=__magic_name__			, tokenizer=__magic_name__				)
										lowercase    :      List[Any]       = fill_masker(f"""This is a {tokenizer.mask_token}"""			, targets=__magic_name__				)
										self.assertEqual(
										    __magic_name__			, [
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										    ]			, )
										lowercase    :      int       = {vocab[el] for el in targets}
										self.assertEqual({el["""token"""] for el in outputs}			, __magic_name__				)
										lowercase    :      Dict       = [tokenizer.decode([x]				) for x in target_ids]
										self.assertEqual({el["""token_str"""] for el in outputs}			, set(__magic_name__				)				)

										# Score equivalence
										lowercase    :      Optional[int]       = fill_masker(f"""This is a {tokenizer.mask_token}"""			, targets=__magic_name__				)
										lowercase    :      Optional[int]       = [top_mask["""token_str"""] for top_mask in outputs]
										lowercase    :      str       = [top_mask["""score"""] for top_mask in outputs]

										# For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
										if set(__magic_name__				) == set(__magic_name__				):
														lowercase    :      Tuple       = fill_masker(f"""This is a {tokenizer.mask_token}"""			, targets=__magic_name__				)
														lowercase    :      Dict       = [top_mask["""score"""] for top_mask in unmasked_targets]
														self.assertEqual(nested_simplify(__magic_name__				)			, nested_simplify(__magic_name__				)				)

										# Raises with invalid
										with self.assertRaises(__magic_name__				):
														lowercase    :      List[str]       = fill_masker(f"""This is a {tokenizer.mask_token}"""			, targets=[]				)
										# For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised
										if "" not in tokenizer.get_vocab():
														with self.assertRaises(__magic_name__				):
																		lowercase    :      Optional[int]       = fill_masker(f"""This is a {tokenizer.mask_token}"""			, targets=[""""""]				)
														with self.assertRaises(__magic_name__				):
																		lowercase    :      str       = fill_masker(f"""This is a {tokenizer.mask_token}"""			, targets=""""""				)






						def      __snake_case      (		self  :Tuple			, __magic_name__  :str			, __magic_name__  :List[Any]				)      ->Optional[int]:
										lowercase    :      Any       = FillMaskPipeline(model=__magic_name__			, tokenizer=__magic_name__			, top_k=2				)
										lowercase    :      Dict       = fill_masker(f"""This is a {tokenizer.mask_token}"""				)
										self.assertEqual(
										    __magic_name__			, [
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										    ]			, )

										lowercase    :      Dict       = FillMaskPipeline(model=__magic_name__			, tokenizer=__magic_name__				)
										lowercase    :      Optional[int]       = fill_masker(f"""This is a {tokenizer.mask_token}"""			, top_k=2				)
										self.assertEqual(
										    __magic_name__			, [
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										    ]			, )
										self.assertEqual(nested_simplify(__magic_name__				)			, nested_simplify(__magic_name__				)				)






						def      __snake_case      (		self  :Any			, __magic_name__  :Optional[int]			, __magic_name__  :Any				)      ->Union[str, Any]:
										lowercase    :      Optional[Any]       = tokenizer.get_vocab()
										lowercase    :      Optional[Any]       = FillMaskPipeline(model=__magic_name__			, tokenizer=__magic_name__				)

										# top_k=2, ntargets=3
										lowercase    :      str       = sorted(vocab.keys()				)[:3]
										lowercase    :      Optional[Any]       = fill_masker(f"""This is a {tokenizer.mask_token}"""			, top_k=2			, targets=__magic_name__				)

										# If we use the most probably targets, and filter differently, we should still
										# have the same results
										lowercase    :      List[Any]       = [el["""token_str"""] for el in sorted(__magic_name__			, key=lambda __magic_name__				: x["score"]			, reverse=__magic_name__				)]
										# For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
										if set(__magic_name__				).issubset(__magic_name__				):
														lowercase    :      int       = fill_masker(f"""This is a {tokenizer.mask_token}"""			, top_k=3			, targets=__magic_name__				)
														# They should yield exactly the same result
														self.assertEqual(nested_simplify(__magic_name__				)			, nested_simplify(__magic_name__				)				)






						def      __snake_case      (		self  :Optional[Any]			, __magic_name__  :Tuple			, __magic_name__  :List[Any]				)      ->str:
										lowercase    :      Dict       = FillMaskPipeline(model=__magic_name__			, tokenizer=__magic_name__				)
										lowercase    :      Union[str, Any]       = tokenizer.get_vocab()
										# String duplicates + id duplicates
										lowercase    :      Union[str, Any]       = sorted(vocab.keys()				)[:3]
										lowercase    :      Union[str, Any]       = [targets[0], targets[1], targets[0], targets[2], targets[1]]
										lowercase    :      Optional[int]       = fill_masker(f"""My name is {tokenizer.mask_token}"""			, targets=__magic_name__			, top_k=10				)

										# The target list contains duplicates, so we can't output more
										# than them
										self.assertEqual(len(__magic_name__				)			, 3				)






						def      __snake_case      (		self  :Tuple			, __magic_name__  :Union[str, Any]			, __magic_name__  :Optional[Any]				)      ->Union[str, Any]:
										lowercase    :      List[str]       = FillMaskPipeline(model=__magic_name__			, tokenizer=__magic_name__				)

										lowercase    :      Dict       = fill_masker(
										    f"""This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}"""			, top_k=2				)
										self.assertEqual(
										    __magic_name__			, [
										        [
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        ],
										        [
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        ],
										        [
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										            {"""sequence""": ANY(__magic_name__				), """score""": ANY(__magic_name__				), """token""": ANY(__magic_name__				), """token_str""": ANY(__magic_name__				)},
										        ],
										    ]			, )








"""simple docstring"""







from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

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_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
				import PIL

_lowerCAmelCase            =	logging.get_logger(__name__)


def      UpperCamelCase       ( _A )     ->       List[List[ImageInput]]:
				if isinstance(_A  ,				(list, tuple) ) and isinstance(videos[0]  ,				(list, tuple) ) and is_valid_image(videos[0][0] ):
								return videos

				elif isinstance(_A  ,				(list, tuple) ) and is_valid_image(videos[0] ):
								return [videos]

				elif is_valid_image(_A ):
								return [[videos]]

				raise ValueError(F"""Could not make batched video from {videos}""" )


class      UpperCamelCase		(__snake_case     ):
						_SCREAMING_SNAKE_CASE  : str												=		["""pixel_values"""]






						def __init__(		self  :str			, __magic_name__  :bool = True			, __magic_name__  :Dict[str, int] = None			, __magic_name__  :PILImageResampling = PILImageResampling.BILINEAR			, __magic_name__  :bool = True			, __magic_name__  :Dict[str, int] = None			, __magic_name__  :bool = True			, __magic_name__  :Union[int, float] = 1 / 255			, __magic_name__  :bool = True			, __magic_name__  :bool = True			, __magic_name__  :Optional[Union[float, List[float]]] = None			, __magic_name__  :Optional[Union[float, List[float]]] = None			, **__magic_name__  :List[str]			, )      ->None:
										super().__init__(**__magic_name__				)
										lowercase    :      Union[str, Any]       = size if size is not None else {"""shortest_edge""": 256}
										lowercase    :      Tuple       = get_size_dict(__magic_name__			, default_to_square=__magic_name__				)
										lowercase    :      List[Any]       = crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
										lowercase    :      List[str]       = get_size_dict(__magic_name__			, param_name="""crop_size"""				)

										lowercase    :      Tuple       = do_resize
										lowercase    :      Optional[Any]       = size
										lowercase    :      Union[str, Any]       = do_center_crop
										lowercase    :      List[Any]       = crop_size
										lowercase    :      Union[str, Any]       = resample
										lowercase    :      Optional[Any]       = do_rescale
										lowercase    :      int       = rescale_factor
										lowercase    :      Dict       = offset
										lowercase    :      Optional[int]       = do_normalize
										lowercase    :      Optional[Any]       = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
										lowercase    :      Tuple       = image_std if image_std is not None else IMAGENET_STANDARD_STD






						def      __snake_case      (		self  :Any			, __magic_name__  :np.ndarray			, __magic_name__  :Dict[str, int]			, __magic_name__  :PILImageResampling = PILImageResampling.BILINEAR			, __magic_name__  :Optional[Union[str, ChannelDimension]] = None			, **__magic_name__  :Optional[Any]			, )      ->np.ndarray:
										lowercase    :      Optional[int]       = get_size_dict(__magic_name__			, default_to_square=__magic_name__				)
										if "shortest_edge" in size:
														lowercase    :      int       = get_resize_output_image_size(__magic_name__			, size["""shortest_edge"""]			, default_to_square=__magic_name__				)
										elif "height" in size and "width" in size:
														lowercase    :      Tuple       = (size["""height"""], size["""width"""])
										else:
														raise ValueError(f"""Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}"""				)
										return resize(__magic_name__			, size=__magic_name__			, resample=__magic_name__			, data_format=__magic_name__			, **__magic_name__				)






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






						def      __snake_case      (		self  :Tuple			, __magic_name__  :np.ndarray			, __magic_name__  :Union[int, float]			, __magic_name__  :bool = True			, __magic_name__  :Optional[Union[str, ChannelDimension]] = None			, **__magic_name__  :Optional[Any]			, )      ->Any:
										lowercase    :      Optional[int]       = image.astype(np.floataa				)
										if offset:
														lowercase    :      Optional[int]       = image - (scale / 2)
										return rescale(__magic_name__			, scale=__magic_name__			, data_format=__magic_name__			, **__magic_name__				)






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






						def      __snake_case      (		self  :List[Any]			, __magic_name__  :ImageInput			, __magic_name__  :bool = None			, __magic_name__  :Dict[str, int] = None			, __magic_name__  :PILImageResampling = None			, __magic_name__  :bool = None			, __magic_name__  :Dict[str, int] = None			, __magic_name__  :bool = None			, __magic_name__  :float = None			, __magic_name__  :bool = None			, __magic_name__  :bool = None			, __magic_name__  :Optional[Union[float, List[float]]] = None			, __magic_name__  :Optional[Union[float, List[float]]] = None			, __magic_name__  :Optional[ChannelDimension] = ChannelDimension.FIRST			, )      ->np.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_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."""				)

										if offset and not do_rescale:
														raise ValueError("""For offset, do_rescale must also be set to True."""				)

										# All transformations expect numpy arrays.
										lowercase    :      Optional[Any]       = to_numpy_array(__magic_name__				)

										if do_resize:
														lowercase    :      Any       = self.resize(image=__magic_name__			, size=__magic_name__			, resample=__magic_name__				)

										if do_center_crop:
														lowercase    :      Any       = self.center_crop(__magic_name__			, size=__magic_name__				)

										if do_rescale:
														lowercase    :      Optional[int]       = self.rescale(image=__magic_name__			, scale=__magic_name__			, offset=__magic_name__				)

										if do_normalize:
														lowercase    :      Union[str, Any]       = self.normalize(image=__magic_name__			, mean=__magic_name__			, std=__magic_name__				)

										lowercase    :      Optional[Any]       = to_channel_dimension_format(__magic_name__			, __magic_name__				)
										return image






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

										lowercase    :      Optional[int]       = size if size is not None else self.size
										lowercase    :      Optional[Any]       = get_size_dict(__magic_name__			, default_to_square=__magic_name__				)
										lowercase    :      Tuple       = crop_size if crop_size is not None else self.crop_size
										lowercase    :      List[str]       = get_size_dict(__magic_name__			, param_name="""crop_size"""				)

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

										lowercase    :      Tuple       = make_batched(__magic_name__				)

										lowercase    :      List[str]       = [
										    [
										        self._preprocess_image(
										            image=__magic_name__			, do_resize=__magic_name__			, size=__magic_name__			, resample=__magic_name__			, do_center_crop=__magic_name__			, crop_size=__magic_name__			, do_rescale=__magic_name__			, rescale_factor=__magic_name__			, offset=__magic_name__			, do_normalize=__magic_name__			, image_mean=__magic_name__			, image_std=__magic_name__			, data_format=__magic_name__			, )
										        for img in video
										    ]
										    for video in videos
										]

										lowercase    :      Dict       = {"""pixel_values""": videos}
										return BatchFeature(data=__magic_name__			, tensor_type=__magic_name__				)
