o
    2h>                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ dadZdZd	Zd
ZdZdZdZdZe	dd#ddZejedede_e	dd$ddZdd Zdd Z	d%ddZdd  Zd!d" ZdS )&    N)activations)backend)ops)keras_export)
file_utilszUhttps://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.jsona:  
  Preprocesses a tensor or Numpy array encoding a batch of images.

  Usage example with `applications.MobileNet`:

  ```python
  i = keras.layers.Input([None, None, 3], dtype="uint8")
  x = ops.cast(i, "float32")
  x = keras.applications.mobilenet.preprocess_input(x)
  core = keras.applications.MobileNet()
  x = core(x)
  model = keras.Model(inputs=[i], outputs=[x])
  result = model(image)
  ```

  Args:
        x: A floating point `numpy.array` or a backend-native tensor,
            3D or 4D with 3 color
            channels, with values in the range [0, 255].
            The preprocessed data are written over the input data
        if the data types are compatible. To avoid this
        behaviour, `numpy.copy(x)` can be used.
        data_format: Optional data format of the image tensor/array. None, means
        the global setting `keras.backend.image_data_format()` is used
        (unless you changed it, it uses "channels_last").{mode}
        Defaults to `None`.

  Returns:
      Preprocessed array with type `float32`.
      {ret}

  Raises:
      {error}
  a  
    mode: One of "caffe", "tf" or "torch".
      - caffe: will convert the images from RGB to BGR,
          then will zero-center each color channel with
          respect to the ImageNet dataset,
          without scaling.
      - tf: will scale pixels between -1 and 1,
          sample-wise.
      - torch: will scale pixels between 0 and 1 and then
          will normalize each channel with respect to the
          ImageNet dataset.
      Defaults to `"caffe"`.
  zE
    ValueError: In case of unknown `mode` or `data_format` argument.z;
    ValueError: In case of unknown `data_format` argument.zH
      The inputs pixel values are scaled between -1 and 1, sample-wise.z
      The input pixels values are scaled between 0 and 1 and each channel is
      normalized with respect to the ImageNet dataset.z
      The images are converted from RGB to BGR, then each color channel is
      zero-centered with respect to the ImageNet dataset, without scaling.z2keras.applications.imagenet_utils.preprocess_inputcaffec                 C   sf   |dvrt d| |du rt }n|dvrt d| t| tjr,t| ||dS t| ||dS )z@Preprocesses a tensor or Numpy array encoding a batch of images.>   tfr   torchzDExpected mode to be one of `caffe`, `tf` or `torch`. Received: mode=N>   channels_lastchannels_firstz]Expected data_format to be one of `channels_first` or `channels_last`. Received: data_format=)data_formatmode)
