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"""
2122
2223import importlib
3738 unary_from_softmax ,
3839 )
3940
40- __author__ = "Yves Paychère, Colin Hofmann, Cyril Achard"
41- __credits__ = [
42- "Yves Paychère" ,
43- "Colin Hofmann" ,
44- "Cyril Achard" ,
45- "Philipp Krähenbühl" ,
46- "Vladlen Koltun" ,
47- "Liang-Chieh Chen" ,
48- "George Papandreou" ,
49- "Iasonas Kokkinos" ,
50- "Kevin Murphy" ,
51- "Alan L. Yuille" ,
52- "Xide Xia" ,
53- "Brian Kulis" ,
54- "Lucas Beyer" ,
55- ]
56-
57-
5841def correct_shape_for_crf (image , desired_dims = 4 ):
5942 """Corrects the shape of the image to be compatible with the CRF post-processing step."""
6043 logger .debug (f"Correcting shape for CRF, desired_dims={ desired_dims } " )
6144 logger .debug (f"Image shape: { image .shape } " )
6245 if len (image .shape ) > desired_dims :
63- # if image.shape[0] > 1:
64- # raise ValueError(
65- # f"Image shape {image.shape} might have several channels"
66- # )
6746 image = np .squeeze (image , axis = 0 )
6847 elif len (image .shape ) < desired_dims :
6948 image = np .expand_dims (image , axis = 0 )
@@ -72,7 +51,7 @@ def correct_shape_for_crf(image, desired_dims=4):
7251
7352
7453def crf_batch (images , probs , sa , sb , sg , w1 , w2 , n_iter = 5 ):
75- """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.
7655
7756 Args:
7857 images (np.ndarray): Array of shape (N, C, H, W, D) containing the input images.
@@ -100,7 +79,7 @@ def crf_batch(images, probs, sa, sb, sg, w1, w2, n_iter=5):
10079
10180
10281def crf (image , prob , sa , sb , sg , w1 , w2 , n_iter = 5 ):
103- """Implements the CRF post-processing step for the W-Net .
82+ """Implements the CRF post-processing step for the WNet3D .
10483
10584 Inspired by https://arxiv.org/abs/1210.5644, https://arxiv.org/abs/1606.00915 and https://arxiv.org/abs/1711.08506.
10685 Implemented using the pydensecrf library.
@@ -120,18 +99,14 @@ def crf(image, prob, sa, sb, sg, w1, w2, n_iter=5):
12099 """
121100 if not CRF_INSTALLED :
122101 logger .info (
123- "pydensecrf not installed, CRF post-processing will not be available. "
124- "Please install by running : pip install pydensecrf@git+https://github.com/lucasb-eyer/pydensecrf.git#egg=master"
125- "This is not a hard requirement, you do not need it to install it unless you want to use the CRF post-processing step."
102+ "pydensecrf not installed, therefore CRF post-processing will not be available! Please install the package. "
103+ "Please install by running: pip install pydensecrf2 "
126104 )
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,14 +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 :
235210 logger .info (
236- "pydensecrf not installed, CRF post-processing will not be available. "
237- "Please install by running : pip install pydensecrf@git+https://github.com/lucasb-eyer/pydensecrf.git#egg=master"
238- "This is not a hard requirement, you do not need it to install it unless you want to use the CRF post-processing step."
211+ "pydensecrf not installed, therefore CRF post-processing will not be available! Please install the package. "
212+ "Please install by running: pip install pydensecrf2 "
239213 )
240- # raise ImportError("pydensecrf is not installed.")
241214
242215 if len (self .images ) != len (self .labels ):
243216 raise ValueError ("Number of images and labels must be the same." )
0 commit comments