-
Notifications
You must be signed in to change notification settings - Fork 1
/
dic_learning_rotUpdate.py
351 lines (269 loc) · 11.9 KB
/
dic_learning_rotUpdate.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 12 20:24:48 2015
@author: Arash
"""
import numpy as np
import pylab as plt
from sklearn.utils import shuffle
from scipy.ndimage.interpolation import rotate
from OMP import rot_invar_omp, omp
def vis_filters(D, patch_size):
m, k = D.shape
V = D.T
plt.figure(figsize=(8.4, 8))
vmin = V.min()
vmax = V.max()
for i, comp in enumerate(V[:k]):
# plt.subplot(n_theta,k/n_theta, i + 1)
plt.subplot(20,24, i + 1)
#comp_rgb = cv2.cvtColor(np.array(comp*255,'uint8').reshape(patch_size), cv2.COLOR_HSV2BGR)
plt.imshow(comp.reshape(patch_size), interpolation='nearest', vmin=vmin, vmax=vmax)
plt.xticks(())
plt.yticks(())
plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)
def rotate_filters(filters, n_theta, theta_t, patch_size):
m, n_filters = filters.shape
D_r = np.zeros((m, n_filters*n_theta))
for j in range(n_filters):
ker = filters[:,j].reshape(patch_size)
for t in range(n_theta):
D_r[:,j*n_theta+t] = rotate(ker, (t-theta_t[j])*360./n_theta,
axes=(1, 0), reshape=False, order=3,
mode='nearest').flatten()
return D_r
def shift_filters(filters, n_theta, theta_t, n_sections):
m, n_filters = filters.shape
n_col = m/n_sections
D_r = np.zeros((m, n_filters*n_theta))
for j in range(n_filters):
ker = filters[:,j].reshape((n_sections, n_col))
for t in range(n_theta):
D_r[:,j*n_theta+t] = np.roll(ker, (t-theta_t[j])*n_sections/n_theta, axis=0).flatten()
return D_r
def select_correlated_orientation(D_r, x_t, n_theta, prev_theta = None, B_t = None,
patch_size = None, n_sections = None):
m, k = D_r.shape
n_filters = k/n_theta
D_t = np.zeros((m,n_filters))
theta_t = np.zeros(n_filters,'int')
B_r = np.zeros_like(B_t)
for j in range(n_filters):
dr = D_r[:,j*n_theta:(j+1)*n_theta]
theta_t[j] = np.argmax( np.abs(np.dot(x_t.T, dr)).flatten()/np.linalg.norm(dr, axis = 0)) #/np.linalg.norm(dr, axis = 0)
#theta_t[j] = 0
D_t[:,j] = dr[:, theta_t[j]]
if B_t is not None:
b_t = B_t[:, j].reshape(patch_size)
#B_r[:,j] = np.roll(b_t, (-theta_t[j])*n_sections/n_theta, axis=0).flatten()
B_r[:, j] = rotate(b_t, (theta_t[j]-prev_theta[j])*360./n_theta,
axes=(1, 0), reshape=False, order=3, mode='nearest').flatten()
return theta_t, D_t, B_r
def rotate_col(b_t, pre_theta, theta_t, n_theta, patch_size):
m, sparsity = b_t.shape
b_t_new = np.zeros_like(b_t)
for j in range(sparsity):
b_t_new[:, j] = rotate(b_t[:,j].reshape(patch_size), (theta_t[j]-pre_theta[j])*360./n_theta,
axes=(1, 0), reshape=False, order=3, mode='nearest').flatten()
return b_t_new
def shift_col(b_t, pre_theta, theta_t, n_theta, n_sections) :
m, sparsity = b_t.shape
b_t_new = np.zeros_like(b_t)
for j in range(sparsity):
b_t_new[:, j] = np.roll(b_t[:,j], (theta_t[j]-pre_theta[j])*n_sections/n_theta, axis=0).flatten()
return b_t_new
def sq_dict_learning(row_data, mask, D_0 = None, n_filters = 20,
eta = 0.001, sparsity = 10, n_epochs = 4, EV_SCORE = True):
'''
k: Number of dictionary items
n_theta: Number of orientated realization of the filter
'''
#Shuffle the data
data = shuffle(row_data).T
m, n = data.shape
effective_dim = mask.sum()
dummy_dim = mask.shape[0]*mask.shape[1]
dim_ratio = float(dummy_dim)/effective_dim
if D_0 is None:
D_base = 1-2*np.random.rand(m,n_filters)
D_base -= np.expand_dims(np.mean(D_base, axis=0), 0)*dim_ratio
D_base /= np.linalg.norm(D_base,axis=0)
D_t = D_base
else:
D_t = D_0
losses = []
for epoch in range(n_epochs):
for t in range(n):
x_t = data[:,t]
# Sparse Coding
idx_t, alphas_t = omp(D_t, x_t, sparsity)
# Dictionary Update
##Rotation update
d_t = D_t[:,idx_t]
eta_prime = eta*m
y_t = np.dot(d_t,alphas_t)
y_t /= np.linalg.norm(y_t,axis=0)
lmbd = np.sqrt(1-(np.dot(y_t, x_t))**2)
half_S = np.dot(np.expand_dims(x_t,1), np.expand_dims(y_t,0))
S = half_S - half_S.T
update = np.identity(m) + np.sin(2 * eta_prime * lmbd)/lmbd * S + (1 - np.cos(2 * eta_prime * lmbd))/lmbd**2 * np.dot(S,S)
D_t[:,idx_t] = np.dot(update, d_t)
D_t -= np.expand_dims(np.mean(D_t, axis=0), 0)*dim_ratio
D_t /= np.expand_dims(np.linalg.norm(D_t, axis=0), axis=0)
if EV_SCORE and (t%500 == 0):
loss = score_dict(data, D_t, sparsity )
losses.append(loss)
data = shuffle(data.T).T
return D_t, losses
def sq_rot_invar(row_data, mask, D_0 = None, n_filters = 20, n_theta = 6,
eta = 0.0003, sparsity = 10, n_epochs = 4, EV_SCORE = True):
'''
k: Number of dictionary items
n_theta: Number of orientated realization of the filter
'''
#Shuffle the data
#data = shuffle(row_data).T
data = row_data.T
m, n = data.shape
effective_dim = mask.sum()
dummy_dim = mask.shape[0]*mask.shape[1]
dim_ratio = float(dummy_dim)/effective_dim
# Number of iterations
patch_size = mask.shape
mask_D = np.repeat(mask.reshape((m,1)),n_filters,axis=1)
if D_0 is None:
D_base = 1-2*np.random.rand(m,n_filters)
D_base -= np.expand_dims(np.mean(D_base, axis=0), 0)*dim_ratio
D_base *= mask_D
D_base /= np.linalg.norm(D_base,axis=0)
D_t = D_base
else:
D_t = mask_D*D_0
Theta_t = np.zeros(n_filters,'int')
D_r = rotate_filters(D_t, n_theta, Theta_t, patch_size)
D_r = D_r - np.expand_dims(np.mean(D_r, axis=0), 0)*dim_ratio
D_r /= np.expand_dims(np.linalg.norm(D_r, axis=0), axis=0)
losses = []
for epoch in range(n_epochs):
for t in range(n):
x_t = data[:,t]
# Selecting theta s that correlate most with x_t
idx_t, alphas_t, theta_t = rot_invar_omp(D_r, x_t, sparsity, n_theta)
# extract corresponding columns from B_t and rotate them according to theta t
d_t = D_r[:,idx_t]
## Dictionary Update
#Rotation update
eta_prime = eta*m
y_t = np.dot(d_t,alphas_t)
y_t /= np.linalg.norm(y_t,axis=0)
lmbd = np.sqrt(1-(np.dot(y_t, x_t))**2)
half_S = np.dot(np.expand_dims(x_t,1), np.expand_dims(y_t,0))
S = half_S - half_S.T
update = np.identity(m) + np.sin(2 * eta_prime * lmbd)/lmbd * S + (1 - np.cos(2 * eta_prime * lmbd))/lmbd**2 * np.dot(S,S)
D_t[:,idx_t/n_theta] = np.dot(update, d_t)
# Rotate D_t back to generate D_r
Theta_t[idx_t/n_theta] = theta_t
D_r = rotate_filters(D_t, n_theta, Theta_t, patch_size)
if EV_SCORE and (t%500 == 0):
loss = score_rot_invar_dic(data, D_r, n_theta, sparsity, mask )
losses.append(loss)
data = shuffle(data.T).T
return D_r, losses
def sec_rot_invar(row_data, D_0 = None, n_filters = 20, n_theta = 6,
n_sections = 6, eta = 0.0003, sparsity = 10, n_epochs = 4, EV_SCORE = True):
'''
k: Number of dictionary items
n_theta: Number of orientated realization of the filter
'''
#Shuffle the data
#data = shuffle(row_data).T
data = row_data.T
m, n = data.shape
if D_0 is None:
D_base = 1-2*np.random.rand(m,n_filters)
D_base -= np.expand_dims(np.mean(D_base, axis=0), 0)
D_base /= np.linalg.norm(D_base,axis=0)
D_t = D_base
else:
D_t = D_0
Theta_t = np.zeros(n_filters,'int')
D_r = shift_filters(D_t, n_theta, Theta_t, n_sections)
D_r = D_r - np.expand_dims(np.mean(D_r, axis=0), 0)
D_r /= np.expand_dims(np.linalg.norm(D_r, axis=0), axis=0)
losses = []
for epoch in range(n_epochs):
for t in range(n):
x_t = data[:,t]
# Selecting theta s that correlate most with x_t
idx_t, alphas_t, theta_t = rot_invar_omp(D_r, x_t, sparsity, n_theta)
# extract corresponding columns from B_t and rotate them according to theta t
d_t = D_r[:,idx_t]
D_t[:,idx_t/n_theta] = d_t
Alpha_t = np.zeros((n_filters,1))
Alpha_t[idx_t/n_theta,0] = alphas_t
## Dictionary Update
##Rotation update
eta_prime = eta*m
y_t = np.dot(d_t,alphas_t)
y_t /= np.linalg.norm(y_t,axis=0)
lmbd = np.sqrt(1-(np.dot(y_t, x_t))**2)
half_S = np.dot(np.expand_dims(x_t,1), np.expand_dims(y_t,0))
S = half_S - half_S.T
update = np.identity(m) + np.sin(2 * eta_prime * lmbd)/lmbd * S + (1 - np.cos(2 * eta_prime * lmbd))/lmbd**2 * np.dot(S,S)
D_t[:,idx_t/n_theta] = np.dot(update, d_t)
# Rotate D_t back to generate D_r
Theta_t[idx_t/n_theta] = theta_t
D_r = shift_filters(D_t, n_theta, Theta_t, n_sections)
if EV_SCORE and (t%500 == 0):
loss = score_rot_invar_dic(data, D_r, n_theta, sparsity)
losses.append(loss)
data = shuffle(data.T).T
return D_r, losses
def score_rot_invar_dic(data, D_r, n_theta, sparsity, mask = None):
m, n = data.shape
L = 0
if mask is None:
mask_e = 1
else:
mask_e = mask.flatten()
for t in range(n):
x_t = data[:,t]
idx_t, alphas_t, theta_t = rot_invar_omp(D_r, x_t, sparsity, n_theta)
d_t = D_r[:,idx_t]
e_t = (x_t - np.dot(d_t, alphas_t))*mask_e
L += 1./(2*n)*np.dot(e_t.T,e_t).sum()
return L
def code_rot_invar_dic(data, D_r, n_theta, sparsity):
m, n = data.shape
m, ktheta = D_r.shape
k = ktheta/n_theta
activation_map = np.zeros((k, n_theta))
for t in range(n):
x_t = data[:,t]
idx_t, alphas_t, theta_t = rot_invar_omp(D_r, x_t, sparsity, n_theta)
for i in range(sparsity):
activation_map[idx_t[i]/n_theta, idx_t[i]%n_theta] += alphas_t[i]**2
return activation_map
def code_samples_rot_invar_dic(data, D_r, n_theta, sparsity):
m, n = data.shape
m, ktheta = D_r.shape
k = ktheta/n_theta
coef_samples = []
for t in range(n):
x_t = data[:,t]
coeffs = np.zeros((k, n_theta))
idx_t, alphas_t, theta_t = rot_invar_omp(D_r, x_t, sparsity, n_theta)
for i in range(sparsity):
coeffs[idx_t[i]/n_theta, idx_t[i]%n_theta] += np.abs(alphas_t[i]) #**2
coef_samples.append(coeffs.flatten())
return np.vstack(coef_samples)
def score_dict(data, D_t, sparsity):
m, n = data.shape
L = 0
for t in range(0,n):
x_t = data[:,t]
idx_t, alphas_t = omp(D_t, x_t, sparsity)
d_t = D_t[:,idx_t]
e_t = np.dot(d_t,alphas_t) - x_t
L += 1./(2*n)*np.dot(e_t.T,e_t).sum()
return L