U Ph@sddlmZddlmZddlmZmZddlZddlm Z ddl m Z ddl m Z e dd d \ZZd d d dgZGdddejjZGdddejjZGdd d ZGdd d eZGdd d eZGdddeZdS)) annotations)partial)AnyCallableN)replace_modules_temp)optional_import)ModelWithHookstqdmtrange)name VanillaGrad SmoothGradGuidedBackpropGradGuidedBackpropSmoothGradc@s$eZdZeddZeddZdS) _AutoGradReLUcCs*|dk|}t||}||||SNr)type_astorchmulsave_for_backward)ctxxpos_maskoutputrS/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/visualize/gradient_based.pyforwards  z_AutoGradReLU.forwardcCsB|j\}}|dk|}|dk|}t||}t||}|Sr) saved_tensorsrrr)r grad_outputr_Z pos_mask_1Z pos_mask_2y grad_inputrrrbackward%s    z_AutoGradReLU.backwardN)__name__ __module__ __qualname__ staticmethodrr"rrrrrs rc@s eZdZdZdddddZdS) _GradReLUzx A customized ReLU with the backward pass imputed for guided backpropagation (https://arxiv.org/abs/1412.6806). torch.Tensor)rreturncCst|}|SN)rapply)selfroutrrrr4s z_GradReLU.forwardN)r#r$r%__doc__rrrrrr'/sr'c@sheZdZdZdddddZeddZejd dZdd d d dd dddZdd d dd dddZ dS)r a Given an input image ``x``, calling this class will perform the forward pass, then set to zero all activations except one (defined by ``index``) and propagate back to the image to achieve a gradient-based saliency map. If ``index`` is None, argmax of the output logits will be used. See also: - Simonyan et al. Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps (https://arxiv.org/abs/1312.6034) torch.nn.ModuleNone)modelr)cCs&t|tst|ddd|_n||_dS)NrT)target_layer_namesregister_backward) isinstancer_model)r,r1rrr__init__Gs zVanillaGrad.__init__cCs|jjSr*)r5r1)r,rrrr1MszVanillaGrad.modelcCst|ts||j_n||_dSr*)r4rr5r1)r,mrrrr1Qs  Tr(torch.Tensor | int | Noneboolr)rindex retain_graphkwargsr)cKsB|jddkrtdd|_|j|f||d||j}|S)Nrzexpect batch size of 1T) class_idxr;)shape ValueError requires_gradr5graddetach)r,rr:r;r<rBrrrget_gradXs  zVanillaGrad.get_gradNrr:r<r)cKs|j||f|Sr*)rDr,rr:r<rrr__call__cszVanillaGrad.__call__)T)N) r#r$r%r.r6propertyr1setterrDrGrrrrr 9s    csHeZdZdZddddddd d fd d ZddddddddZZS)r z Compute averaged sensitivity map based on ``n_samples`` (Gaussian additive) of noisy versions of the input image ``x``. See also: - Smilkov et al. SmoothGrad: removing noise by adding noise https://arxiv.org/abs/1706.03825 333333?Tr/floatintr9r0)r1 stdev_spread n_samples magnitudeverboser)csNt|||_||_||_||rDtrDttd|jj d|_ nt |_ dS)Nz Computing )desc) superr6rNrOrP has_trangerr __class__r#range)r,r1rNrOrPrQrUrrr6qs zSmoothGrad.__init__Nr(r8rrEc Ks|j||}t|}||jD]T}tjd||j tj |j d}||}| }|j ||f|} ||jr~| | n| 7}q0|jr|d}||jS)Nr)sizedtypedeviceg?)rNmaxminitemr zeros_likerVrOnormalr?float32rZrCrDrP) r,rr:r<stdevZtotal_gradientsrnoiseZ x_plus_noiserBrrrrGs zSmoothGrad.__call__)rJrKTT)N)r#r$r%r.r6rG __classcell__rrrWrr gs cs.eZdZdZd dddddfdd ZZS) rag Based on Springenberg and Dosovitskiy et al. https://arxiv.org/abs/1412.6806, compute gradient-based saliency maps by backpropagating positive gradients and inputs (see ``_AutoGradReLU``). See also: - Springenberg and Dosovitskiy et al. Striving for Simplicity: The All Convolutional Net (https://arxiv.org/abs/1412.6806) Nr(r8rrEc s>t|jdtdd tj||f|W5QRSQRXdSNreluF) strict_matchrr1r'rSrGrFrWrrrGszGuidedBackpropGrad.__call__)Nr#r$r%r.rGrcrrrWrrs cs.eZdZdZd dddddfdd ZZS) rzg Compute gradient-based saliency maps based on both ``GuidedBackpropGrad`` and ``SmoothGrad``. Nr(r8rrEc s>t|jdtdd tj||f|W5QRSQRXdSrdrgrFrWrrrGsz!GuidedBackpropSmoothGrad.__call__)NrhrrrWrrs) __future__r functoolsrtypingrrrmonai.networks.utilsrmonai.utils.modulerZ%monai.visualize.class_activation_mapsrr rT__all__autogradFunctionrnnModuler'r r rrrrrr s       .0