ValueErrorr   image_data_format
isinstancenpndarray_preprocess_numpy_input_preprocess_tensor_input)xr   r    r   `/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/applications/imagenet_utils.pypreprocess_inputV   s"   
r    )r   reterrorz4keras.applications.imagenet_utils.decode_predictions   c                    s   t | jdks| jd dkrtd| j tdu r<tjdtddd	}t|}t	|aW d   n1 s7w   Y  g }t
| } | D ]'   | d ddd
 } fdd|D }|jdd dd || qE|S )a  Decodes the prediction of an ImageNet model.

    Args:
        preds: NumPy array encoding a batch of predictions.
        top: Integer, how many top-guesses to return. Defaults to `5`.

    Returns:
        A list of lists of top class prediction tuples
        `(class_name, class_description, score)`.
        One list of tuples per sample in batch input.

    Raises:
        ValueError: In case of invalid shape of the `pred` array
            (must be 2D).
          i  z{`decode_predictions` expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Received array with shape: Nzimagenet_class_index.jsonmodels c2c37ea517e94d9795004a39431a14cb)cache_subdir	file_hashc                    s&   g | ]}t tt|  | f qS r   )tupleCLASS_INDEXstr.0ipredr   r   
<listcomp>   s   & z&decode_predictions.<locals>.<listcomp>c                 S   s   | d S )Nr   r   r   r   r   r   <lambda>   s    z$decode_predictions.<locals>.<lambda>T)keyreverse)lenshaper   r%   r   get_fileCLASS_INDEX_PATHopenjsonloadr   convert_to_numpyargsortsortappend)predstopfpathfresultstop_indicesresultr   r*   r   decode_predictionst   s0   

rC   c                 C   s6  t | jjtjs| jt dd} |dkr| d } | d8 } | S |dkr0| d } g d}g d	}n1|d
krRt| j	dkrE| ddddf } n| ddddddf } n	| ddddf } g d}d}|d
krXt| j	dkr| dddddf  |d 8  < | dddddf  |d 8  < | dddddf  |d 8  < |dur| dddddf  |d   < | dddddf  |d   < | dddddf  |d   < | S | dddddddf  |d 8  < | dddddddf  |d 8  < | dddddddf  |d 8  < |durV| dddddddf  |d   < | dddddddf  |d   < | dddddddf  |d   < | S | d  |d 8  < | d  |d 8  < | d  |d 8  < |dur| d  |d   < | d  |d   < | d  |d   < | S )a  Preprocesses a NumPy array encoding a batch of images.

    Args:
      x: Input array, 3D or 4D.
      data_format: Data format of the image array.
      mode: One of "caffe", "tf" or "torch".
        - caffe: will convert the images from RGB to BGR,
            then will zero-center each color channel with
            respect to the ImageNet dataset,
            without scaling.
        - tf: will scale pixels between -1 and 1,
            sample-wise.
        - torch: will scale pixels between 0 and 1 and then
            will normalize each channel with respect to the
            ImageNet dataset.

    Returns:
        Preprocessed Numpy array.
    F)copyr        _@      ?r	        o@g
ףp=
?gv/?gCl?gZd;O?gy&1?g?r      Nr#   .gjtY@g`"1]@gQ^@r   r   r   ).r   ).r   ).r   )

issubclassdtypetyper   floatingastyper   floatxr1   r2   )r   r   r   meanstdr   r   r   r      sV   

""""""(((
(((	
r   c                    sZ  t  j}|dkr d   d8   S |dkr$ d  g d}g d}n=|dkrMt  jd	kr>tj fd
ddD dd ntj fdddD dd ntj fdddD dd g d}d}tjt|  jd}|dkr~t|dd|d   }nt|d|d  d } |7  |durtjt| jd}|dkrt|d} |   S )a  Preprocesses a tensor encoding a batch of images.

    Args:
      x: Input tensor, 3D or 4D.
      data_format: Data format of the image tensor.
      mode: One of "caffe", "tf" or "torch".
        - caffe: will convert the images from RGB to BGR,
            then will zero-center each color channel with
            respect to the ImageNet dataset,
            without scaling.
        - tf: will scale pixels between -1 and 1,
            sample-wise.
        - torch: will scale pixels between 0 and 1 and then
            will normalize each channel with respect to the
            ImageNet dataset.

    Returns:
        Preprocessed tensor.
    r   rE   rF   r	   rG   rH   rI   r   rJ   c                    s   g | ]} |d f qS .r   r'   r-   r   r   r,         z,_preprocess_tensor_input.<locals>.<listcomp>)r   r   r   r   )axisc                    s"   g | ]} d d |d d f qS Nr   r'   r-   r   r   r,     s   " r   c                    s   g | ]} d |f qS rT   r   r'   r-   r   r   r,     rU   r#   rK   N)rM   )r   rJ   )r   r   )rJ   )r#   r   r   )	r1   r2   r   stackconvert_to_tensorr   arrayrM   reshape)r   r   r   ndimrR   rS   mean_tensor
std_tensorr   r-   r   r      s6   

