test_hsigmoid.py 1.06 KB
Newer Older
yamengxi's avatar
yamengxi committed
1
import pytest
2
3
4
5
6
7
import torch

from mmcv.cnn.bricks import HSigmoid


def test_hsigmoid():
yamengxi's avatar
yamengxi committed
8
9
10
11
12
    # test assertion divisor can not be zero
    with pytest.raises(AssertionError):
        HSigmoid(divisor=0)

    # test with default parameters
13
14
15
16
17
    act = HSigmoid()
    input_shape = torch.Size([1, 3, 64, 64])
    input = torch.randn(input_shape)
    output = act(input)
    expected_output = torch.min(
18
        torch.max((input + 3) / 6, torch.zeros(input_shape)),
19
20
21
22
23
        torch.ones(input_shape))
    # test output shape
    assert output.shape == expected_output.shape
    # test output value
    assert torch.equal(output, expected_output)
yamengxi's avatar
yamengxi committed
24
25

    # test with designated parameters
26
    act = HSigmoid(1, 2, 0, 1)
yamengxi's avatar
yamengxi committed
27
28
29
30
    input_shape = torch.Size([1, 3, 64, 64])
    input = torch.randn(input_shape)
    output = act(input)
    expected_output = torch.min(
31
        torch.max((input + 1) / 2, torch.zeros(input_shape)),
yamengxi's avatar
yamengxi committed
32
33
34
35
36
        torch.ones(input_shape))
    # test output shape
    assert output.shape == expected_output.shape
    # test output value
    assert torch.equal(output, expected_output)