Ok it seems that I missed a cruicial part in the loop. checkW
and checkW
are not reset to zero after each radius iteration so the loop would run only once for radius = 15
. After adding checkH = 0
and checkW = 0
at the end of the radius iteration it fixed the problem and my SOM works perfectly.
Here is the code:
while radius > 2:
#print(radius)
while checkW < targetImage.w:
while checkH < targetImage.h:
#print(radius)
nodeDistance = math.sqrt(math.fabs(bmuW - checkW) * math.fabs(bmuW - checkW) + math.fabs(bmuH - checkH) * math.fabs(bmuH - checkH))
if(nodeDistance <= radius):
theta = math.exp((-1) * ((nodeDistance * nodeDistance) / (2 * radius * radius)))
targetImage.canvas[checkW, checkH].w0 = targetImage.canvas[checkW, checkH].w0 + theta * 0.1 * (inputR - targetImage.canvas[checkW, checkH].w0)
targetImage.canvas[checkW, checkH].w1 = targetImage.canvas[checkW, checkH].w1 + theta * 0.1 * (inputG - targetImage.canvas[checkW, checkH].w1)
targetImage.canvas[checkW, checkH].w2 = targetImage.canvas[checkW, checkH].w2 + theta * 0.1 * (inputB - targetImage.canvas[checkW, checkH].w2)
targetImage.canvas[checkW,checkH].r = int(targetImage.canvas[checkW, checkH].w0 * 255)
targetImage.canvas[checkW,checkH].g = int(targetImage.canvas[checkW, checkH].w1 * 255)
targetImage.canvas[checkW,checkH].b = int(targetImage.canvas[checkW, checkH].w2 * 255)
checkH = checkH + 1
checkH = 0
checkW = checkW + 1
radius = radius - 1
checkH = 0
checkW = 0
#print(radius)