
    &Vf%                     |    d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	  ed           G d de                      Z
dS )	    N)backend)keras_export)TFDataLayer)SeedGeneratorzkeras.layers.RandomRotationc                   r     e Zd ZdZdZdZdZdZ	 	 	 	 	 	 d fd	Zd Z	d Z
d Z	 d ZddZd Z fdZ xZS )RandomRotationa
  A preprocessing layer which randomly rotates images during training.

    This layer will apply random rotations to each image, filling empty space
    according to `fill_mode`.

    By default, random rotations are only applied during training.
    At inference time, the layer does nothing. If you need to apply random
    rotations at inference time, pass `training=True` when calling the layer.

    Input pixel values can be of any range (e.g. `[0., 1.)` or `[0, 255]`) and
    of integer or floating point dtype.
    By default, the layer will output floats.

    **Note:** This layer is safe to use inside a `tf.data` pipeline
    (independently of which backend you're using).

    Input shape:
        3D (unbatched) or 4D (batched) tensor with shape:
        `(..., height, width, channels)`, in `"channels_last"` format

    Output shape:
        3D (unbatched) or 4D (batched) tensor with shape:
        `(..., height, width, channels)`, in `"channels_last"` format

    Args:
        factor: a float represented as fraction of 2 Pi, or a tuple of size 2
            representing lower and upper bound for rotating clockwise and
            counter-clockwise. A positive values means rotating
            counter clock-wise,
            while a negative value means clock-wise.
            When represented as a single
            float, this value is used for both the upper and lower bound.
            For instance, `factor=(-0.2, 0.3)`
            results in an output rotation by a random
            amount in the range `[-20% * 2pi, 30% * 2pi]`.
            `factor=0.2` results in an
            output rotating by a random amount
            in the range `[-20% * 2pi, 20% * 2pi]`.
        fill_mode: Points outside the boundaries of the input are filled
            according to the given mode
            (one of `{"constant", "reflect", "wrap", "nearest"}`).
            - *reflect*: `(d c b a | a b c d | d c b a)`
                The input is extended by reflecting about
                the edge of the last pixel.
            - *constant*: `(k k k k | a b c d | k k k k)`
                The input is extended by
                filling all values beyond the edge with
                the same constant value k = 0.
            - *wrap*: `(a b c d | a b c d | a b c d)` The input is extended by
                wrapping around to the opposite edge.
            - *nearest*: `(a a a a | a b c d | d d d d)`
                The input is extended by the nearest pixel.
        interpolation: Interpolation mode. Supported values: `"nearest"`,
            `"bilinear"`.
        seed: Integer. Used to create a random seed.
        fill_value: a float represents the value to be filled outside
            the boundaries when `fill_mode="constant"`.
    z^The `factor` argument should be a number (or a list of two numbers) in the range [-1.0, 1.0]. z<The `value_range` argument should be a list of two numbers. )reflectwrapconstantnearest)r   bilinearr	   r   N        r      c                     t                      j        di | || _        t          |          | _        |                     |           |                     |           t          j        |          | _	        || _
        || _        || _        d| _        | j
        | j        vrt          d| d| j         d          | j        | j        vrt          d| d| j         d          d S )NFzUnknown `fill_mode` z. Expected of one .zUnknown `interpolation`  )super__init__seedr   	generator_set_factor_set_value_ranger   standardize_data_formatdata_format	fill_modeinterpolation
