o  i@s`ddlmZddlmZddlZddlmZddlmZm Z ddl m Z m Z GdddeZ dS) ) annotations)SequenceN)_Loss) KernelType SSIMMetric) LossReductionensure_tuple_repcs^eZdZdZdejddddejfd"fdd Ze d#ddZ e j d$ddZ d%d d!Z Z S)&SSIMLossa} Compute the loss function based on the Structural Similarity Index Measure (SSIM) Metric. For more info, visit https://vicuesoft.com/glossary/term/ssim-ms-ssim/ SSIM reference paper: Wang, Zhou, et al. "Image quality assessment: from error visibility to structural similarity." IEEE transactions on image processing 13.4 (2004): 600-612. g? g?g{Gz?gQ? spatial_dimsint data_rangefloat kernel_typeKernelType | strwin_sizeint | Sequence[int] kernel_sigmafloat | Sequence[float]k1k2 reductionLossReduction | strc stjt|jd||_||_||_t|tst ||}||_ t|ts*t ||}||_ ||_ ||_ t|j|j|j|j |j |j |j d|_dS)ab Args: spatial_dims: number of spatial dimensions of the input images. data_range: value range of input images. (usually 1.0 or 255) kernel_type: type of kernel, can be "gaussian" or "uniform". win_size: window size of kernel kernel_sigma: standard deviation for Gaussian kernel. k1: stability constant used in the luminance denominator k2: stability constant used in the contrast denominator 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. )r)r r rrrrrN)super__init__rvaluer _data_ranger isinstancerr kernel_sizerrrr ssim_metric) selfr r rrrrrr __class__X/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/losses/ssim_loss.pyr#s*     zSSIMLoss.__init__returncCs|jSN)r)r r#r#r$r XszSSIMLoss.data_rangerNonecCs||_||j_dSr&)rrr )r rr#r#r$r \s input torch.TensortargetcCsV|j||dd}d|}|jtjjkrt|}|S|jtj jkr)t |}|S)a Args: input: batch of predicted images with shape (batch_size, channels, spatial_dim1, spatial_dim2[, spatial_dim3]) target: batch of target images with shape (batch_size, channels, spatial_dim1, spatial_dim2[, spatial_dim3]) Returns: 1 minus the ssim index (recall this is meant to be a loss function) Example: .. code-block:: python import torch # 2D data x = torch.ones([1,1,10,10])/2 y = torch.ones([1,1,10,10])/2 print(1-SSIMLoss(spatial_dims=2)(x,y)) # pseudo-3D data x = torch.ones([1,5,10,10])/2 # 5 could represent number of slices y = torch.ones([1,5,10,10])/2 print(1-SSIMLoss(spatial_dims=2)(x,y)) # 3D data x = torch.ones([1,1,10,10,10])/2 y = torch.ones([1,1,10,10,10])/2 print(1-SSIMLoss(spatial_dims=3)(x,y)) ) rZ_compute_tensorviewrrMEANrtorchmeanSUMsum)r r(r*Z ssim_valuelossr#r#r$forwardas  zSSIMLoss.forward)r r r rrrrrrrrrrrrr)r%r)rrr%r')r(r)r*r)r%r))__name__ __module__ __qualname____doc__rGAUSSIANrr.rpropertyr setterr4 __classcell__r#r#r!r$r s5  r ) __future__rcollections.abcrr/torch.nn.modules.lossrZmonai.metrics.regressionrr monai.utilsrrr r#r#r#r$s