forked from kdickerson/Sompy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.py
More file actions
54 lines (45 loc) · 1.67 KB
/
Test.py
File metadata and controls
54 lines (45 loc) · 1.67 KB
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
from sompy import *
if __name__ == "__main__":
print "Initialization..."
colors = [[0, 0, 0], [255, 255, 255], [0, 255, 0], [0, 255, 255], [255, 0, 0], [255, 0, 255], [255, 255, 0], [0, 0, 255]]
#transform = sklearn.decomposition.PCA()
#colors = transform.fit_transform(colors)
import time
t0 = time.time()
width = 20
height = 20
iterations = 400
som = SOM(width=width,height=height,FV_size=np.shape(colors)[1],learning_rate=0.5, init_mode='random')
print "Training colors..."
som.train(iterations=iterations, train_vector=colors, num_samples = 2, res = False )
r = som.get_residual()
if len(r) > 1:
print 'Residual:', r[-1]
t1 = time.time()
print 'time: %0.2f seconds' %(t1-t0)
som.save_similarity_mask("test_sim")
print "Saving Image: test_colors.png..."
try:
img = Image.new("RGB", (width, height))
for r in range(height):
for c in range(width):
data = som.nodes[som._get_index(r,c)]
# data = transform.inverse_transform(data)
img.putpixel((c,r), (int(data[0]), int(data[1]), int(data[2])))
img = img.resize((width*10, height*10),Image.NEAREST)
img.save("test_colors.png")
except:
print "Error saving the image, do you have PIL (Python Imaging Library) installed?"
print "Saving Image: original_colors.png..."
colors = np.asarray(colors)
img = Image.new("RGB", (4, 2))
img.putpixel((0,0), (0, 0, 0))
img.putpixel((1,0), (255, 255, 255))
img.putpixel((2,0), (0, 255, 0))
img.putpixel((3,0), (0, 255, 255))
img.putpixel((0,1), (255, 0, 0))
img.putpixel((1,1), (255, 0, 255))
img.putpixel((2,1), (255, 255, 0))
img.putpixel((3,1), (0, 0, 255))
img = img.resize((width*10, height*10),Image.NEAREST)
img.save("original_colors.png")