senoquant 1.0.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- senoquant/__init__.py +6 -0
- senoquant/_reader.py +7 -0
- senoquant/_widget.py +33 -0
- senoquant/napari.yaml +83 -0
- senoquant/reader/__init__.py +5 -0
- senoquant/reader/core.py +369 -0
- senoquant/tabs/__init__.py +15 -0
- senoquant/tabs/batch/__init__.py +10 -0
- senoquant/tabs/batch/backend.py +641 -0
- senoquant/tabs/batch/config.py +270 -0
- senoquant/tabs/batch/frontend.py +1283 -0
- senoquant/tabs/batch/io.py +326 -0
- senoquant/tabs/batch/layers.py +86 -0
- senoquant/tabs/quantification/__init__.py +1 -0
- senoquant/tabs/quantification/backend.py +228 -0
- senoquant/tabs/quantification/features/__init__.py +80 -0
- senoquant/tabs/quantification/features/base.py +142 -0
- senoquant/tabs/quantification/features/marker/__init__.py +5 -0
- senoquant/tabs/quantification/features/marker/config.py +69 -0
- senoquant/tabs/quantification/features/marker/dialog.py +437 -0
- senoquant/tabs/quantification/features/marker/export.py +879 -0
- senoquant/tabs/quantification/features/marker/feature.py +119 -0
- senoquant/tabs/quantification/features/marker/morphology.py +285 -0
- senoquant/tabs/quantification/features/marker/rows.py +654 -0
- senoquant/tabs/quantification/features/marker/thresholding.py +46 -0
- senoquant/tabs/quantification/features/roi.py +346 -0
- senoquant/tabs/quantification/features/spots/__init__.py +5 -0
- senoquant/tabs/quantification/features/spots/config.py +62 -0
- senoquant/tabs/quantification/features/spots/dialog.py +477 -0
- senoquant/tabs/quantification/features/spots/export.py +1292 -0
- senoquant/tabs/quantification/features/spots/feature.py +112 -0
- senoquant/tabs/quantification/features/spots/morphology.py +279 -0
- senoquant/tabs/quantification/features/spots/rows.py +241 -0
- senoquant/tabs/quantification/frontend.py +815 -0
- senoquant/tabs/segmentation/__init__.py +1 -0
- senoquant/tabs/segmentation/backend.py +131 -0
- senoquant/tabs/segmentation/frontend.py +1009 -0
- senoquant/tabs/segmentation/models/__init__.py +5 -0
- senoquant/tabs/segmentation/models/base.py +146 -0
- senoquant/tabs/segmentation/models/cpsam/details.json +65 -0
- senoquant/tabs/segmentation/models/cpsam/model.py +150 -0
- senoquant/tabs/segmentation/models/default_2d/details.json +69 -0
- senoquant/tabs/segmentation/models/default_2d/model.py +664 -0
- senoquant/tabs/segmentation/models/default_3d/details.json +69 -0
- senoquant/tabs/segmentation/models/default_3d/model.py +682 -0
- senoquant/tabs/segmentation/models/hf.py +71 -0
- senoquant/tabs/segmentation/models/nuclear_dilation/__init__.py +1 -0
- senoquant/tabs/segmentation/models/nuclear_dilation/details.json +26 -0
- senoquant/tabs/segmentation/models/nuclear_dilation/model.py +96 -0
- senoquant/tabs/segmentation/models/perinuclear_rings/__init__.py +1 -0
- senoquant/tabs/segmentation/models/perinuclear_rings/details.json +34 -0
- senoquant/tabs/segmentation/models/perinuclear_rings/model.py +132 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/__init__.py +2 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/__init__.py +3 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/data/__init__.py +6 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/data/generate.py +470 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/data/prepare.py +273 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/data/rawdata.py +112 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/data/transform.py +384 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/internals/__init__.py +0 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/internals/blocks.py +184 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/internals/losses.py +79 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/internals/nets.py +165 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/internals/predict.py +467 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/internals/probability.py +67 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/internals/train.py +148 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/io/__init__.py +163 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/models/__init__.py +52 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/models/base_model.py +329 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/models/care_isotropic.py +160 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/models/care_projection.py +178 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/models/care_standard.py +446 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/models/care_upsampling.py +54 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/models/config.py +254 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/models/pretrained.py +119 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/scripts/__init__.py +0 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/scripts/care_predict.py +180 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/utils/__init__.py +5 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/utils/plot_utils.py +159 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/utils/six.py +18 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/utils/tf.py +644 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/utils/utils.py +272 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/csbdeep/version.py +1 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/docs/source/conf.py +368 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/setup.py +68 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/tests/test_datagen.py +169 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/tests/test_models.py +462 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/tests/test_utils.py +166 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_csbdeep/tools/create_zip_contents.py +34 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/__init__.py +30 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/big.py +624 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/bioimageio_utils.py +494 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/data/__init__.py +39 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/geometry/__init__.py +10 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/geometry/geom2d.py +215 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/geometry/geom3d.py +349 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/matching.py +483 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/models/__init__.py +28 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/models/base.py +1217 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/models/model2d.py +594 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/models/model3d.py +696 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/nms.py +384 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/plot/__init__.py +2 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/plot/plot.py +74 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/plot/render.py +298 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/rays3d.py +373 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/sample_patches.py +65 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/scripts/__init__.py +0 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/scripts/predict2d.py +90 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/scripts/predict3d.py +93 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/utils.py +408 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/_stardist/version.py +1 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/__init__.py +45 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/convert/__init__.py +17 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/convert/cli.py +55 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/convert/core.py +285 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/inspect/__init__.py +15 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/inspect/cli.py +36 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/inspect/divisibility.py +193 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/inspect/probe.py +100 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/inspect/receptive_field.py +182 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/inspect/rf_cli.py +48 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/inspect/valid_sizes.py +278 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/post/__init__.py +8 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/post/core.py +157 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/pre/__init__.py +17 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/pre/core.py +226 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/predict/__init__.py +5 -0
- senoquant/tabs/segmentation/stardist_onnx_utils/onnx_framework/predict/core.py +401 -0
- senoquant/tabs/settings/__init__.py +1 -0
- senoquant/tabs/settings/backend.py +29 -0
- senoquant/tabs/settings/frontend.py +19 -0
- senoquant/tabs/spots/__init__.py +1 -0
- senoquant/tabs/spots/backend.py +139 -0
- senoquant/tabs/spots/frontend.py +800 -0
- senoquant/tabs/spots/models/__init__.py +5 -0
- senoquant/tabs/spots/models/base.py +94 -0
- senoquant/tabs/spots/models/rmp/details.json +61 -0
- senoquant/tabs/spots/models/rmp/model.py +499 -0
- senoquant/tabs/spots/models/udwt/details.json +103 -0
- senoquant/tabs/spots/models/udwt/model.py +482 -0
- senoquant/utils.py +25 -0
- senoquant-1.0.0b1.dist-info/METADATA +193 -0
- senoquant-1.0.0b1.dist-info/RECORD +148 -0
- senoquant-1.0.0b1.dist-info/WHEEL +5 -0
- senoquant-1.0.0b1.dist-info/entry_points.txt +2 -0
- senoquant-1.0.0b1.dist-info/licenses/LICENSE +28 -0
- senoquant-1.0.0b1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
try:
|
|
3
|
+
from importlib.metadata import requires
|
|
4
|
+
except ImportError:
|
|
5
|
+
from importlib_metadata import requires
|
|
6
|
+
from zipfile import ZipFile
|
|
7
|
+
import numpy as np
|
|
8
|
+
import tempfile
|
|
9
|
+
from packaging.version import Version
|
|
10
|
+
from csbdeep.utils import axes_check_and_normalize, normalize, _raise
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
DEEPIMAGEJ_MACRO = \
|
|
14
|
+
"""
|
|
15
|
+
//*******************************************************************
|
|
16
|
+
// Date: July-2021
|
|
17
|
+
// Credits: StarDist, DeepImageJ
|
|
18
|
+
// URL:
|
|
19
|
+
// https://github.com/stardist/stardist
|
|
20
|
+
// https://deepimagej.github.io/deepimagej
|
|
21
|
+
// This macro was adapted from
|
|
22
|
+
// https://github.com/deepimagej/imagej-macros/blob/648caa867f6ccb459649d4d3799efa1e2e0c5204/StarDist2D_Post-processing.ijm
|
|
23
|
+
// Please cite the respective contributions when using this code.
|
|
24
|
+
//*******************************************************************
|
|
25
|
+
// Macro to run StarDist postprocessing on 2D images.
|
|
26
|
+
// StarDist and deepImageJ plugins need to be installed.
|
|
27
|
+
// The macro assumes that the image to process is a stack in which
|
|
28
|
+
// the first channel corresponds to the object probability map
|
|
29
|
+
// and the remaining channels are the radial distances from each
|
|
30
|
+
// pixel to the object boundary.
|
|
31
|
+
//*******************************************************************
|
|
32
|
+
|
|
33
|
+
// Get the name of the image to call it
|
|
34
|
+
getDimensions(width, height, channels, slices, frames);
|
|
35
|
+
name=getTitle();
|
|
36
|
+
|
|
37
|
+
probThresh={probThresh};
|
|
38
|
+
nmsThresh={nmsThresh};
|
|
39
|
+
|
|
40
|
+
// Isolate the detection probability scores
|
|
41
|
+
run("Make Substack...", "channels=1");
|
|
42
|
+
rename("scores");
|
|
43
|
+
|
|
44
|
+
// Isolate the oriented distances
|
|
45
|
+
run("Fire");
|
|
46
|
+
selectWindow(name);
|
|
47
|
+
run("Delete Slice", "delete=channel");
|
|
48
|
+
selectWindow(name);
|
|
49
|
+
run("Properties...", "channels=" + maxOf(channels, slices) - 1 + " slices=1 frames=1 pixel_width=1.0000 pixel_height=1.0000 voxel_depth=1.0000");
|
|
50
|
+
rename("distances");
|
|
51
|
+
run("royal");
|
|
52
|
+
|
|
53
|
+
// Run StarDist plugin
|
|
54
|
+
run("Command From Macro", "command=[de.csbdresden.stardist.StarDist2DNMS], args=['prob':'scores', 'dist':'distances', 'probThresh':'" + probThresh + "', 'nmsThresh':'" + nmsThresh + "', 'outputType':'Both', 'excludeBoundary':'2', 'roiPosition':'Stack', 'verbose':'false'], process=[false]");
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _import(error=True):
|
|
59
|
+
try:
|
|
60
|
+
from importlib_metadata import metadata
|
|
61
|
+
from bioimageio.core.build_spec import build_model # type: ignore
|
|
62
|
+
import xarray as xr
|
|
63
|
+
import bioimageio.core # type: ignore
|
|
64
|
+
except ImportError:
|
|
65
|
+
if error:
|
|
66
|
+
raise RuntimeError(
|
|
67
|
+
"Required libraries are missing for bioimage.io model export.\n"
|
|
68
|
+
"Please install StarDist as follows: pip install 'stardist[bioimageio]'\n"
|
|
69
|
+
"(You do not need to uninstall StarDist first.)"
|
|
70
|
+
)
|
|
71
|
+
else:
|
|
72
|
+
return None
|
|
73
|
+
return metadata, build_model, bioimageio.core, xr
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _create_stardist_dependencies(outdir):
|
|
77
|
+
from ruamel.yaml import YAML
|
|
78
|
+
from packaging.requirements import Requirement
|
|
79
|
+
from tensorflow import __version__ as tf_version
|
|
80
|
+
from . import __version__ as stardist_version
|
|
81
|
+
# dependencies that start with the name "bioimageio" will be added as conda dependencies
|
|
82
|
+
reqs_conda = []
|
|
83
|
+
for req_str in requires("stardist"):
|
|
84
|
+
req = Requirement(req_str)
|
|
85
|
+
if (
|
|
86
|
+
req.marker is not None
|
|
87
|
+
# only include requirements that are for the "bioimageio" extra
|
|
88
|
+
and req.marker.evaluate({'extra': 'bioimageio'})
|
|
89
|
+
and not req.marker.evaluate({'extra': ''})
|
|
90
|
+
# and package name starts with "bioimageio"
|
|
91
|
+
and req.name.startswith('bioimageio')
|
|
92
|
+
):
|
|
93
|
+
# https://packaging.pypa.io/en/stable/requirements.html
|
|
94
|
+
reqs_conda.append(f"{req.name}{req.specifier}")
|
|
95
|
+
# only stardist and tensorflow as pip dependencies
|
|
96
|
+
v_tf = Version(tf_version)
|
|
97
|
+
reqs_pip = (f"stardist>={stardist_version}", f"tensorflow>={v_tf.major}.{v_tf.minor},<{v_tf.major+1}")
|
|
98
|
+
# conda environment
|
|
99
|
+
env = dict(
|
|
100
|
+
name = 'stardist',
|
|
101
|
+
channels = ['defaults', 'conda-forge'],
|
|
102
|
+
dependencies = [
|
|
103
|
+
('python>=3.7,<3.8' if v_tf.major == 1 else 'python>=3.7'),
|
|
104
|
+
*reqs_conda,
|
|
105
|
+
'pip', {'pip': reqs_pip},
|
|
106
|
+
],
|
|
107
|
+
)
|
|
108
|
+
yaml = YAML(typ='safe')
|
|
109
|
+
path = outdir / "environment.yaml"
|
|
110
|
+
with open(path, "w") as f:
|
|
111
|
+
yaml.dump(env, f)
|
|
112
|
+
return f"conda:{path}"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _create_stardist_doc(outdir):
|
|
116
|
+
doc_path = outdir / "README.md"
|
|
117
|
+
text = (
|
|
118
|
+
"# StarDist Model\n"
|
|
119
|
+
"This is a model for object detection with star-convex shapes.\n"
|
|
120
|
+
"Please see the [StarDist repository](https://github.com/stardist/stardist) for details."
|
|
121
|
+
)
|
|
122
|
+
with open(doc_path, "w") as f:
|
|
123
|
+
f.write(text)
|
|
124
|
+
return doc_path
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _get_stardist_metadata(outdir, model, generate_default_deps):
|
|
128
|
+
metadata, *_ = _import()
|
|
129
|
+
package_data = metadata("stardist")
|
|
130
|
+
doi_2d = "https://doi.org/10.1007/978-3-030-00934-2_30"
|
|
131
|
+
doi_3d = "https://doi.org/10.1109/WACV45572.2020.9093435"
|
|
132
|
+
authors = {
|
|
133
|
+
'Martin Weigert': dict(name='Martin Weigert', github_user='maweigert'),
|
|
134
|
+
'Uwe Schmidt': dict(name='Uwe Schmidt', github_user='uschmidt83'),
|
|
135
|
+
}
|
|
136
|
+
data = dict(
|
|
137
|
+
description=package_data["Summary"],
|
|
138
|
+
authors=list(authors.get(name.strip(),dict(name=name.strip())) for name in package_data["Author"].split(",")),
|
|
139
|
+
git_repo=package_data["Home-Page"],
|
|
140
|
+
license=package_data["License"],
|
|
141
|
+
cite=[{"text": "Cell Detection with Star-Convex Polygons", "doi": doi_2d},
|
|
142
|
+
{"text": "Star-convex Polyhedra for 3D Object Detection and Segmentation in Microscopy", "doi": doi_3d}],
|
|
143
|
+
tags=[
|
|
144
|
+
'fluorescence-light-microscopy', 'whole-slide-imaging', 'other', # modality
|
|
145
|
+
f'{model.config.n_dim}d', # dims
|
|
146
|
+
'cells', 'nuclei', # content
|
|
147
|
+
'tensorflow', # framework
|
|
148
|
+
'fiji', # software
|
|
149
|
+
'unet', # network
|
|
150
|
+
'instance-segmentation', 'object-detection', # task
|
|
151
|
+
'stardist',
|
|
152
|
+
],
|
|
153
|
+
covers=["https://raw.githubusercontent.com/stardist/stardist/main/images/stardist_logo.jpg"],
|
|
154
|
+
documentation=_create_stardist_doc(outdir),
|
|
155
|
+
)
|
|
156
|
+
if generate_default_deps: # only if requested, as not required for bioimage.io
|
|
157
|
+
data['dependencies'] = _create_stardist_dependencies(outdir)
|
|
158
|
+
|
|
159
|
+
return data
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _predict_tf(model_path, test_input):
|
|
163
|
+
import tensorflow as tf
|
|
164
|
+
from csbdeep.utils.tf import IS_TF_1
|
|
165
|
+
# need to unzip the model assets
|
|
166
|
+
model_assets = model_path.parent / "tf_model"
|
|
167
|
+
with ZipFile(model_path, "r") as f:
|
|
168
|
+
f.extractall(model_assets)
|
|
169
|
+
if IS_TF_1:
|
|
170
|
+
# make a new graph, i.e. don't use the global default graph
|
|
171
|
+
with tf.Graph().as_default():
|
|
172
|
+
with tf.Session() as sess:
|
|
173
|
+
tf_model = tf.saved_model.load_v2(str(model_assets))
|
|
174
|
+
x = tf.convert_to_tensor(test_input, dtype=tf.float32)
|
|
175
|
+
model = tf_model.signatures["serving_default"]
|
|
176
|
+
y = model(x)
|
|
177
|
+
sess.run(tf.global_variables_initializer())
|
|
178
|
+
output = sess.run(y["output"])
|
|
179
|
+
else:
|
|
180
|
+
tf_model = tf.saved_model.load(str(model_assets))
|
|
181
|
+
x = tf.convert_to_tensor(test_input, dtype=tf.float32)
|
|
182
|
+
model = tf_model.signatures["serving_default"]
|
|
183
|
+
y = model(x)
|
|
184
|
+
output = y["output"].numpy()
|
|
185
|
+
return output
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _get_weights_and_model_metadata(outdir, model, test_input, test_input_axes, test_input_norm_axes, mode, min_percentile, max_percentile):
|
|
189
|
+
|
|
190
|
+
# get the path to the exported model assets (saved in outdir)
|
|
191
|
+
if mode == "keras_hdf5":
|
|
192
|
+
raise NotImplementedError("Export to keras format is not supported yet")
|
|
193
|
+
elif mode == "tensorflow_saved_model_bundle":
|
|
194
|
+
assets_uri = outdir / "TF_SavedModel.zip"
|
|
195
|
+
model_csbdeep = model.export_TF(assets_uri, single_output=True, upsample_grid=True)
|
|
196
|
+
else:
|
|
197
|
+
raise ValueError(f"Unsupported mode: {mode}")
|
|
198
|
+
|
|
199
|
+
# to force "inputs.data_type: float32" in the spec (bonus: disables normalization warning in model._predict_setup)
|
|
200
|
+
test_input = test_input.astype(np.float32)
|
|
201
|
+
|
|
202
|
+
# convert test_input to axes_net semantics and shape, also resize if necessary (to adhere to axes_net_div_by)
|
|
203
|
+
test_input, axes_img, axes_net, axes_net_div_by, *_ = model._predict_setup(
|
|
204
|
+
img=test_input,
|
|
205
|
+
axes=test_input_axes,
|
|
206
|
+
normalizer=None,
|
|
207
|
+
n_tiles=None,
|
|
208
|
+
show_tile_progress=False,
|
|
209
|
+
predict_kwargs={},
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
# normalization axes string and numeric indices
|
|
213
|
+
axes_norm = set(axes_net).intersection(set(axes_check_and_normalize(test_input_norm_axes, disallowed='S')))
|
|
214
|
+
axes_norm = "".join(a for a in axes_net if a in axes_norm) # preserve order of axes_net
|
|
215
|
+
axes_norm_num = tuple(axes_net.index(a) for a in axes_norm)
|
|
216
|
+
|
|
217
|
+
# normalize input image
|
|
218
|
+
test_input_norm = normalize(test_input, pmin=min_percentile, pmax=max_percentile, axis=axes_norm_num)
|
|
219
|
+
|
|
220
|
+
net_axes_in = axes_net.lower()
|
|
221
|
+
net_axes_out = axes_check_and_normalize(model._axes_out).lower()
|
|
222
|
+
ndim_tensor = len(net_axes_out) + 1
|
|
223
|
+
|
|
224
|
+
input_min_shape = list(axes_net_div_by)
|
|
225
|
+
input_min_shape[axes_net.index('C')] = model.config.n_channel_in
|
|
226
|
+
input_step = list(axes_net_div_by)
|
|
227
|
+
input_step[axes_net.index('C')] = 0
|
|
228
|
+
|
|
229
|
+
# add the batch axis to shape and step
|
|
230
|
+
input_min_shape = [1] + input_min_shape
|
|
231
|
+
input_step = [0] + input_step
|
|
232
|
+
|
|
233
|
+
# the axes strings in bioimageio convention
|
|
234
|
+
input_axes = "b" + net_axes_in.lower()
|
|
235
|
+
output_axes = "b" + net_axes_out.lower()
|
|
236
|
+
|
|
237
|
+
if mode == "keras_hdf5":
|
|
238
|
+
output_names = ("prob", "dist") + (("class_prob",) if model._is_multiclass() else ())
|
|
239
|
+
output_n_channels = (1, model.config.n_rays,) + ((1,) if model._is_multiclass() else ())
|
|
240
|
+
# the output shape is computed from the input shape using
|
|
241
|
+
# output_shape[i] = output_scale[i] * input_shape[i] + 2 * output_offset[i]
|
|
242
|
+
output_scale = [1]+list(1/g for g in model.config.grid) + [0]
|
|
243
|
+
output_offset = [0]*(ndim_tensor)
|
|
244
|
+
|
|
245
|
+
elif mode == "tensorflow_saved_model_bundle":
|
|
246
|
+
if model._is_multiclass():
|
|
247
|
+
raise NotImplementedError("Tensorflow SavedModel not supported for multiclass models yet")
|
|
248
|
+
# regarding input/output names: https://github.com/CSBDeep/CSBDeep/blob/b0d2f5f344ebe65a9b4c3007f4567fe74268c813/csbdeep/utils/tf.py#L193-L194
|
|
249
|
+
input_names = ["input"]
|
|
250
|
+
output_names = ["output"]
|
|
251
|
+
output_n_channels = (1 + model.config.n_rays,)
|
|
252
|
+
# the output shape is computed from the input shape using
|
|
253
|
+
# output_shape[i] = output_scale[i] * input_shape[i] + 2 * output_offset[i]
|
|
254
|
+
# same shape as input except for the channel dimension
|
|
255
|
+
output_scale = [1]*(ndim_tensor)
|
|
256
|
+
output_scale[output_axes.index("c")] = 0
|
|
257
|
+
# no offset, except for the input axes, where it is output channel / 2
|
|
258
|
+
output_offset = [0.0]*(ndim_tensor)
|
|
259
|
+
output_offset[output_axes.index("c")] = output_n_channels[0] / 2.0
|
|
260
|
+
|
|
261
|
+
assert all(s in (0, 1) for s in output_scale), "halo computation assumption violated"
|
|
262
|
+
halo = model._axes_tile_overlap(output_axes.replace('b', 's'))
|
|
263
|
+
halo = [int(np.ceil(v/8)*8) for v in halo] # optional: round up to be divisible by 8
|
|
264
|
+
|
|
265
|
+
# the output shape needs to be valid after cropping the halo, so we add the halo to the input min shape
|
|
266
|
+
input_min_shape = [ms + 2 * ha for ms, ha in zip(input_min_shape, halo)]
|
|
267
|
+
|
|
268
|
+
# make sure the input min shape is still divisible by the min axis divisor
|
|
269
|
+
input_min_shape = input_min_shape[:1] + [ms + (-ms % div_by) for ms, div_by in zip(input_min_shape[1:], axes_net_div_by)]
|
|
270
|
+
assert all(ms % div_by == 0 for ms, div_by in zip(input_min_shape[1:], axes_net_div_by))
|
|
271
|
+
|
|
272
|
+
metadata, *_ = _import()
|
|
273
|
+
package_data = metadata("stardist")
|
|
274
|
+
is_2D = model.config.n_dim == 2
|
|
275
|
+
|
|
276
|
+
weights_file = outdir / "stardist_weights.h5"
|
|
277
|
+
model.keras_model.save_weights(str(weights_file))
|
|
278
|
+
|
|
279
|
+
config = dict(
|
|
280
|
+
stardist=dict(
|
|
281
|
+
python_version=package_data["Version"],
|
|
282
|
+
thresholds=dict(model.thresholds._asdict()),
|
|
283
|
+
weights=weights_file.name,
|
|
284
|
+
config=vars(model.config),
|
|
285
|
+
)
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
if is_2D:
|
|
289
|
+
macro_file = outdir / "stardist_postprocessing.ijm"
|
|
290
|
+
with open(str(macro_file), 'w', encoding='utf-8') as f:
|
|
291
|
+
f.write(DEEPIMAGEJ_MACRO.format(probThresh=model.thresholds.prob, nmsThresh=model.thresholds.nms))
|
|
292
|
+
config['stardist'].update(postprocessing_macro=macro_file.name)
|
|
293
|
+
|
|
294
|
+
n_inputs = len(input_names)
|
|
295
|
+
assert n_inputs == 1
|
|
296
|
+
input_config = dict(
|
|
297
|
+
input_names=input_names,
|
|
298
|
+
input_min_shape=[input_min_shape],
|
|
299
|
+
input_step=[input_step],
|
|
300
|
+
input_axes=[input_axes],
|
|
301
|
+
input_data_range=[["-inf", "inf"]],
|
|
302
|
+
preprocessing=[[dict(
|
|
303
|
+
name="scale_range",
|
|
304
|
+
kwargs=dict(
|
|
305
|
+
mode="per_sample",
|
|
306
|
+
axes=axes_norm.lower(),
|
|
307
|
+
min_percentile=min_percentile,
|
|
308
|
+
max_percentile=max_percentile,
|
|
309
|
+
))]]
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
n_outputs = len(output_names)
|
|
313
|
+
output_config = dict(
|
|
314
|
+
output_names=output_names,
|
|
315
|
+
output_data_range=[["-inf", "inf"]] * n_outputs,
|
|
316
|
+
output_axes=[output_axes] * n_outputs,
|
|
317
|
+
output_reference=[input_names[0]] * n_outputs,
|
|
318
|
+
output_scale=[output_scale] * n_outputs,
|
|
319
|
+
output_offset=[output_offset] * n_outputs,
|
|
320
|
+
halo=[halo] * n_outputs
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
in_path = outdir / "test_input.npy"
|
|
324
|
+
np.save(in_path, test_input[np.newaxis])
|
|
325
|
+
|
|
326
|
+
if mode == "tensorflow_saved_model_bundle":
|
|
327
|
+
test_outputs = _predict_tf(assets_uri, test_input_norm[np.newaxis])
|
|
328
|
+
else:
|
|
329
|
+
test_outputs = model.predict(test_input_norm)
|
|
330
|
+
|
|
331
|
+
# out_paths = []
|
|
332
|
+
# for i, out in enumerate(test_outputs):
|
|
333
|
+
# p = outdir / f"test_output{i}.npy"
|
|
334
|
+
# np.save(p, out)
|
|
335
|
+
# out_paths.append(p)
|
|
336
|
+
assert n_outputs == 1
|
|
337
|
+
out_paths = [outdir / "test_output.npy"]
|
|
338
|
+
np.save(out_paths[0], test_outputs)
|
|
339
|
+
|
|
340
|
+
from tensorflow import __version__ as tf_version
|
|
341
|
+
data = dict(weight_uri=assets_uri, test_inputs=[in_path], test_outputs=out_paths,
|
|
342
|
+
config=config, tensorflow_version=tf_version)
|
|
343
|
+
data.update(input_config)
|
|
344
|
+
data.update(output_config)
|
|
345
|
+
_files = [str(weights_file)]
|
|
346
|
+
if is_2D:
|
|
347
|
+
_files.append(str(macro_file))
|
|
348
|
+
data.update(attachments=dict(files=_files))
|
|
349
|
+
|
|
350
|
+
return data
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def export_bioimageio(
|
|
354
|
+
model,
|
|
355
|
+
outpath,
|
|
356
|
+
test_input,
|
|
357
|
+
test_input_axes=None,
|
|
358
|
+
test_input_norm_axes='ZYX',
|
|
359
|
+
name=None,
|
|
360
|
+
mode="tensorflow_saved_model_bundle",
|
|
361
|
+
min_percentile=1.0,
|
|
362
|
+
max_percentile=99.8,
|
|
363
|
+
overwrite_spec_kwargs=None,
|
|
364
|
+
generate_default_deps=False,
|
|
365
|
+
):
|
|
366
|
+
"""Export stardist model into bioimage.io format, https://github.com/bioimage-io/spec-bioimage-io.
|
|
367
|
+
|
|
368
|
+
Parameters
|
|
369
|
+
----------
|
|
370
|
+
model: StarDist2D, StarDist3D
|
|
371
|
+
the model to convert
|
|
372
|
+
outpath: str, Path
|
|
373
|
+
where to save the model
|
|
374
|
+
test_input: np.ndarray
|
|
375
|
+
input image for generating test data
|
|
376
|
+
test_input_axes: str or None
|
|
377
|
+
the axes of the test input, for example 'YX' for a 2d image or 'ZYX' for a 3d volume
|
|
378
|
+
using None assumes that axes of test_input are the same as those of model
|
|
379
|
+
test_input_norm_axes: str
|
|
380
|
+
the axes of the test input which will be jointly normalized, for example 'ZYX' for all spatial dimensions ('Z' ignored for 2D input)
|
|
381
|
+
use 'ZYXC' to also jointly normalize channels (e.g. for RGB input images)
|
|
382
|
+
name: str
|
|
383
|
+
the name of this model (default: None)
|
|
384
|
+
if None, uses the (folder) name of the model (i.e. `model.name`)
|
|
385
|
+
mode: str
|
|
386
|
+
the export type for this model (default: "tensorflow_saved_model_bundle")
|
|
387
|
+
min_percentile: float
|
|
388
|
+
min percentile to be used for image normalization (default: 1.0)
|
|
389
|
+
max_percentile: float
|
|
390
|
+
max percentile to be used for image normalization (default: 99.8)
|
|
391
|
+
overwrite_spec_kwargs: dict or None
|
|
392
|
+
spec keywords that should be overloaded (default: None)
|
|
393
|
+
generate_default_deps: bool
|
|
394
|
+
not required for bioimage.io, i.e. StarDist models don't need a dependencies field in rdf.yaml (default: False)
|
|
395
|
+
if True, generate an environment.yaml file recording the python, bioimageio.core, stardist and tensorflow requirements
|
|
396
|
+
from which a conda environment can be recreated to run this export
|
|
397
|
+
"""
|
|
398
|
+
_, build_model, *_ = _import()
|
|
399
|
+
from .models import StarDist2D, StarDist3D
|
|
400
|
+
isinstance(model, (StarDist2D, StarDist3D)) or _raise(ValueError("not a valid model"))
|
|
401
|
+
0 <= min_percentile < max_percentile <= 100 or _raise(ValueError("invalid percentile values"))
|
|
402
|
+
|
|
403
|
+
if name is None:
|
|
404
|
+
name = model.name
|
|
405
|
+
name = str(name)
|
|
406
|
+
|
|
407
|
+
outpath = Path(outpath)
|
|
408
|
+
if outpath.suffix == "":
|
|
409
|
+
outdir = outpath
|
|
410
|
+
zip_path = outdir / f"{name}.zip"
|
|
411
|
+
elif outpath.suffix == ".zip":
|
|
412
|
+
outdir = outpath.parent
|
|
413
|
+
zip_path = outpath
|
|
414
|
+
else:
|
|
415
|
+
raise ValueError(f"outpath has to be a folder or zip file, got {outpath}")
|
|
416
|
+
outdir.mkdir(exist_ok=True, parents=True)
|
|
417
|
+
|
|
418
|
+
with tempfile.TemporaryDirectory() as _tmp_dir:
|
|
419
|
+
tmp_dir = Path(_tmp_dir)
|
|
420
|
+
kwargs = _get_stardist_metadata(tmp_dir, model, generate_default_deps)
|
|
421
|
+
model_kwargs = _get_weights_and_model_metadata(tmp_dir, model, test_input, test_input_axes, test_input_norm_axes, mode,
|
|
422
|
+
min_percentile=min_percentile, max_percentile=max_percentile)
|
|
423
|
+
kwargs.update(model_kwargs)
|
|
424
|
+
if overwrite_spec_kwargs is not None:
|
|
425
|
+
kwargs.update(overwrite_spec_kwargs)
|
|
426
|
+
|
|
427
|
+
build_model(name=name, output_path=zip_path, add_deepimagej_config=(model.config.n_dim==2), root=tmp_dir, **kwargs)
|
|
428
|
+
print(f"\nbioimage.io model with name '{name}' exported to '{zip_path}'")
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
def import_bioimageio(source, outpath):
|
|
432
|
+
"""Import stardist model from bioimage.io format, https://github.com/bioimage-io/spec-bioimage-io.
|
|
433
|
+
|
|
434
|
+
Load a model in bioimage.io format from the given `source` (e.g. path to zip file, URL)
|
|
435
|
+
and convert it to a regular stardist model, which will be saved in the folder `outpath`.
|
|
436
|
+
|
|
437
|
+
Parameters
|
|
438
|
+
----------
|
|
439
|
+
source: str, Path
|
|
440
|
+
bioimage.io resource (e.g. path, URL)
|
|
441
|
+
outpath: str, Path
|
|
442
|
+
folder to save the stardist model (must not exist previously)
|
|
443
|
+
|
|
444
|
+
Returns
|
|
445
|
+
-------
|
|
446
|
+
StarDist2D or StarDist3D
|
|
447
|
+
stardist model loaded from `outpath`
|
|
448
|
+
|
|
449
|
+
"""
|
|
450
|
+
import shutil, uuid
|
|
451
|
+
from csbdeep.utils import save_json
|
|
452
|
+
from .models import StarDist2D, StarDist3D
|
|
453
|
+
*_, bioimageio_core, _ = _import()
|
|
454
|
+
|
|
455
|
+
outpath = Path(outpath)
|
|
456
|
+
not outpath.exists() or _raise(FileExistsError(f"'{outpath}' already exists"))
|
|
457
|
+
|
|
458
|
+
with tempfile.TemporaryDirectory() as _tmp_dir:
|
|
459
|
+
tmp_dir = Path(_tmp_dir)
|
|
460
|
+
# download the full model content to a temporary folder
|
|
461
|
+
zip_path = tmp_dir / f"{str(uuid.uuid4())}.zip"
|
|
462
|
+
bioimageio_core.export_resource_package(source, output_path=zip_path)
|
|
463
|
+
with ZipFile(zip_path, "r") as zip_ref:
|
|
464
|
+
zip_ref.extractall(tmp_dir)
|
|
465
|
+
zip_path.unlink()
|
|
466
|
+
rdf_path = tmp_dir / "rdf.yaml"
|
|
467
|
+
biomodel = bioimageio_core.load_resource_description(rdf_path)
|
|
468
|
+
|
|
469
|
+
# read the stardist specific content
|
|
470
|
+
'stardist' in biomodel.config or _raise(RuntimeError("bioimage.io model not compatible"))
|
|
471
|
+
config = biomodel.config['stardist']['config']
|
|
472
|
+
thresholds = biomodel.config['stardist']['thresholds']
|
|
473
|
+
weights = biomodel.config['stardist']['weights']
|
|
474
|
+
|
|
475
|
+
# make sure that the keras weights are in the attachments
|
|
476
|
+
weights_file = None
|
|
477
|
+
for f in biomodel.attachments.files:
|
|
478
|
+
if f.name == weights and f.exists():
|
|
479
|
+
weights_file = f
|
|
480
|
+
break
|
|
481
|
+
weights_file is not None or _raise(FileNotFoundError(f"couldn't find weights file '{weights}'"))
|
|
482
|
+
|
|
483
|
+
# save the config and threshold to json, and weights to hdf5 to enable loading as stardist model
|
|
484
|
+
# copy bioimageio files to separate sub-folder
|
|
485
|
+
outpath.mkdir(parents=True)
|
|
486
|
+
save_json(config, str(outpath / 'config.json'))
|
|
487
|
+
save_json(thresholds, str(outpath / 'thresholds.json'))
|
|
488
|
+
shutil.copy(str(weights_file), str(outpath / "weights_bioimageio.h5"))
|
|
489
|
+
shutil.copytree(str(tmp_dir), str(outpath / "bioimageio"))
|
|
490
|
+
|
|
491
|
+
model_class = (StarDist2D if config['n_dim'] == 2 else StarDist3D)
|
|
492
|
+
model = model_class(None, outpath.name, basedir=str(outpath.parent))
|
|
493
|
+
|
|
494
|
+
return model
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
def abspath(path):
|
|
2
|
+
import os
|
|
3
|
+
base_path = os.path.abspath(os.path.dirname(__file__))
|
|
4
|
+
return os.path.join(base_path, path)
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_image_nuclei_2d(return_mask=False):
|
|
8
|
+
""" Fluorescence microscopy image and mask from the 2018 kaggle DSB challenge
|
|
9
|
+
|
|
10
|
+
Caicedo et al. "Nucleus segmentation across imaging experiments: the 2018 Data Science Bowl." Nature methods 16.12
|
|
11
|
+
"""
|
|
12
|
+
from tifffile import imread
|
|
13
|
+
img = imread(abspath("images/img2d.tif"))
|
|
14
|
+
mask = imread(abspath("images/mask2d.tif"))
|
|
15
|
+
if return_mask:
|
|
16
|
+
return img, mask
|
|
17
|
+
else:
|
|
18
|
+
return img
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_image_he_2d():
|
|
22
|
+
""" H&E stained RGB example image from the Cancer Imaging Archive
|
|
23
|
+
https://www.cancerimagingarchive.net
|
|
24
|
+
"""
|
|
25
|
+
from imageio import imread
|
|
26
|
+
img = imread(abspath("images/histo.jpg"))
|
|
27
|
+
return img
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_image_nuclei_3d(return_mask=False):
|
|
31
|
+
""" synthetic nuclei
|
|
32
|
+
"""
|
|
33
|
+
from tifffile import imread
|
|
34
|
+
img = imread(abspath("images/img3d.tif"))
|
|
35
|
+
mask = imread(abspath("images/mask3d.tif"))
|
|
36
|
+
if return_mask:
|
|
37
|
+
return img, mask
|
|
38
|
+
else:
|
|
39
|
+
return img
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from __future__ import absolute_import, print_function
|
|
2
|
+
|
|
3
|
+
# TODO: rethink naming for 2D/3D functions
|
|
4
|
+
|
|
5
|
+
from .geom2d import star_dist, relabel_image_stardist, ray_angles, dist_to_coord, polygons_to_label, polygons_to_label_coord
|
|
6
|
+
from .geom3d import star_dist3D, polyhedron_to_label, relabel_image_stardist3D, dist_to_coord3D, export_to_obj_file3D
|
|
7
|
+
|
|
8
|
+
from .geom2d import _dist_to_coord_old, _polygons_to_label_old
|
|
9
|
+
|
|
10
|
+
#, dist_to_volume, dist_to_centroid
|