-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgos.py
574 lines (468 loc) · 18.9 KB
/
algos.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import copy
import tensorly as tl
import numpy as np
from helpers import *
def cmtf4si(corrupt_img, y=None, c_m=None, r=10, omega=None, alpha=1, tol=1e-4, maxiter=800, init='random', printitn=1000):
"""
CMTF Compute a Coupled Matrix and Tensor Factorization (and recover the Tensor).
---------
:param 'corrupt_img' - The corrupted input tensor.
:param 'y' - Coupled Matries
:param 'c_m' - Coupled Modes
:param 'r' - Tensor Rank
:param 'omega'- Index Tensor of Obseved Entries
:param 'alpha'- Impact factor for HiL part {0.0-1.0}
:param 'tol' - Tolerance on difference in fit {1.0e-4}
:param 'maxiters' - Maximum number of iterations {50}
:param 'init' - Initial guess [{'random'}|'nvecs'|cell array]
:param 'printitn' - Print fit every n iterations; 0 for no printing {1}
---------
:return
P: Decompose result.(kensor)
x: Recovered Tensor.
V: Projection Matrix.
---------
"""
x = corrupt_img.copy()
# Construct omega if no input
if omega is None:
omega = x * 0 + 1
bool_omeg = np.array(omega, dtype=bool)
# Extract number of dimensions and norm of x.
N = len(x.shape)
normX = np.linalg.norm(x)
dimorder = np.arange(N) # 'dimorder' - Order to loop through dimensions {0:(ndims(A)-1)}
# Define convergence tolerance & maximum iteration
fitchangetol = 1e-4
maxiters = maxiter
# Recover or just decomposition
recover = 0
if 0 in omega:
recover = 1
Uinit = []
Uinit.append([])
for n in dimorder[1:]:
Uinit.append(np.random.random([x.shape[n], r]))
# Set up for iterations - initializing U and the fit.
# STEP 1: a random V is initialized - y x V = |mode x rank|
U = Uinit[:]
if type(c_m) == int:
V = np.random.random([y.shape[1], r])
else:
V = [np.random.random([y[i].shape[1], r]) for i in range(len(c_m))]
fit = 0
# Save hadamard product of each U[n].T*U[n]
UtU = np.zeros([N, r, r])
for n in range(N):
if len(U[n]):
UtU[n, :, :] = np.dot(U[n].T, U[n])
for iter in range(1, maxiters + 1):
fitold = fit
oldX = x * 1.0
# Iterate over all N modes of the Tensor
for n in range(N):
# Calculate Unew = X[n]* khatrirao(all U except n, 'r').
ktr = tl.tenalg.khatri_rao(U, weights=None, skip_matrix=n)
Unew = np.dot(tl.unfold(x, n) ,ktr)
# Compute the matrix of coefficients for linear system
temp = list(range(n))
temp[len(temp):len(temp)] = list(range(n + 1, N))
B = np.prod(UtU[temp, :, :], axis=0)
if int != type(c_m):
tempCM = [i for i, a in enumerate(c_m) if a == n]
elif c_m == n:
tempCM = [0]
else:
tempCM = []
if tempCM != [] and int != type(c_m):
for i in tempCM:
B = B + np.dot(V[i].T, V[i])
Unew = Unew + np.dot(y[i], V[i])
V[i] = np.dot(y[i].T, Unew)
V[i] = V[i].dot(np.linalg.inv(np.dot(Unew.T, Unew)))
elif tempCM != []:
B = B + np.dot(V.T, V)
Unew = Unew + np.dot((alpha)*(y*~bool_omeg)+(y*bool_omeg), V)
V = np.dot(((alpha)*(y*~bool_omeg)+(y*bool_omeg)).T, Unew)
V = V.dot(np.linalg.inv(np.dot(Unew.T, Unew)))
Unew = Unew.dot(np.linalg.inv(B))
U[n] = Unew
UtU[n, :, :] = np.dot(U[n].T, U[n])
# Reconstructed fitted Ktensor
lamb = np.ones(r)
final_shape = tuple(u.shape[0] for u in U)
P = np.dot(lamb.T, tl.tenalg.khatri_rao(U).T)
P = P.reshape(final_shape)
x[bool_omeg] = corrupt_img[bool_omeg]
x[~bool_omeg] = P[~bool_omeg]
fitchange = np.linalg.norm(x - oldX)
# Check for convergence
if (iter > 1) and (fitchange < fitchangetol):
flag = 0
else:
flag = 1
if (printitn != 0 and iter % printitn == 0) or ((printitn > 0) and (flag == 0)):
if recover == 0:
print ('CMTF: iterations=',iter, 'f=',fit, 'f-delta=',fitchange)
else:
print ('CMTF: iterations=',iter, 'f-delta=',fitchange)
if flag == 0:
break
return P, x, V
def cmtf(corrupt_img, y=None, c_m=None, r=20, omega=None, tol=1e-4, maxiter=800, init='random', printitn=500):
"""
CMTF Compute a Coupled Matrix and Tensor Factorization (and recover the Tensor).
---------
:param corrupt_img : Tensor
:param y : Coupled Matrices
:param c_m : Coupled Modes
:param r : Tensor Rank
:param omega : Index Tensor of Observed Entries (mask)
:param tol : Tolerance on difference in fit {1.0e-4}
:param maxiters : Maximum number of iterations {50}
:param init : Initial guess [{'random'}|'nvecs'|cell array]
:param printitn : Print fit every n iterations; 0 for no printing {1}
---------
:return
P: Decompose result(Tensor)
x: Recovered Tensor.
# V: Projection Matrix.
---------
"""
print('CMTF:')
x = copy.deepcopy(corrupt_img)
if c_m is None:
c_m = 0
elif int == type(c_m):
c_m = c_m - 1
else:
c_m = [i - 1 for i in c_m]
# Construct omega if no input
if omega is None:
omega = x * 0 + 1
bool_omeg = np.array(omega, dtype=bool)
# Extract number of dimensions and norm of x.
N = len(x.shape)
normX = np.linalg.norm(x)
dimorder = np.arange(N) #[0,1,2] for N=3 # 'dimorder' - Order to loop through dimensions {0:(ndims(A)-1)}
# Define convergence tolerance & maximum iteration
fitchangetol = 1e-4
maxiters = maxiter
# Recover or just decomposition
recover = 0
if 0 in omega:
recover = 1
Uinit = []
Uinit.append([])
for n in dimorder[1:]:
Uinit.append(np.random.random([x.shape[n], r]))
# Set up for iterations - initializing U and the fit.
U = Uinit[:]
if type(c_m) == int:
V = np.random.random([y.shape[1], r])
else:
V = [np.random.random([y[i].shape[1], r]) for i in range(len(c_m))]
fit = 0
# Save hadamard product of each U[n].T*U[n]
UtU = np.zeros([N, r, r])
for n in range(N):
if len(U[n]):
UtU[n, :, :] = np.dot(U[n].T, U[n])
for iter in range(1, maxiters + 1):
fitold = fit
oldX = x * 1.0
# Iterate over all N modes of the Tensor
for n in range(N):
# Calculate Unew = X[n]* khatrirao(all U except n, 'r').
ktr = tl.tenalg.khatri_rao(U, weights=None, skip_matrix=n) #000
Unew = np.dot(tl.unfold(x, n) ,ktr) #000
# Compute the matrix of coefficients for linear system
temp = list(range(n))
temp[len(temp):len(temp)] = list(range(n + 1, N))
B = np.prod(UtU[temp, :, :], axis=0)
if int != type(c_m):
tempCM = [i for i, a in enumerate(c_m) if a == n]
elif c_m == n:
tempCM = [0]
else:
tempCM = []
if tempCM != [] and int != type(c_m):
for i in tempCM:
B = B + np.dot(V[i].T, V[i])
Unew = Unew + np.dot(y[i], V[i])
V[i] = np.dot(y[i].T, Unew)
V[i] = V[i].dot(np.linalg.inv(np.dot(Unew.T, Unew)))
elif tempCM != []:
B = B + np.dot(V.T, V)
tempvt = np.dot(y, V)
Unew = Unew + tempvt
V = np.dot(y.T, Unew)
V = V.dot(np.linalg.inv(np.dot(Unew.T, Unew)))
Unew = Unew.dot(np.linalg.inv(B))
U[n] = Unew
UtU[n, :, :] = np.dot(U[n].T, U[n])
# Reconstructed fitted Ktensor
lamb = np.ones(r)
# P = pyten.tenclass.Ktensor(lamb, U)
final_shape = tuple(u.shape[0] for u in U)
P = np.dot(lamb.T, tl.tenalg.khatri_rao(U).T)
P = P.reshape(final_shape)
x[bool_omeg] = corrupt_img[bool_omeg]
x[~bool_omeg] = P[~bool_omeg]
# x, _ = replacePixels(x, np.ravel(~bool_omeg), P)
fitchange = np.linalg.norm(x - oldX)
# Check for convergence
if (iter > 1) and (fitchange < fitchangetol):
flag = 0
else:
flag = 1
if (printitn != 0 and iter % printitn == 0) or ((printitn > 0) and (flag == 0)):
if recover == 0:
print ('CMTF: iterations=',iter, 'f=',fit, 'f-delta=',fitchange)
else:
print ('CMTF: iterations=',iter, 'f-delta=',fitchange)
if flag == 0:
break
return P, x
def cp_als(y, r=12, omega=None, tol=1e-4, maxiter=800, init='random', printitn=500, original_img = None):
""" CP_ALS Compute a CP decomposition of a Tensor (and recover it).
---------
:param 'y' : Tensor with Missing data
:param 'r' : Rank of the tensor
:param 'omega' : Missing data Index Tensor
:param 'tol' : Tolerance on difference in fit
:param 'maxiters' : Maximum number of iterations
:param 'init' : Initial guess ['random'|'nvecs'|'eigs']
:param 'printitn' : Print fit every n iterations; 0 for no printing
---------
:return
'P' : Decompose result.(kensor)
'X' : Recovered Tensor.
---------
"""
print('cp_als:')
X = copy.deepcopy(y)
if omega is None:
omega = X.data * 0 + 1
bool_omeg = np.array(omega, dtype=bool)
# Extract number of dimensions and norm of X.
N = len(X.shape)
normX = np.linalg.norm(X)
dimorder = np.arange(N) # 'dimorder' - Order to loop through dimensions {0:(ndims(A)-1)}
# Define convergence tolerance & maximum iteration
fitchangetol = tol
maxiters = maxiter
Uinit = []
Uinit.append([])
for n in dimorder[1:]:
Uinit.append(np.random.random([X.shape[n], r]))
# Set up for iterations - initializing U and the fit.
U = Uinit[:]
fit = 0
if printitn > 0:
print('\nCP_ALS:\n')
# Save hadamard product of each U[n].T*U[n]
UtU = np.zeros([N, r, r])
for n in range(N):
if len(U[n]):
UtU[n, :, :] = np.dot(U[n].T, U[n])
for iter in range(1, maxiters + 1):
fitold = fit
oldX = X * 1.0
# Iterate over all N modes of the Tensor
for n in range(N):
# Calculate Unew = corrupt_img(n) * khatrirao(all U except n, 'r').
ktr = tl.tenalg.khatri_rao(U, weights=None, skip_matrix=n)
Unew = np.dot(tl.unfold(X, n) ,ktr)
# Compute the matrix of coefficients for linear system
temp = list(range(n))
temp[len(temp):len(temp)] = list(range(n + 1, N))
B = np.prod(UtU[temp, :, :], axis=0)
Unew = Unew.dot(np.linalg.inv(B))
# Normalize each vector to prevent singularities in coefmatrix
if iter == 1:
lamb = np.sqrt(np.sum(np.square(Unew), 0)) # 2-norm
else:
lamb = np.max(Unew, 0)
lamb = np.max([lamb, np.ones(r)], 0) # max-norm
lamb = np.array([x * 1.0 for x in lamb])
Unew = Unew / lamb
U[n] = Unew
UtU[n, :, :] = np.dot(U[n].T, U[n])
# Reconstructed fitted Ktensor
final_shape = tuple(u.shape[0] for u in U)
P = np.dot(lamb.T, tl.tenalg.khatri_rao(U).T)
P = P.reshape(final_shape)
X[bool_omeg] = y[bool_omeg]
X[~bool_omeg] = P[~bool_omeg]
# X[~bool_omeg] = original_img[~bool_omeg]
# X, _ = replacePixels(X, np.ravel(~bool_omeg), P)
fitchange = np.linalg.norm(X - oldX)
# Check for convergence
if (iter > 1) and (fitchange < fitchangetol):
flag = 0
else:
flag = 1
if (printitn != 0 and iter % printitn == 0) or ((printitn > 0) and (flag == 0)):
print ('cp_als: iterations=',iter, 'f-delta=',fitchange)
# Check for convergence
if flag == 0:
break
return P, X
def siLRTC(corrupt_imgorig, mask2D, a, b, K):
"""
Simple Low-Rank Tensor Completion (siLRTC) algorithm.
:param corrupt_imgorig (ndarray): The corrupted input tensor/image.
:param mask2D (ndarray): Binary mask indicating missing entries in the tensor.
:param a (list or ndarray): List of regularization parameters for each mode.
:param b (list or ndarray): List of scaling factors for each mode.
:param K (int): Number of iterations for the algorithm.
:return
ndarray: The completed tensor after running the siLRTC algorithm.
"""
print('SiLRTC:')
# Convert the mask to a boolean array
bool_omeg = np.array(mask2D, dtype=bool)
# Deep copy the input tensor to avoid modifying the original data
corrupt_img = copy.deepcopy(corrupt_imgorig)
orig = copy.deepcopy(corrupt_imgorig)
# Get the dimensions of the input tensor
imSize = corrupt_img.shape
# Main algorithm loop
for _ in range(K):
print(_, end=" ")
# Initialize an array to accumulate results for each mode
M = np.zeros(imSize)
# Iterate over each mode of the tensor
for j in range(3):
# Unfold the tensor along the current mode
unf = b[j]*shrinkage(tl.base.unfold(corrupt_img, j),a[j]/b[j])
# Fold the updated unfolded tensor back to its original shape and add to M
M = M + tl.fold(unf, j, imSize)
# Normalize M by sum of scaling factors
M/=sum(b)
# Update indices that we know from Image into M and set corrupt_img equal to M
M[bool_omeg] = orig[bool_omeg]
corrupt_img = M
return corrupt_img
def haLRTC(corrupt_imgorig, mask2D, a, b, K):
"""
Perform High-accuracy Low-Rank Tensor Completion (HaLRTC) algorithm.
---------
Parameters:
:param corrupt_imgorig (ndarray): The corrupted input tensor/image.
:param mask2D (ndarray): Binary mask indicating missing entries in the tensor.
:param a (list or ndarray): regularization parameters for each mode.
:param b (float): regularization parameter to control shrinkage.
:param K (int): Number of iterations for the algorithm.
:returns
ndarray: The completed tensor after running the HaLRTC algorithm.
"""
# Deep copy the input tensor to avoid modifying the original data
corrupt_img = copy.deepcopy(corrupt_imgorig)
orig = copy.deepcopy(corrupt_imgorig)
print('HaLRTC:')
# Convert the mask to a boolean array
bool_omeg = np.array(mask2D, dtype=bool)
p = 1e-6
# Get the dimensions of the input tensor
imSize = corrupt_img.shape
imSize = corrupt_img.shape
ArrSize = np.array(imSize)
ArrSize = np.append(ArrSize, 3) # array of Mi / Yi
# Initialize arrays to store intermediate results
Mi = np.zeros(ArrSize)
Yi = np.zeros(ArrSize)
# Main algorithm loop
for _ in range(K):
print(_, end=" ")
# Iterate over each mode of the tensor
for i in range(ArrSize[3]):
# Unfold the tensor along the current mode
elem = tl.unfold(corrupt_img, i)
# Update elem using the unfolded tensor from Yi divided by p
elem = np.add(elem,tl.unfold(np.squeeze(Yi[:, :, :, i]), i) / p,out = elem, casting="unsafe")
# Apply shrinkage operation to elem using regularization parameter a[i] / p
elem = shrinkage(elem, a[i] / p)
# Fold the updated elem tensor back to its original shape and store in Mi
Mi[:,:,:,i] = tl.fold(elem, i, imSize)
# Update the current tensor using the average of Mi and Yi divided by p
corrupt_img = (1/ArrSize[3]) * np.sum(Mi - Yi/p, ArrSize[3])
# Restore the original values in the tensor's missing entries indicated by bool_omeg
corrupt_img[bool_omeg] = orig[bool_omeg]
# Update Yi using the difference between Mi and the updated tensor
for i in range(ArrSize[3]):
Yi[:, :, :, i] = Yi[:, :, :, i] - p * (Mi[:, :, :, i] - corrupt_img)
# Update the parameter p
p = 1.2 * p
# Return the completed tensor after K iterations
return corrupt_img
def faLRTC(corrupt_imgorig, mask2D, a, b, K, img):
"""
Fast Low-Rank Tensor Completion (FaLRTC) algorithm.
:param corrupt_imgorig (ndarray): The corrupted input tensor.
:param mask2D (ndarray): Binary mask indicating missing entries in the tensor.
:param a (list or ndarray): List of regularization parameters for each mode.
:param b (list or ndarray): Not used in the provided code snippet.
:param K (int): Number of iterations for the algorithm.
:param (ndarray): Not used in the provided code snippet.
:return:
ndarray: The completed tensor after running the FaLRTC algorithm.
"""
print('FaLRTC:')
# Deep copy the input tensor to avoid modifying the original data
corrupt_img = copy.deepcopy(corrupt_imgorig)
imSize = corrupt_img.shape
# Declaring Constants
C = 0.5
u = 10^5
ui = a/u
Z = copy.deepcopy(corrupt_img)
W = copy.deepcopy(corrupt_img)
B = 0
L = np.sum(ui)
# Main algorithm loop
for _ in range(K):
print(_, end=" ")
# Inner loop for optimization
while True:
theta = (1 + np.sqrt(1 + 4 * L * B)) / (2 * L)
thetabyL = theta/L
W = thetabyL/(B + thetabyL) * Z + B/(B+thetabyL) * corrupt_img
dfw = np.zeros(imSize)
fx = 0
fw = 0
for i in range(3):
Sig, T = Trimming(tl.unfold(corrupt_img,i), ui[i]/a[i])
fx += np.sum(Sig)
Sig, T = Trimming(tl.unfold(W,i), ui[i]/a[i])
fw += np.sum(Sig)
dfw += tl.fold(a[i]**2/ui[i]*T, i, imSize)
# print(dfw.shape)
dfw = np.array(np.gradient(dfw, axis = 0))
# print(dfw.shape)
# print(fx)
# print(fw - np.sum(dfw**2)/L)
if fx <= fw - np.sum(dfw**2)/(2*L):
# print('break1')
break
Xp = W - dfw/L
fxp = 0
for i in range(3):
Sig, T = Trimming(tl.unfold(Xp,i), ui[i]/a[i])
fxp += np.sum(Sig)
# print(fxp)
# print(fw - np.sum(dfw**2)/(2*L))
if fxp-20 <= fw - np.sum(dfw**2)/(2*L):
corrupt_img[mask2D] = Xp[mask2D]
# print('break2')
break
L = L/C
# print(L)
if L > 1e10:
print("L too large, ending function..")
return corrupt_img
Z = np.subtract(Z,thetabyL*dfw, out = Z, casting="unsafe") #check uint8/float64 issue
B += thetabyL
# Return the completed tensor after K iterations
return corrupt_img