

'''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_video_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import VivitImageProcessor



class 				_snake_case					(   unittest.TestCase			):

  def __init__(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE=7      , _SCREAMING_SNAKE_CASE=3      , _SCREAMING_SNAKE_CASE=10      , _SCREAMING_SNAKE_CASE=18      , _SCREAMING_SNAKE_CASE=30      , _SCREAMING_SNAKE_CASE=4_00      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=None      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]      , _SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]      , _SCREAMING_SNAKE_CASE=None      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							size if size is not None else {'shortest_edge': 18}
      lowerCAmelCase 						=							crop_size if crop_size is not None else {'height': 18, 'width': 18}

      lowerCAmelCase 						=							parent
      lowerCAmelCase 						=							batch_size
      lowerCAmelCase 						=							num_channels
      lowerCAmelCase 						=							num_frames
      lowerCAmelCase 						=							image_size
      lowerCAmelCase 						=							min_resolution
      lowerCAmelCase 						=							max_resolution
      lowerCAmelCase 						=							do_resize
      lowerCAmelCase 						=							size
      lowerCAmelCase 						=							do_normalize
      lowerCAmelCase 						=							image_mean
      lowerCAmelCase 						=							image_std
      lowerCAmelCase 						=							crop_size


  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

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



@require_torch
@require_vision
class 				_snake_case					(   a_     , unittest.TestCase			):
  SCREAMING_SNAKE_CASE			:       str				=     VivitImageProcessor if is_vision_available() else None

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							VivitImageProcessingTester(self	)

  @property
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return self.image_processor_tester.prepare_image_processor_dict()

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class(**self.image_processor_dict	)
      self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      , 'image_mean'	)	)
      self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      , 'image_std'	)	)
      self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      , 'do_normalize'	)	)
      self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      , 'do_resize'	)	)
      self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      , 'do_center_crop'	)	)
      self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      , 'size'	)	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class.from_dict(self.image_processor_dict	)
      self.assertEqual(image_processor.size      , {'shortest_edge': 18}	)
      self.assertEqual(image_processor.crop_size      , {'height': 18, 'width': 18}	)

      lowerCAmelCase 						=							self.image_processing_class.from_dict(self.image_processor_dict      , size=42      , crop_size=84	)
      self.assertEqual(image_processor.size      , {'shortest_edge': 42}	)
      self.assertEqual(image_processor.crop_size      , {'height': 84, 'width': 84}	)

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class(**self.image_processor_dict	)
      # create random PIL videos
      lowerCAmelCase 						=							prepare_video_inputs(self.image_processor_tester      , equal_resolution=_SCREAMING_SNAKE_CASE	)
      for video in video_inputs:
          self.assertIsInstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)
          self.assertIsInstance(video[0]      , Image.Image	)

      # Test not batched input
      lowerCAmelCase 						=							image_processing(video_inputs[0]      , return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_videos.shape      , (
              1,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )      , )

      # Test batched
      lowerCAmelCase 						=							image_processing(_SCREAMING_SNAKE_CASE      , return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_videos.shape      , (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )      , )

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class(**self.image_processor_dict	)
      # create random numpy tensors
      lowerCAmelCase 						=							prepare_video_inputs(self.image_processor_tester      , equal_resolution=_SCREAMING_SNAKE_CASE      , numpify=_SCREAMING_SNAKE_CASE	)
      for video in video_inputs:
          self.assertIsInstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)
          self.assertIsInstance(video[0]      , np.ndarray	)

      # Test not batched input
      lowerCAmelCase 						=							image_processing(video_inputs[0]      , return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_videos.shape      , (
              1,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )      , )

      # Test batched
      lowerCAmelCase 						=							image_processing(_SCREAMING_SNAKE_CASE      , return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_videos.shape      , (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )      , )


  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class(**self.image_processor_dict	)
      # create random PyTorch tensors
      lowerCAmelCase 						=							prepare_video_inputs(self.image_processor_tester      , equal_resolution=_SCREAMING_SNAKE_CASE      , torchify=_SCREAMING_SNAKE_CASE	)
      for video in video_inputs:
          self.assertIsInstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)
          self.assertIsInstance(video[0]      , torch.Tensor	)

      # Test not batched input
      lowerCAmelCase 						=							image_processing(video_inputs[0]      , return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_videos.shape      , (
              1,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )      , )

      # Test batched
      lowerCAmelCase 						=							image_processing(_SCREAMING_SNAKE_CASE      , return_tensors='pt'	).pixel_values
      self.assertEqual(
          encoded_videos.shape      , (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_frames,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size['height'],
              self.image_processor_tester.crop_size['width'],
          )      , )








'''simple docstring'''



from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
     from PIL import Image

     from ..image_utils import load_image

if is_tf_available():
     import tensorflow as tf

     from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
     from ..tf_utils import stable_softmax

if is_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

_UpperCamelCase						:  Optional[int]							       =				logging.get_logger(__name__)



@add_end_docstrings(a_			)
class 				_snake_case					(   a_			):

  def __init__(				self      , *_SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      super().__init__(*_SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	)
      requires_backends(self      , 'vision'	)
      self.check_model_type(
          TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
          if self.framework == 'tf'
          else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE=None	):





      '''simple docstring'''

      lowerCAmelCase 						=							{}
      if top_k is not None:
          lowerCAmelCase 						=							top_k
      return {}, {}, postprocess_params

  def __call__(				self      , _SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      return super().__call__(_SCREAMING_SNAKE_CASE      , **_SCREAMING_SNAKE_CASE	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							load_image(_SCREAMING_SNAKE_CASE	)
      lowerCAmelCase 						=							self.image_processor(images=_SCREAMING_SNAKE_CASE      , return_tensors=self.framework	)
      return model_inputs

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.model(**_SCREAMING_SNAKE_CASE	)
      return model_outputs


  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE=5	):





      '''simple docstring'''

      if top_k > self.model.config.num_labels:
          lowerCAmelCase 						=							self.model.config.num_labels

      if self.framework == "pt":
          lowerCAmelCase 						=							model_outputs.logits.softmax(-1	)[0]
          lowerCAmelCase						,   lowerCAmelCase 						=							probs.topk(_SCREAMING_SNAKE_CASE	)
      elif self.framework == "tf":
          lowerCAmelCase 						=							stable_softmax(model_outputs.logits      , axis=-1	)[0]
          lowerCAmelCase 						=							tf.math.top_k(_SCREAMING_SNAKE_CASE      , k=_SCREAMING_SNAKE_CASE	)
          lowerCAmelCase						,   lowerCAmelCase 						=							topk.values.numpy(), topk.indices.numpy()
      else:
          raise ValueError(F'Unsupported framework: {self.framework}'	)

      lowerCAmelCase 						=							scores.tolist()
      lowerCAmelCase 						=							ids.tolist()
      return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	)]





