U tPh@s`ddlmZddlmZddlZddlmZddlmZm Z ddl m Z m Z GdddeZ dS) ) annotations)SequenceN)_Loss) KernelType SSIMMetric) LossReductionensure_tuple_repc seZdZdZdejddddejfddd d d ddd d fdd Ze ddddZ e j dddddZ ddddddZ 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?intfloatzKernelType | strzint | Sequence[int]zfloat | Sequence[float]zLossReduction | str) spatial_dims data_range kernel_typewin_size kernel_sigmak1k2 reductionc stjt|jd||_||_||_t|ts:t ||}||_ t|tsTt ||}||_ ||_ ||_ 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 rrrrrrN)super__init__rvaluer _data_ranger isinstancerr kernel_sizerrrr ssim_metric) selfr rrrrrrr __class__K/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/losses/ssim_loss.pyr#s*    zSSIMLoss.__init__)returncCs|jSN)r)rrrr rXszSSIMLoss.data_rangeNone)rr!cCs||_||j_dSr")rrr)rrrrr r\sz torch.Tensor)inputtargetr!cCsT|j||dd}d|}|jtjjkr8t|}n|jtj jkrPt |}|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)rr$r%Z ssim_valuelossrrr forwardas  zSSIMLoss.forward)__name__ __module__ __qualname____doc__rGAUSSIANrr)rpropertyrsetterr/ __classcell__rrrr r s"5r ) __future__rcollections.abcrr*torch.nn.modules.lossrZmonai.metrics.regressionrr monai.utilsrrr rrrr  s