|
| 1 | +"""Unit test package for SCCNN.""" |
| 2 | +import numpy as np |
| 3 | +import torch |
| 4 | + |
| 5 | +from tiatoolbox import utils |
| 6 | +from tiatoolbox.models.architecture import fetch_pretrained_weights |
| 7 | +from tiatoolbox.models.architecture.sccnn import SCCNN |
| 8 | +from tiatoolbox.wsicore.wsireader import WSIReader |
| 9 | + |
| 10 | + |
| 11 | +def _load_sccnn(tmp_path, name): |
| 12 | + """Loads SCCNN model with specified weights.""" |
| 13 | + model = SCCNN() |
| 14 | + fetch_pretrained_weights(name, f"{tmp_path}/weights.pth") |
| 15 | + map_location = utils.misc.select_device(utils.env_detection.has_gpu()) |
| 16 | + pretrained = torch.load(f"{tmp_path}/weights.pth", map_location=map_location) |
| 17 | + model.load_state_dict(pretrained) |
| 18 | + |
| 19 | + return model |
| 20 | + |
| 21 | + |
| 22 | +def test_functionality(remote_sample, tmp_path): |
| 23 | + """Functionality test for SCCNN. |
| 24 | +
|
| 25 | + Tests the functionality of SCCNN model for inference at the patch level. |
| 26 | +
|
| 27 | + """ |
| 28 | + tmp_path = str(tmp_path) |
| 29 | + sample_wsi = str(remote_sample("wsi1_2k_2k_svs")) |
| 30 | + reader = WSIReader.open(sample_wsi) |
| 31 | + |
| 32 | + # * test fast mode (architecture used in PanNuke paper) |
| 33 | + patch = reader.read_bounds( |
| 34 | + (30, 30, 61, 61), resolution=0.25, units="mpp", coord_space="resolution" |
| 35 | + ) |
| 36 | + batch = torch.from_numpy(patch)[None] |
| 37 | + model = _load_sccnn(tmp_path=tmp_path, name="sccnn-crchisto") |
| 38 | + output = model.infer_batch(model, batch, on_gpu=False) |
| 39 | + output = model.postproc(output[0]) |
| 40 | + assert np.all(output == [[8, 7]]) |
| 41 | + |
| 42 | + model = _load_sccnn(tmp_path=tmp_path, name="sccnn-conic") |
| 43 | + output = model.infer_batch(model, batch, on_gpu=False) |
| 44 | + output = model.postproc(output[0]) |
| 45 | + assert np.all(output == [[7, 8]]) |
0 commit comments