o
    i                      @  s   d dl mZ d dlmZ d dlmZmZ d dlZd dlm	Z	 d dl
mZmZ ddlmZ g d	ZG d
d deZG dd deZG dd deZG dd deZdS )    )annotations)abstractmethod)ceilsqrtN)get_track_meta)convert_to_dst_typeconvert_to_tensor   )RandomizableTransform)MixUpCutMixCutOutMixerc                      s>   e Zd Zdd fdd	ZedddZdd fddZ  ZS )r         ?
batch_sizeintalphafloatreturnNonec                   s0   t    |dkrtd||| _|| _dS )a  
        Mixer is a base class providing the basic logic for the mixup-class of
        augmentations. In all cases, we need to sample the mixing weights for each
        sample (lambda in the notation used in the papers). Also, pairs of samples
        being mixed are picked by randomly shuffling the batch samples.

        Args:
            batch_size (int): number of samples per batch. That is, samples are expected tp
                be of size batchsize x channels [x depth] x height x width.
            alpha (float, optional): mixing weights are sampled from the Beta(alpha, alpha)
                distribution. Defaults to 1.0, the uniform distribution.
        r   z*Expected positive number, but got alpha = N)super__init__
ValueErrorr   r   )selfr   r   	__class__ g/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/transforms/regularization/array.pyr      s
   

zMixer.__init__datatorch.Tensorc                 C  s   t  N)NotImplementedErrorr   r   r   r   r   apply0   s   zMixer.applyNc                   sj   t  d t j j j jtj	 j
 j|dur/ fdd|jdd D ng f _dS )a  
        Sometimes you need may to apply the same transform to different tensors.
        The idea is to get a sample and then apply it with apply() as often
        as needed. You need to call this method everytime you apply the transform to a new
        batch.
        Nc              	     s$   g | ]}t  jjd |ddqS )r      )size)torch
from_numpyRrandint.0dr   r   r   
<listcomp>?   s   $ z#Mixer.randomize.<locals>.<listcomp>r	   )r   	randomizer'   r(   r)   betar   r   typefloat32permutationshape_paramsr"   r   r.   r   r0   4   s
   "&
zMixer.randomize)r   )r   r   r   r   r   r   r   r   r    )r   r   )__name__
__module____qualname__r   r   r#   r0   __classcell__r   r   r   r   r      s
    r   c                   @  s&   e Zd ZdZdddZddd
dZdS )r   a$  MixUp as described in:
    Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz.
    mixup: Beyond Empirical Risk Minimization, ICLR 2018

    Class derived from :py:class:`monai.transforms.Mixer`. See corresponding
    documentation for details on the constructor parameters.
    r   r   c                 C  s   | j \}}}|j^}}t||krtdt| d| t|dvr'td|tfdt|   }|| d| ||df   S )NExpected batch of size: 
, but got )      zUnexpected number of dimensionsr    r%   .r6   r5   lenr   Ellipsis)r   r   weightperm_nsamplesdims	mixweightr   r   r   r#   L   s   
zMixUp.applyNTlabelstorch.Tensor | Nonec                 C  s~   t |t d}|}|d urt |t d}|r|   |d u r)t| ||dd S t| ||dd t| ||dd fS N)
track_meta)dstr   r   r   r0   r   r#   )r   r   rI   r0   data_tlabels_tr   r   r   __call__X   s   zMixUp.__call__r7   NTr   r   rI   rJ   r8   r9   r:   __doc__r#   rQ   r   r   r   r   r   C   s    
r   c                   @  s0   e Zd ZdZdddZdddZddddZd	S )r   a  CutMix augmentation as described in:
        Sangdoo Yun, Dongyoon Han, Seong Joon Oh, Sanghyuk Chun, Junsuk Choe, Youngjoon Yoo.
        CutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features,
        ICCV 2019

        Class derived from :py:class:`monai.transforms.Mixer`. See corresponding
        documentation for details on the constructor parameters. Here, alpha not only determines
        the mixing weight but also the size of the random rectangles used during for mixing.
        Please refer to the paper for details.

        Please note that there is a change in behavior starting from version 1.4.0. In the previous
        implementation, the transform would generate a different label each time it was called.
        To ensure determinism, the new implementation will now generate the same label for
        the same input image when using the same operation.

        The most common use case is something close to:

    .. code-block:: python

        cm = CutMix(batch_size=8, alpha=0.5)
        for batch in loader:
            images, labels = batch
            augimg, auglabels = cm(images, labels)
            output = model(augimg)
            loss = loss_function(output, auglabels)
            ...

    r   r   c                   s   | j \}}}|j^}}}t||krtdt| d| t|}t|D ]#\}	  fdd|D }
td gdd t||
|D  }d||	 |< q'|| d| ||df   S )	Nr<   r=   c                      g | ]
}|t d    qS r$   r   r+   rC   r   r   r/          z CutMix.apply.<locals>.<listcomp>c                 S  *   g | ]\}}}t |tt|| |qS r   sliceminr   r,   clnr-   r   r   r   r/         * r   r%   .	r6   r5   rA   r   r'   	ones_like	enumerater\   zip)r   r   weightsrD   coordsrF   rE   rG   maskslengthsidxr   rX   r   r#      s   
 zCutMix.applyrI   c                 C  sl   | j \}}}|j^}}t||krtdt| d| |tfdt|   }|| d| ||df   S )Nr<   r=   r    r%   .r@   )r   rI   rf   rD   rE   rF   rG   rH   r   r   r   apply_on_labels   s   
zCutMix.apply_on_labelsNTrJ   c                 C  s~   t |t d}d }|d urt |t d}|r| | t| ||dd }|d ur5t| ||dd }|d ur=||fS |S rK   rN   )r   r   rI   r0   rO   Zaugmented_labelrP   Z	augmentedr   r   r   rQ      s   
zCutMix.__call__r7   )rI   r   rR   rS   )r8   r9   r:   rU   r#   rl   rQ   r   r   r   r   r   h   s
    

	r   c                   @  s&   e Zd ZdZd
ddZdd
ddZd	S )r   a  Cutout as described in the paper:
    Terrance DeVries, Graham W. Taylor.
    Improved Regularization of Convolutional Neural Networks with Cutout,
    arXiv:1708.04552

    Class derived from :py:class:`monai.transforms.Mixer`. See corresponding
    documentation for details on the constructor parameters. Here, alpha not only determines
    the mixing weight but also the size of the random rectangles being cut put.
    Please refer to the paper for details.
    r   r   c                   s   | j \}}}|j^}}}t||krtdt| d| t|}t|D ]#\}  fdd|D }	td gdd t||	|D  }
d|| |
< q'|| S )Nr<   r=   c                   rV   r$   rW   r+   rX   r   r   r/      rY   z CutOut.apply.<locals>.<listcomp>c                 S  rZ   r   r[   r^   r   r   r   r/      ra   r   rb   )r   r   rf   rE   rg   rF   rG   rh   ri   rj   rk   r   rX   r   r#      s   
 zCutOut.applyTc                 C  s2   t |t d}|r| | t| ||dd S rK   rN   )r   r   r0   rO   r   r   r   rQ      s   
zCutOut.__call__Nr7   )TrT   r   r   r   r   r      s    
r   )
__future__r   abcr   mathr   r   r'   monai.data.meta_objr   monai.utils.type_conversionr   r   	transformr
   __all__r   r   r   r   r   r   r   r   <module>   s   (%C