-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconftest.py
420 lines (353 loc) · 15.5 KB
/
conftest.py
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from __future__ import annotations
import faulthandler
import io
import logging.handlers
import multiprocessing as mp
import signal
import sys
import tempfile
import threading
from datetime import datetime
from enum import Enum
from os import getenv
from pathlib import Path
from random import randint
from typing import List
import numpy as np
import pytest
import torch
from bioimageio.core import AxisId
from bioimageio.spec import save_bioimageio_package_to_stream
from bioimageio.spec.model import v0_4
from bioimageio.spec.model.v0_5 import (
ArchitectureFromLibraryDescr,
Author,
BatchAxis,
ChannelAxis,
CiteEntry,
Doi,
FileDescr,
HttpUrl,
Identifier,
InputAxis,
InputTensorDescr,
LicenseId,
ModelDescr,
OutputAxis,
OutputTensorDescr,
ParameterizedSize,
PytorchStateDictWeightsDescr,
SizeReference,
SpaceInputAxis,
SpaceOutputAxis,
TensorId,
TorchscriptWeightsDescr,
Version,
WeightsDescr,
)
from torch import nn
class WeightsFormat(Enum):
PYTORCH = ("pytorch",)
TORCHSCRIPT = "torchscript"
@pytest.fixture
def srv_port():
return getenv("TEST_TIKTORCH_PORT", randint(5500, 8000))
@pytest.fixture
def pub_port():
return getenv("TEST_TIKTORCH_PUB_PORT", randint(8000, 9999))
@pytest.fixture(scope="session", autouse=True)
def register_faulthandler():
if not sys.platform.startswith("win"):
faulthandler.register(signal.SIGUSR1, file=sys.stderr, all_threads=True, chain=False)
class QueueListener(logging.handlers.QueueListener):
def start(self):
# Redefine to provide meaningful thread name
self._thread = t = threading.Thread(target=self._monitor, name="QueueListener")
t.daemon = True
t.start()
@pytest.fixture(scope="module")
def log_queue():
q = mp.Queue()
logger = logging.getLogger()
listener = QueueListener(q, *logger.handlers)
listener.start()
yield q
listener.stop()
@pytest.fixture(scope="session")
def assert_threads_cleanup():
yield
running_threads = [str(t) for t in threading.enumerate() if t != threading.current_thread() and not t.daemon]
if len(running_threads):
pytest.fail("Threads still running:\n\t%s" % "\n\t".join(running_threads))
@pytest.fixture(params=[WeightsFormat.PYTORCH, WeightsFormat.TORCHSCRIPT])
def bioimage_model_explicit_add_one_siso_v5(request) -> io.BytesIO:
input_axes = [
BatchAxis(),
ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
SpaceInputAxis(id=AxisId("x"), size=10),
SpaceInputAxis(id=AxisId("y"), size=20),
]
input_test_tensor = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
if request.param == WeightsFormat.PYTORCH:
return _bioimage_model_dummy_add_one_siso_pytorch_v5(input_axes, input_test_tensor)
elif request.param == WeightsFormat.TORCHSCRIPT:
return _bioimage_model_dummy_add_one_siso_torchscript_v5(input_axes, input_test_tensor)
else:
raise NotImplementedError(f"{request.param}")
@pytest.fixture(params=[WeightsFormat.PYTORCH, WeightsFormat.TORCHSCRIPT])
def bioimage_model_param_add_one_siso_v5(request) -> io.BytesIO:
input_test_tensor = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
input_axes = [
BatchAxis(),
ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
SpaceInputAxis(id=AxisId("x"), size=ParameterizedSize(min=10, step=2)),
SpaceInputAxis(id=AxisId("y"), size=ParameterizedSize(min=20, step=3)),
]
if request.param == WeightsFormat.PYTORCH:
return _bioimage_model_dummy_add_one_siso_pytorch_v5(input_axes, input_test_tensor)
elif request.param == WeightsFormat.TORCHSCRIPT:
return _bioimage_model_dummy_add_one_siso_torchscript_v5(input_axes, input_test_tensor)
else:
raise NotImplementedError(f"{request.param}")
@pytest.fixture
def bioimage_model_add_one_miso_v5() -> io.BytesIO:
"""
Mocked bioimageio prediction pipeline with three inputs single output
"""
test_tensor1 = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
test_tensor2 = np.arange(1 * 2 * 12 * 21, dtype="float32").reshape(1, 2, 12, 21)
test_tensor3 = np.arange(1 * 2 * 12 * 20, dtype="float32").reshape(1, 2, 12, 20)
with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as test_tensor1_file:
np.save(test_tensor1_file.name, test_tensor1)
with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as test_tensor2_file:
np.save(test_tensor2_file.name, test_tensor2)
with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as test_tensor3_file:
np.save(test_tensor3_file.name, test_tensor3)
input1 = InputTensorDescr(
id=TensorId("input1"),
axes=[
BatchAxis(),
ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
SpaceInputAxis(id=AxisId("x"), size=10),
SpaceInputAxis(id=AxisId("y"), size=SizeReference(tensor_id=TensorId("input3"), axis_id=AxisId("y"))),
],
description="",
test_tensor=FileDescr(source=Path(test_tensor1_file.name)),
)
input2 = InputTensorDescr(
id=TensorId("input2"),
axes=[
BatchAxis(),
ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
SpaceInputAxis(id=AxisId("x"), size=ParameterizedSize(min=10, step=2)),
SpaceInputAxis(id=AxisId("y"), size=ParameterizedSize(min=21, step=5)),
],
description="",
test_tensor=FileDescr(source=Path(test_tensor2_file.name)),
)
input3 = InputTensorDescr(
id=TensorId("input3"),
axes=[
BatchAxis(),
ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
SpaceInputAxis(id=AxisId("x"), size=SizeReference(tensor_id=TensorId("input2"), axis_id=AxisId("x"))),
SpaceInputAxis(id=AxisId("y"), size=20),
],
description="",
test_tensor=FileDescr(source=Path(test_tensor3_file.name)),
)
dummy_network = _DummyNetworkMultipleInputAddOne()
with tempfile.NamedTemporaryFile(suffix=".pts", delete=False) as weights_file:
torch.save(dummy_network.state_dict(), weights_file.name)
weights = WeightsDescr(
pytorch_state_dict=PytorchStateDictWeightsDescr(
source=Path(weights_file.name),
architecture=ArchitectureFromLibraryDescr(
import_from="tests.conftest",
callable=Identifier(f"{_DummyNetworkMultipleInputAddOne.__name__}"),
),
pytorch_version=Version("1.1.1"),
)
)
output_test_tensor = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
output_axes = [
BatchAxis(),
ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
SpaceOutputAxis(id=AxisId("x"), size=10),
SpaceOutputAxis(id=AxisId("y"), size=20),
]
with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as output_test_tensor_file:
np.save(output_test_tensor_file.name, output_test_tensor)
output_tensor = OutputTensorDescr(
id=TensorId("output"),
axes=output_axes,
description="",
test_tensor=FileDescr(source=Path(output_test_tensor_file.name)),
)
model_bytes = _bioimage_model_v5(weights=weights, inputs=[input1, input2, input3], outputs=[output_tensor])
return model_bytes
def _bioimage_model_dummy_add_one_siso_torchscript_v5(
input_axes: List[InputAxis], input_test_tensor: np.ndarray
) -> io.BytesIO:
dummy_network = _DummyNetworkSingleInputAddOne()
traced_model = torch.jit.trace(dummy_network, example_inputs=torch.from_numpy(input_test_tensor))
with tempfile.NamedTemporaryFile(suffix=".pt", delete=False) as model_file:
traced_model.save(model_file.name)
weights = WeightsDescr(
torchscript=TorchscriptWeightsDescr(source=Path(model_file.name), pytorch_version=Version("1.1.1"))
)
output_test_tensor = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
output_axes = [
BatchAxis(),
ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
SpaceOutputAxis(id=AxisId("x"), size=10),
SpaceOutputAxis(id=AxisId("y"), size=20),
]
return _bioimage_model_siso_v5(
weights=weights,
input_axes=input_axes,
output_axes=output_axes,
input_test_tensor=input_test_tensor,
output_test_tensor=output_test_tensor,
)
def _bioimage_model_dummy_add_one_siso_pytorch_v5(
input_axes: List[InputAxis], input_test_tensor: np.ndarray
) -> io.BytesIO:
dummy_network = _DummyNetworkSingleInputAddOne()
with tempfile.NamedTemporaryFile(suffix=".pts", delete=False) as weights_file:
torch.save(dummy_network.state_dict(), weights_file.name)
weights = WeightsDescr(
pytorch_state_dict=PytorchStateDictWeightsDescr(
source=Path(weights_file.name),
architecture=ArchitectureFromLibraryDescr(
import_from="tests.conftest",
callable=Identifier(f"{_DummyNetworkSingleInputAddOne.__name__}"),
),
pytorch_version=Version("1.1.1"),
)
)
output_test_tensor = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
output_axes = [
BatchAxis(),
ChannelAxis(channel_names=[Identifier("channel1"), Identifier("channel2")]),
SpaceOutputAxis(id=AxisId("x"), size=10),
SpaceOutputAxis(id=AxisId("y"), size=20),
]
return _bioimage_model_siso_v5(
weights=weights,
input_axes=input_axes,
output_axes=output_axes,
input_test_tensor=input_test_tensor,
output_test_tensor=output_test_tensor,
)
def _bioimage_model_siso_v5(
weights: WeightsDescr,
input_axes: List[InputAxis],
output_axes: List[OutputAxis],
input_test_tensor: np.ndarray,
output_test_tensor: np.ndarray,
) -> io.BytesIO:
with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as input_test_tensor_file:
np.save(input_test_tensor_file.name, input_test_tensor)
input_tensor = InputTensorDescr(
id=TensorId("input"),
axes=input_axes,
description="",
test_tensor=FileDescr(source=Path(input_test_tensor_file.name)),
)
with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as output_test_tensor_file:
np.save(output_test_tensor_file.name, output_test_tensor)
output_tensor = OutputTensorDescr(
id=TensorId("output"),
axes=output_axes,
description="",
test_tensor=FileDescr(source=Path(output_test_tensor_file.name)),
)
return _bioimage_model_v5(weights=weights, inputs=[input_tensor], outputs=[output_tensor])
def _bioimage_model_v5(
weights: WeightsDescr, inputs: List[InputTensorDescr], outputs: List[OutputTensorDescr]
) -> io.BytesIO:
mocked_descr = ModelDescr(
name="mocked v5 model",
description="A test model for demonstration purposes only",
authors=[Author(name="me", affiliation="my institute", github_user="bioimageiobot")],
# change github_user to your GitHub account name
cite=[CiteEntry(text="for model training see my paper", doi=Doi("10.1234something"))],
license=LicenseId("MIT"),
documentation=HttpUrl("https://raw.githubusercontent.com/bioimage-io/spec-bioimage-io/main/README.md"),
git_repo=HttpUrl("https://github.com/bioimage-io/spec-bioimage-io"),
inputs=inputs,
outputs=outputs,
weights=weights,
)
model_bytes = io.BytesIO()
save_bioimageio_package_to_stream(mocked_descr, output_stream=model_bytes)
return model_bytes
@pytest.fixture(params=[WeightsFormat.PYTORCH, WeightsFormat.TORCHSCRIPT])
def bioimage_model_add_one_v4(request) -> io.BytesIO:
if request.param == WeightsFormat.PYTORCH:
return _bioimage_model_dummy_add_one_siso_pytorch_v4()
elif request.param == WeightsFormat.TORCHSCRIPT:
return _bioimage_model_dummy_add_one_siso_torchscript_v4()
else:
raise NotImplementedError(f"{request.param}")
def _bioimage_model_dummy_add_one_siso_pytorch_v4() -> io.BytesIO:
dummy_network = _DummyNetworkSingleInputAddOne()
input_test_tensor = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
output_test_tensor = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
traced_model = torch.jit.trace(dummy_network, example_inputs=torch.from_numpy(input_test_tensor))
with tempfile.NamedTemporaryFile(suffix=".pt", delete=False) as weights_file:
traced_model.save(weights_file.name)
weights = v0_4.WeightsDescr(torchscript=v0_4.TorchscriptWeightsDescr(source=Path(weights_file.name)))
model_bytes = _bioimage_model_siso_v4(
weights=weights, input_test_tensor=input_test_tensor, output_test_tensor=output_test_tensor
)
return model_bytes
def _bioimage_model_dummy_add_one_siso_torchscript_v4() -> io.BytesIO:
dummy_network = _DummyNetworkSingleInputAddOne()
input_test_tensor = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
output_test_tensor = np.arange(1 * 2 * 10 * 20, dtype="float32").reshape(1, 2, 10, 20)
traced_model = torch.jit.trace(dummy_network, example_inputs=torch.from_numpy(input_test_tensor))
with tempfile.NamedTemporaryFile(suffix=".pt", delete=False) as model_file:
traced_model.save(model_file.name)
weights = v0_4.WeightsDescr(torchscript=v0_4.TorchscriptWeightsDescr(source=Path(model_file.name)))
model_bytes = _bioimage_model_siso_v4(
weights=weights, input_test_tensor=input_test_tensor, output_test_tensor=output_test_tensor
)
return model_bytes
def _bioimage_model_siso_v4(
weights: v0_4.WeightsDescr, input_test_tensor: np.ndarray, output_test_tensor: np.ndarray
) -> io.BytesIO:
input_tensor = v0_4.InputTensorDescr(
name=v0_4.TensorName("input"), description="", axes="bcxy", shape=input_test_tensor.shape, data_type="float32"
)
output_tensor = v0_4.OutputTensorDescr(
name=v0_4.TensorName("output"), description="", axes="bcxy", shape=output_test_tensor.shape, data_type="float32"
)
with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as output_test_tensor_file:
np.save(output_test_tensor_file.name, output_test_tensor)
with tempfile.NamedTemporaryFile(suffix=".npy", delete=False) as input_test_tensor_file:
np.save(input_test_tensor_file.name, input_test_tensor)
model_descr = v0_4.ModelDescr(
name="mocked v4 model",
authors=[v0_4.Author(name="me")],
cite=[v0_4.CiteEntry(text="for model training see my paper", url=HttpUrl("https://doi.org/10.1234something"))],
description="",
inputs=[input_tensor],
outputs=[output_tensor],
documentation=HttpUrl("https://raw.githubusercontent.com/bioimage-io/spec-bioimage-io/main/README.md"),
license="MIT",
test_inputs=[Path(input_test_tensor_file.name)],
test_outputs=[Path(output_test_tensor_file.name)],
timestamp=v0_4.Datetime(root=datetime.now()),
weights=weights,
)
model_bytes = io.BytesIO()
save_bioimageio_package_to_stream(model_descr, output_stream=model_bytes)
return model_bytes
class _DummyNetworkSingleInputAddOne(nn.Module):
def forward(self, tensor: torch.Tensor) -> torch.Tensor:
return tensor + 1
class _DummyNetworkMultipleInputAddOne(nn.Module):
def forward(self, *tensors) -> torch.Tensor:
return tensors[0] + 1