U žPÓhã ã@sTddlmZddlmZddlZddlmZGdd„deƒZddddd œd d „ZdS) é)Ú annotations)ÚCallableN)ÚMetriccs<eZdZdZd dddœ‡fdd„ Zdddd œd d „Z‡ZS) Ú MMDMetricaÅ Unbiased Maximum Mean Discrepancy (MMD) is a kernel-based method for measuring the similarity between two distributions. It is a non-negative metric where a smaller value indicates a closer match between the two distributions. Gretton, A., et al,, 2012. A kernel two-sample test. The Journal of Machine Learning Research, 13(1), pp.723-773. Args: y_mapping: Callable to transform the y tensors before computing the metric. It is usually a Gaussian or Laplace filter, but it can be any function that takes a tensor as input and returns a tensor as output such as a feature extractor or an Identity function., e.g. `y_mapping = lambda x: x.square()`. NúCallable | NoneÚNone)Ú y_mappingÚreturncstƒ ¡||_dS©N)ÚsuperÚ__init__r)Úselfr©Ú __class__©úF/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/metrics/mmd.pyr #s zMMDMetric.__init__ú torch.Tensor)ÚyÚy_predr cCst|||jƒSr )Ú compute_mmdr)r rrrrrÚ__call__'szMMDMetric.__call__)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__r rÚ __classcell__rrrrrs rrr)rrrr cCs‚|jddks|jddkr$tdƒ‚|dk r<||ƒ}||ƒ}|j|jkr`td|j›d|j›ƒ‚tt|jƒdddƒD]}|j|d}|j|d}qv| |jdd¡}| |jdd¡}t || ¡¡}t || ¡¡}t || ¡¡}|jd}|jd}d||d} t  |t  t  |¡¡¡} d||d} t  |t  t  |¡¡¡} d ||} t  |¡}| | | | | |}|S) a- Args: y: first sample (e.g., the reference image). Its shape is (B,C,W,H) for 2D data and (B,C,W,H,D) for 3D. y_pred: second sample (e.g., the reconstructed image). It has similar shape as y. y_mapping: Callable to transform the y tensors before computing the metric. réz9MMD metric requires at least two samples in y and y_pred.Nz[y_pred and y shapes dont match after being processed by their transforms, received y_pred: z and y: éÿÿÿÿ)Údimé) ÚshapeÚ ValueErrorÚrangeÚlenÚsqueezeÚviewÚtorchÚmmÚtÚsumÚdiagÚdiagonal)rrrÚdZy_yZ y_pred_y_predZy_pred_yÚmÚnÚc1ÚaÚc2ÚbÚc3ÚcÚmmdrrrr+s6 ÿ     r) Ú __future__rÚcollections.abcrr&Zmonai.metrics.metricrrrrrrrÚ s