
    2Vh                     l    d dl 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	)
    )backend)keras_export)BaseImagePreprocessingLayer)_saturate_cast)SeedGeneratorzkeras.layers.Solarizationc                        e Zd ZdZdZdZdZ	 	 	 	 d fd	Zd Zd Z	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 )Solarizationa  Applies `(max_value - pixel + min_value)` for each pixel in the image.

    When created without `threshold` parameter, the layer performs solarization
    to all values. When created with specified `threshold` the layer only
    augments pixels that are above the `threshold` value.

    Args:
        addition_factor: (Optional)  A tuple of two floats or a single float,
            between 0 and 1.
            For each augmented image a value is
            sampled from the provided range. If a float is passed, the range is
            interpreted as `(0, addition_factor)`. If specified, this value
            (times the value range of input images, e.g. 255), is
            added to each pixel before solarization and thresholding.
            Defaults to 0.0.
        threshold_factor: (Optional)  A tuple of two floats or a single float.
            For each augmented image a value is
            sampled from the provided range. If a float is passed, the range is
            interpreted as `(0, threshold_factor)`. If specified, only pixel
            values above this threshold will be solarized.
        value_range: a tuple or a list of two elements. The first value
            represents the lower bound for values in input images, the second
            represents the upper bound. Images passed to the layer should have
            values within `value_range`. Typical values to pass
            are `(0, 255)` (RGB image) or `(0., 1.)` (scaled image).
        seed: Integer. Used to create a random seed.
        **kwargs: Base layer keyword arguments, such as `name` and `dtype`.

    Example:

    ```python
    (images, labels), _ = keras.datasets.cifar10.load_data()
    print(images[0, 0, 0])
    # [59 62 63]
    # Note that images are Tensor with values in the range [0, 255]
    solarization = Solarization(value_range=(0, 255))
    images = solarization(images)
    print(images[0, 0, 0])
    # [196, 193, 192]
    ```
    Fz<The `value_range` argument should be a list of two numbers. zzThe `addition_factor` and `threshold_factor` arguments should be a number (or a list of two numbers) in the range [0, 1]. c                     t        |   di | || _        t        |      | _        | j                  |d      | _        | j                  |d      | _        | j                  |       y )Naddition_factorthreshold_factor )	super__init__seedr   	generator_set_factorr   r   _set_value_range)selfr   r   value_ranger   kwargs	__class__s         o/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/solarization.pyr   zSolarization.__init__@   sl     	"6"	&t,#//. 
 !% 0 00!
 	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Solarization._set_value_rangeS   sx    +t}522*;-89  {q 22*;-89  "+.r   c                    t        |t        t        f      rft        |      dk7  rt	        | j
                  d| d| z         | j                  |d          | j                  |d          t        |      \  }}||fS t        |t        t        f      r| j                  |       d|g\  }}||fS t	        | j
                  d| d| z         )Nr   z
Received: =r      )
r   r   r   r!   r   _FACTOR_VALIDATION_ERROR_check_factor_ranger"   intfloat)r   factorfactor_nameloweruppers        r   r   zSolarization._set_factor`   s    fudm,6{a 11";-q9:  $$VAY/$$VAY/!&>LE5 e| e-$$V,v;LE5 e|	 --{m1VH56 r   c                 N    |dkD  s|dk  rt        | j                  d| z         y )Ng      ?r   zReceived: input_number=)r   r&   )r   input_numbers     r   r'   z Solarization._check_factor_ranget   s;    #!1--+L>:;  "2r   c                 6   |sy t        |t              r|d   }n|}| j                  j                  |      }t	        |      dk(  r
|d   dddf}nd}|%| j                  | j                  j                        }| j                  j                  j                  | j                  d   | j                  d   dz  ||| j                        | j                  j                  j                  | j                  d   | j                  d   dz  ||| j                        dS )	Nimages   r   r%   )r%   r%   r%      )minvalmaxvalshaper   dtype)	additions
thresholds)r   dictr   r6   r!   _get_seed_generator_backendrandomuniformr   compute_dtyper   )r   datatrainingr   r1   images_shapefactor_shapes          r   get_random_transformationz&Solarization.get_random_transformation{   s   dD!(^FF||))&1|!(OQ15L$L<++DLL,A,ABD ,,44++A.++A.4"(( 5  ,,--55,,Q/,,Q/#5"(( 6 
 	
r   c                    | j                   j                  || j                        }|r9||S |d   }|d   }| j                  || j                  d| j                        }||z   }| j                   j
                  j                  |dd      }| j                   j
                  j                  ||k  |d|z
        }| j                  |d| j                  | j                        }|j                  |j                  k(  r|S t        j                  |j                        r%| j                   j
                  j                  |      }t        ||j                  | j                         S |S )Nr9   r8   r   r3   )original_rangetarget_ranger7   r   r3   )r   castr?   _transform_value_ranger   numpyclipwherer7   is_int_dtyperoundr   )r   r1   transformationrA   r9   r8   resultss          r   transform_imageszSolarization.transform_images   sO   ""64+=+=>%'5J&{3I00#//%((	 1 F y(Gll((--gq#>Gll((..*$gsW}G 11'!--((	 2 G }},##FLL1,,,,227;!'6<<FFr   c                     |S Nr   )r   labelsrP   rA   s       r   transform_labelszSolarization.transform_labels   s    r   c                     |S rT   r   )r   bounding_boxesrP   rA   s       r   transform_bounding_boxesz%Solarization.transform_bounding_boxes   s
     r   c                     |S rT   r   )r   segmentation_masksrP   rA   s       r   transform_segmentation_masksz)Solarization.transform_segmentation_masks   s
     "!r   c                     t         |          }| j                  | j                  | j                  | j
                  d}i ||S )N)r   r   r   r   )r   
get_configr   r   r   r   )r   base_configconfigr   s      r   r^   zSolarization.get_config   sK    g(*++#33 $ 5 5II	
 )+(((r   c                     |S rT   r   )r   input_shapes     r   compute_output_shapez!Solarization.compute_output_shape   s    r   )        rd   rF   N)TN)T)__name__
__module____qualname____doc___USE_BASE_FACTORr    r&   r   r   r   r'   rD   rR   rV   rY   r\   r^   rc   __classcell__)r   s   @r   r	   r	   
   su    (T F "	   +&/( 
DB 8< <@"
)r   r	   N)	keras.srcr   keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   keras.src.ops.corer   keras.src.random.seed_generatorr   r	   r   r   r   <module>rp      s=     - . 9 )*K. K +Kr   