
    &Vf?                     h    d dl mZ d dlmZ d dlmZ  ed           G d de                      ZdS )    )keras_export)TFDataLayer)SeedGeneratorzkeras.layers.RandomBrightnessc                   \     e Zd ZdZdZdZd fd	Zd Zd Zd	 Z	ddZ
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 `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. r      Nc                      t                      j        di | |                     |           |                     |           || _        t          |          | _        d S )N )super__init___set_factor_set_value_rangeseedr   	generator)selffactorvalue_ranger   kwargs	__class__s        m/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/layers/preprocessing/random_brightness.pyr   zRandomBrightness.__init__I   sb    ""6"""   k***	&t,,    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!RandomBrightness._set_value_rangeP   s    +t}55 	28;889   {q  2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   r    r   _FACTOR_VALIDATION_ERROR_check_factor_ranger!   _factorintfloatabs)r   r   s     r   r   zRandomBrightness._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   r$   )r   input_numbers     r   r%   z$RandomBrightness._check_factor_rangeo   sG    #!4!4-:L::;   "5!4r   Tc                 t    | j                             || j                  }|r|                     |          S |S N)backendcastcompute_dtype_randomly_adjust_brightness)r   inputstrainings      r   callzRandomBrightness.callv   s=    ""64+=>> 	33F;;;Mr   c                 T   | j                             |          }t          |          }|dk    rd}n%|dk    r|d         dddg}nt          d|           |                     | j         j                  }| j         j                            | j        d         | j        d         ||          }|| j	        d         | j	        d         z
  z  }| j         
                    ||j                  }||z  }| j         j                            || j	        d         | j	        d                   S )N   )r#   r#   r#      r   r#   zBExpected the input image to be rank 3 or 4. Received inputs.shape=)minvalmaxvalshaper   )r.   r:   r    r   _get_seed_generator_backendrandomuniformr&   r   r/   dtypenumpyclip)r   imagesimages_shaperankrgb_delta_shapeseed_generator	rgb_deltas          r   r1   z,RandomBrightness._randomly_adjust_brightness}   s@   |))&11<  199'OOQYY  ,A1a8OO/ ,/ /  
 11$,2GHHL'//<?<?!	 0 
 
	 !1!!4t7G7J!JK	L%%i>>	)|!&&D$Q')9!)<
 
 	
r   c                     |S r-   r   )r   input_shapes     r   compute_output_shapez%RandomBrightness.compute_output_shape   s    r   c                 z    | j         | j        | j        d}t                                                      }i ||S )N)r   r   r   )r&   r   r   r   
get_config)r   configbase_configr   s      r   rL   zRandomBrightness.get_config   sF    l+I
 

 gg((**(+(((r   )r   N)T)__name__
__module____qualname____doc__r$   r   r   r   r   r%   r4   r1   rJ   rL   __classcell__)r   s   @r   r   r      s        7 7t	% 
 	G "- - - - - -/ / /  $     
 
 
:  ) ) ) ) ) ) ) ) )r   r   N)keras.src.api_exportr   ,keras.src.layers.preprocessing.tf_data_layerr   keras.src.random.seed_generatorr   r   r   r   r   <module>rW      s    - - - - - - D D D D D D 9 9 9 9 9 9 -..]) ]) ]) ]) ]){ ]) ]) /.]) ]) ])r   