




'''simple docstring'''



import re
from typing import Callable, List, Optional, Union

import tensorflow as tf


try:
     from tensorflow.keras.optimizers.legacy import Adam
except ImportError:
     from tensorflow.keras.optimizers import Adam





class      lowerCAmelCase (			tf.keras.optimizers.schedules.LearningRateSchedule):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE = 1.0     ,	__SCREAMING_SNAKE_CASE = None     ,	)   ->	Optional[int]:



            '''simple docstring'''




            super().__init__()
            __snake_case            =	initial_learning_rate
            __snake_case            =	warmup_steps
            __snake_case            =	power
            __snake_case            =	decay_schedule_fn
            __snake_case            =	name


       def __call__( self     ,	__SCREAMING_SNAKE_CASE	)   ->	Optional[Any]:



            '''simple docstring'''




            with tf.name_scope(self.name or '''WarmUp'''	) as name:
                 # Implements polynomial warmup. i.e., if global_step < warmup_steps, the
                 # learning rate will be `global_step/num_warmup_steps * init_lr`.
                 __snake_case            =	tf.cast(__SCREAMING_SNAKE_CASE     ,	tf.floataa	)
                 __snake_case            =	tf.cast(self.warmup_steps     ,	tf.floataa	)
                 __snake_case            =	global_step_float / warmup_steps_float
                 __snake_case            =	self.initial_learning_rate * tf.math.pow(__SCREAMING_SNAKE_CASE     ,	self.power	)
                 return tf.cond(
                     global_step_float < warmup_steps_float     ,	lambda: warmup_learning_rate     ,	lambda: self.decay_schedule_fn(step - self.warmup_steps	)     ,	name=__SCREAMING_SNAKE_CASE     ,	)




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



            '''simple docstring'''




            return {
                "initial_learning_rate": self.initial_learning_rate,
                "decay_schedule_fn": self.decay_schedule_fn,
                "warmup_steps": self.warmup_steps,
                "power": self.power,
                "name": self.name,
            }







def       _UpperCamelCase			(_lowerCamelCase				:  float ,						_lowerCamelCase				:  int ,						_lowerCamelCase				:  int ,						_lowerCamelCase				:  float = 0.0 ,						_lowerCamelCase				:  float = 0.9 ,						_lowerCamelCase				:  float = 0.999 ,						_lowerCamelCase				:  float = 1E-8 ,						_lowerCamelCase				:  Optional[float] = None ,						_lowerCamelCase				:  Optional[float] = None ,						_lowerCamelCase				:  float = 0.0 ,						_lowerCamelCase				:  float = 1.0 ,						_lowerCamelCase				:  Optional[List[str]] = None ,						)-> List[str]:




     '''simple docstring'''





     __snake_case            =	tf.keras.optimizers.schedules.PolynomialDecay(
         initial_learning_rate=_lowerCamelCase ,						decay_steps=num_train_steps - num_warmup_steps ,						end_learning_rate=init_lr * min_lr_ratio ,						power=_lowerCamelCase ,						)
     if num_warmup_steps:
          __snake_case            =	WarmUp(
              initial_learning_rate=_lowerCamelCase ,						decay_schedule_fn=_lowerCamelCase ,						warmup_steps=_lowerCamelCase ,						)
     if weight_decay_rate > 0.0:
          __snake_case            =	AdamWeightDecay(
              learning_rate=_lowerCamelCase ,						weight_decay_rate=_lowerCamelCase ,						beta_a=_lowerCamelCase ,						beta_a=_lowerCamelCase ,						epsilon=_lowerCamelCase ,						clipnorm=_lowerCamelCase ,						global_clipnorm=_lowerCamelCase ,						exclude_from_weight_decay=['''LayerNorm''', '''layer_norm''', '''bias'''] ,						include_in_weight_decay=_lowerCamelCase ,						)
     else:
          __snake_case            =	tf.keras.optimizers.Adam(
              learning_rate=_lowerCamelCase ,						beta_a=_lowerCamelCase ,						beta_a=_lowerCamelCase ,						epsilon=_lowerCamelCase ,						clipnorm=_lowerCamelCase ,						global_clipnorm=_lowerCamelCase ,						)
     # We return the optimizer and the LR scheduler in order to better track the
     # evolution of the LR independently of the optimizer.
     return optimizer, lr_schedule





