U “PÓhHFã@s´ddlmZddlZddlmZedƒ\ZZddddgZGd d„dejj ƒZ Gd d„dejj ƒZ Gd d „d ejj ƒZ Gd d„dej jƒZGdd„dejj ƒZGdd„dej jƒZdS)é)Ú annotationsN)Úoptional_importzmonai._CÚBilateralFilterÚ PHLFilterÚTrainableBilateralFilterÚTrainableJointBilateralFilterc@s*eZdZdZed dd„ƒZedd„ƒZd S) ra: Blurs the input tensor spatially whilst preserving edges. Can run on 1D, 2D, or 3D, tensors (on top of Batch and Channel dimensions). Two implementations are provided, an exact solution and a much faster approximation which uses a permutohedral lattice. See: https://en.wikipedia.org/wiki/Bilateral_filter https://graphics.stanford.edu/papers/permutohedral/ Args: input: input tensor. spatial_sigma: the standard deviation of the spatial blur. Higher values can hurt performance when not using the approximate method (see fast approx). color_sigma: the standard deviation of the color blur. Lower values preserve edges better whilst higher values tend to a simple gaussian spatial blur. fast approx: This flag chooses between two implementations. The approximate method may produce artifacts in some scenarios whereas the exact solution may be intolerably slow for high spatial standard deviations. Returns: output (torch.Tensor): output tensor. éçà?TcCs&||_||_||_t ||||¡}|S)zautograd forward©ÚssÚcsÚfaÚ_CZbilateral_filter)ÚctxÚinputÚ spatial_sigmaÚ color_sigmaÚ fast_approxÚ output_data©rúT/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/layers/filtering.pyÚforward/s zBilateralFilter.forwardcCs2|j|j|j}}}t ||||¡}|dddfS)zautograd backwardNr )rÚ grad_outputrrrÚ grad_inputrrrÚbackward8szBilateralFilter.backwardN)rr T©Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú staticmethodrrrrrrrs  c@s*eZdZdZeddd„ƒZedd„ƒZdS)rab Filters input based on arbitrary feature vectors. Uses a permutohedral lattice data structure to efficiently approximate n-dimensional gaussian filtering. Complexity is broadly independent of kernel size. Most applicable to higher filter dimensions and larger kernel sizes. See: https://graphics.stanford.edu/papers/permutohedral/ Args: input: input tensor to be filtered. features: feature tensor used to filter the input. sigmas: the standard deviations of each feature in the filter. Returns: output (torch.Tensor): output tensor. NcCsX|}|dk r>t| d¡ƒD]"}|dd…|df||<q| |¡t ||¡}|S)Né.)ÚrangeÚsizeÚsave_for_backwardrZ phl_filter)rrÚfeaturesÚsigmasZscaled_featuresÚirrrrrSs   zPHLFilter.forwardcCs tdƒ‚dS)Nz4PHLFilter does not currently support Backpropagation)ÚNotImplementedError)rrrrrr^szPHLFilter.backward)Nrrrrrr@s  c@s(eZdZdZedd„ƒZedd„ƒZdS)Ú TrainableBilateralFilterFunctiona, torch.autograd.Function for the TrainableBilateralFilter layer. See: F. Wagner, et al., Ultralow-parameter denoising: Trainable bilateral filter layers in computed tomography, Medical Physics (2022), https://doi.org/10.1002/mp.15718 Args: input: input tensor to be filtered. sigma x: trainable standard deviation of the spatial filter kernel in x direction. sigma y: trainable standard deviation of the spatial filter kernel in y direction. sigma z: trainable standard deviation of the spatial filter kernel in z direction. color sigma: trainable standard deviation of the intensity range kernel. This filter parameter determines the degree of edge preservation. Returns: output (torch.Tensor): filtered tensor. c CsDt |||||¡\}}}} } } } | ||||||||| | | | ¡ |S©N)rZ tbf_forwardr$) rÚ input_imgÚsigma_xÚsigma_yÚsigma_zrÚ output_tensorÚoutput_weights_tensorÚdo_dx_kiÚ do_dsig_rÚ do_dsig_xÚ do_dsig_yÚ do_dsig_zrrrrzs,ÿôz(TrainableBilateralFilterFunction.forwardc CsØ|jd}|jd}|jd}|jd}|jd}|jd}|jd}|jd} |jd } |jd } |jd } |jd } t || ¡}t || ¡}t || ¡}t || ¡}t ||||| ||||¡ }|||||fS) Nrr!éééréééé é é )Ú saved_tensorsÚtorchÚsumrZ tbf_backward)rrr+r,r-r.rr/r0r1r2r3r4r5Úgrad_color_sigmaÚ grad_sig_xÚ grad_sig_yÚ grad_sig_zÚgrad_output_tensorrrrr‘s8            ÷ z)TrainableBilateralFilterFunction.backwardNrrrrrr)fs  r)cs(eZdZdZ‡fdd„Zdd„Z‡ZS)ra. Implementation of a trainable bilateral filter layer as proposed in the corresponding publication. All filter parameters can be trained data-driven. The spatial filter kernels x, y, and z determine image smoothing whereas the color parameter specifies the amount of edge preservation. Can run on 1D, 2D, or 3D tensors (on top of Batch and Channel dimensions). See: F. Wagner, et al., Ultralow-parameter denoising: Trainable bilateral filter layers in computed tomography, Medical Physics (2022), https://doi.org/10.1002/mp.15718 Args: input: input tensor to be filtered. spatial_sigma: tuple (sigma_x, sigma_y, sigma_z) initializing the trainable standard deviations of the spatial filter kernels. Tuple length must equal the number of spatial input dimensions. color_sigma: trainable standard deviation of the intensity range kernel. This filter parameter determines the degree of edge preservation. Returns: output (torch.Tensor): filtered tensor. cstƒ ¡t|tƒr&|||g}d|_nŠt|ƒdkrH|dddg}d|_nht|ƒdkrn|d|ddg}d|_nBt|ƒdkr˜|d|d|dg}d|_ntd|›d|j›dƒ‚tj   t  |d¡¡|_ tj   t  |d¡¡|_ tj   t  |d¡¡|_tj   t  |¡¡|_dS© Nr7r!rg{®Gáz„?r6zlen(spatial_sigma) z# must match number of spatial dims Ú.©ÚsuperÚ__init__Ú isinstanceÚfloatÚlen_spatial_sigmaÚlenÚ ValueErrorZken_spatial_sigmar@ÚnnÚ ParameterÚtensorr,r-r.Ú sigma_color©Úselfrr©Ú __class__rrrKÌs(      ÿz!TrainableBilateralFilter.__init__cCs¾|jddkr$td|jd›dƒ‚t|jƒ}|dkrH| d¡ d¡}n|dkrZ| d¡}|j|krttd|›dƒ‚t ||j|j|j |j ¡}|dkr¨|  d¡  d¡}n|dkrº|  d¡}|S)Nr!ú!Currently channel dimensions >1 (úh) are not supported. Please use multiple parallel filter layers if you want to filter multiple channels.r7r8úSpatial dimension (ú,) must match initialized len(spatial_sigma).) ÚshaperPrOÚ unsqueezerNr)Úapplyr,r-r.rTÚsqueeze)rVÚ input_tensorÚ len_inputÚ predictionrrrræs.ÿ   ÿ z TrainableBilateralFilter.forward©rrrrrKrÚ __classcell__rrrWrrµs c@s(eZdZdZedd„ƒZedd„ƒZdS)Ú%TrainableJointBilateralFilterFunctiona| torch.autograd.Function for the TrainableJointBilateralFilter layer. See: F. Wagner, et al., Trainable joint bilateral filters for enhanced prediction stability in low-dose CT, Scientific Reports (2022), https://doi.org/10.1038/s41598-022-22530-4 Args: input: input tensor to be filtered. guide: guidance image tensor to be used during filtering. sigma x: trainable standard deviation of the spatial filter kernel in x direction. sigma y: trainable standard deviation of the spatial filter kernel in y direction. sigma z: trainable standard deviation of the spatial filter kernel in z direction. color sigma: trainable standard deviation of the intensity range kernel. This filter parameter determines the degree of edge preservation. Returns: output (torch.Tensor): filtered tensor. cCsHt ||||||¡\}}} } } } } | |||||||| | | | | |¡ |Sr*)rZ tjbf_forwardr$)rr+Ú guidance_imgr,r-r.rr/r0r1r2r3r4r5rrrrs0ÿóz-TrainableJointBilateralFilterFunction.forwardc Csê|jd}|jd}|jd}|jd}|jd}|jd}|jd}|jd} |jd } |jd } |jd } |jd } |jd }t || ¡}t || ¡}t || ¡}t || ¡}t |||||| ||||¡ \}}||||||fS)Nrr!r6r7r8rr9r:r;r<r=r>é )r?r@rArZ tjbf_backward)rrr+r,r-r.rr/r0r1r2r3r4r5rgrBrCrDrErFZgrad_guidance_tensorrrrr3s<             ö z.TrainableJointBilateralFilterFunction.backwardNrrrrrrfs  rfcs(eZdZdZ‡fdd„Zdd„Z‡ZS)ra! Implementation of a trainable joint bilateral filter layer as proposed in the corresponding publication. The guidance image is used as additional (edge) information during filtering. All filter parameters and the guidance image can be trained data-driven. The spatial filter kernels x, y, and z determine image smoothing whereas the color parameter specifies the amount of edge preservation. Can run on 1D, 2D, or 3D tensors (on top of Batch and Channel dimensions). Input tensor shape must match guidance tensor shape. See: F. Wagner, et al., Trainable joint bilateral filters for enhanced prediction stability in low-dose CT, Scientific Reports (2022), https://doi.org/10.1038/s41598-022-22530-4 Args: input: input tensor to be filtered. guide: guidance image tensor to be used during filtering. spatial_sigma: tuple (sigma_x, sigma_y, sigma_z) initializing the trainable standard deviations of the spatial filter kernels. Tuple length must equal the number of spatial input dimensions. color_sigma: trainable standard deviation of the intensity range kernel. This filter parameter determines the degree of edge preservation. Returns: output (torch.Tensor): filtered tensor. cstƒ ¡t|tƒr&|||g}d|_nŠt|ƒdkrH|dddg}d|_nht|ƒdkrn|d|ddg}d|_nBt|ƒdkr˜|d|d|dg}d|_ntd|›d|j›dƒ‚tj   t  |d¡¡|_ tj   t  |d¡¡|_ tj   t  |d¡¡|_tj   t  |¡¡|_dSrGrIrUrWrrrKss(      ÿz&TrainableJointBilateralFilter.__init__cCs|jddkr$td|jd›dƒ‚|j|jkrJtd|j›d|j›dƒ‚t|jƒ}|dkr~| d¡ d¡}| d¡ d¡}n|dkrš| d¡}| d¡}|j|kr´td |›d ƒ‚t |||j|j|j |j ¡}|dkrê|  d¡  d¡}n|dkrü|  d¡}|S) Nr!rYrZz s    )&OQS