o  i@sddlmZddlmZddlmZmZddlZddlm Z ddl m Z ddl m Z e dd d \ZZgd ZGd d d ejjZGdddejjZGdddZGdddeZGdddeZGdddeZdS)) annotations)partial)AnyCallableN)replace_modules_temp)optional_import)ModelWithHookstqdmtrange)name) VanillaGrad SmoothGradGuidedBackpropGradGuidedBackpropSmoothGradc@s$eZdZeddZeddZdS) _AutoGradReLUcCs*|dk|}t||}||||SNr)type_astorchmulsave_for_backward)ctxxpos_maskoutputr`/home/dell461/cl/sdc2/last_ska_mid/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@seZdZdZdddZdS) _GradReLUzx A customized ReLU with the backward pass imputed for guided backpropagation (https://arxiv.org/abs/1412.6806). r torch.TensorreturncCst|}|SN)rapply)selfroutrrrr4s z_GradReLU.forwardN)rr(r)r()r#r$r%__doc__rrrrrr'/sr'c@sNeZdZdZdddZedd Zejd d Z ddddZddddZ 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) modeltorch.nn.Moduler)NonecCs(t|tst|ddd|_dS||_dS)NrT)target_layer_namesregister_backward) isinstancer_model)r,r/rrr__init__Gs  zVanillaGrad.__init__cCs|jjSr*)r5r/)r,rrrr/MszVanillaGrad.modelcCs t|ts ||j_dS||_dSr*)r4rr5r/)r,mrrrr/Qs   Trr(indextorch.Tensor | int | None retain_graphboolkwargsrcKsB|jddkr tdd|_|j|f||d||j}|S)Nrzexpect batch size of 1T) class_idxr:)shape ValueError requires_gradr5graddetach)r,rr8r:r<rBrrrget_gradXs  zVanillaGrad.get_gradNcKs|j||fi|Sr*)rDr,rr8r<rrr__call__cszVanillaGrad.__call__)r/r0r)r1)T) rr(r8r9r:r;r<rr)r(r*rr(r8r9r<rr)r() r#r$r%r.r6propertyr/setterrDrFrrrrr 9s     r cs8eZdZdZ    ddfdd ZddddZZS)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/r0 stdev_spreadfloat n_samplesint magnituder;verboser)r1csPt|||_||_||_||r#tr#ttd|jj d|_ dSt |_ dS)Nz Computing )desc) superr6rLrNrP has_trangerr __class__r#range)r,r/rLrNrPrQrUrrr6qs  zSmoothGrad.__init__Nrr(r8r9r<rc Ks|j||}t|}||jD],}tjd||j tj |j d}||}| }|j ||fi|} ||jrA| | n| 7}q|jrL|d}||jS)Nr)sizedtypedeviceg?)rLmaxminitemr zeros_likerVrNnormalr?float32rZrCrDrP) r,rr8r<stdevZtotal_gradientsrnoiseZ x_plus_noiserBrrrrFs  zSmoothGrad.__call__)rJrKTT) r/r0rLrMrNrOrPr;rQr;r)r1r*rG)r#r$r%r.r6rF __classcell__rrrWrr gs r c$eZdZdZd d fd d 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) Nrr(r8r9r<rr)c Nt|jdtddtj||fi|WdS1s wYdSNreluF) strict_matchrr/r'rSrFrErWrrrF$zGuidedBackpropGrad.__call__r*rGr#r$r%r.rFrcrrrWrrs rcrd)rzg Compute gradient-based saliency maps based on both ``GuidedBackpropGrad`` and ``SmoothGrad``. Nrr(r8r9r<rr)c rerfrirErWrrrFrjz!GuidedBackpropSmoothGrad.__call__r*rGrkrrrWrrsr) __future__r functoolsrtypingrrrmonai.networks.utilsrmonai.utils.modulerZ%monai.visualize.class_activation_mapsrr rT__all__autogradFunctionrnnModuler'r r rrrrrrs     .0