
    2Vh                     T    d dl mZ d dlmZ d dlmZ  ed       G d de             Zy)    )keras_export)BaseImagePreprocessingLayer)SeedGeneratorzkeras.layers.RandomSharpnessc                   x     e Zd 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	 ddZ fdZd Z xZS )RandomSharpnessaB  Randomly performs the sharpness operation on given images.

    The sharpness operation first performs a blur, then blends between the
    original image and the processed image. This operation adjusts the clarity
    of the edges in an image, ranging from blurred to enhanced sharpness.

    Args:
        factor: A tuple of two floats or a single float.
            `factor` controls the extent to which the image sharpness
            is impacted. `factor=0.0` results in a fully blurred image,
            `factor=0.5` applies no operation (preserving the original image),
            and `factor=1.0` enhances the sharpness beyond the original. Values
            should be between `0.0` and `1.0`. If a tuple is used, a `factor`
            is sampled between the two values for every image augmented.
            If a single float is used, a value between `0.0` and the passed
            float is sampled. To ensure the value is always the same,
            pass a tuple with two identical floats: `(0.5, 0.5)`.
        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.
        seed: Integer. Used to create a random seed.
    F)r      z<The `value_range` argument should be a list of two numbers. c                     t        |   dd|i| | j                  |       | j                  |       || _        t        |      | _        y )Ndata_format )super__init___set_factor_set_value_rangeseedr   	generator)selffactorvalue_ranger
   r   kwargs	__class__s         s/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/random_sharpness.pyr   zRandomSharpness.__init__)   sI     	;[;F; k*	&t,    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 RandomSharpness._set_value_range7   sx    +t}522*;-89  {q 22*;-89  "+.r   c                    t        |t              r|d   }n|}| j                  j                  |      }t	        |      }|dk(  rd}n|dk(  r|d   }nt        d|       |%| j                  | j                  j                        }| j                  j                  j                  |f| j                  d   | j                  d   |      }d|iS )	Nimages   r      r   zCExpected the input image to be rank 3 or 4. Received: inputs.shape=)minvalmaxvalr   r   )r   dictbackendshaper    r   _get_seed_generator_backendrandomuniformr   )	r   datatrainingr   r#   images_shaperank
batch_sizer   s	            r   get_random_transformationz)RandomSharpness.get_random_transformationD   s    dD!(^FF||))&1< 19JQY%aJ  ,~/ 
 <++DLL,A,ABD$$,,M;;q>;;q>	 - 
 &!!r   c                    | j                   j                  || j                        }|r+| j                  dk(  r'| j                   j                  j                  |dd      }| j                   j                  |d   dz  | j                        }| j                   j                  j                  |d      }| j                   j                  |      d   }d\  }}| j                   j                  |||g|||g|||gg| j                        }| j                   j                  j                  |d	      }| j                   j                  j                  |d
d
|d
g      }| j                   j                  || j                        }| j                   j                  j                  ||d
dd      }	| j                   j                  |	| j                        }	|d|z
  |	|z
  z  z   }| j                   j                  j                  || j                  d   | j                  d
         }| j                  dk(  r'| j                   j                  j                  |dd      }|S )Nchannels_firstr   r   )dtype)r8   r   r   r   )g;;?gى؉?)r$   r$   r   r   r   samechannels_last)stridespaddingr
   g      ?r   )r)   castcompute_dtyper
   numpyswapaxesreshaper*   convert_to_tensortilenndepthwise_convclipr   )
r   r#   transformationr0   sharpness_factornum_channelsabkernelsmoothed_images
             r   transform_imagesz RandomSharpness.transform_images`   s?   ""64+=+=>#33++44VRD#||00x(1,D4F4F  1    $||1199 -   <<--f5b9L)DAq\\33QQ1I1ay19K9K 4 F \\''//EF\\'',,VaL!5LMF\\&&vt/A/ABF!\\__;;+ < N "\\..d&8&8 / N s%55' F \\'',,((+T-=-=a-@F #33++44VRDr   c                     |S Nr   )r   labelsrH   r0   s       r   transform_labelsz RandomSharpness.transform_labels   s    r   c                     |S rQ   r   )r   segmentation_masksrH   r0   s       r   transform_segmentation_masksz,RandomSharpness.transform_segmentation_masks   s
     "!r   c                     |S rQ   r   )r   bounding_boxesrH   r0   s       r   transform_bounding_boxesz(RandomSharpness.transform_bounding_boxes   s
     r   c                     t         |          }|j                  | j                  | j                  | j
                  d       |S )N)r   r   r   )r   
get_configupdater   r   r   )r   configr   s     r   r[   zRandomSharpness.get_config   s?    #%++#//			
 r   c                     |S rQ   r   )r   input_shapes     r   compute_output_shapez$RandomSharpness.compute_output_shape   s    r   ))r      NN)TN)NT)T)__name__
__module____qualname____doc___USE_BASE_FACTOR_FACTOR_BOUNDSr   r   r   r4   rO   rS   rV   rY   r[   r`   __classcell__)r   s   @r   r   r      sd    0 N 	G " -/"8-^ <@" 8<
	r   r   N)keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   keras.src.randomr   r   r   r   r   <module>rl      s7    - + ,-_1 _ ._r   