-
Notifications
You must be signed in to change notification settings - Fork 121
/
make_pca.py
36 lines (27 loc) · 845 Bytes
/
make_pca.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
from __future__ import division, print_function
import click
import numpy as np
import data
import util
@click.command()
@click.option('--directory', default='data/train_tiny')
def main(directory):
filenames = data.get_image_files(directory)
bs = 1000
batches = [filenames[i * bs : (i + 1) * bs]
for i in range(int(len(filenames) / bs) + 1)]
Us, evs = [], []
for batch in batches:
images = np.array([data.load_augment(f, 128, 128) for f in batch])
X = images.transpose(0, 2, 3, 1).reshape(-1, 3)
cov = np.dot(X.T, X) / X.shape[0]
U, S, V = np.linalg.svd(cov)
ev = np.sqrt(S)
Us.append(U)
evs.append(ev)
print('U')
print(np.mean(Us, axis=0))
print('eigenvalues')
print(np.mean(evs, axis=0))
if __name__ == '__main__':
main()