U jPh*@spddlmZddlZddlZddlmZddlmZddlm Z ddl m Z Gddde Z Gd d d eZ dS) ) annotationsN)_Loss) get_act_layer) LossReduction)StrEnumc@seZdZdZdZdZdS)AdversarialCriterionsZbceZhinge least_squaresN)__name__ __module__ __qualname__BCEHINGE LEAST_SQUARErrR/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/losses/adversarial_loss.pyrsrcseZdZdZejejdfdddddfdd Zd dd d d d Z d d dddZ dddddddZ d d d dddZ 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. FzLossReduction | strstrboolNone) reduction criterionno_activation_leastsqreturncstjt|d|ttkr4tddtd|_d|_ ||tj krjt d|_ t jj|d|_nV|tjkrt d|_ d|_ n:|tjkr|rd|_ nt d d d ifd |_ t jj|d|_||_||_dS) N)rzGUnrecognised criterion entered for Adversarial Loss. Must be one in: %sz, g?gSIGMOIDZTANHg LEAKYRELUnegative_slopeg?)name)super__init__rlowerlistr ValueErrorjoin real_label fake_labelr r activationtorchnnBCELossloss_fctr rMSELossrr)selfrrr __class__rrr2s.     zPatchAdversarialLoss.__init__z torch.Tensor)inputtarget_is_realrcCsJ|r |jn|j}td|||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&tensorfill_typetodevicerequires_grad_ expand_as)r+r.r/Z filling_labelZ label_tensorrrrget_target_tensorTs & z&PatchAdversarialLoss.get_target_tensor)r.rcCs8td|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&r1r3r4r5r6r7)r+r.Zzero_label_tensorrrrget_zero_tensorcs $ z$PatchAdversarialLoss.get_zero_tensorztorch.Tensor | listz!torch.Tensor | list[torch.Tensor])r.r/for_discriminatorrc Cs$|s|sd}tdt|ts&|g}g}t|D]8\}}|jtjkrZ|| ||q2|| |q2g}t|D]Z\}}|j dk r| |}|jtjkr|s| | ||} n| |||} || qx|dk r |j tjkrtt|} n$|j tjkrtt|} n|} | 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. TzVariable 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)warningswarn isinstancer enumeraterrr appendr8r9r%_forward_singlerrMEANr&meanstackSUMsum) r+r.r/r:target__Zdisc_outZ loss_listZdisc_indZloss_lossrrrforwardps6       zPatchAdversarialLoss.forward)r.targetrcCsX|jtjks|jtjkr&|||}n.|jtjkrTt|d||}t | }|S)Nr0) rrr rr)r r&minr9rB)r+r.rJrIminvalrrrr@s   z$PatchAdversarialLoss._forward_single)r r r __doc__rrArrrr8r9rIr@ __classcell__rrr,rrs" 6r) __future__rr;r&torch.nn.modules.lossrmonai.networks.layers.utilsr monai.utilsrmonai.utils.enumsrrrrrrr s