a
    0Sic|                     @   sx   d Z ddlmZ ddlmZ ddlmZ dd Zdd Zd	d
 Z	dd Z
dddZdd Zdd ZdddZdd ZdS )zDContains layer utilities for input validation and format conversion.    )
smart_cond)control_flow_ops)	variablesc                 C   s   | dkr>|dkrdS |dkr dS |dkr,dS t d| d	nN| d
kr||dkrRdS |dkr^dS |dkrjdS t d| d	nt d|  dd S )Nchannels_last   NWC   NHWC   NDHWCzInput rank: z5 not supported. We only support input rank 3, 4 or 5.channels_firstNCWNCHWNCDHWzInvalid data_format: z5. We only support "channels_first" or "channels_last")
ValueError)data_formatndim r   Z/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/layers/utils.pyconvert_data_format   s"    r   c                 C   s   t | tr| f| S zt| }W n4 tyT   td| dt| dt|  Y n0 t||krtd| dt| dt|  |D ]b}zt| W q ttfy   td| dt| dt|  dt| dtt| 
Y q0 q|S dS )a  Transforms a single integer or iterable of integers into an integer tuple.

  Args:
    value: The value to validate and convert. Could an int, or any iterable
      of ints.
    n: The size of the tuple to be returned.
    name: The name of the argument being validated, e.g. "strides" or
      "kernel_size". This is only used to format error messages.

  Returns:
    A tuple of n integers.

  Raises:
    ValueError: If something else than an int/long or iterable thereof was
      passed.
  z
Argument `z` must be a tuple of z integers. Received: z including element z	 of type N)
isinstanceinttuple	TypeErrorr   strlentype)valuennamevalue_tuplesingle_valuer   r   r   normalize_tuple0   s0    


r"   c                 C   s(   |   }|dvr$tdt|  d|S )N>   r   r   zWThe `data_format` argument must be one of "channels_first", "channels_last". Received: .lowerr   r   )r   r   r   r   r   normalize_data_formatW   s    
r&   c                 C   s(   |   }|dvr$tdt| d|S )N>   validsamezAThe `padding` argument must be one of "valid", "same". Received: r#   r$   )r   paddingr   r   r   normalize_padding`   s    
r*      c                 C   st   | du rdS |dv sJ ||d |d   }|dkr:| }n*|dkrP| | d }n|dkrd| | d }|| d | S )a  Determines output length of a convolution given input length.

  Args:
      input_length: integer.
      filter_size: integer.
      padding: one of "same", "valid", "full".
      stride: integer.
      dilation: dilation rate, integer.

  Returns:
      The output length (integer).
  N>   r'   r(   fullr+   r(   r'   r,   r   )input_lengthfilter_sizer)   stridedilationdilated_filter_sizeoutput_lengthr   r   r   conv_output_lengthh   s    r3   c                 C   s`   | du rdS |dv sJ |dkr*|d }n|dkr8d}n|dkrH|d }| d | d|  | S )	zDetermines input length of a convolution given output length.

  Args:
      output_length: integer.
      filter_size: integer.
      padding: one of "same", "valid", "full".
      stride: integer.

  Returns:
      The input length (integer).
  N>   r'   r(   r,   r(      r'   r   r,   r+   r   )r2   r.   r)   r/   padr   r   r   conv_input_length   s    
r6   c                 C   sL   | du rdS | |9 } |dkr0| t || d7 } n|dkrH| || d 8 } | S )a  Determines output length of a transposed convolution given input length.

  Args:
      input_length: integer.
      filter_size: integer.
      padding: one of "same", "valid", "full".
      stride: integer.

  Returns:
      The output length (integer).
  Nr'   r   r,   r4   )max)r-   r.   r)   r/   r   r   r   deconv_output_length   s    r8   Nc                 C   s0   t | tjrtj| |||dS tj| |||dS )a  Return either `true_fn()` if predicate `pred` is true else `false_fn()`.

  If `pred` is a bool or has a constant value, we return either `true_fn()`
  or `false_fn()`, otherwise we use `tf.cond` to dynamically route to both.

  Args:
    pred: A scalar determining whether to return the result of `true_fn` or
      `false_fn`.
    true_fn: The callable to be performed if pred is true.
    false_fn: The callable to be performed if pred is false.
    name: Optional name prefix when using `tf.cond`.

  Returns:
    Tensors returned by the call to either `true_fn` or `false_fn`.

  Raises:
    TypeError: If `true_fn` or `false_fn` is not callable.
  )true_fnfalse_fnr   )r   r   Variabler   condsmart_moduler   )predr9   r:   r   r   r   r   r      s    r   c                 C   s>   t | tr$| dkrd} n| dkr$d} t | tjr4dS t| S )a  Return the bool value for `pred`, or None if `pred` had a dynamic value.

    Args:
      pred: A scalar, either a Python bool or a TensorFlow boolean variable
        or tensor, or the Python integer 1 or 0.

    Returns:
      True or False if `pred` has a constant boolean value, None otherwise.

    Raises:
      TypeError: If `pred` is not a Variable, Tensor or bool, or Python
        integer 1 or 0.
    r+   Tr   FN)r   r   r   r;   r=   smart_constant_value)r>   r   r   r   constant_value   s    
r@   )r+   )NNN)__doc__tensorflow.python.frameworkr   r=   tensorflow.python.opsr   r   r   r"   r&   r*   r3   r6   r8   r@   r   r   r   r   <module>   s   '	

