Skip to content

Commit a72e931

Browse files
C-AchardMMathisLab
andauthored
CRF adjustments (#106)
* Remove optional CRF dep+update warning * Update crf.py * Update pyproject.toml --------- Co-authored-by: Mackenzie Mathis <[email protected]>
1 parent 8c6c306 commit a72e931

File tree

3 files changed

+24
-49
lines changed

3 files changed

+24
-49
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ The strength of our approach is we can match supervised model performance with p
5252

5353
![FIG1 (1)](https://github.com/user-attachments/assets/0d970b45-79ff-4c58-861f-e1e7dc9abc65)
5454

55-
**Figure 1. Performance of 3D Semantic and Instance Segmentation Models.**
56-
**a:** Raw mesoSPIM whole-brain sample, volumes and corresponding ground truth labels from somatosensory (S1) and visual (V1) cortical regions.
55+
**Figure 1. Performance of 3D Semantic and Instance Segmentation Models.**
56+
**a:** Raw mesoSPIM whole-brain sample, volumes and corresponding ground truth labels from somatosensory (S1) and visual (V1) cortical regions.
5757
**b:** Evaluation of instance segmentation performance for baseline
5858
thresholding-only, supervised models: Cellpose, StartDist, SwinUNetR, SegResNet, and our self-supervised model WNet3D over three data subsets.
5959
F1-score is computed from the Intersection over Union (IoU) with ground truth labels, then averaged. Error bars represent 50% Confidence Intervals
60-
(CIs).
61-
**c:** View of 3D instance labels from supervised models, as noted, for visual cortex volume in b evaluation.
60+
(CIs).
61+
**c:** View of 3D instance labels from supervised models, as noted, for visual cortex volume in b evaluation.
6262
**d:** Illustration of our WNet3D architecture showcasing the dual 3D U-Net structure with our modifications.
6363

6464

@@ -141,7 +141,7 @@ Before testing, install all requirements using ``pip install napari-cellseg3d[te
141141

142142
To run tests locally:
143143

144-
- Locally : run ``pytest`` in the plugin folder
144+
- Locally : run ``pytest napari_cellseg3d\_tests`` in the plugin folder.
145145
- Locally with coverage : In the plugin folder, run ``coverage run --source=napari_cellseg3d -m pytest`` then ``coverage xml`` to generate a .xml coverage file.
146146
- With tox : run ``tox`` in the plugin folder (will simulate tests with several python and OS configs, requires substantial storage space)
147147

napari_cellseg3d/code_models/crf.py

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Implements the CRF post-processing step for the WNet3D.
1+
"""Implements the CRF post-processing step for WNet3D.
22
33
The CRF requires the following parameters:
44
@@ -16,8 +16,10 @@
1616
Philipp Krähenbühl and Vladlen Koltun
1717
NIPS 2011
1818
19-
Implemented using the pydense library available at https://github.com/lucasb-eyer/pydensecrf.
19+
Implemented using the pydensecrf library available at https://github.com/lucasb-eyer/pydensecrf.
20+
However, this is not maintained, thus we maintain this pacakge at https://github.com/AdaptiveMotorControlLab/pydensecrf.
2021
"""
22+
2123
import importlib
2224

2325
import numpy as np
@@ -28,47 +30,19 @@
2830

2931
spec = importlib.util.find_spec("pydensecrf")
3032
CRF_INSTALLED = spec is not None
31-
if not CRF_INSTALLED:
32-
logger.info(
33-
"pydensecrf not installed, CRF post-processing will not be available. "
34-
"Please install by running : pip install pydensecrf "
35-
"This is not a hard requirement, you do not need it to install it unless you want to use the CRF post-processing step. "
36-
)
37-
else:
33+
if CRF_INSTALLED:
3834
import pydensecrf.densecrf as dcrf
3935
from pydensecrf.utils import (
4036
create_pairwise_bilateral,
4137
create_pairwise_gaussian,
4238
unary_from_softmax,
4339
)
4440

45-
__author__ = "Yves Paychère, Colin Hofmann, Cyril Achard"
46-
__credits__ = [
47-
"Yves Paychère",
48-
"Colin Hofmann",
49-
"Cyril Achard",
50-
"Philipp Krähenbühl",
51-
"Vladlen Koltun",
52-
"Liang-Chieh Chen",
53-
"George Papandreou",
54-
"Iasonas Kokkinos",
55-
"Kevin Murphy",
56-
"Alan L. Yuille",
57-
"Xide Xia",
58-
"Brian Kulis",
59-
"Lucas Beyer",
60-
]
61-
62-
6341
def correct_shape_for_crf(image, desired_dims=4):
6442
"""Corrects the shape of the image to be compatible with the CRF post-processing step."""
6543
logger.debug(f"Correcting shape for CRF, desired_dims={desired_dims}")
6644
logger.debug(f"Image shape: {image.shape}")
6745
if len(image.shape) > desired_dims:
68-
# if image.shape[0] > 1:
69-
# raise ValueError(
70-
# f"Image shape {image.shape} might have several channels"
71-
# )
7246
image = np.squeeze(image, axis=0)
7347
elif len(image.shape) < desired_dims:
7448
image = np.expand_dims(image, axis=0)
@@ -77,7 +51,7 @@ def correct_shape_for_crf(image, desired_dims=4):
7751

7852

7953
def crf_batch(images, probs, sa, sb, sg, w1, w2, n_iter=5):
80-
"""CRF post-processing step for the W-Net, applied to a batch of images.
54+
"""CRF post-processing step for the WNet3D, applied to a batch of images.
8155
8256
Args:
8357
images (np.ndarray): Array of shape (N, C, H, W, D) containing the input images.
@@ -105,7 +79,7 @@ def crf_batch(images, probs, sa, sb, sg, w1, w2, n_iter=5):
10579

10680

10781
def crf(image, prob, sa, sb, sg, w1, w2, n_iter=5):
108-
"""Implements the CRF post-processing step for the W-Net.
82+
"""Implements the CRF post-processing step for the WNet3D.
10983
11084
Inspired by https://arxiv.org/abs/1210.5644, https://arxiv.org/abs/1606.00915 and https://arxiv.org/abs/1711.08506.
11185
Implemented using the pydensecrf library.
@@ -124,14 +98,15 @@ def crf(image, prob, sa, sb, sg, w1, w2, n_iter=5):
12498
np.ndarray: Array of shape (K, H, W, D) containing the refined class probabilities for each pixel.
12599
"""
126100
if not CRF_INSTALLED:
101+
logger.info(
102+
"pydensecrf not installed, therefore CRF post-processing will not be available! Please install the package. "
103+
"Please install by running: pip install pydensecrf2 "
104+
)
127105
return None
128106

129107
d = dcrf.DenseCRF(
130108
image.shape[1] * image.shape[2] * image.shape[3], prob.shape[0]
131109
)
132-
# print(f"Image shape : {image.shape}")
133-
# print(f"Prob shape : {prob.shape}")
134-
# d = dcrf.DenseCRF(262144, 3) # npoints, nlabels
135110

136111
# Get unary potentials from softmax probabilities
137112
U = unary_from_softmax(prob)
@@ -165,7 +140,7 @@ def crf(image, prob, sa, sb, sg, w1, w2, n_iter=5):
165140

166141

167142
def crf_with_config(image, prob, config: CRFConfig = None, log=logger.info):
168-
"""Implements the CRF post-processing step for the W-Net.
143+
"""Implements the CRF post-processing step for the WNet3D.
169144
170145
Args:
171146
image (np.ndarray): Array of shape (C, H, W, D) containing the input image.
@@ -202,7 +177,7 @@ def crf_with_config(image, prob, config: CRFConfig = None, log=logger.info):
202177

203178

204179
class CRFWorker(GeneratorWorker):
205-
"""Worker for the CRF post-processing step for the W-Net."""
180+
"""Worker for the CRF post-processing step for the WNet3D."""
206181

207182
def __init__(
208183
self,
@@ -230,9 +205,12 @@ def __init__(
230205
self.log = log
231206

232207
def _run_crf_job(self):
233-
"""Runs the CRF post-processing step for the W-Net."""
208+
"""Runs the CRF post-processing step for the WNet3D."""
234209
if not CRF_INSTALLED:
235-
raise ImportError("pydensecrf is not installed.")
210+
logger.info(
211+
"pydensecrf not installed, therefore CRF post-processing will not be available! Please install the package. "
212+
"Please install by running: pip install pydensecrf2 "
213+
)
236214

237215
if len(self.images) != len(self.labels):
238216
raise ValueError("Number of images and labels must be the same.")

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ dependencies = [
4545
"pyclesperanto-prototype",
4646
"tqdm",
4747
"matplotlib",
48+
"pydensecrf2",
4849
]
4950
dynamic = ["version", "entry-points"]
5051

@@ -123,9 +124,6 @@ profile = "black"
123124
line_length = 79
124125

125126
[project.optional-dependencies]
126-
crf = [
127-
# "pydensecrf@git+https://github.com/lucasb-eyer/pydensecrf.git#egg=master",
128-
]
129127
pyqt5 = [
130128
"pyqt5",
131129
]
@@ -164,7 +162,6 @@ test = [
164162
"coverage",
165163
"tox",
166164
"twine",
167-
# "pydensecrf@git+https://github.com/lucasb-eyer/pydensecrf.git#egg=master",
168165
"onnx",
169166
"onnxruntime",
170167
]

0 commit comments

Comments
 (0)