Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Doc] Add tutorial for customizing losses #839

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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