
    2Vh                     z    d dl Z d dlmc mZ 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)	    N)keras_export)BaseImagePreprocessingLayer)SeedGenerator)backend_utilszkeras.layers.RandAugmentc                        e Zd ZdZdZdZg 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 )RandAugmenta  RandAugment performs the Rand Augment operation on input images.

    This layer can be thought of as an all-in-one image augmentation layer. The
    policy implemented by this layer has been benchmarked extensively and is
    effective on a wide variety of datasets.

    References:
        - [RandAugment](https://arxiv.org/abs/1909.13719)

    Args:
        value_range: The range of values the input image can take.
            Default is `(0, 255)`. Typically, this would be `(0, 1)`
            for normalized images or `(0, 255)` for raw images.
        num_ops: The number of augmentation operations to apply sequentially
            to each image. Default is 2.
        factor: The strength of the augmentation as a normalized value
            between 0 and 1. Default is 0.5.
        interpolation: The interpolation method to use for resizing operations.
            Options include `nearest`, `bilinear`. Default is `bilinear`.
        seed: Integer. Used to create a random seed.

    F)r      )random_shearrandom_translationrandom_rotationrandom_brightnessrandom_color_degenerationrandom_contrastrandom_sharpnessrandom_posterizationsolarizationauto_contrastequalizationc                    t        |   d
d|i| || _        || _        | j	                  |       || _        || _        t        |      | _        t        j                  d
| j                  | j                  || j                  |d|| _        t        j                  d
| j                  | j                  || j                  |d|| _        t        j                  d
| j                  || j                  |d|| _        t        j"                  d
| j                  | j                  | j                  |d|| _        t        j&                  d
| j                  | j                  | j                  |d|| _        t        j*                  d
| j                  | j                  | j                  |d|| _        t        j.                  d
| j                  | j                  | j                  |d|| _        t        j2                  d
| j                  | j                  | j                  | j                  |d|| _        t        j6                  d
t9        dt;        d| j                  d   z              | j                  | j                  |d|| _        t        j>                  d
| j                  |d	|| _         t        jB                  d
| j                  |d	|| _"        y )Ndata_format)x_factory_factorinterpolationseedr   )height_factorwidth_factorr   r   r   )factorr   r   r   )r   value_ranger   r   )addition_factorthreshold_factorr   r   r   r	      )r   r    )#super__init__r   num_ops_set_factorr   r   r   	generatorlayersRandomShearr   r
   RandomTranslationr   RandomRotationr   RandomBrightnessr   RandomColorDegenerationr   RandomContrastr   RandomSharpnessr   Solarizationr   RandomPosterizationmaxintr   AutoContrastr   Equalizationr   )	selfr   r%   r   r   r   r   kwargs	__class__s	           o/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/rand_augment.pyr$   zRandAugment.__init__6   s    	;[;F;& *	&t,".. 
[[[['#
 
 #)":": #
++'##
 #
  &44  
;;'#	 

  
 "(!8!8 "
;;((#	"

 "
 *0)G)G *
;;((#	*

 *
&  &44  
;;((#	 

  
 !' 6 6 !
;;((#	!

 !
 #// 
 KK![[((#
 
 %+$>$> %
q#a$++a.012((#	%

 %
! $00 
((k
EK
 #// 
((k
EK
    c                 `    | j                   D ]  }t        | |      }|j                  |       ! y N)_AUGMENT_LAYERSgetattrbuild)r6   input_shape
layer_nameaugmentation_layers       r9   r?   zRandAugment.build   s2    .. 	2J!(z!:$$[1	2r:   c                    |sy t        j                         rS| j                  j                  d       | j                  D ])  }t        | |      }|j                  j                  d       + i }t        j                  | j                         | j                  d | j                   D ]H  }t        | |      }|j                  ||| j                  | j                  j                              ||<   J |S )N
tensorflow)trainingr   )r   in_tf_graphbackendset_backendr=   r>   randomshuffler%   get_random_transformation_get_seed_generator_backend)r6   datarE   r   rA   rB   transformations          r9   rK   z%RandAugment.get_random_transformation   s    $$&LL$$\2"22 E
%,T:%>""**66|DE t++,..~> 	J!(z!:"<<%11$,,2G2GH =  :&	 r:   c                    |r\| j                   j                  || j                        }|j                         D ]#  \  }}t	        | |      }|j                  ||      }% | j                   j                  || j                        }|S r<   )rG   castcompute_dtypeitemsr>   transform_images)r6   imagesrO   rE   rA   transformation_valuerB   s          r9   rT   zRandAugment.transform_images   s    \\&&vt/A/ABF4B4H4H4J 0
0%,T:%>"+<<0 ""64+=+=>r:   c                     |S r<   r"   )r6   labelsrO   rE   s       r9   transform_labelszRandAugment.transform_labels   s    r:   c                 z    |r8|j                         D ]%  \  }}t        | |      }|j                  |||      }' |S N)rE   )rS   r>   transform_bounding_boxes)r6   bounding_boxesrO   rE   rA   rV   rB   s          r9   r\   z$RandAugment.transform_bounding_boxes   sX     4B4H4H4J 0
0%,T:%>"!3!L!L"$88 "M "
 r:   c                 *    | j                  |||      S r[   )rT   )r6   segmentation_masksrO   rE   s       r9   transform_segmentation_masksz(RandAugment.transform_segmentation_masks   s#     $$ % 
 	
r:   c                     |S r<   r"   )r6   r@   s     r9   compute_output_shapez RandAugment.compute_output_shape   s    r:   c                     | j                   | j                  | j                  | j                  | j                  d}t
        |          }i ||S )N)r   r%   r   r   r   )r   r%   r   r   r   r#   
get_config)r6   configbase_configr8   s      r9   rd   zRandAugment.get_config   sP    ++||kk!//II
 g(*(+(((r:   ))r         g      ?bilinearNN)TN)T)__name__
__module____qualname____doc___USE_BASE_FACTOR_FACTOR_BOUNDSr=   r$   r?   rK   rT   rY   r\   r`   rb   rd   __classcell__)r8   s   @r9   r   r      so    . NO   d
L2
2 	 <@
	) 	)r:   r   )rI   keras.src.layerssrcr(   keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   keras.src.randomr   keras.src.utilsr   r   r"   r:   r9   <module>rw      sC     ! ! - + ) ()^)- ^) *^)r:   