
    2Vh                     H    d dl mZ d dlmZ  ed       G d de             Zy)    )keras_export)BaseImagePreprocessingLayerz keras.layers.RandomPosterizationc                   |     e Zd 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 )RandomPosterizationa  Reduces the number of bits for each color channel.

    References:
    - [AutoAugment: Learning Augmentation Policies from Data](https://arxiv.org/abs/1805.09501)
    - [RandAugment: Practical automated data augmentation with a reduced search space](https://arxiv.org/abs/1909.13719)

    Args:
        value_range: a tuple or a list of two elements. The first value
            represents the lower bound for values in passed images, the second
            represents the upper bound. Images passed to the layer should have
            values within `value_range`. Defaults to `(0, 255)`.
        factor: integer, the number of bits to keep for each channel. Must be a
            value between 1-8.
    F)      r   z<The `value_range` argument should be a list of two numbers. c                     t        |   dd|i| | j                  |       | j                  |       || _        | j
                  j                  j                  |      | _        y )Ndata_format )	super__init___set_factor_set_value_rangeseedbackendrandomSeedGenerator	generator)selffactorvalue_ranger
   r   kwargs	__class__s         w/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/random_posterization.pyr   zRandomPosterization.__init__   sY     	;[;F; k*	,,::4@    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$RandomPosterization._set_value_range-   sx    +t}522*;-89  {q 22*;-89  "+.r   c                 n   t        |t              r|d   }n|}| j                  j                  |      }t	        |      }|dk(  rd}n|dk(  r|d   }nt        d|       |%| j                  | j                  j                        }| j                  d   | j                  d   k7  rF| j                  j                  j                  |f| j                  d   | j                  d   |d      }n8| j                  j                  j                  |fd	      | j                  d   z  }| j                  |z
  }	d
|	iS )Nimages   r      r   zCExpected the input image to be rank 3 or 4. Received: inputs.shape=uint8)minvalmaxvalr   dtype)r,   shift_factor)r   dictr   shaper#   r!   _get_seed_generator_backendr   r   randintnumpyones_MAX_FACTOR)
r   datatrainingr   r&   images_shaperank
batch_sizer   r-   s
             r   get_random_transformationz-RandomPosterization.get_random_transformation:   s<   dD!(^FF||))&1< 19JQY%aJ  ,~/ 
 <++DLL,A,ABD;;q>T[[^+\\((00{{1~{{1~ 1 F ""''W'E++a.! 
 ''&0--r   c                 V   |r%|d   }| j                   j                  j                  || j                   j                  |      dz         }| j	                  || j
                  d| j                        }| j                   j                  |d      }| j                   j                  j                  | j                   j                  j                  ||      |      }| j                   j                  || j                        }| j	                  |d| j
                  | j                        }|S )Nr-   )r   r   r   r      )original_rangetarget_ranger,   r)   )
r   r3   reshaper/   _transform_value_ranger   compute_dtypecastbitwise_left_shiftbitwise_right_shift)r   r&   transformationr7   r-   s        r   transform_imagesz$RandomPosterization.transform_images_   s   ).9L<<--55dll00>JL 00#//%((	 1 F \\&&vw7F\\''::""66v|LF \\&&vt/A/ABF00'!--((	 1 F r   c                     |S Nr   )r   labelsrG   r7   s       r   transform_labelsz$RandomPosterization.transform_labels~   s    r   c                     |S rJ   r   )r   segmentation_masksrG   r7   s       r   transform_segmentation_masksz0RandomPosterization.transform_segmentation_masks   s
     "!r   c                     |S rJ   r   )r   bounding_boxesrG   r7   s       r   transform_bounding_boxesz,RandomPosterization.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   rT   zRandomPosterization.get_config   s?    #%++#//			
 r   c                     |S rJ   r   )r   input_shapes     r   compute_output_shapez(RandomPosterization.compute_output_shape   s    r   )r=   NN)TN)NT)T)__name__
__module____qualname____doc___USE_BASE_FACTOR_FACTOR_BOUNDSr5   r"   r   r   r;   rH   rL   rO   rR   rT   rY   __classcell__)r   s   @r   r   r      sg     NKF " A/#.J> <@" 8<
	r   r   N)keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   r   r   r   r   <module>rc      s2    -
 01O5 O 2Or   