-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainable_segmentation.py
224 lines (199 loc) · 7 KB
/
trainable_segmentation.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
import sys
sys.version
from itertools import combinations_with_replacement
from js import document
from matplotlib import pyplot as plt
from skimage import filters, feature, img_as_float32
from skimage import io as skio
from sklearn.ensemble import RandomForestClassifier
from time import time
import itertools
import numpy as np
import io
import base64
try:
from sklearn.exceptions import NotFittedError
has_sklearn = True
except ImportError:
has_sklearn = False
class NotFittedError(Exception):
pass
def create_div(self):
div = document.createElement('div')
document.body.appendChild(div)
return div
SEL = None
def _find_div(self):
el = document.querySelector(SEL)
div = document.createElement("div")
el.appendChild(div)
return div
def create_root_element(f, sel):
global SEL
SEL = sel
f.canvas.create_root_element = _find_div.__get__(_find_div, f.canvas.__class__)
def _texture_filter(gaussian_filtered):
H_elems = [
np.gradient(np.gradient(gaussian_filtered)[ax0], axis=ax1)
for ax0, ax1 in combinations_with_replacement(range(gaussian_filtered.ndim), 2)
]
eigvals = feature.hessian_matrix_eigvals(H_elems)
return eigvals
def _mutiscale_basic_features_singlechannel(
img, intensity=True, edges=True, texture=True, sigma_min=0.5, sigma_max=16
):
"""Features for a single channel nd image.
Parameters
----------
"""
# computations are faster as float32
img = np.ascontiguousarray(img_as_float32(img))
sigmas = np.logspace(
np.log2(sigma_min),
np.log2(sigma_max),
num=int(np.log2(sigma_max) - np.log2(sigma_min) + 1),
base=2,
endpoint=True,
)
all_filtered = [filters.gaussian(img, sigma) for sigma in sigmas]
features = []
if intensity:
features += all_filtered
if edges:
all_edges = [filters.sobel(filtered_img) for filtered_img in all_filtered]
features += all_edges
if texture:
all_texture = [_texture_filter(filtered_img) for filtered_img in all_filtered]
features += itertools.chain.from_iterable(all_texture)
return features
def multiscale_basic_features(
image,
multichannel=True,
intensity=True,
edges=True,
texture=True,
sigma_min=0.5,
sigma_max=16,
):
"""Local features for a single- or multi-channel nd image.
Intensity, gradient intensity and local structure are computed at
different scales thanks to Gaussian blurring.
Parameters
----------
image : ndarray
Input image, which can be grayscale or multichannel.
multichannel : bool, default False
True if the last dimension corresponds to color channels.
intensity : bool, default True
If True, pixel intensities averaged over the different scales
are added to the feature set.
edges : bool, default True
If True, intensities of local gradients averaged over the different
scales are added to the feature set.
texture : bool, default True
If True, eigenvalues of the Hessian matrix after Gaussian blurring
at different scales are added to the feature set.
sigma_min : float, optional
Smallest value of the Gaussian kernel used to average local
neighbourhoods before extracting features.
sigma_max : float, optional
Largest value of the Gaussian kernel used to average local
neighbourhoods before extracting features.
Returns
-------
features : np.ndarray
Array of shape ``(n_features,) + image.shape``
"""
if image.ndim >= 3 and multichannel:
all_results = (
_mutiscale_basic_features_singlechannel(
image[..., dim],
intensity=intensity,
edges=edges,
texture=texture,
sigma_min=sigma_min,
sigma_max=sigma_max,
)
for dim in range(image.shape[-1])
)
features = list(itertools.chain.from_iterable(all_results))
else:
features = _mutiscale_basic_features_singlechannel(
image,
intensity=intensity,
edges=edges,
texture=texture,
sigma_min=sigma_min,
sigma_max=sigma_max,
)
return np.array(features, dtype=np.float32)
def fit_segmenter(labels, features, clf):
"""
Segmentation using labeled parts of the image and a classifier.
Parameters
----------
labels : ndarray of ints
Image of labels. Labels >= 1 correspond to the training set and
label 0 to unlabeled pixels to be segmented.
features : ndarray
Array of features, with the first dimension corresponding to the number
of features, and the other dimensions correspond to ``labels.shape``.
clf : classifier object
classifier object, exposing a ``fit`` and a ``predict`` method as in
scikit-learn's API, for example an instance of
``RandomForestClassifier`` or ``LogisticRegression`` classifier.
Returns
-------
output : ndarray
Labeled array, built from the prediction of the classifier trained on
``labels``.
clf : classifier object
classifier trained on ``labels``
Raises
------
NotFittedError if ``self.clf`` has not been fitted yet (use ``self.fit``).
"""
training_data = features[:, labels > 0].T
training_labels = labels[labels > 0].ravel()
clf.fit(training_data, training_labels)
data = features[:, labels == 0].T
predicted_labels = clf.predict(data)
output = np.copy(labels)
output[labels == 0] = predicted_labels
return output, clf
def predict_segmenter(features, clf):
"""
Segmentation of images using a pretrained classifier.
Parameters
----------
features : ndarray
Array of features, with the first dimension corresponding to the number
of features, and the other dimensions are compatible with the shape of
the image to segment.
clf : classifier object
trained classifier object, exposing a ``predict`` method as in
scikit-learn's API, for example an instance of
``RandomForestClassifier`` or ``LogisticRegression`` classifier. The
classifier must be already trained, for example with
:func:`fit_segmenter`.
features_func : function, optional
function computing features on all pixels of the image, to be passed
to the classifier. The output should be of shape
``(m_features, *labels.shape)``. If None,
:func:`multiscale_basic_features` is used.
Returns
-------
output : ndarray
Labeled array, built from the prediction of the classifier.
"""
sh = features.shape
features = features.reshape((sh[0], np.prod(sh[1:]))).T
try:
predicted_labels = clf.predict(features)
except NotFittedError:
raise NotFittedError(
"You must train the classifier `clf` first"
"for example with the `fit_segmenter` function."
)
output = predicted_labels.reshape(sh[1:])
return output