Skip to content

Commit

Permalink
[Doc] Add tutorial for customizing losses (#839)
Browse files Browse the repository at this point in the history
* add tutorial for losses

* add number and index

Co-authored-by: wangruohui <12756472+wangruohui@users.noreply.github.com>
  • Loading branch information
ckkelvinchan and wangruohui authored Apr 19, 2022
1 parent 0ee34f0 commit abd7d37
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/en/tutorials/customize_dataset.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Tutorial: Customize Datasets
# Tutorial 1: Customize Datasets

## Supported Data Format

Expand Down
29 changes: 29 additions & 0 deletions docs/en/tutorials/customize_losses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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:

```python
@masked_loss
def mse_loss(pred, target):
return F.mse_loss(pred, target, reduction='none')

@LOSSES.register_module()
class MSELoss(nn.Module):

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

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

Given the definition of the loss, we can now use the loss by simply defining it in the configuration file:
```python
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/__init__.py` after writing it.
2 changes: 1 addition & 1 deletion docs/en/tutorials/customize_models.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Tutorial: Customize Models
# Tutorial 3: Customize Models

MMEditing supports multiple tasks, each of which has different settings. Fortunately, their customization is similar. Here, we use a super-resolution model, BasicVSR, as an example in this tutorial. You will be able to define your model based on your own needs after this tutorial.

Expand Down
2 changes: 1 addition & 1 deletion docs/en/tutorials/customize_pipeline.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Tutorial: Customize Data Pipelines
# Tutorial 2: Customize Data Pipelines

## Design of Data pipelines

Expand Down
1 change: 1 addition & 0 deletions docs/en/tutorials/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
customize_dataset.md
customize_pipeline.md
customize_models.md
customize_losses.md

0 comments on commit abd7d37

Please sign in to comment.