mmagic.evaluation.metrics.ms_ssim
¶
Module Contents¶
Classes¶
MS-SSIM (Multi-Scale Structure Similarity) metric. |
Functions¶
|
Return a circular symmetric gaussian kernel. |
|
Downsample images with factor equal to 0.5. |
|
Calculate SSIM (structural similarity) and contrast sensitivity. |
|
Calculate MS-SSIM (multi-scale structural similarity). |
- mmagic.evaluation.metrics.ms_ssim._f_special_gauss(size, sigma)[source]¶
Return a circular symmetric gaussian kernel.
Ref: https://github.com/tkarras/progressive_growing_of_gans/blob/master/metrics/ms_ssim.py # noqa
- Parameters
size (int) – Size of Gaussian kernel.
sigma (float) – Standard deviation for Gaussian blur kernel.
- Returns
Gaussian kernel.
- Return type
ndarray
- mmagic.evaluation.metrics.ms_ssim._hox_downsample(img)[source]¶
Downsample images with factor equal to 0.5.
Ref: https://github.com/tkarras/progressive_growing_of_gans/blob/master/metrics/ms_ssim.py # noqa
- Parameters
img (ndarray) – Images with order “NHWC”.
- Returns
Downsampled images with order “NHWC”.
- Return type
ndarray
- mmagic.evaluation.metrics.ms_ssim._ssim_for_multi_scale(img1, img2, max_val=255, filter_size=11, filter_sigma=1.5, k1=0.01, k2=0.03)[source]¶
Calculate SSIM (structural similarity) and contrast sensitivity.
Ref: Image quality assessment: From error visibility to structural similarity.
The results are the same as that of the official released MATLAB code in https://ece.uwaterloo.ca/~z70wang/research/ssim/.
For three-channel images, SSIM is calculated for each channel and then averaged.
This function attempts to match the functionality of ssim_index_new.m by Zhou Wang: http://www.cns.nyu.edu/~lcv/ssim/msssim.zip
- Parameters
img1 (ndarray) – Images with range [0, 255] and order “NHWC”.
img2 (ndarray) – Images with range [0, 255] and order “NHWC”.
max_val (int) – the dynamic range of the images (i.e., the difference between the maximum the and minimum allowed values). Default to 255.
filter_size (int) – Size of blur kernel to use (will be reduced for small images). Default to 11.
filter_sigma (float) – Standard deviation for Gaussian blur kernel (will be reduced for small images). Default to 1.5.
k1 (float) – Constant used to maintain stability in the SSIM calculation (0.01 in the original paper). Default to 0.01.
k2 (float) – Constant used to maintain stability in the SSIM calculation (0.03 in the original paper). Default to 0.03.
- Returns
Pair containing the mean SSIM and contrast sensitivity between img1 and img2.
- Return type
tuple
- mmagic.evaluation.metrics.ms_ssim.ms_ssim(img1, img2, max_val=255, filter_size=11, filter_sigma=1.5, k1=0.01, k2=0.03, weights=None, reduce_mean=True) numpy.ndarray [source]¶
Calculate MS-SSIM (multi-scale structural similarity).
Ref: This function implements Multi-Scale Structural Similarity (MS-SSIM) Image Quality Assessment according to Zhou Wang’s paper, “Multi-scale structural similarity for image quality assessment” (2003). Link: https://ece.uwaterloo.ca/~z70wang/publications/msssim.pdf
Author’s MATLAB implementation: http://www.cns.nyu.edu/~lcv/ssim/msssim.zip
PGGAN’s implementation: https://github.com/tkarras/progressive_growing_of_gans/blob/master/metrics/ms_ssim.py
- Parameters
img1 (ndarray) – Images with range [0, 255] and order “NHWC”.
img2 (ndarray) – Images with range [0, 255] and order “NHWC”.
max_val (int) – the dynamic range of the images (i.e., the difference between the maximum the and minimum allowed values). Default to 255.
filter_size (int) – Size of blur kernel to use (will be reduced for small images). Default to 11.
filter_sigma (float) – Standard deviation for Gaussian blur kernel (will be reduced for small images). Default to 1.5.
k1 (float) – Constant used to maintain stability in the SSIM calculation (0.01 in the original paper). Default to 0.01.
k2 (float) – Constant used to maintain stability in the SSIM calculation (0.03 in the original paper). Default to 0.03.
weights (list) – List of weights for each level; if none, use five levels and the weights from the original paper. Default to None.
- Returns
MS-SSIM score between img1 and img2.
- Return type
np.ndarray
- class mmagic.evaluation.metrics.ms_ssim.MultiScaleStructureSimilarity(fake_nums: int, fake_key: Optional[str] = None, need_cond_input: bool = False, sample_model: str = 'ema', collect_device: str = 'cpu', prefix: Optional[str] = None)[source]¶
Bases:
mmagic.evaluation.metrics.base_gen_metric.GenerativeMetric
MS-SSIM (Multi-Scale Structure Similarity) metric.
Ref: https://github.com/tkarras/progressive_growing_of_gans/blob/master/metrics/ms_ssim.py # noqa
- Parameters
fake_nums (int) – Numbers of the generated image need for the metric.
fake_key (Optional[str]) – Key for get fake images of the output dict. Defaults to None.
real_key (Optional[str]) – Key for get real images from the input dict. Defaults to ‘img’.
need_cond_input (bool) – If true, the sampler will return the conditional input randomly sampled from the original dataset. This require the dataset implement get_data_info and field gt_label must be contained in the return value of get_data_info. Noted that, for unconditional models, set need_cond_input as True may influence the result of evaluation results since the conditional inputs are sampled from the dataset distribution; otherwise will be sampled from the uniform distribution. Defaults to False.
sample_model (str) – Sampling mode for the generative model. Support ‘orig’ and ‘ema’. Defaults to ‘ema’.
collect_device (str, optional) – Device name used for collecting results from different ranks during distributed training. Must be ‘cpu’ or ‘gpu’. Defaults to ‘cpu’.
prefix (str, optional) – The prefix that will be added in the metric names to disambiguate homonymous metrics of different evaluators. If prefix is not provided in the argument, self.default_prefix will be used instead. Defaults to None.
- process(data_batch: dict, data_samples: Sequence[dict]) None [source]¶
Feed data to the metric.
- Parameters
data_batch (dict) – Real images from dataloader. Do not be used in this metric.
data_samples (Sequence[dict]) – Generated images.
- _collect_target_results(target: str) Optional[list] [source]¶
Collected results for MS-SSIM metric. Size of self.fake_results in MS-SSIM does not relay on self.fake_nums but self.num_pairs.
- Parameters
target (str) – Target results to collect.
- Returns
The collected results.
- Return type
Optional[list]