-
Notifications
You must be signed in to change notification settings - Fork 6
/
dvs-gabor-nef.py
182 lines (147 loc) · 4.8 KB
/
dvs-gabor-nef.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
"""Try representing the spiking data with Gabor RFs.
1. Give an ensemble a bunch of Gabor encoders
"""
import numpy as np
import matplotlib.pyplot as plt
import nengo
import vanhateren
plt.ion()
import dvs
import gabor
from hunse_tools.timing import tic, toc
def show_image(ax, image):
plt_img = ax.imshow(image, vmin=-1, vmax=1, cmap='gray', interpolation='none')
ax.set_xticks([])
ax.set_yticks([])
return plt_img
rng = np.random.RandomState(3)
dt = 0.001
# --- load and filter DVS spikes
filename = 'dvs.npz'
# filename = 'dvs-ball-1ms.npz'
events = dvs.load(filename, dt_round=False)
t0 = 0.1
spikes = dvs.make_video(events, t0=t0, dt_frame=dt, tau=0)
video = dvs.make_video(events, t0=t0, dt_frame=dt, tau=0.005)
if 0:
plt.ion()
plt.figure(1)
ax = plt.gca()
img = show_image(ax, video[0])
ax.invert_yaxis()
for frame in video:
img.set_data(frame)
plt.draw()
raw_input("Pause...")
if 0:
plt.figure(1)
ani = dvs.animate([video], [plt.gca()])
plt.show()
# --- generate a filterbank
n = 5000
# rf_shape = (9, 9)
rf_shape = (15, 15)
thetas = rng.uniform(0, np.pi, size=n)
# phases = rng.uniform(-0.5, 0.5, size=n)
freqs = rng.uniform(0.1, 2.0, size=n)
# freqs = rng.uniform(0.2, 0.7, size=n)
# data_set = gabors(thetas, phases=0, freqs=0.5, sigmas_y=10., shape=rf_shape)
bank = gabor.bank(thetas, freqs=freqs, sigmas_y=10., shape=rf_shape)
if 0:
# test Gabors
plt.figure(1)
plt.clf()
r, c = 5, 5
for i in range(min(len(bank), r * c)):
plt.subplot(r, c, i+1)
plt.imshow(bank[i].reshape(rf_shape), cmap='gray', interpolation='none')
plt.xticks([])
# pair = filter_pair((15, 15), 1.0, 0.5, 1.0)
# axs = [plt.subplot(1, 2, i+1) for i in range(2)]
# axs[0].imshow(pair.real)
# axs[1].imshow(pair.imag)
plt.show()
assert False
# --- make encoders
im_shape = (128, 128)
inds = gabor.inds(len(bank), im_shape, rf_shape, rng=rng)
encoders = gabor.matrix_from_inds(bank, inds, im_shape)
# --- build network and solve for decoders
im_dims = np.prod(im_shape)
n_eval_points = 5000
if 0:
# Van Hateren
patches = vanhateren.VanHateren().patches(n_eval_points, im_shape)
patches = vanhateren.preprocess.scale(patches)
patches.shape = (-1, im_dims)
eval_points = patches.reshape(-1, im_dims)
elif 0:
# More gabors
rf_shape = (32, 32)
thetas = rng.uniform(0, np.pi, size=n_eval_points)
# phases = rng.uniform(-0.5, 0.5, size=n)
freqs = rng.uniform(0.1, 2, size=n_eval_points)
eval_bank = gabor.bank(thetas, freqs=freqs, sigmas_y=10., shape=rf_shape)
inds = gabor.inds(len(eval_bank), im_shape, rf_shape, rng=rng)
eval_points = gabor.matrix_from_inds(eval_bank, inds, im_shape)
else:
# Low freq images
cutoff = 0.1
images = rng.normal(size=(n_eval_points,) + im_shape)
X = np.fft.fft2(images)
f0 = np.fft.fftfreq(X.shape[-2])
f1 = np.fft.fftfreq(X.shape[-1])
ff = np.sqrt(f0[:, None]**2 + f1[None, :]**2)
X[..., ff > cutoff] = 0
Y = np.fft.ifft2(X)
assert np.allclose(Y.imag, 0)
eval_points = Y.real.clip(-1, 1).reshape(-1, im_dims)
fig_uv = plt.figure(2)
plt.clf()
plt.subplot(211)
u_plot = plt.imshow(video[0], vmin=-1, vmax=1, cmap='gray')
plt.subplot(212)
v_plot = plt.imshow(video[0], vmin=-1, vmax=1, cmap='gray')
def video_input(t):
i = int(np.round(t / dt))
return video[i % len(video)].ravel()
def video_plot(t, y):
x = video_input(t).reshape(im_shape)
y = y.reshape(im_shape)
u_plot.set_data(x)
v_plot.set_data(y)
fig_uv.canvas.draw()
rmse_total = np.array(0.0)
def rmse_recorder(t, y):
x = video_input(t)
if t > 0:
rmse_total[...] += np.sqrt(((y - x)**2).mean())
net = nengo.Network(seed=9)
with net:
u = nengo.Node(video_input)
# a = nengo.Ensemble(n, im_dims, encoders=encoders)
a = nengo.Ensemble(n, im_dims, encoders=encoders, intercepts=nengo.dists.Choice([0]))
nengo.Connection(u, a)
# a = nengo.Ensemble(n, im_dims)
v = nengo.Node(size_in=im_dims, size_out=im_dims)
c = nengo.Connection(a, v, eval_points=eval_points, synapse=0.01)
w1 = nengo.Node(video_plot, size_in=im_dims)
w2 = nengo.Node(rmse_recorder, size_in=im_dims)
nengo.Connection(v, w1, synapse=None)
nengo.Connection(v, w2, synapse=None)
sim = nengo.Simulator(net)
# show reconstructed images
if 1:
from nengo.builder.connection import build_linear_system
plt.figure(3)
plt.clf()
_, A, y = build_linear_system(sim.model, c, None)
x = sim.data[c].decoders.T
r, c = 2, 5
axes = [[plt.subplot2grid((2*r, c), (2*i+k, j)) for k in range(2)]
for i in range(r) for j in range(c)]
for i in range(r * c):
show_image(axes[i][0], y[i].reshape(im_shape))
show_image(axes[i][1], np.dot(A[i], x).reshape(im_shape))
sim.run(1.)
print(rmse_total)