-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeenrating_stats.py
57 lines (50 loc) · 1.45 KB
/
geenrating_stats.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
import numpy as np
import numpy.linalg as linalg
import sys
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
import time
start = time.time()
A,B=[],[]
for i in range(512,300,-1):
r = i
Maxiter = 500
Ro = float('inf')
t = 0
image = Image.open('Lenna.png')
gray_img = image.convert("L")
M = np.asarray(gray_img)
U,S,V = linalg.svd(M)
Vbarre = V[:,:r]
Ro = float('inf')
t = 0
while 1:
t += 1
VbarrePlus = np.where(Vbarre < 0, 0, Vbarre)
Y = np.dot( Vbarre.T, VbarrePlus)
Ro0 = Ro
Ro = linalg.norm(Y-np.identity(Y.shape[0]),'fro')**2
if(Ro0-Ro < sys.float_info.epsilon) or (t == Maxiter):
break
U,S,V= linalg.svd(Y)
Vbarre = np.dot(Vbarre,np.dot(U,V.T))
#S = np.where(Vbarre < 0, 0, Vbarre) #Vbarre Plus
S = np.dot(Vbarre,Y)
aux = np.dot(M,Vbarre)
W = np.dot(aux,linalg.inv(Y.T)) # transposé de la matrice diagonale inferieure de Y
# Il s'agit de l'inverse de la transposée de la matrice Y
out = np.dot(W,S.T)
data = Image.fromarray(out)
#plt.imshow(data, cmap='gray')
#plt.show()
new_p = data.convert("L")
#new_p.save("output"+str(r)+"v2.png")
loss = mean_absolute_error(out, M) #mean square error
A.append(i)
B.append(loss)
plt.plot(A,B)
plt.gca().invert_xaxis()
plt.show()
end = time.time()
print(end - start)