U Ph@sddlmZddlmZddlmZmZddlZddlm Z ddd d gZ Gd d d e Z Gd dde Z Gd dde Z Gdd d e ZdS)) annotations)abstractmethod)ceilsqrtN)RandomizableTransformMixUpCutMixCutOutMixercsJeZdZdddddfdd Zedd d d Zddd ddZZS)r ?intfloatNone) batch_sizealphareturncs0t|dkr td|||_||_dS)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. rz*Expected positive number, but got alpha = N)super__init__ ValueErrorrr)selfrr __class__Z/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/transforms/regularization/array.pyrs zMixer.__init__ torch.TensordatacCs tdSN)NotImplementedErrorrrrrrapply-sz Mixer.applyN)rcCs8t|j|j|j|jtj|j|jf|_ dS)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. N) torch from_numpyRbetarrtypefloat32 permutation_paramsr rrr randomize1s" zMixer.randomize)r )N)__name__ __module__ __qualname__rrr!r* __classcell__rrrrr sc@s0eZdZdZddddZd dddd d ZdS) ra$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. rrcCs~|j\}}|j^}}t||kr8tdt|d|t|dkrLtd|tfdt|}||d|||dfS)NExpected batch of size: , but got )zUnexpected number of dimensionsr.r)shapelenrEllipsis)rrweightpermnsamplesdims mixweightrrrr!Gs    z MixUp.applyNtorch.Tensor | NonerlabelscCs.||dkr||S||||fSrr*r!)rrr?rrr__call__Ss zMixUp.__call__)Nr+r,r-__doc__r!rArrrrr>s c@s>eZdZdZddddZddddZddd d d d Zd S)r aCutMix 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. 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) ... rrc s|j\}}|j^}}}t||kr:tdt|d|t|}t|D]T\}dd|D} fdd|D} tdgddt| | |D} d||| <qL||d|||d fS) Nr/r0cSsg|]}tjd|ddqSrr3)sizer"randint.0drrr {sz CutMix.apply..csg|]}|tdqSrErrIr8rrrL|scSs*g|]"\}}}t|tt|||qSrsliceminrrJclnrKrrrrL}srr3. r)r5r6rr" ones_like enumeraterPzip) rrweightsr9r:_r;maskscoordslengthsidxrrNrr!ss     z CutMix.apply)r?cCsj|j\}}|j^}}t||kr8tdt|d||tfdt|}||d|||dfS)Nr/r0rr3.r4)rr?rYr9r:r;r<rrrapply_on_labelss    zCutMix.apply_on_labelsNr=r>cCs,|||}|dk r(|||fS|Sr)r*r!r`)rrr?Z augmentedrrrrAs zCutMix.__call__)N)r+r,r-rCr!r`rArrrrr Zs c@s,eZdZdZddddZddddZdS) r aCutout 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. rrc s|j\}}|j^}}}t||kr:tdt|d|t|}t|D]T\}dd|D}fdd|D} tdgddt|| |D} d||| <qL||S)Nr/r0cSsg|]}tjd|ddqSrDrGrIrrrrLsz CutOut.apply..csg|]}|tdqSrErMrIrNrrrLscSs*g|]"\}}}t|tt|||qSrrOrRrrrrLsrrU) rrrYrZr:r;r[r\r]r^r_rrNrr!s     z CutOut.applycCs|||Srr@r rrrrAszCutOut.__call__NrBrrrrr s ) __future__rabcrmathrrr" transformr__all__r rr r rrrr s    &7