





'''simple docstring'''

import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import OwlViTImageProcessor





class lowerCamelCase    (unittest.TestCase       ):







  def __init__(		self  ,  lowercase__  ,  lowercase__=7  ,  lowercase__=3  ,  lowercase__=18  ,  lowercase__=30  ,  lowercase__=400  ,  lowercase__=True  ,  lowercase__=None  ,  lowercase__=True  ,  lowercase__=None  ,  lowercase__=True  ,  lowercase__=[0.48_145_466, 0.4_578_275, 0.40_821_073]  ,  lowercase__=[0.26_862_954, 0.26_130_258, 0.27_577_711]  ,  lowercase__=True  ,  )		->    int:






        """simple docstring"""





        _snake_case		:						Optional[Any]         =  parent
        _snake_case		:						Optional[Any]         =  batch_size
        _snake_case		:						Optional[int]         =  num_channels
        _snake_case		:						str         =  image_size
        _snake_case		:						Optional[int]         =  min_resolution
        _snake_case		:						Optional[Any]         =  max_resolution
        _snake_case		:						List[str]         =  do_resize
        _snake_case		:						Any         =  size if size is not None else {'''height''': 18, '''width''': 18}
        _snake_case		:						Optional[Any]         =  do_center_crop
        _snake_case		:						int         =  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
        _snake_case		:						Any         =  do_normalize
        _snake_case		:						Tuple         =  image_mean
        _snake_case		:						Optional[Any]         =  image_std
        _snake_case		:						Any         =  do_convert_rgb







  def 					UpperCAmelCase_					(		self		)		->    List[Any]:






        """simple docstring"""





        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_center_crop": self.do_center_crop,
            "crop_size": self.crop_size,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_convert_rgb": self.do_convert_rgb,
        }





