
    2Vhn                         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 d dl	m
Z
 dZdZd	Z ed
       G d de             Zy)    )keras_export)BaseImagePreprocessingLayer)clip_to_image_size)convert_format)SeedGenerator)backend_utils
horizontalverticalhorizontal_and_verticalzkeras.layers.RandomFlipc                   p     e Zd ZdZdZeddf fd	ZddZddZddZ		 ddZ
	 dd	Zd
 Zd Z fdZ xZS )
RandomFlipa  A preprocessing layer which randomly flips images during training.

    This layer will flip the images horizontally and or vertically based on the
    `mode` attribute. During inference time, the output will be identical to
    input. Call the layer with `training=True` to flip the input.
    Input pixel values can be of any range (e.g. `[0., 1.)` or `[0, 255]`) and
    of integer or floating point dtype.
    By default, the layer will output floats.

    **Note:** This layer is safe to use inside a `tf.data` pipeline
    (independently of which backend you're using).

    Input shape:
        3D (unbatched) or 4D (batched) tensor with shape:
        `(..., height, width, channels)`, in `"channels_last"` format.

    Output shape:
        3D (unbatched) or 4D (batched) tensor with shape:
        `(..., height, width, channels)`, in `"channels_last"` format.

    Args:
        mode: String indicating which flip mode to use. Can be `"horizontal"`,
            `"vertical"`, or `"horizontal_and_vertical"`. `"horizontal"` is a
            left-right flip and `"vertical"` is a top-bottom flip. Defaults to
            `"horizontal_and_vertical"`
        seed: Integer. Used to create a random seed.
        **kwargs: Base layer keyword arguments, such as
            `name` and `dtype`.
    FNc                     t        |   dd|i| || _        t        |      | _        || _        d| _        d| _        y )Ndata_formatFT )super__init__seedr   	generatormode_convert_input_args!_allow_non_tensor_positional_args)selfr   r   r   kwargs	__class__s        n/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/random_flip.pyr   zRandomFlip.__init__5   sD     	;[;F;	&t,	#( 15.    c                    |sy t        |t              r|d   }n|}| j                  j                  j	                  |      }t        |      dk(  rd}n	|d   dddf}|%| j                  | j                  j                        }| j                  j                  j                  | j                  j                  j                  ||      d      }||dS )	Nimages   )   r    r    r   r    )shaper   g      ?)flipsinput_shape)
isinstancedictbackendcorer!   len_get_seed_generator_backendnumpy
less_equalrandomuniform)r   datatrainingr   r   r!   flips_shaper"   s           r   get_random_transformationz$RandomFlip.get_random_transformationC   s    dD!(^FF!!''/u:?#K 8Q1-K<++DLL,A,ABD""--LL''k'Es
 u55r   c                 z    | j                   j                  || j                        }|r| j                  ||      S |S N)r&   castcompute_dtype_flip_inputs)r   r   transformationr0   s       r   transform_imageszRandomFlip.transform_imagesY   s9    ""64+=+=>$$V^<<r   c                     |S r4   r   )r   labelsr8   r0   s       r   transform_labelszRandomFlip.transform_labels_   s    r   c                    
  fd
 fd
 fd}|rt        j                         r j                  j                  d        j                  j                  j                  |d   d      } j                  dk(  rd	}d}nd
}d	}|d   |   |d   |   }	}t        | j                  d||	      } |||      |d<   t        |||	d      }t        |d j                  ||	      } j                  j                          |S )Nc                     j                   j                  j                  | dd      \  }}}}j                   j                  j                  d|z
  |d|z
  |gd      }|S N   axisr    r&   r+   splitconcatenateboxesx1x2x3x4outputsr   s         r   _flip_boxes_horizontalzCRandomFlip.transform_bounding_boxes.<locals>._flip_boxes_horizontalh   sh    !\\//55eQR5HNBBll((44RQVR(r 5 G Nr   c                     j                   j                  j                  | dd      \  }}}}j                   j                  j                  |d|z
  |d|z
  gd      }|S r?   rD   rG   s         r   _flip_boxes_verticalzARandomFlip.transform_bounding_boxes.<locals>._flip_boxes_verticalo   sh    !\\//55eQR5HNBBll((44QVRR(r 5 G Nr   c                 &   | d   }j                   t        t        hv r-j                  j                  j                  | |      |      }j                   t        t        hv r-j                  j                  j                  | |      |      }|S )NrH   )r   
HORIZONTALHORIZONTAL_AND_VERTICALr&   r+   whereVERTICAL)rH   	box_flipsbboxesrN   rP   r   s      r   _transform_xyxyz<RandomFlip.transform_bounding_boxes.<locals>._transform_xyxyv   s    7^FyyZ)@AA++11*62
 yyX'>??++11(0
 Mr   
tensorflowr"   rA   rB   channels_firstr#   rel_xyxy)sourcetargetheightwidthrH   xyxy)bounding_boxesr`   ra   bounding_box_format)r   in_tf_graphr&   set_backendr+   squeezer   r   rd   r   reset)r   rc   r8   r0   rX   r"   height_axis
width_axisinput_heightinput_widthrN   rP   s   `         @@r   transform_bounding_boxesz#RandomFlip.transform_bounding_boxesb   s"   			  ((*((6LL&&..~g/FR.PE#33 
 
 }-k:}-j9 &L
 ,//!#!N '6ne&LN7#/-#!$*	N ,!//#!N LL r   c                 *    | j                  |||      S )N)r0   )r9   )r   segmentation_masksr8   r0   s       r   transform_segmentation_masksz'RandomFlip.transform_segmentation_masks   s#     $$ % 
 	
r   c                 
   ||S |d   }| j                   j                  |      }t        |      dk(  }|r'| j                   j                  j	                  |d      }|}| j
                  dk(  rd}d}nd}d}| j                  t        k(  s| j                  t        k(  rL| j                   j                  j                  || j                   j                  j                  ||      |      }| j                  t        k(  s| j                  t        k(  rL| j                   j                  j                  || j                   j                  j                  ||      |      }|r'| j                   j                  j                  |d      }|S )	Nr"   r   r   rB   channels_lastr[   r\   rA   )r&   r!   r(   r+   expand_dimsr   r   rR   rS   rT   fliprU   rg   )	r   inputsr8   r"   inputs_shape	unbatchedflipped_outputshorizontal_axisvertical_axiss	            r   r7   zRandomFlip._flip_inputs   s`   !Mw'||))&1%*	\\''33F3CF . OM OM99
"dii3J&J"ll0066""''o'NO
 99 DII1H$H"ll0066""''m'LO
 "ll0088a 9 O r   c                     |S r4   r   )r   r#   s     r   compute_output_shapezRandomFlip.compute_output_shape   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RandomFlip.get_config   s?    #%				#//	
 r   )TN)T)__name__
__module____qualname____doc___USE_BASE_FACTORrS   r   r2   r9   r<   rm   rp   r7   r|   r~   __classcell__)r   s   @r   r   r      s[    <  %	66, 	Qh <@
"H	 	r   r   N)keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   Lkeras.src.layers.preprocessing.image_preprocessing.bounding_boxes.convertersr   r   keras.src.random.seed_generatorr   keras.src.utilsr   rR   rU   rS   r   r   r   r   <module>r      sT    - : )
3  '(X, X )Xr   