2011/results/att11/spikevision: LCAsal.py

File LCAsal.py, 3.8 KB (added by sshapero, 7 years ago)

Network for sparse representation of a 24x24 input. Computes a saliency map. Needs some tweaking.

Line 
1import nef
2from numeric import *
3
4def sth(x):
5        if x>0.1:
6                return (x-.05)
7        elif x<-0.1:
8                return (x+.05)
9        else:
10                return 0
11
12def sthn(x):
13        return [sth(y) for y in x]
14
15def absn(x):
16        return [abs(sth(y)) for y in x]
17
18def rect(x):
19    if x>0:
20        return x
21    else:
22        return 0
23
24def rectn(x):
25    return [rect(y) for y in x]
26
27def zero(m,n):
28    # Create zero matrix
29    new_matrix = [[0 for row in range(n)] for col in range(m)]
30    return new_matrix
31
32
33def multmin(matrix1):
34        # Matrix multiplication
35        new_matrix = zero(len(matrix1),len(matrix1))
36        for i in range(len(matrix1)):
37                for j in range(len(matrix1)):
38                        if(i != j):
39                                for k in range(len(matrix1[0])):
40                                        new_matrix[i][j] -= matrix1[i][k]*matrix1[j][k]
41
42        return new_matrix
43
44
45def transpose(matrix1):
46        new_matrix = zero(len(matrix1[0]),len(matrix1))
47        for i in range(len(matrix1)):
48                for j in range(len(matrix1[0])):
49                        new_matrix[j][i] = matrix1[i][j]
50
51        return new_matrix
52
53f = open('smoneclip.txt')
54
55num_images = 1
56clippre = []
57for img in range(num_images):
58        line = f.readline()
59        imgpre=[]
60        first = 0
61        for char in range(len(line)):
62                if (line[char] == ' '):
63                        last = char
64                        imgpre.append(round(float(line[first:last]),4))
65                        first = char+1
66               
67        clippre.append(imgpre)
68               
69f.close()
70
71
72f = open('smdictionary.txt')
73
74num_dict = 128*8
75PHI_pre = []
76for img in range(num_dict):
77        line = f.readline()
78        dictpre=[]
79        first = 0
80        for char in range(len(line)):
81                if (line[char] == ' '):
82                        last = char
83                        dictpre.append(round(float(line[first:last]),4))
84                        first = char+1
85        #print img     
86        PHI_pre.append(dictpre)
87f.close()
88
89f = open('smrecur.txt')
90
91recur_pre = []
92for img in range(num_dict):
93        line = f.readline()
94        dictpre=[]
95        first = 0
96        for char in range(len(line)):
97                if (line[char] == ' '):
98                        last = char
99                        dictpre.append(round(float(line[first:last]),4))
100                        first = char+1
101               
102        recur_pre.append(dictpre)       
103f.close()
104
105f = open('salrecur.txt')
106
107salrec_pre = []
108for img in range(num_dict):
109        line = f.readline()
110        dictpre=[]
111        first = 0
112        for char in range(len(line)):
113                if (line[char] == ' '):
114                        last = char
115                        dictpre.append(round(float(line[first:last]),4))
116                        first = char+1
117       
118        salrec_pre.append(dictpre)
119
120f.close()
121
122
123class MyInput(nef.SimpleNode):
124        def origin_value(self):
125                t=int(self.t_start/.1)
126                return clippre[t]
127
128
129
130dict = array(PHI_pre)
131absdict_pre = [[abs(x) for x in y] for y in PHI_pre]
132absdict = array(absdict_pre)
133numNodes = len(dict)
134numInputs = len(dict[0])
135
136recur = array(recur_pre)
137salrec = array(salrec_pre)
138
139net=nef.Network('LCA',quick=True)
140#net.add_to(world)
141
142input=net.make_input('input',clippre[0])
143#myinput=MyInput('input')
144#net.add(myinput)
145neuron=net.make_array('neurons',30,numNodes,intercept=(0,1))
146outs=net.make('decoders',1,numInputs,mode='direct')
147neuron_value=net.make('neuron_value',1,numNodes,mode='direct')
148salience=net.make_array('salience',20,numNodes,intercept=(0,1))
149sal_decode=net.make('sal_decoder',1,numInputs,mode='direct')
150
151net.connect(input,neuron,transform=dict)
152#net.connect(myinput.getOrigin('value'),neuron,transform=dict)
153net.connect(neuron,neuron,transform=recur,func=sthn)
154net.connect(neuron,outs,transform=dict.T,func=sthn,pstc=.001)
155net.connect(neuron,neuron_value,func=sthn,pstc=.001)
156net.connect(neuron,salience,func=absn,pstc=.005)
157net.connect(salience,salience,transform=salrec,func=rectn,pstc=.001)
158net.connect(salience,sal_decode,transform=absdict.T,func=rectn,pstc=.001)
159
160
161net.view(play=.2)
162
163
164#sim=net.network.simulator
165#sim.run(0,1,0.001)
166
167#print neuron_value.getOrigin('X').getValues().getValues()
168#print outs.getOrigin('X').getValues().getValues()