
    2Vh                     H    d dl mZ d dlmZ  ed       G d de             Zy)    )keras_export)BaseImagePreprocessingLayerzkeras.layers.RandomHuec                   n     e Zd 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 fd
Zd Z xZS )	RandomHuea  Randomly adjusts the hue on given images.

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

    The image hue is adjusted by converting the image(s) to HSV and rotating the
    hue channel (H) by delta. The image is then converted back to RGB.

    Args:
        factor: A single float or a tuple of two floats.
            `factor` controls the extent to which the
            image hue is impacted. `factor=0.0` makes this layer perform a
            no-op operation, while a value of `1.0` performs the most aggressive
            contrast adjustment available. 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. In order to ensure the value is always the same, please
            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()
    random_hue = keras.layers.RandomHue(factor=0.5, value_range=[0, 1])
    images = keras.ops.cast(images, "float32")
    augmented_images_batch = random_hue(images[:8])
    ```
    Tr      c                     t        |   dd|i| | j                  |       || _        || _        | j
                  j                  j                  |      | _        y )Ndata_format )	super__init___set_factorvalue_rangeseedbackendrandomSeedGenerator	generator)selffactorr   r
   r   kwargs	__class__s         m/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/random_hue.pyr   zRandomHue.__init__.   sS     	;[;F; &	,,::4@    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                  j                  j                  |dkD  | j                  j                  j                  |       | j                  j                  j                  |            }| j                  j                  j                  |f| j                  d   | j                  d   |	      }	d
||	z  dz  iS )Nimages   r      r   zBExpected the input image to be rank 3 or 4. Received inputs.shape=)r   g      ?)minvalmaxvalr   r   )
isinstancedictr   shapelen
ValueError_get_seed_generator_backendr   uniformnumpywhere	ones_liker   )
r   datatrainingr   r   images_shaperank
batch_sizeinvertr   s
             r   get_random_transformationz#RandomHue.get_random_transformation<   s[   dD!(^FF||))&1< 19JQY%aJ  ,~/ 
 <++DLL,A,ABD$$,,j],F##))SL\\))&11LL((0

 $$,,M;;q>;;q>	 - 
 &6/C/00r   c                 (      fd}|r	 |||      }|S )Nc           
         j                   j                  | j                        } j                  | j                  d      } |d   }j                   j                  || j
                        }j                   j                  j                  |d      }j                   j                  j                  |d      }j                   j                  j                  | j                        } j                  dk(  r| d d dd d d d f   |z   }j                   j                  j                  |dkD  |dz
  |      }j                   j                  j                  |dk  |dz   |      }j                   j                  j                  || d d d	d d d d f   | d d d
d d d d f   gd	      } n| d   |z   }j                   j                  j                  |dkD  |dz
  |      }j                   j                  j                  |dk  |dz   |      }j                   j                  j                  || d   | d   gd      } j                   j                  j                  | j                        } j                   j                  j                  | dd	      } j                  | dj                        } j                   j                  | j                        } | S )Nr   r   )r
   channels_firstr   g      ?g        r      )axis).r   ).r   ).r7   )r   castcompute_dtype_transform_value_ranger   dtyper)   expand_dimsimage
rgb_to_hsvr
   r*   stack
hsv_to_rgbclip)r   transformationadjust_factors	h_channelr   s       r   _apply_random_huez5RandomHue.transform_images.<locals>._apply_random_hue_   s   \\&&vt/A/ABF00((&F ,H5N!\\..~v||LN!\\//;;NBON!\\//;;NBON\\''22D$4$4 3 F #33"1aA:.?	 LL..44OY_i	 !LL..44OY_i	 ++11q!Qz 2F1aA:4FGa 2  #6N^;	 LL..44OY_i	 !LL..44OY_i	 ++11vv?b 2  \\''22D$4$4 3 F \\'',,VQ:F00 0 0F \\&&vt/A/ABFMr   r   )r   r   rC   r-   rF   s   `    r   transform_imageszRandomHue.transform_images^   s    *	X &v~>Fr   c                     |S Nr   )r   labelsrC   r-   s       r   transform_labelszRandomHue.transform_labels   s    r   c                     |S rI   r   )r   segmentation_masksrC   r-   s       r   transform_segmentation_masksz&RandomHue.transform_segmentation_masks   s
     "!r   c                     |S rI   r   )r   bounding_boxesrC   r-   s       r   transform_bounding_boxesz"RandomHue.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RandomHue.get_config   s?    #%++#//			
 r   c                     |S rI   r   )r   input_shapes     r   compute_output_shapezRandomHue.compute_output_shape   s    r   ))r      NN)TN)NT)T)__name__
__module____qualname____doc___USE_BASE_FACTOR_FACTOR_BOUNDSr   r2   rG   rK   rN   rQ   rS   rX   __classcell__)r   s   @r   r   r      sW     D N
 A 1D/b <@" 8<
	r   r   N)keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   r   r   r   r   <module>rc      s2    -
 &'`+ ` (`r   