
    &Vfv)                     t    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 )	    )backend)keras_export)TFDataLayer)SeedGeneratorzkeras.layers.RandomTranslationc                   j     e Zd ZdZdZdZdZ	 	 	 	 	 d fd		Zd
 Zd Z	ddZ
d Zd Zd Z fdZ xZS )RandomTranslationa  A preprocessing layer which randomly translates images during training.

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

    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.

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

    Output shape:
        3D (unbatched) or 4D (batched) tensor with shape:
        `(..., target_height, target_width, channels)`,
        or `(..., channels, target_height, target_width)`,
        in `"channels_first"` format.

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

    Args:
        height_factor: a float represented as fraction of value, or a tuple of
            size 2 representing lower and upper bound for shifting vertically. A
            negative value means shifting image up, while a positive value means
            shifting image down. When represented as a single positive float,
            this value is used for both the upper and lower bound. For instance,
            `height_factor=(-0.2, 0.3)` results in an output shifted by a random
            amount in the range `[-20%, +30%]`. `height_factor=0.2` results in
            an output height shifted by a random amount in the range
            `[-20%, +20%]`.
        width_factor: a float represented as fraction of value, or a tuple of
            size 2 representing lower and upper bound for shifting horizontally.
            A negative value means shifting image left, while a positive value
            means shifting image right. When represented as a single positive
            float, this value is used for both the upper and lower bound. For
            instance, `width_factor=(-0.2, 0.3)` results in an output shifted
            left by 20%, and shifted right by 30%. `width_factor=0.2` results
            in an output height shifted left or right by 20%.
        fill_mode: Points outside the boundaries of the input are filled
            according to the given mode. Available methods are `"constant"`,
            `"nearest"`, `"wrap"` and `"reflect"`. Defaults to `"constant"`.
            - `"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 specified by
                `fill_value`.
            - `"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.
            Note that when using torch backend, `"reflect"` is redirected to
            `"mirror"` `(c d c b | a b c d | c b a b)` because torch does not
            support `"reflect"`.
            Note that torch backend does not support `"wrap"`.
        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"`.
        data_format: string, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, height, width, channels)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, channels, height, width)`. It defaults to the
            `image_data_format` value found in your Keras config file at
            `~/.keras/keras.json`. If you never set it, then it will be
            `"channels_last"`.
        **kwargs: Base layer keyword arguments, such as `name` and `dtype`.
    z^The `factor` argument should be a number (or a list of two numbers) in the range [-1.0, 1.0]. )reflectwrapconstantnearest)r   bilinearr	   r   N        c                     t                      j        di | || _        |                     |d          \  | _        | _        || _        |                     |d          \  | _        | _        || j	        vrt          d| d| j	         d          || j        vrt          d| d| j         d          || _        || _        || _        || _        t!          |          | _        t%          j        |          | _        d| _        d S )	Nheight_factorwidth_factorzUnknown `fill_mode` z. Expected of one .zUnknown `interpolation` F )super__init__r   _set_factorheight_lowerheight_upperr   width_lowerwidth_upper_SUPPORTED_FILL_MODENotImplementedError_SUPPORTED_INTERPOLATION	fill_mode
fill_valueinterpolationseedr   	generatorr   standardize_data_formatdata_formatsupports_jit)
selfr   r   r   r    r!   r   r$   kwargs	__class__s
            n/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/layers/preprocessing/random_translation.pyr   zRandomTranslation.__init__[   s]    	""6"""*/3/?/??0
 0
,4, )-1-=-=..
 .
*$* D555%0y 0 0,0 0 0    ===%4= 4 404 4 4  
 #$*	&t,,":;GG!    c                     t          |t          t          f          ryt          |          dk    rt	          | j        d| d| z             |                     |d                    |                     |d                    t          |          \  }}nft          |t          t          f          r-|                     |           t          |          }| |g\  }}nt	          | j        d| d| z             ||fS )N   z
Received: =r      )
isinstancetuplelistlen
ValueError_FACTOR_VALIDATION_ERROR_check_factor_rangesortedintfloatabs)r&   factorfactor_nameloweruppers        r)   r   zRandomTranslation._set_factor   s   fudm,, 	6{{a 19;9999:   $$VAY///$$VAY///!&>>LE55e-- 	$$V,,,[[F#GV,LE55-5{55V556   e|r*   c                 R    |dk    s|dk     rt          | j        d| z             d S )Ng      ?g      zReceived: input_number=)r3   r4   )r&   input_numbers     r)   r5   z%RandomTranslation._check_factor_range   sG    #!4!4-:L::;   "5!4r*   Tc                 t    | j                             || j                  }|r|                     |          S |S N)r   castcompute_dtype_randomly_translate_inputs)r&   inputstrainings      r)   callzRandomTranslation.call   s=    ""64+=>> 	226:::Mr*   c                    | j                             |          }t          |          dk    }|r;| j         j                            |d          }| j                             |          }|d         }| j        dk    r|d         }|d         }n|d         }|d         }|                     | j         j                  }| j         j        	                    | j
        | j        |dg|	          }| j         j                            ||          }| j         j        	                    | j        | j        |dg|	          }	| j         j                            |	|          }	| j                             | j         j                            |	|gd          d
          }
| j         j                            ||                     |
          | j        | j        | j        | j                  }|r!| j         j                            |d          }|S )N   r   axischannels_firstr.   )minvalmaxvalshaper!   float32)dtype)	transformr    r   r   r$   )r   rR   r2   numpyexpand_dimsr$   _get_seed_generator_backendrandomuniformr   r   multiplyr   r   rB   concatenateimageaffine_transform_get_translation_matrixr    r   r   squeeze)r&   rE   inputs_shape	unbatched
batch_sizeheightwidthseed_generatorheight_translatewidth_translatetranslationsoutputss               r)   rD   z,RandomTranslation._randomly_translate_inputs   s#   |))&11%%*	 	6\'33F3CCF<--f55L!!_
///!"%F $EE!"%F $E11$,2GHH<.66$$q/	 7 
 
  <-667GPP,-55##q/	 6 
 
 ,,55ouMM|((L** "23! +   	 ) 
 
 ,$5522<@@,n( 6 
 
  	Bl(00q0AAGr*   c                    | j                             |          d         }| j         j                            | j         j                            |df          | j         j                            |df          |d d ddf          | j         j                            |df          | j         j                            |df          |d d dd f          | j         j                            |df          gd          S )Nr   r.   r,   rJ   )r   rR   rV   r]   oneszeros)r&   rj   num_translationss      r)   r`   z)RandomTranslation._get_translation_matrix   s   <--l;;A> |!--"'')91(=>>"((*:A)>??aaa1f%%"((*:A)>??"'')91(=>>aaae$$"((*:A)>??  . 
 
 	
r*   c                     |S rA   r   )r&   input_shapes     r)   compute_output_shapez&RandomTranslation.compute_output_shape   s    r*   c                     t                                                      }| j        | j        | j        | j        | j        | j        | j        d}i ||S )N)r   r   r   r    r!   r   r$   )	r   
get_configr   r   r   r    r!   r   r$   )r&   base_configconfigr(   s      r)   rt   zRandomTranslation.get_config   s]    gg((**!/ -!/I/+
 
 )+(((r*   )r	   r   Nr   N)T)__name__
__module____qualname____doc__r4   r   r   r   r   r5   rG   rD   r`   rr   rt   __classcell__)r(   s   @r)   r   r      s        I IX	%  F6  &" &" &" &" &" &"P  *     0 0 0d
 
 
*  ) ) ) ) ) ) ) ) )r*   r   N)		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 .//s) s) s) s) s) s) s) 0/s) s) s)r*   