
    2Vh                     `    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)	    )backend)keras_export)BaseImagePreprocessingLayer)_saturate_castzkeras.layers.AutoContrastc                   h     e Zd 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 fd
Zd Z xZS )AutoContrasta  Performs the auto-contrast operation on an image.

    Auto contrast stretches the values of an image across the entire available
    `value_range`. This makes differences between pixels more obvious. An
    example of this is if an image only has values `[0, 1]` out of the range
    `[0, 255]`, auto contrast will change the `1` values to be `255`.

    This layer is active at both training and inference time.

    Args:
        value_range: 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.
            Defaults to `(0, 255)`.
    Fz<The `value_range` argument should be a list of two numbers. c                 F    t        |   di | | j                  |       y )N )super__init___set_value_range)selfvalue_rangekwargs	__class__s      p/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/auto_contrast.pyr   zAutoContrast.__init__!   s#    
 	"6"k*    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AutoContrast._set_value_range)   sx    +t}522*;-89  {q 22*;-89  "+.r   c                    |}| j                  || j                  d| j                        }| j                  j	                  || j                        }| j                  j
                  j                  |dd      }| j                  j
                  j                  |dd      }d||z
  z  }| |z  }||z  |z   }| j                  j
                  j                  |dd      }	| j                  |	d| j                  | j                        }	| j                  j
                  j                  | j                  j
                  j                  |	      ||	      }	|	j                  |j                  k(  r|	S t        j                  |j                        r%| j                  j
                  j                  |	      }	t        |	|j                  | j                        S )Nr      )original_rangetarget_rangedtype)   r   T)axiskeepdimsg     o@g        )_transform_value_ranger   compute_dtyper   castnumpyminmaxclipwhereisnanr"   is_int_dtyperoundr   )
r   imagestransformationtrainingoriginal_imageslowhighscaleoffsetresultss
             r   transform_imageszAutoContrast.transform_images6   s    ,,++!$$	 - 
 ""64+=+=>ll  $$V&4$H||!!%%f6D%I$%&(,,$$))&#u=--#))$$	 . 
 ,,$$**LL$$W-
 ==FLL(N-ll((..w7Ggv||T\\BBr   c                     |S Nr
   )r   labelsr2   r3   s       r   transform_labelszAutoContrast.transform_labelsW   s    r   c                     |S r<   r
   )r   bounding_boxesr2   r3   s       r   transform_bounding_boxesz%AutoContrast.transform_bounding_boxesZ   s
     r   c                     |S r<   r
   )r   segmentation_masksr2   r3   s       r   transform_segmentation_masksz)AutoContrast.transform_segmentation_masksb   s
     "!r   c                 ^    t         |          }|j                  d| j                  i       |S )Nr   )r   
get_configupdater   )r   configr   s     r   rF   zAutoContrast.get_configg   s,    #%}d&6&678r   c                     |S r<   r
   )r   input_shapes     r   compute_output_shapez!AutoContrast.compute_output_shapel   s    r   )r   )NT)T)__name__
__module____qualname____doc___USE_BASE_FACTORr   r   r   r:   r>   rA   rD   rF   rK   __classcell__)r   s   @r   r   r   	   sR    " F " +/CB 	 <@"

r   r   N)		keras.srcr   keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   keras.src.ops.corer   r   r
   r   r   <module>rV      s:     - . )*c. c +cr   