mmedit.models.base_models.base_mattor¶
Module Contents¶
Classes¶
Base class for trimap-based matting models. |
Functions¶
|
Pad image to a multiple of give down-sampling factor. |
|
Resize image to multiple of give down-sampling factor. |
Attributes¶
- mmedit.models.base_models.base_mattor._pad(batch_image: torch.Tensor, ds_factor: int, mode: str = 'reflect') Tuple[torch.Tensor, Tuple[int, int]][源代码]¶
Pad image to a multiple of give down-sampling factor.
- mmedit.models.base_models.base_mattor._interpolate(batch_image: torch.Tensor, ds_factor: int, mode: str = 'bicubic') Tuple[torch.Tensor, Tuple[int, int]][源代码]¶
Resize image to multiple of give down-sampling factor.
- class mmedit.models.base_models.base_mattor.BaseMattor(data_preprocessor: Union[dict, mmengine.config.Config], backbone: dict, init_cfg: Optional[dict] = None, train_cfg: Optional[dict] = None, test_cfg: Optional[dict] = None)[源代码]¶
Bases:
mmengine.model.BaseModelBase class for trimap-based matting models.
A matting model must contain a backbone which produces pred_alpha, a dense prediction with the same height and width of input image. In some cases (such as DIM), the model has a refiner which refines the prediction of the backbone.
Subclasses should overwrite the following functions:
_forward_train(), to return a loss_forward_test(), to return a prediction_forward(), to return raw tensors
For test, this base class provides functions to resize inputs and post-process pred_alphas to get predictions
- 参数
backbone (dict) – Config of backbone.
data_preprocessor (dict) – Config of data_preprocessor. See
MattorPreprocessorfor details.init_cfg (dict, optional) – Initialization config dict.
train_cfg (dict) – Config of training. Customized by subclassesCustomized bu In
train_cfg,train_backboneshould be specified. If the model has a refiner,train_refinershould be specified.test_cfg (dict) – Config of testing. In
test_cfg, If the model has a refiner,train_refinershould be specified.
- resize_inputs(batch_inputs: torch.Tensor) torch.Tensor[源代码]¶
Pad or interpolate images and trimaps to multiple of given factor.
- restore_size(pred_alpha: torch.Tensor, data_sample: mmedit.structures.EditDataSample) torch.Tensor[源代码]¶
Restore the predicted alpha to the original shape.
The shape of the predicted alpha may not be the same as the shape of original input image. This function restores the shape of the predicted alpha.
- 参数
pred_alpha (torch.Tensor) – A single predicted alpha of shape (1, H, W).
data_sample (EditDataSample) – Data sample containing original shape as meta data.
- 返回
The reshaped predicted alpha.
- 返回类型
torch.Tensor
- postprocess(batch_pred_alpha: torch.Tensor, data_samples: mmedit.structures.EditDataSample) List[mmedit.structures.EditDataSample][源代码]¶
Post-process alpha predictions.
- This function contains the following steps:
Restore padding or interpolation
Mask alpha prediction with trimap
Clamp alpha prediction to 0-1
Convert alpha prediction to uint8
Pack alpha prediction into EditDataSample
Currently only batch_size 1 is actually supported.
- 参数
batch_pred_alpha (torch.Tensor) – A batch of predicted alpha of shape (N, 1, H, W).
data_samples (List[EditDataSample]) – List of data samples.
- 返回
- A list of predictions.
Each data sample contains a pred_alpha, which is a torch.Tensor with dtype=uint8, device=cuda:0
- 返回类型
List[EditDataSample]
- forward(inputs: torch.Tensor, data_samples: DataSamples = None, mode: str = 'tensor') List[mmedit.structures.EditDataSample][源代码]¶
General forward function.
- 参数
inputs (torch.Tensor) – A batch of inputs. with image and trimap concatenated alone channel dimension.
data_samples (List[EditDataSample], optional) – A list of data samples, containing: - Ground-truth alpha / foreground / background to compute loss - other meta information
mode (str) –
mode should be one of
loss,predictandtensor. Default: ‘tensor’.loss: Called bytrain_stepand return lossdictused for loggingpredict: Called byval_stepandtest_stepand return list ofBaseDataElementresults used for computing metric.tensor: Called by custom use to getTensortype results.
- 返回
Sequence of predictions packed into EditDataElement
- 返回类型
List[EditDataElement]
- convert_to_datasample(predictions: List[mmedit.structures.EditDataSample], data_samples: mmedit.structures.EditDataSample) List[mmedit.structures.EditDataSample][源代码]¶
Add predictions to data samples.
- 参数
predictions (List[EditDataSample]) – The predictions of the model.
data_samples (EditDataSample) – The data samples loaded from dataloader.
- 返回
Modified data samples.
- 返回类型
List[EditDataSample]