o
    پi)#                     @   s   d Z ddlZddlZddlmZmZmZmZ ddlZdddddd	eej	 d
e
de
de
dedeeej	 ee
 eeef f fddZdddej	dede
dej	fddZdddej	deeef dee
 dede
dej	fddZG dd dZdS )u  Variable‑size Mixup / CutMix utilities for NaFlex data loaders.

This module provides:

* `mix_batch_variable_size` – pixel‑level Mixup/CutMix that operates on a
  list of images whose spatial sizes differ, mixing only their central overlap
  so no resizing is required.
* `pairwise_mixup_target` – builds soft‑label targets that exactly match the
  per‑sample pixel provenance produced by the mixer.
* `NaFlexMixup` – a callable functor that wraps the two helpers and stores
  all augmentation hyper‑parameters in one place, making it easy to plug into
  different dataset wrappers.

Hacked together by / Copyright 2025, Ross Wightman, Hugging Face
    N)DictListTupleUnion皙?      ?      ?   mixup_alphacutmix_alphaswitch_problocal_shuffleimgsr   r   r   r   returnc          /         s  t  dk r
td|dkr"|dkr"td |k }|r|n|}n|dkr+d}|}n|dkr4d}|}ntdtj||  }tdt	d|}t
tt   fd	d
d}|dkrstdt ||D ]}	t||	|	|   qei }
t|ddd |ddd D ]\}}||
|< ||
|< qt  d r|d nd}dgt   }dgt   }tt  D ]}||kr | ||< q|
| } |  | }}|j\}}}|j\}}}|| }t	||t	||}}|| }|| d || d }}|| d || d }} | }|rwtd| }!t||! t||! }"}#|"|# }$td||" }%td||# }&||% ||& }'}(||% | |& })}*|dd|)|)|" |*|*|# f |dd|'|'|" |(|(|# f< |||< d|$t|  }+|+||< q|dd||| ||| f },|dd||| | | | f }-|,|j|-d| d}.|.|dd||| ||| f< |||< || | || |  }+|+||< q|||
fS )aO  Apply Mixup or CutMix on a batch of variable-sized images.

    Sorts images by aspect ratio and pairs neighboring samples. Only the mutual
    central overlap region of each pair is mixed.

    Args:
        imgs: List of transformed images shaped (C, H, W).
        mixup_alpha: Beta distribution alpha for Mixup. Set to 0 to disable.
        cutmix_alpha: Beta distribution alpha for CutMix. Set to 0 to disable.
        switch_prob: Probability of using CutMix when both modes are enabled.
        local_shuffle: Size of local windows for shuffling after aspect sorting.

    Returns:
        Tuple of (mixed_imgs, lam_list, pair_to) where:
            - mixed_imgs: List of mixed images
            - lam_list: Per-sample lambda values representing mixing degree
            - pair_to: Mapping i -> j of which sample was mixed with which
       z1Need at least two images to perform Mixup/CutMix.         FTu=   Both mixup_alpha and cutmix_alpha are zero – nothing to do.r   c                    s    |  j d  |  j d  S )Nr      )shape)ir   r   J/home/ubuntu/.local/lib/python3.10/site-packages/timm/data/naflex_mixup.py<lambda>E   s    z)mix_batch_variable_size.<locals>.<lambda>)keyr   r   Nalpha)len
ValueErrortorchranditemdistributionsBetasamplemaxminsortedrangerandomshufflezipr   clonemathsqrtintrandintfloatmuladd_)/r   r   r   r   r   
use_cutmixr   lam_raworderstartpair_toabodd_one
mixed_imgslam_listr   jxixj_hiwihjwj	dest_areaohowoverlap_areatop_ileft_itop_jleft_j	cut_ratiochcwcut_areay_offx_offyl_ixl_iyl_jxl_jcorrected_lampatch_ipatch_jblendedr   r   r   mix_batch_variable_size   sv   &
@
"""

r]   r   )	smoothingtargetsnum_classesr^   c                C   sL   || }d| | }t j| d|f|t j| jd}|d| d| |S )Nr   r   )dtypedevicer   )r    fullsizefloat32rb   scatter_	unsqueeze)r_   r`   r^   off_valon_valy_onehotr   r   r   smoothed_sparse_target   s   rk   r9   r>   c          	      C   sT   t | ||d}| } | D ]\}}|| }| | |j|| d| d q| S )u  Create soft targets that match the pixel‑level mixing performed.

    Args:
        targets: (B,) tensor of integer class indices.
        pair_to: Mapping of sample index to its mixed partner as returned by mix_batch_variable_size().
        lam_list: Per‑sample fractions of own pixels, also from the mixer.
        num_classes: Total number of classes in the dataset.
        smoothing: Label‑smoothing value in the range [0, 1).

    Returns:
        Tensor of shape (B, num_classes) whose rows sum to 1.
    r`   r^   r   r   )rk   r-   itemsmul_r4   )	r_   r9   r>   r`   r^   rj   r   r?   lamr   r   r   pairwise_mixup_target   s   "rp   c                   @   s|   e Zd ZdZdddddddded	ed
edededededdfddZdeej	 dej	de
eej	 eej	 f fddZdS )NaFlexMixupz<Callable wrapper that combines mixing and target generation.r   r   r   r	   r   )r   r   r   probr   label_smoothingr`   r   r   r   rr   r   rs   r   Nc                C   s.   || _ || _|| _|| _|| _|| _|| _dS )u4  Configure the augmentation.

        Args:
            num_classes: Total number of classes.
            mixup_alpha: Beta α for Mixup. 0 disables Mixup.
            cutmix_alpha: Beta α for CutMix. 0 disables CutMix.
            switch_prob: Probability of selecting CutMix when both modes are enabled.
            prob: Probability of applying any mixing per batch.
            local_shuffle: Window size used to shuffle images after aspect sorting so pairings vary between epochs.
            smoothing: Label‑smoothing value. 0 disables smoothing.
        N)r`   r   r   r   rr   r   r^   )selfr`   r   r   r   rr   r   rs   r   r   r   __init__   s   
zNaFlexMixup.__init__r   r_   c                 C   s   t |tjst|}t | jkr"t|| j| jd}||	dfS t
|| j| j| j| jd\}}}t|||| j| jd}||	dfS )u  Apply the augmentation and generate matching targets.

        Args:
            imgs: List of already transformed images shaped (C, H, W).
            targets: Hard labels with shape (B,).

        Returns:
            mixed_imgs: List of mixed images in the same order and shapes as the input.
            targets: Soft‑label tensor shaped (B, num_classes) suitable for cross‑entropy with soft targets.
        rl   r   r
   )
isinstancer    Tensortensorr*   rr   rk   r`   r^   unbindr]   r   r   r   r   rp   )rt   r   r_   r=   r>   r9   r   r   r   __call__   s(   
zNaFlexMixup.__call__)__name__
__module____qualname____doc__r0   r2   ru   r   r    rw   r   rz   r   r   r   r   rq      s@    	

rq   )r~   r.   r*   typingr   r   r   r   r    rw   r2   r0   r]   rk   rp   rq   r   r   r   r   <module>   s^    
q


