
    2Vh.                     x    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
  ed       G d d	e             Zy
)    )backend)keras_export)BaseImagePreprocessingLayer)clip_to_image_size)convert_format)_saturate_castzkeras.layers.Resizingc                        e Zd ZdZdZ	 	 	 	 	 	 	 d fd	ZddZ	 ddZddZddZ		 ddZ
d	 Zd
 Zd Zd Zd Z fdZ xZS )Resizinga  A preprocessing layer which resizes images.

    This layer resizes an image input to a target height and width. The input
    should be a 4D (batched) or 3D (unbatched) tensor in `"channels_last"`
    format. Input pixel values can be of any range
    (e.g. `[0., 1.)` or `[0, 255]`).

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

    Output shape:
        3D (unbatched) or 4D (batched) tensor with shape:
        `(..., target_height, target_width, channels)`,
        or `(..., channels, target_height, target_width)`,
        in `"channels_first"` format.

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

    Args:
        height: Integer, the height of the output shape.
        width: Integer, the width of the output shape.
        interpolation: String, the interpolation method.
            Supports `"bilinear"`, `"nearest"`, `"bicubic"`,
            `"lanczos3"`, `"lanczos5"`. Defaults to `"bilinear"`.
        crop_to_aspect_ratio: If `True`, resize the images without aspect
            ratio distortion. When the original aspect ratio differs
            from the target aspect ratio, the output image will be
            cropped so as to return the
            largest possible window in the image (of size `(height, width)`)
            that matches the target aspect ratio. By default
            (`crop_to_aspect_ratio=False`), aspect ratio may not be preserved.
        pad_to_aspect_ratio: If `True`, pad the images without aspect
            ratio distortion. When the original aspect ratio differs
            from the target aspect ratio, the output image will be
            evenly padded on the short side.
        fill_mode: When using `pad_to_aspect_ratio=True`, padded areas
            are filled according to the given mode. Only `"constant"` is
            supported at this time
            (fill with constant value, equal to `fill_value`).
        fill_value: Float. Padding value to use when `pad_to_aspect_ratio=True`.
        data_format: string, either `"channels_last"` or `"channels_first"`.
            The ordering of the dimensions in the inputs. `"channels_last"`
            corresponds to inputs with shape `(batch, height, width, channels)`
            while `"channels_first"` corresponds to inputs with shape
            `(batch, channels, height, width)`. It defaults to the
            `image_data_format` value found in your Keras config file at
            `~/.keras/keras.json`. If you never set it, then it will be
            `"channels_last"`.
        **kwargs: Base layer keyword arguments, such as `name` and `dtype`.
    Fc
                 R   t        |   di |
 || _        || _        || _        t        j                  |	      | _        || _        || _	        || _
        || _        t        |      | _        | j                  dk(  rd| _        d| _        y | j                  dk(  rd| _        d| _        y y )Nchannels_firstchannels_last )super__init__heightwidthinterpolationr   standardize_data_formatdata_formatcrop_to_aspect_ratiopad_to_aspect_ratio	fill_mode
fill_valuebool	antialiasheight_axis
width_axis)selfr   r   r   r   r   r   r   r   r   kwargs	__class__s              k/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/preprocessing/image_preprocessing/resizing.pyr   zResizing.__init__I   s     	"6"
*"::;G$8!#6 "$i//!D DO0!D DO 1    c                    | j                   | j                  f}| j                  j                  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)sizer   r   r   r   r   r   r   )r   r   r   imageresizer   r   r   r   r   r   r   dtypeis_int_dtypenumpyroundr   )r!   imagestransformationtrainingr'   resizeds         r$   transform_imageszResizing.transform_imagesg   s    TZZ(,,$$++,,nn((!%!:!: $ 8 8nn , 

 ==FLL(N-ll((..w7Ggv||T\\BBr%   c                 $    | j                  |      S N)r2   )r!   segmentation_masksr/   r0   s       r$   transform_segmentation_masksz%Resizing.transform_segmentation_masksz   s     $$%788r%   c                     |S r4   r   )r!   labelsr/   r0   s       r$   transform_labelszResizing.transform_labels   s    r%   c                     t        |t              r| j                  j                  |d         }n| j                  j                  |      }|| j                     || j
                     }}||fS )Nr.   )
isinstancedictr   shaper   r    )r!   datar0   seedinput_shapeinput_heightinput_widths          r$   get_random_transformationz"Resizing.get_random_transformation   sh    dD!,,,,T(^<K,,,,T2K (()( "
 [((r%   c                    | j                   }|\  }}|j                  j                  |d   dk(  d      }|j                  j                  |d   dk(  d      }|j                  j                  ||      }	t	        || j
                  d||      }| j                  |d   ||      |d<   t        || j                  | j                        }|j                  j                  |j                  j                  |	d      |j                  g d	|d   j                  
      |d         |d<   t	        |d| j
                  | j                  | j                        }|S )Nboxesr   axisr   xyxy)sourcetargetr   r   )rA   rB   )bounding_boxesr   r   )        rL   rL   rL   r*   )r   r,   all
