o  i*ã@spddlmZddlZddlZddlmZddlmZddlm Z ddl m Z Gdd„de ƒZ Gd d „d eƒZ dS) é)Ú annotationsN)Ú_Loss)Ú get_act_layer)Ú LossReduction)ÚStrEnumc@seZdZdZdZdZdS)ÚAdversarialCriterionsZbceZhingeÚ least_squaresN)Ú__name__Ú __module__Ú __qualname__ÚBCEÚHINGEÚ LEAST_SQUARE©rrú_/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/losses/adversarial_loss.pyrsrcsVeZdZdZejejdfd‡fd d „ Zddd„Z ddd„Z ddd„Z d dd„Z ‡Z S)!ÚPatchAdversarialLossa- Calculates an adversarial loss on a Patch Discriminator or a Multi-scale Patch Discriminator. Warning: due to the possibility of using different criterions, the output of the discrimination mustn't be passed to a final activation layer. That is taken care of internally within the loss. Args: reduction: {``"none"``, ``"mean"``, ``"sum"``} Specifies the reduction to apply to the output. Defaults to ``"mean"``. - ``"none"``: no reduction will be applied. - ``"mean"``: the sum of the output will be divided by the number of elements in the output. - ``"sum"``: the output will be summed. criterion: which criterion (hinge, least_squares or bce) you want to use on the discriminators outputs. Depending on the criterion, a different activation layer will be used. Make sure you don't run the outputs through an activation layer prior to calling the loss. no_activation_leastsq: if True, the activation layer in the case of least-squares is removed. FÚ reductionúLossReduction | strÚ criterionÚstrÚno_activation_leastsqÚboolÚreturnÚNonecsÐtƒjt|ƒd| ¡ttƒvrtdd t¡ƒ‚d|_d|_ ||tj kr5t dƒ|_ t jj|d|_n+|tjkrCt dƒ|_ d|_ n|tjkr`|rNd|_ n t d d d ifd |_ t jj|d|_||_||_dS) N)rzGUnrecognised criterion entered for Adversarial Loss. Must be one in: %sz, gð?gÚSIGMOIDZTANHgð¿Ú LEAKYRELUÚnegative_slopegš™™™™™©?)Úname)ÚsuperÚ__init__rÚlowerÚlistrÚ ValueErrorÚjoinÚ real_labelÚ fake_labelr rÚ activationÚtorchÚnnÚBCELossÚloss_fctr rÚMSELossrr)Úselfrrr©Ú __class__rrr2s.ÿÿ      zPatchAdversarialLoss.__init__Úinputú torch.TensorÚtarget_is_realcCsJ|r|jn|j}t d¡ |¡ | ¡¡ |dj¡}| d¡|  |¡S)aŸ Gets the ground truth tensor for the discriminator depending on whether the input is real or fake. Args: input: input tensor from the discriminator (output of discriminator, or output of one of the multi-scale discriminator). This is used to match the shape. target_is_real: whether the input is real or wannabe-real (1s) or fake (0s). Returns: érF) r$r%r'ÚtensorÚfill_ÚtypeÚtoÚdeviceÚrequires_grad_Ú expand_as)r,r/r1Z filling_labelZ label_tensorrrrÚget_target_tensorTs &  z&PatchAdversarialLoss.get_target_tensorcCs8t d¡ |d ¡¡ |dj¡}| d¡| |¡S)z— Gets a zero tensor. Args: input: tensor which shape you want the zeros tensor to correspond to. Returns: rF)r'r3r5r6r7r8r9)r,r/Zzero_label_tensorrrrÚget_zero_tensorcs$  z$PatchAdversarialLoss.get_zero_tensorútorch.Tensor | listÚfor_discriminatorú!torch.Tensor | list[torch.Tensor]c Cs$|s |s d}t d¡t|tƒs|g}g}t|ƒD]\}}|jtjkr-| |  ||¡¡q| |  |¡¡qg}t|ƒD]-\}}|j durJ|  |¡}|jtjkr\|s\|  | ||¡} n|  |||¡} | | ¡q<|dur|j tjkr~t t |¡¡} | S|j tjkrŽt t |¡¡} | S|} | S)aL Args: input: output of Multi-Scale Patch Discriminator or Patch Discriminator; being a list of tensors or a tensor; they shouldn't have gone through an activation layer. target_is_real: whereas the input corresponds to discriminator output for real or fake images for_discriminator: whereas this is being calculated for discriminator or generator loss. In the last case, target_is_real is set to True, as the generator wants the input to be dimmed as real. Returns: if reduction is None, returns a list with the loss tensors of each discriminator if multi-scale discriminator is active, or the loss tensor if there is just one discriminator. Otherwise, it returns the summed or mean loss over the tensor and discriminator/s. Tz‘Variable target_is_real has been set to False, but for_discriminator is setto False. To optimise a generator, target_is_real must be set to True.N)ÚwarningsÚwarnÚ isinstancer!Ú enumeraterrr Úappendr:r;r&Ú_forward_singlerrÚMEANr'ÚmeanÚstackÚSUMÚsum) r,r/r1r=Útarget_Ú_Zdisc_outZ loss_listZdisc_indZloss_ÚlossrrrÚforwardps:ÿ       üÿzPatchAdversarialLoss.forwardÚtargetcCsZ|jtjks |jtjkr| ||¡}|S|jtjkr+t |d| |¡¡}t  |¡ }|S)Nr2) rrr rr*r r'Úminr;rF)r,r/rNrMÚminvalrrrrD¦s  ý z$PatchAdversarialLoss._forward_single)rrrrrrrr)r/r0r1rrr0)r/r0rr0)r/r<r1rr=rrr>)r/r0rNr0rr0)r r r Ú__doc__rrErrrr:r;rMrDÚ __classcell__rrr-rrsü "  6r)Ú __future__rr?r'Útorch.nn.modules.lossrÚmonai.networks.layers.utilsrÚ monai.utilsrÚmonai.utils.enumsrrrrrrrÚs