1- """Implements the CRF post-processing step for the WNet3D.
1+ """Implements the CRF post-processing step for WNet3D.
22
33The CRF requires the following parameters:
44
1616Philipp Krähenbühl and Vladlen Koltun
1717NIPS 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+
2123import importlib
2224
2325import numpy as np
2830
2931spec = importlib .util .find_spec ("pydensecrf" )
3032CRF_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-
6341def 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
7953def 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
10781def 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
167142def 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
204179class 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." )
0 commit comments