
    2Vh                     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.RandomBrightnessc                   j     e Zd ZdZdZd fd	Zd ZddZddZddZ		 ddZ
	 dd	Zd
 Z fdZ xZS )RandomBrightnessa	  A preprocessing layer which randomly adjusts brightness during training.

    This layer will randomly increase/reduce the brightness for the input RGB
    images. At inference time, the output will be identical to the input.
    Call the layer with `training=True` to adjust the brightness of the input.

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

    Args:
        factor: Float or a list/tuple of 2 floats between -1.0 and 1.0. The
            factor is used to determine the lower bound and upper bound of the
            brightness adjustment. A float value will be chosen randomly between
            the limits. When -1.0 is chosen, the output image will be black, and
            when 1.0 is chosen, the image will be fully white.
            When only one float is provided, eg, 0.2,
            then -0.2 will be used for lower bound and 0.2
            will be used for upper bound.
        value_range: Optional list/tuple of 2 floats
            for the lower and upper limit
            of the values of the input data.
            To make no change, use `[0.0, 1.0]`, e.g., if the image input
            has been scaled before this layer. Defaults to `[0.0, 255.0]`.
            The brightness adjustment will be scaled to this range, and the
            output values will be clipped to this range.
        seed: optional integer, for fixed RNG behavior.

    Inputs: 3D (HWC) or 4D (NHWC) tensor, with float or int dtype. Input pixel
        values can be of any range (e.g. `[0., 1.)` or `[0, 255]`)

    Output: 3D (HWC) or 4D (NHWC) tensor with brightness adjusted based on the
        `factor`. By default, the layer will output floats.
        The output value will be clipped to the range `[0, 255]`,
        the valid range of RGB colors, and
        rescaled based on the `value_range` if needed.

    Example:

    ```python
    random_bright = keras.layers.RandomBrightness(factor=0.2)

    # An image with shape [2, 2, 3]
    image = [[[1, 2, 3], [4 ,5 ,6]], [[7, 8, 9], [10, 11, 12]]]

    # Assume we randomly select the factor to be 0.1, then it will apply
    # 0.1 * 255 to all the channel
    output = random_bright(image, training=True)

    # output will be int64 with 25.5 added to each channel and round down.
    >>> array([[[26.5, 27.5, 28.5]
                [29.5, 30.5, 31.5]]
               [[32.5, 33.5, 34.5]
                [35.5, 36.5, 37.5]]],
              shape=(2, 2, 3), dtype=int64)
    ```
    z<The `value_range` argument should be a list of two numbers. c                 x    t        |   dd|i| || _        t        |      | _        | j                  |       y )Nfactor )super__init__seedr   	generator_set_value_range)selfr	   value_ranger   kwargs	__class__s        t/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/random_brightness.pyr   zRandomBrightness.__init__G   s9    11&1	&t,k*    c                     t        |t        t        f      st        | j                  d| z         t        |      dk7  rt        | j                  d| z         t        |      | _        y )NzReceived: value_range=   )
isinstancetuplelist
ValueError_VALUE_RANGE_VALIDATION_ERRORlensortedr   )r   r   s     r   r   z!RandomBrightness._set_value_rangeM   sx    +t}522*;-89  {q 22*;-89  "+.r   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                  | j                  d   | j                  d   ||	      }|| j                  d   | j                  d   z
  z  }d|iS )
Nimages   )   r"   r"      r   r"   zBExpected the input image to be rank 3 or 4. Received inputs.shape=	rgb_delta)minvalmaxvalshaper   )r   dictbackendr'   r   r   numpyzeros_get_seed_generator_backendrandomuniformr	   r   )	r   datatrainingr   r    images_shaperankrgb_delta_shaper$   s	            r   get_random_transformationz*RandomBrightness.get_random_transformationZ   s,   dD!(^FF||))&1< 19'OQY  ,A1a8O  ,~/  !3!3!9!9/!JKK<++DLL,A,ABDLL''//;;q>;;q>!	 0 
	 !1!1!!4t7G7G7J!JK	Y''r   c                     |rq|d   }| j                   j                  ||j                        }||z  }| j                   j                  j	                  || j
                  d   | j
                  d         S |S )Nr$   r   r"   )r)   castdtyper*   clipr   )r   r    transformationr1   r$   s        r   transform_imagesz!RandomBrightness.transform_imagesz   st    &{3I)))V\\BIiF<<%%**((+T-=-=a-@  r   c                     |S Nr
   )r   labelsr:   r1   s       r   transform_labelsz!RandomBrightness.transform_labels   s    r   c                     |S r=   r
   )r   bounding_boxesr:   r1   s       r   transform_bounding_boxesz)RandomBrightness.transform_bounding_boxes   s
     r   c                     |S r=   r
   )r   segmentation_masksr:   r1   s       r   transform_segmentation_masksz-RandomBrightness.transform_segmentation_masks   s
     "!r   c                     |S r=   r
   )r   input_shapes     r   compute_output_shapez%RandomBrightness.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   rJ   zRandomBrightness.get_config   s@    kk++II

 g(*(+(((r   ))r      N)TN)T)__name__
__module____qualname____doc__r   r   r   r5   r;   r?   rB   rE   rH   rJ   __classcell__)r   s   @r   r   r      sR    7t 	G "+/(@ 	 <@"
) )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>rV      s7    - : -.U)2 U) /U)r   