mirror of
https://github.com/open-mmlab/mmsegmentation.git
synced 2025-06-03 22:03:48 +08:00
Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers. ## Motivation It's OpenMMLab Codecamp task. ## Modification Implementd Kullback-Leibler divergence loss and also added tests for it. ## Checklist 1. Pre-commit or other linting tools are used to fix the potential lint issues. 2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness. 3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMDet3D. 4. The documentation has been modified accordingly, like docstring or example tutorials. --------- Co-authored-by: xiexinch <xiexinch@outlook.com>
41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
# Copyright (c) OpenMMLab. All rights reserved.
|
|
import torch
|
|
|
|
from mmseg.models.losses.kldiv_loss import KLDivLoss
|
|
|
|
|
|
def test_kldiv_loss_with_none_reduction():
|
|
loss_class = KLDivLoss
|
|
pred = torch.rand((8, 5, 5))
|
|
target = torch.rand((8, 5, 5))
|
|
reduction = 'none'
|
|
|
|
# Test loss forward
|
|
loss = loss_class(reduction=reduction)(pred, target)
|
|
assert isinstance(loss, torch.Tensor)
|
|
assert loss.shape == (8, 5, 5), f'{loss.shape}'
|
|
|
|
|
|
def test_kldiv_loss_with_mean_reduction():
|
|
loss_class = KLDivLoss
|
|
pred = torch.rand((8, 5, 5))
|
|
target = torch.rand((8, 5, 5))
|
|
reduction = 'mean'
|
|
|
|
# Test loss forward
|
|
loss = loss_class(reduction=reduction)(pred, target)
|
|
assert isinstance(loss, torch.Tensor)
|
|
assert loss.shape == (8, ), f'{loss.shape}'
|
|
|
|
|
|
def test_kldiv_loss_with_sum_reduction():
|
|
loss_class = KLDivLoss
|
|
pred = torch.rand((8, 5, 5))
|
|
target = torch.rand((8, 5, 5))
|
|
reduction = 'sum'
|
|
|
|
# Test loss forward
|
|
loss = loss_class(reduction=reduction)(pred, target)
|
|
assert isinstance(loss, torch.Tensor)
|
|
assert loss.shape == (8, ), f'{loss.shape}'
|