|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +torch = pytest.importorskip("torch") |
| 6 | + |
| 7 | +from opensr_srgan.model.model_blocks import ( |
| 8 | + ConvolutionalBlock, |
| 9 | + DenseBlock5, |
| 10 | + LKA, |
| 11 | + LKAResBlock, |
| 12 | + RCAB, |
| 13 | + RRDB, |
| 14 | + ResidualBlock, |
| 15 | + ResidualBlockNoBN, |
| 16 | + SubPixelConvolutionalBlock, |
| 17 | + make_upsampler, |
| 18 | +) |
| 19 | +from opensr_srgan.model.model_blocks import _icnr_ |
| 20 | + |
| 21 | + |
| 22 | +def test_convolutional_block_rejects_unknown_activation() -> None: |
| 23 | + with pytest.raises(AssertionError, match="activation must be one of"): |
| 24 | + ConvolutionalBlock(16, 16, 3, activation="relu") |
| 25 | + |
| 26 | + |
| 27 | +def test_convolutional_block_tanh_path_keeps_spatial_shape() -> None: |
| 28 | + block = ConvolutionalBlock(16, 16, 3, batch_norm=True, activation="tanh") |
| 29 | + x = torch.randn(2, 16, 8, 8) |
| 30 | + y = block(x) |
| 31 | + assert y.shape == x.shape |
| 32 | + |
| 33 | + |
| 34 | +def test_subpixel_block_upsamples_by_scaling_factor() -> None: |
| 35 | + block = SubPixelConvolutionalBlock(n_channels=16, scaling_factor=2) |
| 36 | + x = torch.randn(1, 16, 8, 8) |
| 37 | + y = block(x) |
| 38 | + assert y.shape == (1, 16, 16, 16) |
| 39 | + |
| 40 | + |
| 41 | +def test_residual_and_attention_blocks_preserve_shape() -> None: |
| 42 | + x = torch.randn(1, 16, 8, 8) |
| 43 | + |
| 44 | + assert ResidualBlock(n_channels=16)(x).shape == x.shape |
| 45 | + assert ResidualBlockNoBN(n_channels=16)(x).shape == x.shape |
| 46 | + assert RCAB(n_channels=16)(x).shape == x.shape |
| 47 | + assert DenseBlock5(n_features=16, growth_channels=8)(x).shape == x.shape |
| 48 | + assert RRDB(n_features=16, growth_channels=8)(x).shape == x.shape |
| 49 | + assert LKA(n_channels=16)(x).shape == x.shape |
| 50 | + assert LKAResBlock(n_channels=16)(x).shape == x.shape |
| 51 | + |
| 52 | + |
| 53 | +def test_icnr_requires_divisible_output_channels() -> None: |
| 54 | + weight = torch.empty(10, 4, 3, 3) |
| 55 | + with pytest.raises(ValueError, match=r"divisible by scale\*\*2"): |
| 56 | + _icnr_(weight, scale=2) |
| 57 | + |
| 58 | + |
| 59 | +def test_make_upsampler_scale_4_with_icnr_produces_expected_shape() -> None: |
| 60 | + upsampler = make_upsampler(16, scale=4, use_icnr=True) |
| 61 | + x = torch.randn(1, 16, 8, 8) |
| 62 | + y = upsampler(x) |
| 63 | + assert y.shape == (1, 16, 32, 32) |
| 64 | + |
| 65 | + |
| 66 | +def test_make_upsampler_rejects_non_power_of_two_scale() -> None: |
| 67 | + with pytest.raises(ValueError, match="power of two"): |
| 68 | + make_upsampler(16, scale=3) |
0 commit comments