@require_torch
@require_vision
class lowerCamelCase    (lowerCAmelCase__    ,   unittest.TestCase       ):
  _lowercase	:						Optional[int]							       =					OwlViTImageProcessor if is_vision_available() else None







  def 					UpperCAmelCase_					(		self		)		->    str:






        """simple docstring"""





        _snake_case		:						Union[str, Any]         =  OwlViTImageProcessingTester(self		)







  @property
  def 					UpperCAmelCase_					(		self		)		->    Dict:






        """simple docstring"""





        return self.image_processor_tester.prepare_image_processor_dict()







  def 					UpperCAmelCase_					(		self		)		->    Dict:






        """simple docstring"""





        _snake_case		:						Tuple         =  self.image_processing_class(**self.image_processor_dict		)
        self.assertTrue(hasattr(_A  ,  '''do_resize'''		)		)
        self.assertTrue(hasattr(_A  ,  '''size'''		)		)
        self.assertTrue(hasattr(_A  ,  '''do_center_crop'''		)		)
        self.assertTrue(hasattr(_A  ,  '''center_crop'''		)		)
        self.assertTrue(hasattr(_A  ,  '''do_normalize'''		)		)
        self.assertTrue(hasattr(_A  ,  '''image_mean'''		)		)
        self.assertTrue(hasattr(_A  ,  '''image_std'''		)		)
        self.assertTrue(hasattr(_A  ,  '''do_convert_rgb'''		)		)







  def 					UpperCAmelCase_					(		self		)		->    int:






        """simple docstring"""





        _snake_case		:						Optional[Any]         =  self.image_processing_class.from_dict(self.image_processor_dict		)
        self.assertEqual(image_processor.size  ,  {'''height''': 18, '''width''': 18}		)
        self.assertEqual(image_processor.crop_size  ,  {'''height''': 18, '''width''': 18}		)

        _snake_case		:						List[Any]         =  self.image_processing_class.from_dict(self.image_processor_dict  ,  size=42  ,  crop_size=84		)
        self.assertEqual(image_processor.size  ,  {'''height''': 42, '''width''': 42}		)
        self.assertEqual(image_processor.crop_size  ,  {'''height''': 84, '''width''': 84}		)







  def 					UpperCAmelCase_					(		self		)		->    Union[str, Any]:






        """simple docstring"""





        _snake_case		:						Union[str, Any]         =  self.image_processing_class(**self.image_processor_dict		)
        # create random PIL images
        _snake_case		:						int         =  prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=_A		)

        for image in image_inputs:
              self.assertIsInstance(_A  ,  Image.Image		)

        # Test not batched input
        _snake_case		:						Dict         =  image_processing(image_inputs[0]  ,  return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_images.shape  ,  (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,  )

        # Test batched
        _snake_case		:						Dict         =  image_processing(_A  ,  return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_images.shape  ,  (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,  )







  def 					UpperCAmelCase_					(		self		)		->    Union[str, Any]:






        """simple docstring"""





        _snake_case		:						Any         =  self.image_processing_class(**self.image_processor_dict		)
        # create random numpy tensors
        _snake_case		:						Any         =  prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=_A  ,  numpify=_A		)
        for image in image_inputs:
              self.assertIsInstance(_A  ,  np.ndarray		)

        # Test not batched input
        _snake_case		:						Dict         =  image_processing(image_inputs[0]  ,  return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_images.shape  ,  (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,  )

        # Test batched
        _snake_case		:						Dict         =  image_processing(_A  ,  return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_images.shape  ,  (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,  )







  def 					UpperCAmelCase_					(		self		)		->    Union[str, Any]:






        """simple docstring"""





        _snake_case		:						int         =  self.image_processing_class(**self.image_processor_dict		)
        # create random PyTorch tensors
        _snake_case		:						str         =  prepare_image_inputs(self.image_processor_tester  ,  equal_resolution=_A  ,  torchify=_A		)
        for image in image_inputs:
              self.assertIsInstance(_A  ,  torch.Tensor		)

        # Test not batched input
        _snake_case		:						List[str]         =  image_processing(image_inputs[0]  ,  return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_images.shape  ,  (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,  )

        # Test batched
        _snake_case		:						List[Any]         =  image_processing(_A  ,  return_tensors='''pt'''		).pixel_values
        self.assertEqual(
            encoded_images.shape  ,  (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,  )





from typing import Optional

from ..utils.logging import get_logger
from .audio_classificiation import AudioClassification
from .automatic_speech_recognition import AutomaticSpeechRecognition
from .base import TaskTemplate
from .image_classification import ImageClassification
from .language_modeling import LanguageModeling
from .question_answering import QuestionAnsweringExtractive
from .summarization import Summarization
from .text_classification import TextClassification


UpperCamelCase__	:   int								=     [
    "AutomaticSpeechRecognition",
    "AudioClassification",
    "ImageClassification",
    "LanguageModeling",
    "QuestionAnsweringExtractive",
    "Summarization",
    "TaskTemplate",
    "TextClassification",
]

UpperCamelCase__	:   Any								=     get_logger(__name__)


UpperCamelCase__	:   Union[str, Any]								=     {
    AutomaticSpeechRecognition.task: AutomaticSpeechRecognition,
    AudioClassification.task: AudioClassification,
    ImageClassification.task: ImageClassification,
    LanguageModeling.task: LanguageModeling,
    QuestionAnsweringExtractive.task: QuestionAnsweringExtractive,
    Summarization.task: Summarization,
    TextClassification.task: TextClassification,
}


def        _UpperCAmelCase							(	_SCREAMING_SNAKE_CASE       :		dict							):





    """simple docstring"""


    SCREAMING_SNAKE_CASE_						=    task_template_dict.get('task'							)
    if task_name is None:
        logger.warning(f"""Couldn't find template for task '{task_name}'. Available templates: {list(_SCREAMING_SNAKE_CASE							)}"""							)
        return None
    SCREAMING_SNAKE_CASE_						=    NAMEaTEMPLATE.get(_SCREAMING_SNAKE_CASE							)
    return template.from_dict(_SCREAMING_SNAKE_CASE							)


