Shortcuts

Source code for mmedit.evaluation.metrics.matting_mse

# Copyright (c) OpenMMLab. All rights reserved.
from typing import List, Sequence

from mmengine.evaluator import BaseMetric

from mmedit.registry import METRICS
from .metrics_utils import _fetch_data_and_check, average


@METRICS.register_module()
[docs]class MattingMSE(BaseMetric): """Mean Squared Error metric for image matting. This metric compute per-pixel squared error average across all pixels. i.e. mean((a-b)^2) / norm_const .. note:: Current implementation assume image / alpha / trimap array in numpy format and with pixel value ranging from 0 to 255. .. note:: pred_alpha should be masked by trimap before passing into this metric Default prefix: '' Args: norm_const (int): Divide the result to reduce its magnitude. Default to 1000. Metrics: - MattingMSE (float): Mean of Squared Error """
[docs] default_prefix = ''
def __init__( self, norm_const=1000, **kwargs, ) -> None: self.norm_const = norm_const super().__init__(**kwargs)
[docs] def process(self, data_batch: Sequence[dict], data_samples: Sequence[dict]) -> None: """Process one batch of data and predictions. Args: data_batch (Sequence[dict]): A batch of data from the dataloader. data_samples (Sequence[dict]): A batch of outputs from the model. """ for data_sample in data_samples: pred_alpha, gt_alpha, trimap = _fetch_data_and_check(data_sample) weight_sum = (trimap == 128).sum() if weight_sum != 0: mse_result = ((pred_alpha - gt_alpha)**2).sum() / weight_sum else: mse_result = 0 self.results.append({'mse': mse_result})
[docs] def compute_metrics(self, results: List): """Compute the metrics from processed results. Args: results (dict): The processed results of each batch. Returns: Dict: The computed metrics. The keys are the names of the metrics, and the values are corresponding results. """ mse = average(results, 'mse') return {'MattingMSE': mse}
Read the Docs v: latest
Versions
master
latest
stable
zyh-re-docs
zyh-doc-notfound-extend
zyh-api-rendering
Downloads
pdf
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.