You are reading the documentation for MMEditing 0.x, which will soon be deprecated by the end of 2022. We recommend you upgrade to MMEditing 1.0 to enjoy fruitful new features and better performance brought by OpenMMLab 2.0. Check out the changelog, code and documentation of MMEditing 1.0 for more details.

Tutorial 4: Customize Losses

losses are registered as LOSSES in MMEditing. Customizing losses is similar to customizing any other model. This section is mainly for clarifying the design of loss modules in our repo. Importantly, when writing your own loss modules, you should follow the same design, so that the new loss module can be adopted in our framework without extra effort.

Design of loss modules

In general, to implement a loss module, we will write a function implementation and then wrap it with a class implementation. Take the MSELoss as an example:

def mse_loss(pred, target):
    return F.mse_loss(pred, target, reduction='none')

class MSELoss(nn.Module):

    def __init__(self, loss_weight=1.0, reduction='mean', sample_wise=False):
        # codes can be found in ``mmedit/models/losses/``

    def forward(self, pred, target, weight=None, **kwargs):
        # codes can be found in ``mmedit/models/losses/``

Given the definition of the loss, we can now use the loss by simply defining it in the configuration file:

pixel_loss=dict(type='MSELoss', loss_weight=1.0, reduction='mean')

Note that pixel_loss above must be defined in the model. Please refer to customize_models for more details. Similar to model customization, in order to use your customized loss, you need to import the loss in mmedit/models/losses/ after writing it.

Read the Docs v: master
On Read the Docs
Project Home

Free document hosting provided by Read the Docs.