
    2VhW                     T    d dl mZ d dlmZ d dlmZ  ed       G d de             Zy)    )keras_export)BaseImagePreprocessingLayer)SeedGeneratorzkeras.layers.RandomContrastc                   j     e Zd ZdZdZd fd	ZddZddZddZ	 ddZ		 ddZ
d	 Zd
 Z fdZ xZS )RandomContrasta  A preprocessing layer which randomly adjusts contrast during training.

    This layer will randomly adjust the contrast of an image or images
    by a random factor. Contrast is adjusted independently
    for each channel of each image during training.

    For each channel, this layer computes the mean of the image pixels in the
    channel and then adjusts each component `x` of each pixel to
    `(x - mean) * contrast_factor + mean`.

    Input pixel values can be of any range (e.g. `[0., 1.)` or `[0, 255]`) and
    in 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 positive float represented as fraction of value, or a tuple of
            size 2 representing lower and upper bound.
            When represented as a single float, lower = upper.
            The contrast factor will be randomly picked between
            `[1.0 - lower, 1.0 + upper]`. For any pixel x in the channel,
            the output will be `(x - mean) * factor + mean`
            where `mean` is the mean value of the channel.
        value_range: the range of values the incoming images will have.
            Represented as a two-number tuple written `[low, high]`. This is
            typically either `[0, 1]` or `[0, 255]` depending on how your
            preprocessing pipeline is set up.
        seed: Integer. Used to create a random seed.
    )r      c                     t        |   di | | j                  |       || _        || _        t        |      | _        y )N )super__init___set_factorvalue_rangeseedr   	generator)selffactorr   r   kwargs	__class__s        r/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/random_contrast.pyr   zRandomContrast.__init__4   s<    "6" &	&t,    c                    t        |t              r|d   }n|}| j                  j                  |      }t	        |      }|dk(  rd}n|dk(  r
|d   dddg}nt        d|       |s'd| j                  j                  j                  |      iS |%| j                  | j                  j                        }| j                  j                  j                  |d	| j                  d   z
  d	| j                  d   z   || j                  
      }d|iS )Nimages   )r   r   r      r   r   zBExpected the input image to be rank 3 or 4. Received inputs.shape=contrast_factorg      ?)shapeminvalmaxvalr   dtype)
isinstancedictbackendr   len
ValueErrornumpyzeros_get_seed_generator_backendrandomuniformr   compute_dtype)	r   datatrainingr   r   images_shaperankfactor_shaper   s	            r   get_random_transformationz(RandomContrast.get_random_transformation;   s   dD!(^FF||))&1< 19$LQY )OQ15L  ,~/ 
 %t||'9'9'?'?'MNN<++DLL,A,ABD$$,,Q'Q'$$ - 
 "6**r   c                 <   |r|d   }| j                  ||      }| j                  j                  j                  || j                  d   | j                  d         }| j                  j                  j                  || j                  j                  |             |S |S )Nr   r   r   )_adjust_constrastr"   r%   clipr   reshaper   )r   r   transformationr-   constrast_factoroutputss         r   transform_imageszRandomContrast.transform_images]   s    -.?@,,V5EFGll((--))!,d.>.>q.AG LL&&w0B0B60JKNr   c                     |S Nr
   )r   labelsr6   r-   s       r   transform_labelszRandomContrast.transform_labelsh   s    r   c                     |S r;   r
   )r   bounding_boxesr6   r-   s       r   transform_bounding_boxesz'RandomContrast.transform_bounding_boxesk   s
     r   c                     |S r;   r
   )r   segmentation_masksr6   r-   s       r   transform_segmentation_masksz+RandomContrast.transform_segmentation_maskss   s
     "!r   c                     | j                   dk(  rd}d}nd}d}| j                  j                  j                  ||d      }| j                  j                  j                  ||d      }||z
  |z  |z   }|S )Nchannels_firstT)axiskeepdims)data_formatr"   r%   mean)r   inputsr   height_axis
width_axisinp_meanr8   s          r   r3   z RandomContrast._adjust_constrastx   s    //KJKJ<<%%**t + 
 <<%%**: + 
 H$7(Br   c                     |S r;   r
   )r   input_shapes     r   compute_output_shapez#RandomContrast.compute_output_shape   s    r   c                 t    | j                   | j                  | j                  d}t        |          }i ||S )N)r   r   r   )r   r   r   r   
get_config)r   configbase_configr   s      r   rU   zRandomContrast.get_config   s@    kk++II

 g(*(+(((r   ))r      N)TN)T)__name__
__module____qualname____doc___FACTOR_BOUNDSr   r1   r9   r=   r@   rC   r3   rS   rU   __classcell__)r   s   @r   r   r      sN    &P N- +D	 	 <@"
&) )r   r   N)keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   keras.src.random.seed_generatorr   r   r
   r   r   <module>rb      s7    - : +,L)0 L) -L)r   