forked from vseledkin/Sum-Product-Networks
/
SPN.py
373 lines (329 loc) · 15.4 KB
/
SPN.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from Parameters import Parameters
from Region import Region
from Instance import Instance
from Dataset import Dataset
from Utility import Utility
from Decomposition import Decomposition
from Node import Node
import numpy as np
class SPN():
"""
The main interface of Sum-Product Networks
"""
###########################################################
# SPN Learning part #
###########################################################
def __init__(self):
"""
We will work on multi-resolution in SPN, in hight
level we will work with patches larger than 4 x 4
pixelsand and in low level we will work with the 4 x 4
pixels patches directly.
"""
self.__coarseRowStep = \
Parameters.imageWidth / Parameters.baseResolution
self.__coarseColStep = \
Parameters.imageHeight / Parameters.baseResolution
# This is used to remember all the segmented region id in
# order to avoid those clunky for-loop in other functions
self.__coarseRegionId = []
self.__fineRegionId = []
self.__pixelRegionId = []
def __initUnitRegion(self, r):
# only region for a pixel will have means, variances
r.allocateSumNodes(Parameters.numSumNodePerPixel)
r.means = np.zeros(Parameters.numSumNodePerPixel)
r.variances = np.zeros(Parameters.numSumNodePerPixel)
r.counts = np.zeros(Parameters.numSumNodePerPixel)
num_inst = len(self.__dataset)
quantileSize = num_inst / Parameters.numSumNodePerPixel
values = np.zeros(num_inst)
for i, instance in enumerate(self.__dataset):
values[i] = instance.getValue(r.rowUp, r.columnLeft)
values = np.sort(values)
for step in xrange(Parameters.numSumNodePerPixel):
lowerIndex = step * quantileSize
upperIndex = (step + 1) * quantileSize
upperIndex = upperIndex if upperIndex < num_inst else num_inst
r.means[step] = np.mean(values[lowerIndex : upperIndex])
r.variances[step] = np.var(values[lowerIndex : upperIndex])
r.counts = len(values[lowerIndex : upperIndex])
r.totalCounts = num_inst
def __setInput(self, instance, blankPart = 'NONE'):
for l in xrange(0, Parameters.imageWidth):
r = l + 1
for u in xrange(0, Parameters.imageHeight):
d = u + 1
num_id = Region.getRegionId(u, d, l, r)
region = Region.getRegion(num_id)
if blankPart == 'NONE':
region.setBaseValues(instance.getValue(u, l))
elif blankPart == 'LEFT':
if l < Parameters.imageWidth / 2:
region.setBaseValuesForBlank()
else:
region.setBaseValues(instance.getValue(u, l))
elif blankPart == 'BOTTOM':
if u < Parameters.imageHeight / 2:
region.setBaseValues(instance.getValue(u, l))
else:
region.setBaseValuesForBlank()
def printLearnedModel(self):
for region_id in self.__pixelRegionId:
region = Region.getRegion(region_id)
for sumNode in region.sumNodes:
print sumNode.getLogValue()
print region.prodNodes
def saveModel(self):
"""
Save the learned model to a XML file
"""
pass
def addTrainingSet(self, dataset):
# dataset is a list of Instance objects
self.__dataset = dataset
def getTrainingSetSize(self):
return len(self.__dataset)
def getTrainingInstance(self, index):
assert index < len(self.__dataset)
return self.__dataset[index]
def initialize(self):
"""
Initialize each small region of the picture
and allocate each region a Region object.
"""
# for coarse regions : only work with patches larger
# than 4 x 4 pixels
# for notation simplicity
iw = Parameters.imageWidth
ih = Parameters.imageHeight
br = Parameters.baseResolution
for colStepSize in xrange(1, self.__coarseColStep + 1):
for rowStepSize in xrange(1, self.__coarseRowStep + 1):
# skip the 4 x 4 pixels patches
if colStepSize == 1 and rowStepSize == 1:
continue
# find the range of a patch
# l = left, r = right
# d = down, u = up
for l in xrange(0, iw - colStepSize * br + 1, br):
r = l + colStepSize * br
for u in xrange(0, ih - rowStepSize * br + 1, br):
d = u + rowStepSize * br
num_id = Region.getRegionId(u, d, l, r)
region = Region.getRegion(num_id)
self.__coarseRegionId.append(num_id)
if colStepSize == self.__coarseColStep \
and rowStepSize == self.__coarseRowStep:
# this is the root region
region.allocateSumNodes(1)
self.__rootRegion = region
self.__rootSumNode = region.getSumNode(0)
else:
region.allocateSumNodes(Parameters.numSumNodePerRegion)
# for fine regions: work with 4 x 4 pixels patches
for colStepSize in xrange(0, self.__coarseColStep):
for rowStepSize in xrange(0, self.__coarseRowStep):
for pixelColStep in xrange(1, br + 1):
for pixelRowStep in xrange(1, br + 1):
for l in xrange(colStepSize * br, (colStepSize + 1) * br - pixelColStep + 1):
r = l + pixelColStep
for u in xrange(rowStepSize * br, (rowStepSize + 1) * br - pixelRowStep + 1):
d = u + pixelRowStep
num_id = Region.getRegionId(u, d, l, r)
region = Region.getRegion(num_id)
if pixelRowStep == 1 and pixelColStep == 1:
self.__pixelRegionId.append(num_id)
else:
self.__fineRegionId.append(num_id)
if pixelColStep == 1 and pixelRowStep == 1:
self.__initUnitRegion(region)
else:
region.allocateSumNodes(Parameters.numSumNodePerRegion)
def MAPinference(self, index, instance):
"""
Bottom-up inference
"""
self.__setInput(instance)
# for fine regions: work with 4 x 4 pixels patches
print '......Inference on fine regions'
for region_id in self.__fineRegionId:
region = Region.getRegion(region_id)
region.MAPinference(index, instance)
print '......Inference on coarse regions'
for region_id in self.__coarseRegionId:
region = Region.getRegion(region_id)
region.MAPinference(index, instance)
def setParseToMAP(self, index):
self.__rootRegion.setParseToMAP(index)
def setParseToMAPFromBuffer(self):
while Utility.parseBuffer:
regionRightMax = Utility.parseBuffer.pop()
regionLeftMax = Utility.parseBuffer.pop()
regionRightId = Utility.parseBuffer.pop()
regionLeftId = Utility.parseBuffer.pop()
mapSumNodeIndex = Utility.parseBuffer.pop()
region_id = Utility.parseBuffer.pop()
region = Region.getRegion(region_id)
region.setParseToMAPFromBuffer(mapSumNodeIndex, \
regionLeftId,
regionRightId,
regionLeftMax,
regionRightMax)
def clearParseToMAP(self, index):
self.__rootRegion.clearParseToMAP(index)
def clearParseToMAPFromBuffer(self):
while Utility.parseBuffer:
regionRightMax = Utility.parseBuffer.pop()
regionLeftMax = Utility.parseBuffer.pop()
regionRightId = Utility.parseBuffer.pop()
regionLeftId = Utility.parseBuffer.pop()
mapSumNodeIndex = Utility.parseBuffer.pop()
region_id = Utility.parseBuffer.pop()
region = Region.getRegion(region_id)
region.clearParseToMAPFromBuffer(mapSumNodeIndex, \
regionLeftId, \
regionRightId, \
regionLeftMax, \
regionRightMax)
def clearUnusedDecomp(self):
"""
Top-down scan each region and clear all the
unused decomposition in each region.
"""
# clear coarse region
for region_id in self.__coarseRegionId:
region = Region.getRegion(region_id)
# first of all, gather all the decomposition in use
# (the decomposition is child of one of the SumNode)
alive = []
for node in region.sumNodes:
if node.getNumOfChildren() > 0:
alive.extend(node.getChildren().keys())
all_decomps = region.prodNodes.keys()
# find dead decomp using set compliment operation
dead = set(all_decomps) - set(alive)
# clear these dead decompositions
for ddp in dead:
# remove in local region
del region.prodNodes[ddp]
# remove in global decomposition dict
Decomposition.deleteDecomp(ddp)
#clear fine region
for region_id in self.__fineRegionId:
region = Region.getRegion(region_id)
# first of all, gather all the decomposition in use
# (the decomposition is child of one of the SumNode)
alive = []
for node in region.sumNodes:
if node.getNumOfChildren() > 0:
alive.extend(node.getChildren().keys())
all_decomps = region.prodNodes.keys()
# find dead decomp using set compliment operation
dead = set(all_decomps) - set(alive)
# clear these dead decompositions
for ddp in dead:
# remove in local region
del region.prodNodes[ddp]
# remove in global decomposition dict
Decomposition.deleteDecomp(ddp)
def getLogLikelihood(self, instance):
"""
Get the per instance log-likelihood by setting
the input to the instance and evaluate the SPN,
the value of the root is the log-likelihood
"""
self.__setInput(instance)
self.__evaluateSPN()
return self.__rootSumNode.getLogValue()
def __evaluateSPN(self):
"""
bottom-up evaluate the SPN
"""
# fine region
for region_id in self.__fineRegionId:
region = Region.getRegion(region_id)
region.evaluate()
# coarse region
for region_id in self.__coarseRegionId:
region = Region.getRegion(region_id)
region.evaluate()
def __differentiateSPN(self):
# all the nodes in the SPN should have initialized
# the value of their logDerivatives by their super
# class's contructor, or we should call
# self.__initDerivative()
self.__rootSumNode.setLogDerivative(0.0)
self.__rootSumNode.passDerivative()
for decomp_id in self.__rootSumNode.getChildren():
prodNode = self.__rootSumNode.getChild(decomp_id)
prodNode.passDerivative()
for region_id in reversed(self.__coarseRegionId[:-1]):
region = Region.getRegion(region_id)
region.passDerivative()
for region_id in reversed(self.__fineRegionId):
region = Region.getRegion(region_id)
region.passDerivative()
def __computeMarginal(self, region):
max_value = 100
det = 0.0
numer = 0.0
for node in region.sumNodes:
if node.getLogDerivative() == Node.ZERO:
continue
if max_value == 100 or node.getLogDerivative() > max_value:
max_value = node.getLogDerivative()
for i, node in enumerate(region.sumNodes):
if node.getLogDerivative() == Node.ZERO:
continue
p = np.exp(node.getLogDerivative() - max_value)
det += p * region.means[i]
numer += p
return det / numer
###########################################################
# Image Completion part #
###########################################################
def completeLeftImage(self, instance, mode = 'MARGINAL'):
print 'Complete the left part of the corrupted image'
if mode == 'MARGINAL':
self.__completeImageByMarginal(instance, part = 'LEFT')
elif mode == 'MAP':
self.__completeImageByMAP(instance, part = 'LEFT')
def __completeImageByMarginal(self, instance, part):
self.__setInput(instance, part)
self.__evaluateSPN()
self.__differentiateSPN()
for l in xrange(0, Parameters.imageWidth):
r = l + 1
for u in xrange(0, Parameters.imageHeight):
d = u + 1
num_id = Region.getRegionId(u, d, l, r)
region = Region.getRegion(num_id)
if part == 'LEFT':
if l < Parameters.imageWidth / 2:
tmp = self.__computeMarginal(region)
Utility.parseBuffer.append( \
Utility.getIntValue(instance, tmp))
else:
Utility.parseBuffer.append( \
Utility.getIntValue(instance, instance.getValue(u, l)))
if part == 'BOTTOM':
if u < Parameters.imageHeight / 2:
Utility.parseBuffer.append( \
Utility.getIntValue(instance, instance.getValue(u, l)))
else:
tmp = self.__computeMarginal(region)
Utility.parseBuffer.append( \
Utility.getIntValue(instance, tmp))
def completeBottomImage(self, instance, mode = 'MARGINAL'):
if mode == 'MARGINAL':
self.__completeBottomImageByMarginal(instance)
elif mode == 'MAP':
self.__completeLeftImageByMAP(instance)
if __name__ == '__main__':
s = SPN()
data = Dataset()
data.loadData('olivetti.raw')
s.addTrainingSet(data.getTrainingSet())
print s.getTrainingSetSize()
s.initialize()