Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SRCNN impl and tests #26

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ccrestoration/arch/srcnn_arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, num_channels: int = 1, scale: int = 2) -> None:
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = F.interpolate(x, scale_factor=self.scale, mode="bilinear")

if self.num_channels == 1:
if self.num_channels == 1 and x.size(1) == 3:
# RGB -> YUV
x = rgb_to_yuv(x)
y, u, v = x[:, 0:1, ...], x[:, 1:2, ...], x[:, 2:3, ...]
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ exclude_also = [
[tool.coverage.run]
omit = [
"ccrestoration/arch/*",
"ccrestoration/vs/*"
"ccrestoration/vs/*",
"ccrestoration/util/device.py"
]

[tool.mypy]
Expand Down
40 changes: 40 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import cv2
import pytest
import torch
from torchvision import transforms

from ccrestoration.util.color import rgb_to_yuv, yuv_to_rgb
from ccrestoration.util.device import DEFAULT_DEVICE

from .util import calculate_image_similarity, load_image


def test_device() -> None:
print(DEFAULT_DEVICE)
Comment on lines +12 to +13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (testing): Test function test_device only prints the device without any assertions

This test doesn't verify any behavior. Consider adding assertions to check if DEFAULT_DEVICE is set correctly or has expected properties.



def test_color() -> None:
with pytest.raises(TypeError):
rgb_to_yuv(1)
with pytest.raises(TypeError):
yuv_to_rgb(1)

with pytest.raises(ValueError):
rgb_to_yuv(torch.zeros(1, 1))
with pytest.raises(ValueError):
yuv_to_rgb(torch.zeros(1, 1))
Comment on lines +16 to +25
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Test function test_color could benefit from parametrization and more specific assertions

Consider splitting this test into multiple test cases: one for error conditions, one for RGB->YUV conversion, and one for YUV->RGB conversion. Also, consider adding assertions about the specific values or ranges in the YUV color space to ensure the conversion is correct.

@pytest.mark.parametrize("invalid_input", [1, "string", [1, 2, 3]])
def test_color_type_errors(invalid_input) -> None:
    with pytest.raises(TypeError):
        rgb_to_yuv(invalid_input)
    with pytest.raises(TypeError):
        yuv_to_rgb(invalid_input)

@pytest.mark.parametrize("invalid_shape", [torch.zeros(1, 1), torch.zeros(2, 3, 4)])
def test_color_value_errors(invalid_shape) -> None:
    with pytest.raises(ValueError):
        rgb_to_yuv(invalid_shape)
    with pytest.raises(ValueError):
        yuv_to_rgb(invalid_shape)

def test_color_conversion() -> None:
    img = load_image()
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img_tensor = transforms.ToTensor()(img_rgb).unsqueeze(0).to("cpu")

    yuv = rgb_to_yuv(img_tensor)
    assert yuv.shape == img_tensor.shape
    assert (yuv[:, 0] >= -1).all() and (yuv[:, 0] <= 1).all()  # Y channel bounds

    rgb = yuv_to_rgb(yuv)
    rgb_np = (rgb.squeeze(0).permute(1, 2, 0).cpu().numpy() * 255).clip(0, 255).astype("uint8")
    rgb_np = cv2.cvtColor(rgb_np, cv2.COLOR_RGB2BGR)

    assert calculate_image_similarity(rgb_np, img)


img = load_image()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

img = transforms.ToTensor()(img).unsqueeze(0).to("cpu")

img = rgb_to_yuv(img)
img = yuv_to_rgb(img)

img = img.squeeze(0).permute(1, 2, 0).cpu().numpy()
img = (img * 255).clip(0, 255).astype("uint8")

img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

assert calculate_image_similarity(img, load_image())