
    hE                        S r SSKJr  SSKJrJr  SSKrSSKrSSK	J
r
  SSKJrJrJrJr  SS/r " S	 S\
5      r " S
 S\
5      rg)a  Module containing PyTorch-specific transforms for Albumentations.

This module provides transforms that convert NumPy arrays to PyTorch tensors in
the appropriate format. It handles both 2D image data and 3D volumetric data,
ensuring that the tensor dimensions are correctly arranged according to PyTorch's
expected format (channels first). These transforms are typically used as the final
step in an augmentation pipeline before feeding data to a PyTorch model.
    )annotations)AnyoverloadN)BasicTransform)MONO_CHANNEL_DIMENSIONSNUM_MULTI_CHANNEL_DIMENSIONSNUM_VOLUME_DIMENSIONSTargets
ToTensor3D
ToTensorV2c                     ^  \ rS rSrSr\R                  \R                  4rSSU 4S jjjr	\
SS j5       rSS jrSS jr\SS j5       r\SS j5       rSS	 jrSS
 jrSrU =r$ )r      a  Converts images/masks to PyTorch Tensors, inheriting from BasicTransform.
For images:
    - If input is in `HWC` format, converts to PyTorch `CHW` format
    - If input is in `HW` format, converts to PyTorch `1HW` format (adds channel dimension)

Attributes:
    transpose_mask (bool): If True, transposes 3D input mask dimensions from `[height, width, num_channels]` to
        `[num_channels, height, width]`.
    p (float): Probability of applying the transform. Default: 1.0.

