refiners/tests/e2e/test_mvanet.py

73 lines
2.4 KiB
Python
Raw Permalink Normal View History

from pathlib import Path
from warnings import warn
import pytest
import torch
from PIL import Image
from tests.utils import ensure_similar_images
from refiners.fluxion.utils import image_to_tensor, no_grad, normalize, tensor_to_image
from refiners.foundationals.swin.mvanet import MVANet
def _img_open(path: Path) -> Image.Image:
return Image.open(path) # type: ignore
@pytest.fixture(scope="module")
def ref_path(test_e2e_path: Path) -> Path:
return test_e2e_path / "test_mvanet_ref"
@pytest.fixture(scope="module")
def ref_cactus(ref_path: Path) -> Image.Image:
return _img_open(ref_path / "cactus.png").convert("RGB")
@pytest.fixture
def expected_cactus_mask(ref_path: Path) -> Image.Image:
return _img_open(ref_path / "expected_cactus_mask.png")
@pytest.fixture
2024-10-09 09:28:34 +00:00
def mvanet_model(mvanet_weights_path: Path, test_device: torch.device) -> MVANet:
model = MVANet(device=test_device).eval() # .eval() is important!
2024-10-09 09:28:34 +00:00
model.load_from_safetensors(mvanet_weights_path)
return model
@no_grad()
def test_mvanet(
mvanet_model: MVANet,
ref_cactus: Image.Image,
expected_cactus_mask: Image.Image,
test_device: torch.device,
):
in_t = image_to_tensor(ref_cactus.resize((1024, 1024), Image.Resampling.BILINEAR)).squeeze()
in_t = normalize(in_t, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]).unsqueeze(0)
prediction: torch.Tensor = mvanet_model(in_t.to(test_device)).sigmoid()
cactus_mask = tensor_to_image(prediction).resize(ref_cactus.size, Image.Resampling.BILINEAR)
ensure_similar_images(cactus_mask.convert("RGB"), expected_cactus_mask.convert("RGB"))
2024-08-27 16:02:47 +00:00
@no_grad()
def test_mvanet_to(
2024-10-09 09:28:34 +00:00
mvanet_weights_path: Path,
2024-08-27 16:02:47 +00:00
ref_cactus: Image.Image,
expected_cactus_mask: Image.Image,
test_device: torch.device,
):
if test_device.type == "cpu":
warn("not running on CPU, skipping")
pytest.skip()
model = MVANet(device=torch.device("cpu")).eval()
2024-10-09 09:28:34 +00:00
model.load_from_safetensors(mvanet_weights_path)
2024-08-27 16:02:47 +00:00
model.to(test_device)
in_t = image_to_tensor(ref_cactus.resize((1024, 1024), Image.Resampling.BILINEAR)).squeeze()
in_t = normalize(in_t, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]).unsqueeze(0)
prediction: torch.Tensor = model(in_t.to(test_device)).sigmoid()
cactus_mask = tensor_to_image(prediction).resize(ref_cactus.size, Image.Resampling.BILINEAR)
ensure_similar_images(cactus_mask.convert("RGB"), expected_cactus_mask.convert("RGB"))