-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyFEAST_inference.py
152 lines (129 loc) · 5.91 KB
/
pyFEAST_inference.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
import time
import numpy as np
import glob
import itertools
import scipy.io as sio
from tqdm import tqdm
from scipy.io import loadmat
import matplotlib.pyplot as plt
import scipy.ndimage as ndimage
from matplotlib import font_manager as fm, rcParams
R = 7
xs = 346
ys = 260
D = 2*R+1
tau = 2e4
displayFreq = 1e5
counter = 0
frameCounter = 0
nNeuron = 16
beta = 0.5
stpWindow = 10
oneMinusBeta = 1-beta
downSampleFactor = 10
stpWindowd = int(np.round(stpWindow/downSampleFactor))
sqNeuron = np.ceil(np.sqrt(nNeuron))
wFrozen = loadmat("data/wFrozenL2.mat")
wFrozen = wFrozen["wL2"]
## NMNIST
# event_data = glob.glob(
# "/media/sami/Samsung_T5/MPhil/Dataset/n-mnist/mat/*.mat")
## For DVS gesture
# event_data = glob.glob(
# "/media/sami/Samsung_T5/MPhil/Dataset/gestureDVS/Train/*.mat")
# mat = loadmat(event_data[1])
# events = mat["TD"]
# event_index = events["x"][0][0][0].shape[0]
# events["ts"][0][0][0] = events["ts"][0][0][0] - events["ts"][0][0][0][0]
# nextTimeSample = events["ts"][0][0][0][0]+displayFreq
# events["p"][0][0][0][events["p"][0][0][0] == 0] = -1
## For NMNIST data
mat = loadmat("/media/sami/Samsung_T5/MPhil/Code/DeepGreen/greenhouseCode/recordings/newcolourExperimentNineConditions/cnd1/test/TD.mat")
events = mat["TD"]
event_index = events["x"][0][0].shape[0]
nextTimeSample = events["ts"][0][0][0][0] + displayFreq
# events[:, 3] = events[:, 3] - events[1, 3]
# nextTimeSample = events[1, 3] + displayFreq
S = np.zeros((xs,ys))
T = np.zeros_like(S)
T = T - np.inf
P = np.zeros_like(T)
xdMax = np.round(xs/downSampleFactor)
ydMax = np.round(ys/downSampleFactor)
wn = np.zeros((int(xdMax),int(ydMax))) - np.inf
img = np.full((int(xdMax),int(ydMax),3), fill_value=255,dtype='uint8')
Sd = np.zeros((int(xdMax),int(ydMax)))
Td = np.zeros_like(Sd)
Td = Td -np.inf
Pd = np.zeros_like(Td)
T_Fd = np.empty((int(xdMax),int(ydMax), nNeuron))
T_Fd = T_Fd-np.inf
T_Fd = T_Fd-np.inf
T_FdSimple = T_Fd
P_Fd = T_Fd
P_FdSimple = P_Fd
def moving_average(x, w):
return np.convolve(x, np.ones(w), 'valid') / w
def colorPaletteGenerator():
random_numbers = np.random.randint(low=0, high=256, size=(nNeuron, 3))
generated_colours = [tuple(colour) for colour in random_numbers.tolist()]
return generated_colours
generatedPalette = colorPaletteGenerator()
palette = np.array(generatedPalette)[np.newaxis, :, :]
fig = plt.figure(figsize=(8, 8))
# for idx in tqdm(range(2000000, int(np.round(event_index/10)))):
for idx in tqdm(range(2000000,3000000)):
x = int(events["x"][0][0][idx][0])
y = int(events["y"][0][0][idx][0])
p = int(events["p"][0][0][idx][0])
ts = events["ts"][0][0][idx][0]
# NMNIST
# x = events[idx, 0]
# y = events[idx, 1]
# p = events[idx, 2]
# ts = events[idx, 3]
xd = int(np.round(x/downSampleFactor))
yd = int(np.round(y/downSampleFactor))
T[x,y] = ts
P[x,y] = p
if (x-R>0) and (x+R<xs) and(y-R>0) and (y+R<ys):
ROI = np.multiply(P[x-R:x+R+1,y-R:y+R+1],np.exp((T[x-R:x+R+1,y-R:y+R+1]-ts)/tau))
if xd>1 and yd>1 and xd<xdMax and yd<ydMax:
ROI /= np.linalg.norm(ROI)
dotProducts = np.dot(wFrozen, ROI.flatten())
winnerNeuron = np.unravel_index(np.argmax(dotProducts, axis=None), dotProducts.shape)
counter = counter + 1
T_FdSimple[xd,yd,winnerNeuron] = ts
P_FdSimple[xd,yd,winnerNeuron] = p
if np.isinf(T_FdSimple[xd,yd,winnerNeuron]):
T_FdSimple[xd,yd,winnerNeuron] = ts
T_FdSimple[xd,yd,winnerNeuron] = p
else:
T_FdSimple[xd,yd,winnerNeuron] = oneMinusBeta*T_FdSimple[xd,yd,winnerNeuron] + beta*ts
P_FdSimple[xd,yd,winnerNeuron] = oneMinusBeta*P_FdSimple[xd,yd,winnerNeuron] + beta*p
wn[xd,yd] = winnerNeuron[0] + 1
img[wn==0] = [0,0,0]
img[wn==winnerNeuron[0]+1] = generatedPalette[winnerNeuron[0]-1]
if ts > nextTimeSample:
frameCounter += 1
nextTimeSample = max(nextTimeSample + displayFreq,ts)
############### VISUALISE WINNER NEURON SEPARATELY #############
# timeSurface_featureSurface = np.multiply(P_FdSimple,np.exp((T_FdSimple-ts)/tau))
# for i in range(1, nNeuron+1):
# fig.add_subplot(sqNeuron, sqNeuron, i)
# plt.imshow(np.nan_to_num(timeSurface_featureSurface[:,:,i-1]))
# plt.pause(.01)
# plt.draw()
############### VISUALISE WINNER NEURON COLOUR CODED #############
rotatedImg = ndimage.rotate(img, -90, reshape=True)
plt.subplot(2, 1, 1)
plt.imshow(rotatedImg)
plt.title(r"$\tau$: " + str(tau) + " freq: " +
str(displayFreq) + " ts: "+str(ts))
plt.axis('off')
plt.subplot(2, 1, 2)
plt.imshow(palette)
plt.axis('off')
plt.pause(0.1)
fig.savefig('./frames/frames' + str(frameCounter) + '.svg', format='svg', dpi=1200)
plt.draw()