c                ,   > [         TU ]  US9  Xl        g N)p)super__init__transpose_mask)selfr   r   	__class__s      [/var/www/fran/franai/venv/lib/python3.13/site-packages/albumentations/pytorch/transforms.pyr   ToTensorV2.__init__+   s    1,    c                `    U R                   U R                  U R                  U R                  S.$ )zDefine mapping of target name to target function.

Returns:
    dict[str, Any]: Dictionary mapping target names to corresponding transform functions.

)imageimagesmaskmasks)applyapply_to_imagesapply_to_maskapply_to_masksr   s    r   targetsToTensorV2.targets/   s0     ZZ**&&((	
 	
r   c                    UR                   [        [        1;  a  Sn[        U5      eUR                   [        :X  a  [        R
                  " US5      n[        R                  " UR                  SSS5      5      $ )a  Convert a 2D image array to a PyTorch tensor.

Converts image from HWC or HW format to CHW format, handling both
single-channel and multi-channel images.

Args:
    img (np.ndarray): Image as a numpy array of shape (H,W) or (H,W,C)
    **params (Any): Additional parameters

Returns:
    torch.Tensor: PyTorch tensor in CHW format

Raises:
    ValueError: If image dimensions are neither HW nor HWC

z7Albumentations only supports images in HW or HWC format   r      )	ndimr   r   
ValueErrornpexpand_dimstorch
from_numpy	transpose)r   imgparamsmsgs       r   r   ToTensorV2.apply>   sa    " 8835QRRKCS/!88....a(CaA 677r   c                    U R                   (       a'  UR                  [        :X  a  UR                  SSS5      n[        R
                  " U5      $ )a*  Convert a mask array to a PyTorch tensor.

If transpose_mask is True and mask has 3 dimensions (H,W,C),
converts mask to channels-first format (C,H,W).

Args:
    mask (np.ndarray): Mask as a numpy array
    **params (Any): Additional parameters

Returns:
    torch.Tensor: PyTorch tensor of mask

r'   r   r(   )r   r)   r   r/   r-   r.   )r   r   r1   s      r   r!   ToTensorV2.apply_to_maskX   s<     4990L#L>>!Q*D%%r   c                    g N r   r   r1   s      r   r"   ToTensorV2.apply_to_masksj   s    \_r   c                    g r7   r8   r9   s      r   r"   r:   m   s    PSr   c                    [        U[        5      (       a#  U Vs/ s H  o0R                  " U40 UD6PM     sn$ U R                  (       a+  UR                  [
        :X  a  [        R                  " US5      n[        R                  " U5      $ s  snf )aM  Convert numpy array or list of numpy array masks to torch tensor(s).

Args:
    masks (np.ndarray | list[np.ndarray]): Numpy array of shape (N, H, W) or (N, H, W, C),
        or a list of numpy arrays with shape (H, W) or (H, W, C).
    **params (Any): Additional parameters.

Returns:
    torch.Tensor | list[torch.Tensor]: If transpose_mask is True and input is (N, H, W, C),
        returns tensor of shape (N, C, H, W). If transpose_mask is True and input is (H, W, C), r
        eturns a list of tensors with shape (C, H, W). Otherwise, returns tensors with the same shape as input.

)r      r(   r'   )

isinstancelistr!   r   r)   r	   r+   r/   r-   r.   )r   r   r1   r   s       r   r"   r:   p   sp     eT""CHI54&&t6v65II5::1F#FLL5E&&	 Js   Bc                    UR                   [        :w  a  [        SUR                    S35      e[        R                  " UR                  SSSS5      5      $ )z:Convert batch of images from (N, H, W, C) to (N, C, H, W).z!Expected 4D array (N,H,W,C), got D arrayr   r=   r(   r'   )r)   r	   r*   r-   r.   r/   )r   r   r1   s      r   r    ToTensorV2.apply_to_images   sJ    ;;//@WUVV 0 0Aq! <==r   )r   )F      ?)r   boolr   floatreturnzdict[str, Any])r0   
np.ndarrayr1   r   rG   torch.Tensor)r   rH   r1   r   rG   rI   )r   zlist[np.ndarray]r1   r   rG   zlist[torch.Tensor])r   rH   r1   r   rG   rI   )r   znp.ndarray | list[np.ndarray]r1   r   rG   z!torch.Tensor | list[torch.Tensor])r   rH   r1   r   rG   rI   )__name__
__module____qualname____firstlineno____doc__r
   IMAGEMASK_targetsr   propertyr$   r   r!   r   r"   r    __static_attributes____classcell__r   s   @r   r   r      sq    
 w||,H- - 
 
84&$ _ _S S'*> >r   c                     ^  \ rS rSrSr\R                  \R                  4rSS	U 4S jjjr	\
S
S j5       rSS jrSS jrSrU =r$ )r      a  Convert 3D volumes and masks to PyTorch tensors.

This transform is designed for 3D medical imaging data. It converts numpy arrays
to PyTorch tensors and ensures consistent channel positioning.

For all inputs (volumes and masks):
    - Input:  (D, H, W, C) or (D, H, W) - depth, height, width, [channels]
    - Output: (C, D, H, W) - channels first format for PyTorch
             For single-channel input, adds C=1 dimension

Note:
    This transform always moves channels to first position as this is
    the standard PyTorch format. For masks that need to stay in DHWC format,
    use a different transform or handle the transposition after this transform.

Args:
    p (float): Probability of applying the transform. Default: 1.0

c                    > [         TU ]  US9  g r   )r   r   )r   r   r   s     r   r   ToTensor3D.__init__   s    1r   c                4    U R                   U R                  S.$ )zDefine mapping of target name to target function.

Returns:
    dict[str, Any]: Dictionary mapping target names to corresponding transform functions

)volumemask3d)apply_to_volumeapply_to_mask3dr#   s    r   r$   ToTensor3D.targets   s      ****
 	
r   c                ,   UR                   [        :X  a(  [        R                  " UR	                  SSSS5      5      $ UR                   [        S-
  :X  a)  [        R                  " U[
        R                  S4   5      $ [        SUR                    S35      e)z+Convert 3D volume to channels-first tensor.r=   r   r(   r'   .z2Expected 3D or 4D array (D,H,W) or (D,H,W,C), got rA   )r)   r	   r-   r.   r/   r+   newaxisr*   )r   r[   r1   s      r   r]   ToTensor3D.apply_to_volume   s{    ;;//##F$4$4Q1a$@AA;;/!33##F2::s?$;<<Mfkk]Zabccr   c                (    U R                   " U40 UD6$ )z)Convert 3D mask to channels-first tensor.)r]   )r   r\   r1   s      r   r^   ToTensor3D.apply_to_mask3d   s    ##F5f55r   r8   )rC   )r   rE   rF   )r[   rH   r1   r   rG   rI   )r\   rH   r1   r   rG   rI   )rJ   rK   rL   rM   rN   r
   rO   rP   rQ   r   rR   r$   r]   r^   rS   rT   rU   s   @r   r   r      sJ    ( w||,H  

 

d6 6r   )rN   
__future__r   typingr   r   numpyr+   r-   (albumentations.core.transforms_interfacer   $albumentations.core.type_definitionsr   r   r	   r
   __all__r   r   r8   r   r   <module>rk      sL    #     C  
&m> m>`16 16r   