fill_valuesupports_jit_SUPPORTED_FILL_MODENotImplementedError_SUPPORTED_INTERPOLATION)
selffactorr   r   r   r   value_ranger   kwargs	__class__s
            k/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/layers/preprocessing/random_rotation.pyr   zRandomRotation.__init__Q   s-    	""6"""	&t,,   k***":;GG"*$!>!:::%0y 0 0,0 0 0   T%BBB%4= 4 404 4 4   CB    c                     t          |t          t          f          st          | j        d| z             t          |          dk    rt          | j        d| z             t          |          | _        d S )NzReceived: value_range=   )
isinstancetuplelist
ValueErrorvalue_range_VALIDATION_ERRORlensortedr%   )r#   r%   s     r(   r   zRandomRotation._set_value_ranges   s    +t}55 	18;889   {q  18;889   "+..r)   c                    t          |t          t          f          ryt          |          dk    rt	          | j        d| z             |                     |d                    |                     |d                    t          |          | _        d S t          |t          t          f          r0|                     |           t          |          }| |g| _        d S t	          | j        d| z             )Nr+   zReceived: factor=r      )r,   r-   r.   r1   r/   _FACTOR_VALIDATION_ERROR_check_factor_ranger2   _factorintfloatabs)r#   r$   s     r(   r   zRandomRotation._set_factor   s    fudm,, 	6{{a 14P4P4PP   $$VAY///$$VAY///!&>>DLLLe-- 	$$V,,,[[F#GV,DLLL-0LF0L0LL  r)   c                 R    |dk    s|dk     rt          | j        d| z             d S )Ng      ?g      zReceived: input_number=)r/   r5   )r#   input_numbers     r(   r6   z"RandomRotation._check_factor_range   sG    #!4!4-:L::;   "5!4r)   c                 j   | j         j                            |          }t          |          dk    r=| j        dk    r|d         }|d         }|d         }nG|d         }|d         }|d         }n.d}| j        dk    r|d         }|d         }n|d         }|d         }| j        d         dz  | j                             t          j                  z  }| j        d         dz  | j                             t          j                  z  }| 	                    | j         j
                  }| j         j                            |f|||          }	| j         j                            |	          }
| j         j                            |	          }| j         j                            ||
j                  }| j         j                            ||
j                  }|dz
  |
|dz
  z  ||dz
  z  z
  z
  dz  }|dz
  ||dz
  z  |
|dz
  z  z   z
  dz  }| j         j                            | j         j                            |	          d d d f         | j         j                            |	          d d d f          |d d d f         | j         j                            |	          d d d f         | j         j                            |	          d d d f         |d d d f         | j         j                            |df          gd	          }t          |          dk    r!| j         j                            |d	          }|S )
N   channels_lastr   r4   r+      g       @)shapeminvalmaxvalr   )axis)r   corerA   r1   r   r7   convert_to_tensornppi_get_seed_generator_backendrandomuniformnumpycossincastdtypeconcatenatezerossqueeze)r#   inputsrA   
batch_sizeimage_heightimage_widthlowerupperseed_generatorangle	cos_theta	sin_thetax_offsety_offsetoutputss                  r(   _get_rotation_matrixz#RandomRotation._get_rotation_matrix   s7   !''//u::???22"1X
$Qx#Ah"1X
$Qx#AhJ?22$Qx#Ah$Qx#AhQ#%(F(Fru(M(MMQ#%(F(Fru(M(MM11$,2GHH#++-	 , 
 
 L&**511	L&**511	|(--lIOLLl',,[)/JJ 1_K!O,yL1<L/MMO AK!O,yL1<L/MMO
 ,$00"&&u--aaag6#''..qqq$w77D!"&&u--aaag6"&&u--aaag6D!"((*a99  1 
 
 u::??l(00q0AAGr)   Tc                     | j                             || j                  }|rP|                     |          }| j         j                            ||| j        | j        | j        | j	                  }|S |S )N)image	transformr   r   r   r   )
r   rP   compute_dtyperb   rd   affine_transformr   r   r   r   )r#   rU   trainingrotation_matrixtransformed_images        r(   callzRandomRotation.call   s    ""64+=>> 	"77??O $ 2 C C)"0.? , !D ! ! %$Mr)   c                     |S )Nr   )r#   input_shapes     r(   compute_output_shapez#RandomRotation.compute_output_shape   s    r)   c                     | j         | j        | j        | j        | j        | j        | j        d}t                                                      }i ||S )N)r$   r%   r   r   r   r   r   )	r7   r%   r   r   r   r   r   r   
get_config)r#   configbase_configr'   s      r(   rp   zRandomRotation.get_config   s\    l++/!/I
 
 gg((**(+(((r)   )r	   r   Nr   r   N)T)__name__
__module____qualname____doc__r5   _VALUE_RANGE_VALIDATION_ERRORr    r"   r   r   r   r6   rb   rk   rn   rp   __classcell__)r'   s   @r(   r   r   	   s        9 9x	% 
 	G " F6
             D/ / /  $  < < <|      ) ) ) ) ) ) ) ) )r)   r   )rM   rG   	keras.srcr   keras.src.api_exportr   ,keras.src.layers.preprocessing.tf_data_layerr   keras.src.random.seed_generatorr   r   r   r)   r(   <module>r}      s              - - - - - - D D D D D D 9 9 9 9 9 9 +,,t) t) t) t) t)[ t) t) -,t) t) t)r)   