-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCA.py
43 lines (40 loc) · 1.71 KB
/
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
37
38
39
40
41
42
43
# %% First - Encode all images
from train import *
from sklearn.decomposition import PCA
mean = []
def create_matrix(model_number):
out = np.zeros((70000, 512))
epoch, vae, opt, l1, l2 = load(f"D:\\Github\\misc\\VAE\\models\\number_{model_number}\\checkpoint.pth")
vae.eval()
for i in range(sub_datasets):
print(f'Dataset n° : {i + 1}')
j = 0
batches = batch_provider(data[i], batch_size, process_batch, 4, 16, True)
for image_batch in batches:
with torch.no_grad():
mu, logvar = vae.encode(image_batch)
mu = mu.squeeze()
logvar = logvar.squeeze()
z = vae.reparameterize(mu, logvar).cpu().detach().numpy()
n, _ = z.shape
out[10000*i + batch_size*j : 10000*i + batch_size*j + n, :] = z
j += 1
np.save(f"D:\\Github\\misc\\VAE\\models\\number_{model_number}\\to_pca.npy", out)
def create_mean(model_number):
encoded = np.load(f"D:\\Github\\misc\\VAE\\models\\number_{model_number}\\to_pca.npy")
print(np.mean(encoded, axis = 0).shape)
np.save(f"D:\\Github\\misc\\VAE\\models\\number_{model_number}\\mean.npy", np.mean(encoded, axis = 0))
# %%
def make_pca(model_number):
pca = PCA(n_components=512)
path = f"D:\\Github\\misc\\VAE\\models\\number_{model_number}\\"
np.load(path +"to_pca.npy")
mean = np.load(path + "mean.npy")
to_pca = np.load(path +"to_pca.npy")
mean_matrix = np.array([mean for i in range(to_pca.shape[0])])
pca.fit(to_pca - mean_matrix)
values = np.sqrt(pca.explained_variance_)
vectors = pca.components_
np.save(path + "eigenvalues.npy",values)
np.save(path + "eigenvectors.npy",vectors)
# %%