
    2VhG                     T    d dl mZ d dlmZ d dlmZ  ed       G d de             Zy)    )backend)keras_export)BaseImagePreprocessingLayerzkeras.layers.RandomGrayscalec                   b     e Zd ZdZd fd	ZddZddZd Zd Zd Z	ddZ
	 dd	Z fd
Z xZS )RandomGrayscalea  Preprocessing layer for random conversion of RGB images to grayscale.

    This layer randomly converts input images to grayscale with a specified
    factor. When applied, it maintains the original number of channels
    but sets all channels to the same grayscale value. This can be useful
    for data augmentation and training models to be robust to color
    variations.

    The conversion preserves the perceived luminance of the original color
    image using standard RGB to grayscale conversion coefficients. Images
    that are not selected for conversion remain unchanged.

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

    Args:
        factor: Float between 0 and 1, specifying the factor of
            converting each image to grayscale. Defaults to 0.5. A value of
            1.0 means all images will be converted, while 0.0 means no images
            will be converted.
        data_format: String, one of `"channels_last"` (default) or
            `"channels_first"`. The ordering of the dimensions in the inputs.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)` while `"channels_first"`
            corresponds to inputs with shape
            `(batch, channels, height, width)`.

    Input shape:
        3D (unbatched) or 4D (batched) tensor with shape:
        `(..., height, width, channels)`, in `"channels_last"` format,
        or `(..., channels, height, width)`, in `"channels_first"` format.

    Output shape:
        Same as input shape. The output maintains the same number of channels
        as the input, even for grayscale-converted images where all channels
        will have the same value.
    c                     t        |   di | |dk  s|dkD  rt        d|       || _        t	        j
                  |      | _        || _        | j                  j                  j                  |      | _
        y )Nr      z5`factor` should be between 0 and 1. Received: factor= )super__init__
ValueErrorfactorr   standardize_data_formatdata_formatseedrandomSeedGenerator	generator)selfr   r   r   kwargs	__class__s        s/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/random_grayscale.pyr   zRandomGrayscale.__init__0   sv    "6"A:!GxP  "::;G	,,::4@    c                    |%| j                  | j                  j                        }d}t        |j                        dk(  r(| j                  j
                  j	                  |      d   }| j                  j                  j                  |fdd|      }| j                  j                  j                  || j                  k  g d      }|S )Nr	      r   )shapeminvalmaxvalr   )r	         )axis)_get_seed_generatorr   _backendlenr   corer   uniformnumpyexpand_dimsr   )r   imagestrainingr   
batch_sizerandom_valuesshould_applys          r   get_random_transformationz)RandomGrayscale.get_random_transformation;   s    <++DLL,A,ABD
v||!**008;J++33-	 4 
 ||))55DKK'i 6 
 r   c                     |rm||n| j                  |      }| j                  j                  j                  || j                        }| j                  j
                  j                  |||      S |S )N)r   )r.   r   imagergb_to_grayscaler   r'   where)r   r)   transformationr*   r-   grayscale_imagess         r   transform_imagesz RandomGrayscale.transform_imagesO   s     "- 33F;   $||11BBD$4$4  C   <<%%++.  r   c                     |S Nr
   )r   input_shapes     r   compute_output_shapez$RandomGrayscale.compute_output_shape_   s    r   c                     |S r7   r
   )r   inputsr   s      r   compute_output_specz#RandomGrayscale.compute_output_specb       r   c                     |S r7   r
   )r   bounding_boxesr   s      r   transform_bounding_boxesz(RandomGrayscale.transform_bounding_boxese   s    r   c                     |S r7   r
   )r   labelstransformationsr   s       r   transform_labelsz RandomGrayscale.transform_labelsh   r=   r   c                     |S r7   r
   )r   segmentation_masksrC   r   s       r   transform_segmentation_masksz,RandomGrayscale.transform_segmentation_masksk   s
     "!r   c                 ^    t         |          }|j                  d| j                  i       |S )Nr   )r   
get_configupdater   )r   configr   s     r   rI   zRandomGrayscale.get_configp   s*    #%x-.r   )g      ?NN)TN)Tr7   )__name__
__module____qualname____doc__r   r.   r5   r9   r<   r@   rD   rG   rI   __classcell__)r   s   @r   r   r      sA    $L	A(  37"
 r   r   N)	keras.srcr   keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   r   r
   r   r   <module>rT      s5     -
 ,-j1 j .jr   