r   c                 C   sb  |dkrP| rPt | dkrP|dkr5t | dkrdnd}| | dvr-tjd| d  d	d
d | d ||f}n*| d dvrHtjd| d  d	d
d ||| d f}n|dkrZd||f}n||df}|dkry|ry| durw| |krwtd| d|  |S | r|dkr| durt | dkrtd| d dkr|dkrtd|  d| d dur| d |k s| d
 dur| d
 |k rtd| d| d|  nZ| durt | dkrtd| d dkr|dkrtd|  d| d dur| d |k s| d dur| d |k rtd| d| d|  n|r|} n
|dkrd} nd} |r/d| v r/td|  | S )a  Internal utility to compute/validate a model's input shape.

    Args:
      input_shape: Either None (will return the default network input shape),
        or a user-provided shape to be validated.
      default_size: Default input width/height for the model.
      min_size: Minimum input width/height accepted by the model.
      data_format: Image data format to use.
      require_flatten: Whether the model is expected to
        be linked to a classifier via a Flatten layer.
      weights: One of `None` (random initialization)
        or 'imagenet' (pre-training on ImageNet).
        If weights='imagenet' input channels must be equal to 3.

    Returns:
      An integer shape tuple (may include None entries).

    Raises:
      ValueError: In case of invalid argument values.
    imagenetrJ   r      r   r   >   r   rJ   z]This model usually expects 1 or 3 input channels. However, it was passed an input_shape with z input channels.r   )
stacklevelr#   NzXWhen setting `include_top=True` and loading `imagenet` weights, `input_shape` should be z.  Received: input_shape=z0`input_shape` must be a tuple of three integers.z6The input must have 3 channels; Received `input_shape=`zInput size must be at least r   z; Received: input_shape=)rJ   NN)NNrJ   z[If `include_top` is True, you should specify a static `input_shape`. Received: input_shape=)r1   warningswarnr   )input_shapedefault_sizemin_sizer   require_flattenweightscorrect_channel_axisdefault_shaper   r   r   obtain_input_shape%  s   



rl   c                 C   s   t  dkrdnd}| j||d  }t|tr||f}|d du r%d}nd|d d  d|d d  f}|d d |d d f}|d |d  |d f|d |d  |d ffS )zReturns a tuple for zero-padding for 2D convolution with downsampling.

    Args:
      inputs: Input tensor.
      kernel_size: An integer or tuple/list of 2 integers.

    Returns:
      A tuple.
    r   r   r   r   N)r   r   )r   r   r2   r   int)inputskernel_sizeimg_dim
input_sizeadjustcorrectr   r   r   correct_pad  s   

 rt   c                 C   s@   |du rdS t | } | t dt dhvrtd|  dS )a@  validates that the classifer_activation is compatible with the weights.

    Args:
      classifier_activation: str or callable activation function
      weights: The pretrained weights to load.

    Raises:
      ValueError: if an activation other than `None` or `softmax` are used with
        pretrained weights.
    NsoftmaxzOnly `None` and `softmax` activations are allowed for the `classifier_activation` argument when using pretrained weights, with `include_top=True`; Received: classifier_activation=)r   getr   )classifier_activationri   r   r   r   validate_activation  s   
rx   )Nr   )r   rW   )r6   rc   numpyr   	keras.srcr   r   r   keras.src.api_exportr   keras.src.utilsr   r%   r4   PREPROCESS_INPUT_DOCPREPROCESS_INPUT_MODE_DOC"PREPROCESS_INPUT_DEFAULT_ERROR_DOCPREPROCESS_INPUT_ERROR_DOCPREPROCESS_INPUT_RET_DOC_TFPREPROCESS_INPUT_RET_DOC_TORCHPREPROCESS_INPUT_RET_DOC_CAFFEr   format__doc__rC   r   r   rl   rt   rx   r   r   r   r   <module>   sB    #,IA
x