mmseg.apis¶
mmseg.evaluation¶
metrics¶
- class mmseg.evaluation.metrics.CityscapesMetric(output_dir: str, ignore_index: int = 255, format_only: bool = False, keep_results: bool = False, collect_device: str = 'cpu', prefix: Optional[str] = None, **kwargs)[source]¶
Cityscapes evaluation metric.
- Parameters
output_dir (str) – The directory for output prediction
ignore_index (int) – Index that will be ignored in evaluation. Default: 255.
format_only (bool) – Only format result for results commit without perform evaluation. It is useful when you want to format the result to a specific format and submit it to the test server. Defaults to False.
keep_results (bool) – Whether to keep the results. When
format_only
is True,keep_results
must be True. Defaults to False.collect_device (str) – 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.
- compute_metrics(results: list) → Dict[str, float][source]¶
Compute the metrics from processed results.
- Parameters
results (list) – Testing results of the dataset.
- Returns
float]: Cityscapes evaluation results.
- Return type
dict[str
- process(data_batch: dict, data_samples: Sequence[dict]) → None[source]¶
Process one batch of data and data_samples.
The processed results should be stored in
self.results
, which will be used to computed the metrics when all batches have been processed.- Parameters
data_batch (dict) – A batch of data from the dataloader.
data_samples (Sequence[dict]) – A batch of outputs from the model.
- class mmseg.evaluation.metrics.DepthMetric(depth_metrics: Optional[List[str]] = None, min_depth_eval: float = 0.0, max_depth_eval: float = inf, crop_type: Optional[str] = None, depth_scale_factor: float = 1.0, collect_device: str = 'cpu', output_dir: Optional[str] = None, format_only: bool = False, prefix: Optional[str] = None, **kwargs)[source]¶
Depth estimation evaluation metric.
- Parameters
depth_metrics (List[str], optional) – List of metrics to compute. If not specified, defaults to all metrics in self.METRICS.
min_depth_eval (float) – Minimum depth value for evaluation. Defaults to 0.0.
max_depth_eval (float) – Maximum depth value for evaluation. Defaults to infinity.
crop_type (str, optional) – Specifies the type of cropping to be used during evaluation. This option can affect how the evaluation mask is generated. Currently, ‘nyu_crop’ is supported, but other types can be added in future. Defaults to None if no cropping should be applied.
depth_scale_factor (float) – Factor to scale the depth values. Defaults to 1.0.
collect_device (str) – Device name used for collecting results from different ranks during distributed training. Must be ‘cpu’ or ‘gpu’. Defaults to ‘cpu’.
output_dir (str) – The directory for output prediction. Defaults to None.
format_only (bool) – Only format result for results commit without perform evaluation. It is useful when you want to save the result to a specific format and submit it to the test server. Defaults to False.
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.
- compute_metrics(results: list) → Dict[str, float][source]¶
Compute the metrics from processed results.
- Parameters
results (list) – The processed results of each batch.
- Returns
- The computed metrics. The keys are the names of
the metrics, and the values are corresponding results. The keys are identical with self.metrics.
- Return type
Dict[str, float]
- process(data_batch: dict, data_samples: Sequence[dict]) → None[source]¶
Process one batch of data and data_samples.
The processed results should be stored in
self.results
, which will be used to compute the metrics when all batches have been processed.- Parameters
data_batch (dict) – A batch of data from the dataloader.
data_samples (Sequence[dict]) – A batch of outputs from the model.
- class mmseg.evaluation.metrics.IoUMetric(ignore_index: int = 255, iou_metrics: List[str] = ['mIoU'], nan_to_num: Optional[int] = None, beta: int = 1, collect_device: str = 'cpu', output_dir: Optional[str] = None, format_only: bool = False, prefix: Optional[str] = None, **kwargs)[source]¶
IoU evaluation metric.
- Parameters
ignore_index (int) – Index that will be ignored in evaluation. Default: 255.
iou_metrics (list[str] | str) – Metrics to be calculated, the options includes ‘mIoU’, ‘mDice’ and ‘mFscore’.
nan_to_num (int, optional) – If specified, NaN values will be replaced by the numbers defined by the user. Default: None.
beta (int) – Determines the weight of recall in the combined score. Default: 1.
collect_device (str) – Device name used for collecting results from different ranks during distributed training. Must be ‘cpu’ or ‘gpu’. Defaults to ‘cpu’.
output_dir (str) – The directory for output prediction. Defaults to None.
format_only (bool) – Only format result for results commit without perform evaluation. It is useful when you want to save the result to a specific format and submit it to the test server. Defaults to False.
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.
- compute_metrics(results: list) → Dict[str, float][source]¶
Compute the metrics from processed results.
- Parameters
results (list) – The processed results of each batch.
- Returns
- The computed metrics. The keys are the names of
the metrics, and the values are corresponding results. The key mainly includes aAcc, mIoU, mAcc, mDice, mFscore, mPrecision, mRecall.
- Return type
Dict[str, float]
- static intersect_and_union(pred_label: torch._VariableFunctionsClass.tensor, label: torch._VariableFunctionsClass.tensor, num_classes: int, ignore_index: int)[source]¶
Calculate Intersection and Union.
- Parameters
pred_label (torch.tensor) – Prediction segmentation map or predict result filename. The shape is (H, W).
label (torch.tensor) – Ground truth segmentation map or label filename. The shape is (H, W).
num_classes (int) – Number of categories.
ignore_index (int) – Index that will be ignored in evaluation.
- Returns
- The intersection of prediction and ground truth
histogram on all classes.
- torch.Tensor: The union of prediction and ground truth histogram on
all classes.
torch.Tensor: The prediction histogram on all classes. torch.Tensor: The ground truth histogram on all classes.
- Return type
torch.Tensor
- process(data_batch: dict, data_samples: Sequence[dict]) → None[source]¶
Process one batch of data and data_samples.
The processed results should be stored in
self.results
, which will be used to compute the metrics when all batches have been processed.- Parameters
data_batch (dict) – A batch of data from the dataloader.
data_samples (Sequence[dict]) – A batch of outputs from the model.
- static total_area_to_metrics(total_area_intersect: numpy.ndarray, total_area_union: numpy.ndarray, total_area_pred_label: numpy.ndarray, total_area_label: numpy.ndarray, metrics: List[str] = ['mIoU'], nan_to_num: Optional[int] = None, beta: int = 1)[source]¶
Calculate evaluation metrics :param total_area_intersect: The intersection of prediction
and ground truth histogram on all classes.
- Parameters
total_area_union (np.ndarray) – The union of prediction and ground truth histogram on all classes.
total_area_pred_label (np.ndarray) – The prediction histogram on all classes.
total_area_label (np.ndarray) – The ground truth histogram on all classes.
metrics (List[str] | str) – Metrics to be evaluated, ‘mIoU’ and ‘mDice’.
nan_to_num (int, optional) – If specified, NaN values will be replaced by the numbers defined by the user. Default: None.
beta (int) – Determines the weight of recall in the combined score. Default: 1.
- Returns
- per category evaluation metrics,
shape (num_classes, ).
- Return type
Dict[str, np.ndarray]
mmseg.structures¶
structures¶
- class mmseg.structures.OHEMPixelSampler(context, thresh=None, min_kept=100000)[source]¶
Online Hard Example Mining Sampler for segmentation.
- Parameters
context (nn.Module) – The context of sampler, subclass of
BaseDecodeHead
.thresh (float, optional) – The threshold for hard example selection. Below which, are prediction with low confidence. If not specified, the hard examples will be pixels of top
min_kept
loss. Default: None.min_kept (int, optional) – The minimum number of predictions to keep. Default: 100000.
- sample(seg_logit, seg_label)[source]¶
Sample pixels that have high loss or with low prediction confidence.
- Parameters
seg_logit (torch.Tensor) – segmentation logits, shape (N, C, H, W)
seg_label (torch.Tensor) – segmentation label, shape (N, 1, H, W)
- Returns
segmentation weight, shape (N, H, W)
- Return type
torch.Tensor
- class mmseg.structures.SegDataSample(*, metainfo: Optional[dict] = None, **kwargs)[source]¶
A data structure interface of MMSegmentation. They are used as interfaces between different components.
The attributes in
SegDataSample
are divided into several parts:Examples
>>> import torch >>> import numpy as np >>> from mmengine.structures import PixelData >>> from mmseg.structures import SegDataSample
>>> data_sample = SegDataSample() >>> img_meta = dict(img_shape=(4, 4, 3), ... pad_shape=(4, 4, 3)) >>> gt_segmentations = PixelData(metainfo=img_meta) >>> gt_segmentations.data = torch.randint(0, 2, (1, 4, 4)) >>> data_sample.gt_sem_seg = gt_segmentations >>> assert 'img_shape' in data_sample.gt_sem_seg.metainfo_keys() >>> data_sample.gt_sem_seg.shape (4, 4) >>> print(data_sample)
<SegDataSample(
META INFORMATION
DATA FIELDS gt_sem_seg: <PixelData(
META INFORMATION img_shape: (4, 4, 3) pad_shape: (4, 4, 3)
DATA FIELDS data: tensor([[[1, 1, 1, 0],
[1, 0, 1, 1], [1, 1, 1, 1], [0, 1, 0, 1]]])
) at 0x1c2b4156460>
) at 0x1c2aae44d60>
>>> data_sample = SegDataSample() >>> gt_sem_seg_data = dict(sem_seg=torch.rand(1, 4, 4)) >>> gt_sem_seg = PixelData(**gt_sem_seg_data) >>> data_sample.gt_sem_seg = gt_sem_seg >>> assert 'gt_sem_seg' in data_sample >>> assert 'sem_seg' in data_sample.gt_sem_seg
sampler¶
- class mmseg.structures.sampler.OHEMPixelSampler(context, thresh=None, min_kept=100000)[source]¶
Online Hard Example Mining Sampler for segmentation.
- Parameters
context (nn.Module) – The context of sampler, subclass of
BaseDecodeHead
.thresh (float, optional) – The threshold for hard example selection. Below which, are prediction with low confidence. If not specified, the hard examples will be pixels of top
min_kept
loss. Default: None.min_kept (int, optional) – The minimum number of predictions to keep. Default: 100000.
- sample(seg_logit, seg_label)[source]¶
Sample pixels that have high loss or with low prediction confidence.
- Parameters
seg_logit (torch.Tensor) – segmentation logits, shape (N, C, H, W)
seg_label (torch.Tensor) – segmentation label, shape (N, 1, H, W)
- Returns
segmentation weight, shape (N, H, W)
- Return type
torch.Tensor