
    2VhR+                         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 g dZg dZ ed       G d	 d
e             Zy)    N)keras_export)BaseImagePreprocessingLayer)SeedGenerator)backend_utils)random_shearrandom_translationrandom_rotationrandom_posterizationsolarizationauto_contrastequalizationrandom_brightnessrandom_color_degenerationrandom_contrastrandom_sharpness)r   r   r	   r
   r   r   r   zkeras.layers.AugMixc                        e Zd ZdZdZdZ	 	 	 	 	 	 	 	 	 d fd	Z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 )AugMixa  Performs the AugMix data augmentation technique.

    AugMix aims to produce images with variety while preserving the image
    semantics and local statistics. During the augmentation process,
    the same augmentation is applied across all images in the batch
    in num_chains different ways, with each chain consisting of
    chain_depth augmentations.

    Args:
        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.
        num_chains: an integer representing the number of different chains to
            be mixed, defaults to 3.
        chain_depth: an integer representing the maximum number of
            transformations to be applied in each chain. The actual number
            of transformations in each chain will be sampled randomly
            from the range `[0, `chain_depth`]`. Defaults to 3.
        factor: The strength of the augmentation as a normalized value
            between 0 and 1. Default is 0.3.
        alpha: a float value used as the probability coefficients for the
            Beta and Dirichlet distributions, defaults to 1.0.
        all_ops: Use all operations (including random_brightness,
            random_color_degeneration, random_contrast and random_sharpness).
            Default is True.
        interpolation: The interpolation method to use for resizing operations.
            Options include `"nearest"`, `"bilinear"`. Default is `"bilinear"`.
        seed: Integer. Used to create a random seed.

    References:
        - [AugMix paper](https://arxiv.org/pdf/1912.02781)
        - [Official Code](https://github.com/google-research/augmix)
    F)r      c
                 J   t        |   d
d|	i|
 || _        || _        || _        | j                  |       || _        || _        || _        || _	        t        |      | _        | j                  rt        | _        n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                  | j                  |	d|
| _        t        j2                  d
t5        dt7        d| j"                  d   z              | j                  | j                  |	d|
| _        t        j:                  d
| j                  |	d	|
| _        t        j>                  d
| j                  |	d	|
| _         | j                  rt        jB                  d
| j"                  | j                  | j                  |	d|
| _"        t        jF                  d
| j"                  | j                  | j                  |	d|
| _$        t        jJ                  d
| j"                  | j                  | j                  |	d|
| _&        t        jN                  d
| j"                  | j                  | j                  |	d|
| _(        y y )Ndata_format)x_factory_factorinterpolationseedr   )height_factorwidth_factorr   r   r   )factorr   r   r   )addition_factorthreshold_factorvalue_ranger   r   r      )r   r    r   r   )r    r    ))super__init__r    
num_chainschain_depth_set_factoralphaall_opsr   r   r   	generatorAUGMENT_LAYERS_ALL_augment_layersAUGMENT_LAYERSlayersRandomShearr   r   RandomTranslationr   RandomRotationr	   Solarizationr   RandomPosterizationmaxintr
   AutoContrastr   Equalizationr   RandomBrightnessr   RandomColorDegenerationr   RandomContrastr   RandomSharpnessr   )selfr    r%   r&   r   r(   r)   r   r   r   kwargs	__class__s              j/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/aug_mix.pyr$   zAugMix.__init__L   s)    	;[;F;&$& 
*	&t,<<#5D #1D ".. 
[[[['#
 
 #)":": #
++'##
 #
  &44  
;;'#	 

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

 %
! $00 
((k
EK
 #// 
((k
EK
 <<%+%<%< &{{ ,,YY'	&
 &D" .4-K-K .{{ ,,YY'	.
 .D* $*#8#8 ${{ ,,YY'	$
 $D  %+$:$: %{{ ,,YY'	%
 %D!3     c                 `    | j                   D ]  }t        | |      }|j                  |       ! y N)r,   getattrbuild)r<   input_shape
layer_nameaugmentation_layers       r?   rD   zAugMix.build   s2    .. 	2J!(z!:$$[1	2r@   c                     | j                   j                  j                  |||      }|| j                   j                  j	                  |dd      z  S )N)shaper(   r   T)axiskeepdims)backendrandomgammanumpysum)r<   rI   r(   r   gamma_samples        r?   _sample_from_dirichletzAugMix._sample_from_dirichlet   s\    ||**00 1 

 dll0044rD 5 
 
 	
r@   c                    |sy t        j                         rS| j                  j                  d       | j                  D ])  }t        | |      }|j                  j                  d       + |xs% | j                  | j                  j                        }| j                  | j                  g| j                  |      }| j                  j                  j                  d| j                  | j                  |      }g }t        | j                        D ]  }	g }
t        | j                        D ]|  }	t        j                   | j                  d gz         }|)t        | |      }|
j#                  ||j%                  || j                  | j                  j                              d       ~ |j#                  |
        |||d}|S )N
tensorflowr"   )rI   r(   betar   )r   )rF   transformation)chain_mixing_weightsweight_samplechain_transforms)r   in_tf_graphrM   set_backendr,   rC   _get_seed_generator_backendrS   r%   r(   rN   rV   ranger&   	py_randomchoiceappendget_random_transformation)r<   datatrainingr   rF   rG   rX   rY   rZ   _depth_transformsrW   s               r?   rc   z AugMix.get_random_transformation   s   $$&LL$$\2"22 E
%,T:%>""**66|DE Ft//0E0EF#::__tzz4 
 ++00**	 1 
 t' 	6A!4++, &--d.B.BdV.KL
%%,T:%>" ''&0.HH $%)%=%=$(LL$9$9&" I 
$ ##$45)	6. %9* 0
 r@   c                 "   |re| j                   j                  || j                        }| j                   j                  |d   | j                        }| j                   j                  |d   | j                        }|d   }| j                   j                  j	                  |      }t        |      D ]d  \  }}	| j                   j                  j                  |      }
|	D ]*  }|d   }|d   }t        | |      }|j                  |
|      }
, ||
||   z  z  }f ||z  d|z
  |z  z   }| j                   j                  j                  || j                  d   | j                  d         }| j                   j                  || j                        }|S )	NrX   )dtyperY   rZ   rF   rW   r   r   )rM   castcompute_dtyperP   
zeros_like	enumeratecopyrC   transform_imagesclipr    )r<   imagesrW   re   rX   rY   rZ   
aug_imagesidxchain_transformcopied_imagesdepth_transformrF   layer_transformrG   s                  r?   ro   zAugMix.transform_images  s   \\&&vt/A/ABF#'<<#4#456d>P>P $5 $  !LL--/t7I7I . M  ..@A++66v>J(12B(C 
H$_ $ 2 2 7 7 ?'6 O!0!>J&56F&GO)0z)B&$6$G$G%%M m.B3.GGG

H #V+q=/@J.NNF\\'',,((+T-=-=a-@F ""64+=+=>r@   c                     |S rB   r"   )r<   labelsrW   re   s       r?   transform_labelszAugMix.transform_labels$  s    r@   c                     |S rB   r"   )r<   bounding_boxesrW   re   s       r?   transform_bounding_boxeszAugMix.transform_bounding_boxes'  s
     r@   c                 *    | j                  |||      S )N)re   )ro   )r<   segmentation_masksrW   re   s       r?   transform_segmentation_masksz#AugMix.transform_segmentation_masks/  s#     $$ % 
 	
r@   c                     |S rB   r"   )r<   rE   s     r?   compute_output_shapezAugMix.compute_output_shape6  s    r@   c           	          | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  d}t        | %         }i ||S )N)r    r%   r&   r   r(   r)   r   r   )
r    r&   r%   r   r(   r)   r   r   r#   
get_config)r<   configbase_configr>   s      r?   r   zAugMix.get_config9  sg    ++**??kkZZ||!//II	
 g(*(+(((r@   )	)r         r   g333333?g      ?TbilinearNN)TN)T)__name__
__module____qualname____doc___USE_BASE_FACTOR_FACTOR_BOUNDSr$   rD   rS   rc   ro   rz   r}   r   r   r   __classcell__)r>   s   @r?   r   r   $   sy    !F N  pd2

4lB 	 <@
) )r@   r   )rN   r`   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   r"   r@   r?   <module>r      sR     ! ! - + )  #$`)( `) %`)r@   