forked from MaoXu/jade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitening.py
103 lines (75 loc) · 2.81 KB
/
whitening.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
#https://theclevermachine.wordpress.com/2013/03/30/the-statistical-whitening-transform/
inputdir = "/Users/janicelan/Desktop/Current/231a_data/faces/"
outputdir = "/Users/janicelan/Desktop/Current/231a_data/whitened/"
# https://gist.github.com/duschendestroyer/5170087
import numpy as np
from scipy import linalg
from scipy import misc
from sklearn.utils import array2d, as_float_array
from sklearn.base import TransformerMixin, BaseEstimator
import matplotlib.pyplot as plt
import os
# filename = '00026eb27c4918da6470f236536be805_face_0.jpg'
allfiles = os.listdir(inputdir)
imagefiles = [f for f in allfiles if f[-4:] == '.jpg']
chunksize = len(imagefiles)
X = np.zeros((chunksize, 256*256*3))
for i in xrange(chunksize):
img = misc.imresize(misc.imread(inputdir + imagefiles[i]), (256,256,3))
# print np.reshape(img, (1, 256*256*3))
X[i,:] = np.reshape(img, (1, 256*256*3))
# X = misc.imresize(img, (256,256,3))[:,:,i]
# plt.imshow(X)
# plt.figure()
# the actual whitening: ########
X -= np.mean(X, axis=0) # zero-center the data (important)
cov = np.dot(X.T, X) / X.shape[0] # get the data covariance matrix
U,S,V = np.linalg.svd(cov)
Xrot = np.dot(X, U) # decorrelate the data
Xrot_reduced = np.dot(X, U[:,:]) # Xrot_reduced becomes [N x 100]
# whiten the data:
# divide by the eigenvalues (which are square roots of the singular values)
Xwhite = Xrot / np.sqrt(S + 1e-5)
#####################
for i in xrange(chunksize):
newimg = Xwhite[i,:].reshape((256,256,3))
misc.imsave(outputdir + imagefiles[i], newimg)
# plt.imshow(newimg)
# plt.show()
'''
class ZCA(BaseEstimator, TransformerMixin):
def __init__(self, regularization=10**-5, copy=False):
self.regularization = regularization
self.copy = copy
def fit(self, X, y=None):
X = array2d(X)
X = as_float_array(X, copy = self.copy)
self.mean_ = np.mean(X, axis=0)
X -= self.mean_
sigma = np.dot(X.T,X) / X.shape[1]
U, S, V = linalg.svd(sigma)
tmp = np.dot(U, np.diag(1/np.sqrt(S+self.regularization)))
self.components_ = np.dot(tmp, U.T)
return self
def transform(self, X):
X = array2d(X)
X_transformed = X - self.mean_
X_transformed = np.dot(X_transformed, self.components_.T)
return X_transformed
'''
'''
def whiten(X,fudge=1E-18):
# the matrix X should be observations-by-components
# get the covariance matrix
Xcov = np.dot(X.T,X)
# eigenvalue decomposition of the covariance matrix
d, V = np.linalg.eigh(Xcov)
# a fudge factor can be used so that eigenvectors associated with
# small eigenvalues do not get overamplified.
D = np.diag(1. / np.sqrt(d+fudge))
# whitening matrix
W = np.dot(np.dot(V, D), V.T)
# multiply by the whitening matrix
X_white = np.dot(X, W)
return X_white, W
'''