logical_orr   bounding_box_format_transform_xyxyr   r   r   whereexpand_dimsconvert_to_tensorr*   )
r!   rK   r/   r0   opsrA   rB   mask_negative_1s
mask_zeros
boxes_masks
             r$   transform_bounding_boxesz!Resizing.transform_bounding_boxes   sc    ll$2!k99==)@B)FR=PYY]]>'#:a#?b]I
YY))*:JG
'++
 #'"6"67#%# #7 #
w ,);;**
 #&))//II!!*2!6!!$N7,C,I,I "  7##
w (++;;**
 r%   c                 0   | j                   }|j                  ||j                        }|j                  ||j                        }| j                  r| j	                  |||      S | j
                  r| j                  |||      S | j                  |||      S )NrM   )r   castr*   r   $_transform_boxes_pad_to_aspect_ratior   %_transform_boxes_crop_to_aspect_ratio_transform_boxes_stretch)r!   rE   rA   rB   rU   s        r$   rQ   zResizing._transform_xyxy   s    llxxEKKx@hh{%++h>##<<|[  &&==|[  00|[ r%   c                    | j                   }|j                  | j                  |z  |j                        }|j                  | j                  |z  |j                        }|j
                  j                  ||      }| j                  ||z  z
  dz  }| j                  ||z  z
  dz  }	|j
                  j                  |d   |z  |	z   |d   |z  |z   |d   |z  |	z   |d   |z  |z   gd      S )	z6Transforms bounding boxes for padding to aspect ratio.rM      .r   .   .r`   .   r   rF   )r   r[   r   r*   r   r,   minimumstack)
r!   rE   rA   rB   rU   height_ratiowidth_ratiomin_aspect_ratioy_offsetx_offsets
             r$   r\   z-Resizing._transform_boxes_pad_to_aspect_ratio   s     llxxl :%++xNhhtzzK7u{{hK99,,\;GKK,1A"AAaGJJ/?!??AEyyf 008;f 008;f 008;f 008;	   
 	
r%   c                    | j                   }||z  }| j                  | j                  z  }|j                  j	                  ||kD  | j                  |z  | j                        }|j                  j	                  ||kD  | j                  | j                  |z        }||z  }	||z  }
|| j                  z
  dz  }|| j                  z
  dz  }|j                  j                  |d   |	z  |z
  |d   |
z  |z
  |d   |	z  |z
  |d   |
z  |z
  gd      S )z7Transforms bounding boxes for cropping to aspect ratio.r`   ra   rb   rd   re   r   rF   )r   r   r   r,   rR   rh   )r!   rE   rA   rB   rU   source_aspect_ratiotarget_aspect_ratio	new_width
new_heightscale_xscale_y	crop_leftcrop_tops                r$   r]   z.Resizing._transform_boxes_crop_to_aspect_ratio   s/    ll)L8"jj4;;6IIOO"55KK--JJ
	
 YY__"55KKJJ,,


 k)|++1	,2yyf')3f'(2f')3f'(2	   
 	
r%   c                 4   | j                   }|j                  | j                  |z  |j                        }|j                  | j                  |z  |j                        }|j
                  j                  |d   |z  |d   |z  |d   |z  |d   |z  gd      S )z/Transforms bounding boxes by simple stretching.rM   ra   rb   rd   re   r   rF   )r   r[   r   r*   r   r,   rh   )r!   rE   rA   rB   rU   ri   rj   s          r$   r^   z!Resizing._transform_boxes_stretch  s    llxxl :%++xNhhtzzK7u{{hKyyf+f,f+f,	   
 	
r%   c                    t        |      }t        |      dk(  ra| j                  dk(  r)| j                  |d<   | j                  |d<   t        |      S | j                  |d<   | j                  |d<   t        |      S | j                  dk(  r)| j                  |d<   | j                  |d<   t        |      S | j                  |d<   | j                  |d<   t        |      S )N   r   rc   r`   rf   r   )listlenr   r   r   tuple)r!   r@   s     r$   compute_output_shapezResizing.compute_output_shape  s    ;'{q ?2!%A!%A [!! "&A!%A [!! ?2!%A!%A [!! "&A!%A[!!r%   c           
          t         |          }| j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  d	}i ||S )N)	r   r   r   r   r   r   r   r   r   )r   
get_configr   r   r   r   r   r   r   r   r   )r!   base_configconfigr#   s      r$   r   zResizing.get_config'  sp    g(*kkZZ!//$($=$=#'#;#;//++

 )+(((r%   )bilinearFFconstantrL   FN)NT)TN)T)__name__
__module____qualname____doc___USE_BASE_FACTORr   r2   r6   r9   rC   rY   rQ   r\   r]   r^   r}   r   __classcell__)r#   s   @r$   r
   r
      sw    4l  !"!!<C( AE9
)" 	0d$
(
>
"$) )r%   r
   N)	keras.srcr   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.ops.corer   r
   r   r%   r$   <module>r      sD     - . %&d)* d) 'd)r%   