o  i @sddlmZddlmZddlmZmZddlZddlm Z ddl m Z m Z ddl mZgd ZGd d d eZGd d d eZGdddeZGdddeZdS)) annotations)abstractmethod)ceilsqrtN)get_track_meta)convert_to_dst_typeconvert_to_tensor)RandomizableTransform)MixUpCutMixCutOutMixercs>eZdZddfdd Zedd d Zddfdd ZZS)r? batch_sizeintalphafloatreturnNonecs0t|dkrtd|||_||_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__g/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/transforms/regularization/array.pyrs  zMixer.__init__data torch.TensorcCstN)NotImplementedErrorrrrrrapply0sz Mixer.applyNcsjtdtjjjjtj j j|dur/fdd|j ddDngf_ 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. Nc s$g|]}tjjd|ddqS)r)size)torch from_numpyRrandint.0drrr ?s$z#Mixer.randomize..r ) r randomizer'r(r)betarrtypefloat32 permutationshape_paramsr"rr.rr04s " & zMixer.randomize)r)rrrrrrrrr )rr)__name__ __module__ __qualname__rrr#r0 __classcell__rrrrrs  rc@s&eZdZdZd ddZd dd d ZdS)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. rrcCs|j\}}}|j^}}t||krtdt|d|t|dvr'td|tfdt|}||d|||dfS)NExpected batch of size: , but got )zUnexpected number of dimensionsr r%.r6r5lenrEllipsis)rrweightperm_nsamplesdims mixweightrrrr#Ls    z MixUp.applyNTlabelstorch.Tensor | NonecCs~t|td}|}|durt|td}|r||dur)t|||ddSt|||ddt|||ddfSN) track_meta)dstrrrr0rr#)rrrIr0data_tlabels_trrr__call__XszMixUp.__call__r7NTrrrIrJr8r9r:__doc__r#rQrrrrr Cs  r c@s0eZdZdZdddZdddZddd 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. 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) ... rrc s|j\}}}|j^}}}t||krtdt|d|t|}t|D]#\} fdd|D} tdgddt|| |D} d|| | <q'||d|||dfS) Nr<r=cg|] }|tdqSr$rr+rCrrr/z CutMix.apply..cS*g|]\}}}t|tt|||qSrsliceminrr,clnr-rrrr/*rr%. r6r5rArr' ones_like enumerater\zip) rrweightsrDcoordsrFrErGmaskslengthsidxrrXrr#s     z CutMix.applyrIcCsl|j\}}}|j^}}t||krtdt|d||tfdt|}||d|||dfS)Nr<r=r r%.r@)rrIrfrDrErFrGrHrrrapply_on_labelss   zCutMix.apply_on_labelsNTrJcCs~t|td}d}|durt|td}|r||t|||dd}|dur5t|||dd}|dur=||fS|SrKrN)rrrIr0rOZaugmented_labelrPZ augmentedrrrrQs zCutMix.__call__r7)rIrrRrS)r8r9r:rUr#rlrQrrrrr hs    r c@s&eZdZdZd ddZd d ddZd S) 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||krtdt|d|t|}t|D]#\}fdd|D} tdgddt|| |D} d||| <q'||S)Nr<r=crVr$rWr+rXrrr/rYz CutOut.apply..cSrZrr[r^rrrr/rarrb) rrrfrErgrFrGrhrirjrkrrXrr#s     z CutOut.applyTcCs2t|td}|r||t|||ddSrKrN)rrr0rOrrrrQs zCutOut.__call__Nr7)TrTrrrrr s r ) __future__rabcrmathrrr'monai.data.meta_objrmonai.utils.type_conversionrr transformr __all__rr r r rrrrs   (%C