File size: 7,820 Bytes
12d2e9e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
"""
Copyright 2021, Dana-Farber Cancer Institute and Weill Cornell Medicine
License: GNU GPL 2.0
"""
import os
import numpy as np
import onnx
import torch
from pathml.core import SlideData
from pathml.inference import (
HaloAIInference,
Inference,
InferenceBase,
RemoteMesmer,
RemoteTestHoverNet,
check_onnx_clean,
convert_pytorch_onnx,
remove_initializer_from_input,
)
from pathml.preprocessing import CollapseRunsVectra
def test_remove_initializer_from_input():
# Create a temporary ONNX model file
model_path = "test_model.onnx"
# temp_file = tempfile.NamedTemporaryFile(delete=False)
# temp_file.close()
# Create a sample ONNX model with initializer and graph input
model = onnx.ModelProto()
model.ir_version = 4
# Add inputs to the graph
input_1 = model.graph.input.add()
input_1.name = "input_1"
input_2 = model.graph.input.add()
input_2.name = "input_2"
# Add an initializer that matches one of the inputs
initializer = model.graph.initializer.add()
initializer.name = "input_2"
# Save the model to a file
onnx.save(model, model_path)
# Call the function to remove initializers
new_model_path = "new_model.onnx"
remove_initializer_from_input(model_path, new_model_path)
# Assert that the initializer has been removed from the new model
new_model = onnx.load(new_model_path)
input_names = [input.name for input in new_model.graph.input]
assert initializer.name not in input_names
# Clean up the temporary files
os.remove(model_path)
os.remove(new_model_path)
def test_check_onnx_clean():
# Create a temporary ONNX model file
model_path = "test_model.onnx"
# temp_file = tempfile.NamedTemporaryFile(delete=False)
# temp_file.close()
# Create a sample ONNX model with initializer and graph input
model = onnx.ModelProto()
model.ir_version = 4
# Add inputs to the graph
input_1 = model.graph.input.add()
input_1.name = "input_1"
input_2 = model.graph.input.add()
input_2.name = "input_2"
# Add an initializer that matches one of the inputs
initializer = model.graph.initializer.add()
initializer.name = "input_2"
# Save the model to a file
onnx.save(model, model_path)
if check_onnx_clean(model_path):
pass
else:
raise ValueError("check_onnx_clean function is not working")
# Clean up the temporary files
os.remove(model_path)
def test_InferenceBase():
# initialize InferenceBase
test = InferenceBase()
# test setter functions
test.set_name("name")
test.set_num_classes("num_classes")
test.set_model_type("model_type")
test.set_notes("notes")
test.set_model_input_notes("model_input_notes")
test.set_model_output_notes("model_output_notes")
test.set_citation("citation")
# test model card
for key in test.model_card:
assert key == test.model_card[key], f"function for {key} is not working"
# test repr function
assert "Base class for all ONNX models" == repr(test)
# test get model card fxn
assert test.model_card == test.get_model_card()
# test reshape function
random = np.random.rand(1, 2, 3)
assert test.reshape(random).shape == (
1,
3,
1,
2,
), "reshape function is not working on 3d arrays"
random = np.random.rand(1, 2, 3, 4, 5)
assert test.reshape(random).shape == (
5,
4,
3,
2,
1,
), "reshape function is not working on 5d arrays"
def test_Inference(tileHE):
new_path = "tests/testdata/random_model.onnx"
inference = Inference(
model_path=new_path, input_name="data", num_classes=1, model_type="segmentation"
)
orig_im = tileHE.image
inference.apply(tileHE)
assert np.array_equal(tileHE.image, inference.F(orig_im))
assert repr(inference) == f"Class to handle ONNX model locally stored at {new_path}"
# test initializer catching
bad_model = "tests/testdata/model_with_initalizers.onnx"
try:
inference = Inference(
model_path=bad_model,
input_name="data",
num_classes=1,
model_type="segmentation",
)
except Exception as e:
assert (
str(e)
== "The ONNX model still has graph initializers in the input graph. Use `remove_initializer_from_input` to remove them."
)
# test repr function with local set to False
inference = Inference(
model_path=new_path,
input_name="data",
num_classes=1,
model_type="segmentation",
local=False,
)
fake_model_name = "test model"
inference.set_name(fake_model_name)
assert (
repr(inference)
== f"Class to handle a {fake_model_name} from the PathML model zoo."
)
def test_HaloAIInference(tileHE):
new_path = "tests/testdata/random_model.onnx"
inference = HaloAIInference(
model_path=new_path, input_name="data", num_classes=1, model_type="segmentation"
)
orig_im = tileHE.image
inference.apply(tileHE)
assert np.array_equal(tileHE.image, inference.F(orig_im))
assert (
repr(inference)
== f"Class to handle HALO AI ONNX model locally stored at {new_path}"
)
def test_RemoteTestHoverNet():
inference = RemoteTestHoverNet()
wsi = SlideData("tests/testdata/small_HE.svs")
tiles = wsi.generate_tiles(shape=(256, 256), pad=False)
a = 0
test_tile = None
while a == 0:
for tile in tiles:
test_tile = tile
a += 1
orig_im = test_tile.image
inference.apply(test_tile)
assert np.array_equal(test_tile.image, inference.F(orig_im))
assert (
repr(inference)
== "Class to handle remote TIAToolBox HoverNet test ONNX. See model card for citation."
)
inference.remove()
def test_RemoteMesmer(tileVectra):
vectra_collapse = CollapseRunsVectra()
vectra_collapse.apply(tileVectra)
inference = RemoteMesmer(
nuclear_channel=0,
cytoplasm_channel=1,
postprocess_kwargs_nuclear={
"label_erosion": 10,
"small_objects_threshold": 0.2,
"fill_holes_threshold": 0.2,
"pixel_expansion": 10,
"maxima_algorithm": "peak_local_max",
},
)
orig_im = tileVectra.image
cell, nuclear = inference.F(orig_im)
inference.apply(tileVectra)
assert np.array_equal(tileVectra.masks["cell_segmentation"], cell)
assert np.array_equal(tileVectra.masks["nuclear_segmentation"], nuclear)
assert (
repr(inference)
== "Class to handle remote Mesmer Model from Deepcell. See model card for citation."
)
inference.remove()
def test_convert_pytorch_onnx():
test_tensor = torch.randn(1, 10)
model_test = torch.jit.load("tests/testdata/test.pt")
model_test.eval()
convert_pytorch_onnx(
model=model_test, dummy_tensor=test_tensor, model_name="test_export.onnx"
)
os.remove("test_export.onnx")
# test Value Error Statements
# test lines to check model input
try:
convert_pytorch_onnx(
model=None, dummy_tensor=test_tensor, model_name="test_export.onnx"
)
except Exception as e:
assert (
str(e)
== f"The model is not of type torch.nn.Module. Received {type(None)}."
)
# test lines to check model dummy input
try:
convert_pytorch_onnx(
model=model_test, dummy_tensor=None, model_name="test_export.onnx"
)
except Exception as e:
assert (
str(e)
== f"The dummy tensor needs to be a torch tensor. Received {type(None)}."
)
|