
    2VhV                     `    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y)	    )keras_export)epsilon)BaseImagePreprocessingLayer)SeedGeneratorzkeras.layers.RandomSaturationc                   p     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 fd
Zd Z xZS )RandomSaturationa  Randomly adjusts the saturation on given images.

    This layer will randomly increase/reduce the saturation for the input RGB
    images.

    Args:
        factor: A tuple of two floats or a single float.
            `factor` controls the extent to which the image saturation
            is impacted. `factor=0.5` makes this layer perform a no-op
            operation. `factor=0.0` makes the image fully grayscale.
            `factor=1.0` makes the image fully saturated. Values should
            be between `0.0` and `1.0`. If a tuple is used, a `factor`
            is sampled between the two values for every image augmented.
            If a single float is used, a value between `0.0` and the passed
            float is sampled. To ensure the value is always the same,
            pass a tuple with two identical floats: `(0.5, 0.5)`.
        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.

    Example:
    ```python
    (images, labels), _ = keras.datasets.cifar10.load_data()
    images = images.astype("float32")
    random_saturation = keras.layers.RandomSaturation(factor=0.2)
    augmented_images = random_saturation(images)
    ```
    z<The `value_range` argument should be a list of two numbers. c                     t        |   dd|i| | j                  |       | j                  |       || _        t        |      | _        y )Ndata_format )super__init___set_factor_set_value_rangeseedr   	generator)selffactorvalue_ranger
   r   kwargs	__class__s         t/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/random_saturation.pyr   zRandomSaturation.__init__.   sI     	;[;F; k*	&t,    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!RandomSaturation._set_value_range<   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   }nt        d|       |%| j                  | j                  j                        }| j                  j                  j                  |f| j                  d   | j                  d   |      }|d|z
  t               z   z  }d|iS )	Nimages         r   zCExpected the input image to be rank 3 or 4. Received: inputs.shape=)minvalmaxvalr   r   )r   dictbackendshaper    r   _get_seed_generator_backendrandomuniformr   r   )	r   datatrainingr   r#   images_shaperank
batch_sizer   s	            r   get_random_transformationz*RandomSaturation.get_random_transformationI   s    dD!(^FF||))&1< 19JQY%aJ  ,~/ 
 <++DLL,A,ABD$$,,M;;q>;;q>	 - 
 1v:	12&!!r   c           
      n   |r1|d   }| j                   j                  || j                        }| j                   j                  j	                  || j                   j                  |      dz         }| j                   j                  j                  || j                        }| j                  dk(  r| j                   j                  j                  |d d dd d d d f   |      }| j                   j                  j                  || j                  d   | j                  d         }| j                   j                  j                  |d d dd d d d f   ||d d dd d d d f   gd      }n| j                   j                  j                  |d	   |      }| j                   j                  j                  || j                  d   | j                  d         }| j                   j                  j                  |d
   ||d   gd      }| j                   j                  j                  || j                        }|S )Nr   )r%   r%   )r
   channels_firstr%   r   r   )axis).r%   ).r   ).r   )r*   castcompute_dtypenumpyreshaper+   image
rgb_to_hsvr
   multiplyclipr   stack
hsv_to_rgb)r   r#   transformationr1   adjust_factors	s_channels         r   transform_imagesz!RandomSaturation.transform_imagesf   s   +H5N!\\.. 2 2N "\\//77 2 2> BV KN \\''22D$4$4 3 F #33 LL..771aA:&	 !LL..33t//2D4D4DQ4G	 ++11Aq!QJ'F1aA:4FGa 2  !LL..776NN	 !LL..33t//2D4D4DQ4G	 ++11F^Yv?b 2  \\''22D$4$4 3 F r   c                     |S Nr   )r   labelsrD   r1   s       r   transform_labelsz!RandomSaturation.transform_labels   s    r   c                     |S rI   r   )r   segmentation_masksrD   r1   s       r   transform_segmentation_masksz-RandomSaturation.transform_segmentation_masks   s
     "!r   c                     |S rI   r   )r   bounding_boxesrD   r1   s       r   transform_bounding_boxesz)RandomSaturation.transform_bounding_boxes   s
     r   c                     t         |          }|j                  | j                  | j                  | j
                  d       |S )N)r   r   r   )r   
get_configupdater   r   r   )r   configr   s     r   rS   zRandomSaturation.get_config   s?    #%++#//			
 r   c                     |S rI   r   )r   input_shapes     r   compute_output_shapez%RandomSaturation.compute_output_shape   s    r   ))r      NN)TN)NT)T)__name__
__module____qualname____doc__r   r   r   r5   rG   rK   rN   rQ   rS   rX   __classcell__)r   s   @r   r   r   	   sX    @ 	G " -/":#J <@" 8<
	r   r   N)	keras.src.api_exportr   keras.src.backendr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   keras.src.randomr   r   r   r   r   <module>rc      s:    - % + -.Z2 Z /Zr   