OneStageInpaintor¶
- class mmedit.models.base_models.OneStageInpaintor(data_preprocessor: Union[dict, mmengine.config.config.Config], encdec, disc=None, loss_gan=None, loss_gp=None, loss_disc_shift=None, loss_composed_percep=None, loss_out_percep=False, loss_l1_hole=None, loss_l1_valid=None, loss_tv=None, train_cfg=None, test_cfg=None, init_cfg: Optional[dict] = None)[source]¶
Standard one-stage inpaintor with commonly used losses.
An inpaintor must contain an encoder-decoder style generator to inpaint masked regions. A discriminator will be adopted when adversarial training is needed.
In this class, we provide a common interface for inpaintors. For other inpaintors, only some funcs may be modified to fit the input style or training schedule.
- Parameters
data_preprocessor (dict) – Config of data_preprocessor.
encdec (dict) – Config for encoder-decoder style generator.
disc (dict) – Config for discriminator.
loss_gan (dict) – Config for adversarial loss.
loss_gp (dict) – Config for gradient penalty loss.
loss_disc_shift (dict) – Config for discriminator shift loss.
loss_composed_percep (dict) – Config for perceptual and style loss with composed image as input.
loss_out_percep (dict) – Config for perceptual and style loss with direct output as input.
loss_l1_hole (dict) – Config for l1 loss in the hole.
loss_l1_valid (dict) – Config for l1 loss in the valid region.
loss_tv (dict) – Config for total variation loss.
train_cfg (dict) – Configs for training scheduler. disc_step must be contained for indicates the discriminator updating steps in each training step.
test_cfg (dict) – Configs for testing scheduler.
init_cfg (dict, optional) – Initialization config dict.
- forward(inputs, data_samples, mode='tensor')[source]¶
Forward function.
- Parameters
inputs (torch.Tensor) – batch input tensor collated by
data_preprocessor
.data_samples (List[BaseDataElement], optional) – data samples collated by
data_preprocessor
.mode (str) –
mode should be one of
loss
,predict
andtensor
. Default: ‘tensor’.loss
: Called bytrain_step
and return lossdict
used for loggingpredict
: Called byval_step
andtest_step
and return list ofBaseDataElement
results used for computing metric.tensor
: Called by custom use to getTensor
type results.
- Returns
If
mode == loss
, return adict
of loss tensor used for backward and logging.If
mode == predict
, return alist
ofBaseDataElement
for computing metric and getting inference result.If
mode == tensor
, return a tensor ortuple
of tensor ordict
or tensor for custom use.
- Return type
ForwardResults
- forward_dummy(x)[source]¶
Forward dummy function for getting flops.
- Parameters
x (torch.Tensor) – Input tensor with shape of (n, c, h, w).
- Returns
Results tensor with shape of (n, 3, h, w).
- Return type
torch.Tensor
- forward_tensor(inputs, data_samples)[source]¶
Forward function in tensor mode.
- Parameters
inputs (torch.Tensor) – Input tensor.
data_samples (List[dict]) – List of data sample dict.
- Returns
- Direct output of the generator and composition of fake_res
and ground-truth image.
- Return type
tuple
- forward_test(inputs, data_samples)[source]¶
Forward function for testing.
- Parameters
inputs (torch.Tensor) – Input tensor.
data_samples (List[dict]) – List of data sample dict.
- Returns
- List of prediction saved in
DataSample.
- Return type
predictions (List[DataSample])
- forward_train(*args, **kwargs)[source]¶
Forward function for training.
In this version, we do not use this interface.
- forward_train_d(data_batch, is_real, is_disc)[source]¶
Forward function in discriminator training step.
In this function, we compute the prediction for each data batch (real or fake). Meanwhile, the standard gan loss will be computed with several proposed losses for stable training.
- Parameters
data_batch (torch.Tensor) – Batch of real data or fake data.
is_real (bool) – If True, the gan loss will regard this batch as real data. Otherwise, the gan loss will regard this batch as fake data.
is_disc (bool) – If True, this function is called in discriminator training step. Otherwise, this function is called in generator training step. This will help us to compute different types of adversarial loss, like LSGAN.
- Returns
Contains the loss items computed in this function.
- Return type
dict
- generator_loss(fake_res, fake_img, gt, mask, masked_img)[source]¶
Forward function in generator training step.
In this function, we mainly compute the loss items for generator with the given (fake_res, fake_img). In general, the fake_res is the direct output of the generator and the fake_img is the composition of direct output and ground-truth image.
- Parameters
fake_res (torch.Tensor) – Direct output of the generator.
fake_img (torch.Tensor) – Composition of fake_res and ground-truth image.
gt (torch.Tensor) – Ground-truth image.
mask (torch.Tensor) – Mask image.
masked_img (torch.Tensor) – Composition of mask image and ground-truth image.
- Returns
Dict contains the results computed within this function for visualization and dict contains the loss items computed in this function.
- Return type
tuple(dict)
- train_step(data: List[dict], optim_wrapper)[source]¶
Train step function.
In this function, the inpaintor will finish the train step following the pipeline:
get fake res/image
optimize discriminator (if have)
optimize generator
If self.train_cfg.disc_step > 1, the train step will contain multiple iterations for optimizing discriminator with different input data and only one iteration for optimizing gerator after disc_step iterations for discriminator.
- Parameters
data (List[dict]) – Batch of data as input.
optim_wrapper (dict[torch.optim.Optimizer]) – Dict with optimizers for generator and discriminator (if have).
- Returns
- Dict with loss, information for logger, the number of
samples and results for visualization.
- Return type
dict