-
Notifications
You must be signed in to change notification settings - Fork 1
/
External.py
199 lines (133 loc) · 5.53 KB
/
External.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 19 20:10:44 2017
@author: ahmadnish
"""
import skimage.filters
import skimage.morphology
import skimage.feature
import numpy
import sys
sys.path.append('/Users/ahmadnish/dev/bld/nifty/python')
import nifty
import nifty.graph
import nifty.graph.agglo
import nifty.segmentation
import nifty.filters
import nifty.graph.rag
import nifty.ground_truth
import nifty.graph.opt.multicut
import vigra
def nodeToEdgeFeat(nodeFeatures, rag):
uv = rag.uvIds()
uF = nodeFeatures[uv[:,0], :]
vF = nodeFeatures[uv[:,1], :]
feats = [ numpy.abs(uF-vF), uF + vF, uF * vF,
numpy.minimum(uF,vF), numpy.maximum(uF,vF)]
return numpy.concatenate(feats, axis=1)
def computeFeatures(raw, rag):
nrag = nifty.graph.rag
# list of all edge features we fill
feats = []
# helper function to convert
# node features to edge features
# accumulate features from raw data
fRawEdge, fRawNode = nrag.accumulateStandartFeatures(rag=rag, data=raw,
minVal=0.0, maxVal=255.0, numberOfThreads=1)
feats.append(fRawEdge)
feats.append(nodeToEdgeFeat(fRawNode, rag))
# accumulate node and edge features from
# superpixels geometry
fGeoEdge = nrag.accumulateGeometricEdgeFeatures(rag=rag, numberOfThreads=1)
feats.append(fGeoEdge)
fGeoNode = nrag.accumulateGeometricNodeFeatures(rag=rag, numberOfThreads=1)
feats.append(nodeToEdgeFeat(fGeoNode, rag))
return numpy.concatenate(feats, axis=1)
import warnings
def feat_from_edge_prob(rag, raw, edge_probs, overseg):
ngraph = nifty.graph
new_feats = []
# trivial feature
new_feats.append(edge_probs[:,None])
# ucm features
edgeSizes = numpy.ones(shape=[rag.numberOfEdges])
nodeSizes = numpy.ones(shape=[rag.numberOfNodes])
for r in (0.01,0.1,0.2,0.4,0.5, 0.8):
clusterPolicy = ngraph.agglo.edgeWeightedClusterPolicyWithUcm(
graph=rag, edgeIndicators=edge_probs,
edgeSizes=edgeSizes, nodeSizes=nodeSizes,sizeRegularizer=r)
agglomerativeClustering = ngraph.agglo.agglomerativeClustering(clusterPolicy)
a_new_feat = agglomerativeClustering.runAndGetDendrogramHeight(verbose=False)
new_feats.append(a_new_feat[:,None])
## begin: new features from spatial edge probabilities
vispred = visualize(rag, overseg, edge_probs, numpy.zeros(overseg.shape))
# apply several filters on visualization of the prediction (a max filter is
# applied because Thorsten said so)
# sad, skimage only works on uint8 [0, 255]
with warnings.catch_warnings():
warnings.simplefilter("ignore")
vispred = skimage.filters.rank.maximum(skimage.img_as_ubyte(vispred),
selem = numpy.ones([4,4]))
assert(numpy.max(vispred) <= 255)
vispred = skimage.img_as_float(vispred)
assert(numpy.max(vispred) <= 1)
imgs = []
for sigma in [2.0, 4.0, 6.0]:
res = nifty.filters.gaussianSmoothing(vispred, sigma)
imgs.append(res)
for sigma in [2.,4.,6.]:
res = vigra.filters.hessianOfGaussianEigenvalues(vispred, sigma)
numpy.save('resh',res)
imgs.append(res[...,0])
for inscale in [1.0,2.0,3.0]:
res = vigra.filters.structureTensorEigenvalues(vispred,inscale,inscale*5.)
imgs.append(res[...,0])
nrag = nifty.graph.rag
for img in imgs:
fRawEdge, fRawNode = nrag.accumulateStandartFeatures(rag=rag, data=img,
minVal=0.0, maxVal=1.0, numberOfThreads=1)
new_feats.append(fRawEdge)
new_feats.append(nodeToEdgeFeat(fRawNode, rag))
new_feats = numpy.concatenate(new_feats, axis=1)
return new_feats
#############################################################
# Build the training set:
# ===========================
# We only use high confidence boundaries.
def trainingSetBuilder(featureSet, dataDset, setImages):
trainingSet = {'features':[],'labels':[]}
for i,z in enumerate(setImages):
data = dataDset[z]
edgeGt = data['edgeGt']
feats = featureSet[i]
assert(feats.shape[0] == edgeGt.shape[0])
where1 = numpy.where(edgeGt > 0.85)[0]
where0 = numpy.where(edgeGt < 0.15)[0]
trainingSet['features'].append(feats[where0,:])
trainingSet['features'].append(feats[where1,:])
trainingSet['labels'].append(numpy.zeros(len(where0)))
trainingSet['labels'].append(numpy.ones(len(where1)))
features = numpy.concatenate(trainingSet['features'], axis=0)
labels = numpy.concatenate(trainingSet['labels'], axis=0)
return features, labels
def visualize(rag, overseg, edge_values, image):
shape = overseg.shape
for x in range(shape[0]):
for y in range(shape[1]):
lu = overseg[x,y]
if x + 1 < shape[0]:
lv = overseg[x+1,y]
if lu != lv :
e = rag.findEdge(lu, lv)
# normalization?
image[x,y] = edge_values[e]
image[x+1,y] = edge_values[e]
if y + 1 < shape[1]:
lv = overseg[x,y+1]
if lu != lv :
e = rag.findEdge(lu, lv)
# normalization?
image[x,y] = edge_values[e]
image[x,y+1] = edge_values[e]
return image