forked from open-mmlab/mmagic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Doc] Add tutorial for customizing losses (open-mmlab#839)
* add tutorial for losses * add number and index Co-authored-by: wangruohui <12756472+wangruohui@users.noreply.github.com>
- Loading branch information
1 parent
4cf8ac9
commit cabe6b8
Showing
5 changed files
with
33 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Tutorial: Customize Datasets | ||
# Tutorial 1: Customize Datasets | ||
|
||
## Supported Data Format | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
customize_dataset.md | ||
customize_pipeline.md | ||
customize_models.md | ||
customize_losses.md |