class      lowerCAmelCase (			__lowerCAmelCase):


       def __init__( self     ,	__SCREAMING_SNAKE_CASE = 0.001     ,	__SCREAMING_SNAKE_CASE = 0.9     ,	__SCREAMING_SNAKE_CASE = 0.999     ,	__SCREAMING_SNAKE_CASE = 1E-7     ,	__SCREAMING_SNAKE_CASE = False     ,	__SCREAMING_SNAKE_CASE = 0.0     ,	__SCREAMING_SNAKE_CASE = None     ,	__SCREAMING_SNAKE_CASE = None     ,	__SCREAMING_SNAKE_CASE = "AdamWeightDecay"     ,	**__SCREAMING_SNAKE_CASE     ,	)   ->	Optional[Any]:



            '''simple docstring'''




            super().__init__(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)
            __snake_case            =	weight_decay_rate
            __snake_case            =	include_in_weight_decay
            __snake_case            =	exclude_from_weight_decay


       @classmethod
       def    lowerCAmelCase   ( cls     ,	__SCREAMING_SNAKE_CASE	)   ->	Dict:



            '''simple docstring'''




            __snake_case            =	{'''WarmUp''': WarmUp}
            return super(__SCREAMING_SNAKE_CASE     ,	cls	).from_config(__SCREAMING_SNAKE_CASE     ,	custom_objects=__SCREAMING_SNAKE_CASE	)


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



            '''simple docstring'''




            super(__SCREAMING_SNAKE_CASE     ,	self	)._prepare_local(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
            __snake_case            =	tf.constant(
                self.weight_decay_rate     ,	name='''adam_weight_decay_rate'''	)


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	Optional[int]:



            '''simple docstring'''




            __snake_case            =	self._do_use_weight_decay(var.name	)
            if do_decay:
                 return var.assign_sub(
                     learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]['''weight_decay_rate''']     ,	use_locking=self._use_locking     ,	)
            return tf.no_op()


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=None     ,	**__SCREAMING_SNAKE_CASE	)   ->	List[str]:



            '''simple docstring'''




            __snake_case       ,     __snake_case            =	list(zip(*__SCREAMING_SNAKE_CASE	)	)
            return super(__SCREAMING_SNAKE_CASE     ,	self	).apply_gradients(zip(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)     ,	name=__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)   ->	str:



            '''simple docstring'''




            if apply_state is None:
                 return self._decayed_lr_t[var_dtype], {}

            __snake_case            =	apply_state or {}
            __snake_case            =	apply_state.get((var_device, var_dtype)	)
            if coefficients is None:
                 __snake_case            =	self._fallback_apply_state(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
                 __snake_case            =	coefficients

            return coefficients["lr_t"], {"apply_state": apply_state}


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=None	)   ->	List[Any]:



            '''simple docstring'''




            __snake_case       ,     __snake_case            =	self._get_lr(var.device     ,	var.dtype.base_dtype     ,	__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self._decay_weights_op(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
            with tf.control_dependencies([decay]	):
                 return super(__SCREAMING_SNAKE_CASE     ,	self	)._resource_apply_dense(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=None	)   ->	List[str]:



            '''simple docstring'''




            __snake_case       ,     __snake_case            =	self._get_lr(var.device     ,	var.dtype.base_dtype     ,	__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self._decay_weights_op(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)
            with tf.control_dependencies([decay]	):
                 return super(__SCREAMING_SNAKE_CASE     ,	self	)._resource_apply_sparse(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)


       def    lowerCAmelCase   ( self	)   ->	Dict:



            '''simple docstring'''




            __snake_case            =	super().get_config()
            config.update({'''weight_decay_rate''': self.weight_decay_rate}	)
            return config




       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	Optional[int]:



            '''simple docstring'''




            if self.weight_decay_rate == 0:
                 return False

            if self._include_in_weight_decay:
                 for r in self._include_in_weight_decay:
                      if re.search(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	) is not None:
                           return True

            if self._exclude_from_weight_decay:
                 for r in self._exclude_from_weight_decay:
                      if re.search(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	) is not None:
                           return False
            return True





class      lowerCAmelCase (			__lowerCAmelCase):


       def __init__( self	)   ->	int:



            '''simple docstring'''




            __snake_case            =	[]
            __snake_case            =	None


       @property
       def    lowerCAmelCase   ( self	)   ->	Union[str, Any]:



            '''simple docstring'''




            if self._accum_steps is None:
                 __snake_case            =	tf.Variable(
                     tf.constant(0     ,	dtype=tf.intaa	)     ,	trainable=__SCREAMING_SNAKE_CASE     ,	synchronization=tf.VariableSynchronization.ON_READ     ,	aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA     ,	)

            return self._accum_steps.value()


       @property
       def    lowerCAmelCase   ( self	)   ->	Tuple:



            '''simple docstring'''




            if not self._gradients:
                 raise ValueError('''The accumulator should be called first to initialize the gradients'''	)
            return [gradient.value() if gradient is not None else gradient for gradient in self._gradients]


       def __call__( self     ,	__SCREAMING_SNAKE_CASE	)   ->	int:



            '''simple docstring'''




            if not self._gradients:
                 __snake_case            =	self.step  # Create the step variable.
                 self._gradients.extend(
                     [
                         tf.Variable(
                             tf.zeros_like(__SCREAMING_SNAKE_CASE	)     ,	trainable=__SCREAMING_SNAKE_CASE     ,	synchronization=tf.VariableSynchronization.ON_READ     ,	aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA     ,	)
                         if gradient is not None
                         else gradient
                         for gradient in gradients
                     ]	)
            if len(__SCREAMING_SNAKE_CASE	) != len(self._gradients	):
                 raise ValueError(F'''Expected {len(self._gradients	)} gradients, but got {len(__SCREAMING_SNAKE_CASE	)}'''	)

            for accum_gradient, gradient in zip(self._gradients     ,	__SCREAMING_SNAKE_CASE	):
                 if accum_gradient is not None and gradient is not None:
                      accum_gradient.assign_add(__SCREAMING_SNAKE_CASE	)

            self._accum_steps.assign_add(1	)




       def    lowerCAmelCase   ( self	)   ->	List[str]:



            '''simple docstring'''




            if not self._gradients:
                 return
            self._accum_steps.assign(0	)
            for gradient in self._gradients:
                 if gradient is not None:
                      gradient.assign(tf.zeros_like(__SCREAMING_SNAKE_CASE	)	)












'''simple docstring'''



from collections import UserDict
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_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
     from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
     from ..tf_utils import stable_softmax

UpperCAmelCase_							:	str	     =		logging.get_logger(__name__)





@add_end_docstrings(__lowerCAmelCase)
class      lowerCAmelCase (			__lowerCAmelCase):


       def __init__( self     ,	**__SCREAMING_SNAKE_CASE	)   ->	Optional[int]:



            '''simple docstring'''




            super().__init__(**__SCREAMING_SNAKE_CASE	)

            requires_backends(self     ,	'''vision'''	)
            self.check_model_type(
                TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
                if self.framework == '''tf'''
                else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING	)


       def __call__( self     ,	__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)   ->	List[Any]:



            '''simple docstring'''




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


       def    lowerCAmelCase   ( self     ,	**__SCREAMING_SNAKE_CASE	)   ->	Tuple:



            '''simple docstring'''




            __snake_case            =	{}
            if "candidate_labels" in kwargs:
                 __snake_case            =	kwargs['''candidate_labels''']
            if "hypothesis_template" in kwargs:
                 __snake_case            =	kwargs['''hypothesis_template''']

            return preprocess_params, {}, {}


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE="This is a photo of {}."	)   ->	Optional[Any]:



            '''simple docstring'''




            __snake_case            =	load_image(__SCREAMING_SNAKE_CASE	)
            __snake_case            =	self.image_processor(images=[image]     ,	return_tensors=self.framework	)
            __snake_case            =	candidate_labels
            __snake_case            =	[hypothesis_template.format(__SCREAMING_SNAKE_CASE	) for x in candidate_labels]
            __snake_case            =	self.tokenizer(__SCREAMING_SNAKE_CASE     ,	return_tensors=self.framework     ,	padding=__SCREAMING_SNAKE_CASE	)
            __snake_case            =	[text_inputs]
            return inputs


       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	Dict:



            '''simple docstring'''




            __snake_case            =	model_inputs.pop('''candidate_labels'''	)
            __snake_case            =	model_inputs.pop('''text_inputs'''	)
            if isinstance(text_inputs[0]     ,	__SCREAMING_SNAKE_CASE	):
                 __snake_case            =	text_inputs[0]
            else:
                 # Batching case.
                 __snake_case            =	text_inputs[0][0]

            __snake_case            =	self.model(**__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)

            __snake_case            =	{
                '''candidate_labels''': candidate_labels,
                '''logits''': outputs.logits_per_image,
            }
            return model_outputs




       def    lowerCAmelCase   ( self     ,	__SCREAMING_SNAKE_CASE	)   ->	str:



            '''simple docstring'''




            __snake_case            =	model_outputs.pop('''candidate_labels'''	)
            __snake_case            =	model_outputs['''logits'''][0]
            if self.framework == "pt":
                 __snake_case            =	logits.softmax(dim=-1	).squeeze(-1	)
                 __snake_case            =	probs.tolist()
                 if not isinstance(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	):
                      __snake_case            =	[scores]
            elif self.framework == "tf":
                 __snake_case            =	stable_softmax(__SCREAMING_SNAKE_CASE     ,	axis=-1	)
                 __snake_case            =	probs.numpy().tolist()
            else:
                 raise ValueError(F'''Unsupported framework: {self.framework}'''	)

            __snake_case            =	[
                {'''score''': score, '''label''': candidate_label}
                for score, candidate_label in sorted(zip(__SCREAMING_SNAKE_CASE     ,	__SCREAMING_SNAKE_CASE	)     ,	key=lambda __SCREAMING_SNAKE_CASE	: -x[0]	)
            ]
            return result






