2021-08-17 14:16:55 +08:00
|
|
|
# Copyright (c) OpenMMLab. All rights reserved.
|
2021-03-31 08:55:09 +08:00
|
|
|
import torch
|
|
|
|
|
|
|
|
from mmseg.models.decode_heads import DNLHead
|
|
|
|
from .utils import to_cuda
|
|
|
|
|
|
|
|
|
|
|
|
def test_dnl_head():
|
|
|
|
# DNL with 'embedded_gaussian' mode
|
2021-11-01 22:47:43 +08:00
|
|
|
head = DNLHead(in_channels=8, channels=4, num_classes=19)
|
2021-03-31 08:55:09 +08:00
|
|
|
assert len(head.convs) == 2
|
|
|
|
assert hasattr(head, 'dnl_block')
|
|
|
|
assert head.dnl_block.temperature == 0.05
|
2021-11-01 22:47:43 +08:00
|
|
|
inputs = [torch.randn(1, 8, 23, 23)]
|
2021-03-31 08:55:09 +08:00
|
|
|
if torch.cuda.is_available():
|
|
|
|
head, inputs = to_cuda(head, inputs)
|
|
|
|
outputs = head(inputs)
|
2021-11-01 22:47:43 +08:00
|
|
|
assert outputs.shape == (1, head.num_classes, 23, 23)
|
2021-03-31 08:55:09 +08:00
|
|
|
|
|
|
|
# NonLocal2d with 'dot_product' mode
|
|
|
|
head = DNLHead(
|
2021-11-01 22:47:43 +08:00
|
|
|
in_channels=8, channels=4, num_classes=19, mode='dot_product')
|
|
|
|
inputs = [torch.randn(1, 8, 23, 23)]
|
2021-03-31 08:55:09 +08:00
|
|
|
if torch.cuda.is_available():
|
|
|
|
head, inputs = to_cuda(head, inputs)
|
|
|
|
outputs = head(inputs)
|
2021-11-01 22:47:43 +08:00
|
|
|
assert outputs.shape == (1, head.num_classes, 23, 23)
|
2021-03-31 08:55:09 +08:00
|
|
|
|
|
|
|
# NonLocal2d with 'gaussian' mode
|
2021-11-01 22:47:43 +08:00
|
|
|
head = DNLHead(in_channels=8, channels=4, num_classes=19, mode='gaussian')
|
|
|
|
inputs = [torch.randn(1, 8, 23, 23)]
|
2021-03-31 08:55:09 +08:00
|
|
|
if torch.cuda.is_available():
|
|
|
|
head, inputs = to_cuda(head, inputs)
|
|
|
|
outputs = head(inputs)
|
2021-11-01 22:47:43 +08:00
|
|
|
assert outputs.shape == (1, head.num_classes, 23, 23)
|
2021-03-31 08:55:09 +08:00
|
|
|
|
|
|
|
# NonLocal2d with 'concatenation' mode
|
|
|
|
head = DNLHead(
|
2021-11-01 22:47:43 +08:00
|
|
|
in_channels=8, channels=4, num_classes=19, mode='concatenation')
|
|
|
|
inputs = [torch.randn(1, 8, 23, 23)]
|
2021-03-31 08:55:09 +08:00
|
|
|
if torch.cuda.is_available():
|
|
|
|
head, inputs = to_cuda(head, inputs)
|
|
|
|
outputs = head(inputs)
|
2021-11-01 22:47:43 +08:00
|
|
|
assert outputs.shape == (1, head.num_classes, 23, 23)
|