-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_manifold.py
More file actions
221 lines (175 loc) · 7.74 KB
/
Copy pathload_manifold.py
File metadata and controls
221 lines (175 loc) · 7.74 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 18 09:47:42 2024
@author: fmry
"""
#%% Sources
#%% Modules
import jax.numpy as jnp
from jax import jit, lax
import haiku as hk
from geometry.manifolds.riemannian import nSphere, nEllipsoid, nEuclidean, \
nParaboloid, HyperbolicParaboloid, SPDN, H2, Cylinder, Landmarks, T2, LatentSpaceManifold, FisherRaoGeometry
from vae.model_loader import mnist_generator, svhn_generator, celeba_generator, load_model
from vae.models import mnist_encoder
from vae.models import mnist_decoder
from vae.models import svhn_encoder
from vae.models import svhn_decoder
from vae.models import celeba_encoder
from vae.models import celeba_decoder
#%% Load manifolds
def load_manifold(manifold:str="Euclidean",
dim:int = 2,
svhn_path:str = "../../../Data/SVHN/",
celeba_path:str = "../../../Data/CelebA/",
):
rho = 0.5 #default
if manifold == "Euclidean":
M = nEuclidean(dim=dim)
z0 = -jnp.linspace(0,1,dim)
zT = jnp.ones(dim, dtype=jnp.float32)
rho = 0.5
if manifold == "SPDN":
M = SPDN(N=dim)
x0 = jnp.eye(dim)
z0 = M.invf(x0)
zT = jnp.linspace(0.5,1.0, M.dim)
rho = 0.5
elif manifold == "Paraboloid":
M = nParaboloid(dim=dim)
z0 = -jnp.linspace(0,1,dim)
zT = jnp.ones(dim, dtype=jnp.float32)
rho = 0.5
elif manifold == "Sphere":
M = nSphere(dim=dim)
z0 = -jnp.linspace(0,1,dim)
zT = 0.5*jnp.ones(dim, dtype=jnp.float32)
rho = .5
elif manifold == "Ellipsoid":
params = jnp.linspace(0.5,1.0,dim+1)
M = nEllipsoid(dim=dim, params=params)
z0 = -jnp.linspace(0,1,dim)
zT = 0.5*jnp.ones(dim, dtype=jnp.float32)
rho = 0.5
elif manifold == "H2":
M = H2()
z0 = jnp.array([1.0,1.0])
zT = jnp.array([0.1, 0.1])
rho = 0.5
elif manifold == "Cylinder":
M = Cylinder()
z0 = jnp.array([-5*jnp.pi/4,1.0])
zT = jnp.array([jnp.pi, -1.0])
rho = 0.5
elif manifold == "T2":
M = T2(R=3.0, r=1.0)
z0 = jnp.array([0.0, 0.0])
zT = jnp.array([5*jnp.pi/4, 5*jnp.pi/4])
rho = 0.5
elif manifold == "Landmarks":
M = Landmarks(N=dim, m=2, k_alpha=0.1)
z0 = jnp.vstack((jnp.linspace(-5.0,5.0,M.N),jnp.linspace(0.0,0.0,M.N))).T.flatten()
zT = jnp.vstack((jnp.linspace(-1.0,1.0,M.N),jnp.linspace(0.0, 0.0, M.N))).T.flatten()
rho = 0.5
elif manifold == "Gaussian":
M = FisherRaoGeometry(distribution='Gaussian')
z0 = jnp.array([-1.0, 0.5])
zT = jnp.array([1.0, 1.0])
rho = .5
elif manifold == "Frechet":
M = FisherRaoGeometry(distribution='Frechet')
z0 = jnp.array([0.5, 0.5])
zT = jnp.array([1.0, 1.0])
rho = 0.5
elif manifold == "Cauchy":
M = FisherRaoGeometry(distribution='Cauchy')
z0 = jnp.array([-1.0, 0.5])
zT = jnp.array([1.0, 1.0])
rho = 0.5
elif manifold == "Pareto":
M = FisherRaoGeometry(distribution='Pareto')
z0 = jnp.array([0.5, 0.5])
zT = jnp.array([1.0, 1.0])
rho = 0.5
elif manifold == "celeba":
celeba_state = load_model(''.join(('models/', f'celeba_{dim}/')))
@hk.transform
def celeba_tencoder(x):
encoder = celeba_encoder(latent_dim=32)
return encoder(x)[0]
@hk.transform
def celeba_tdecoder(x):
decoder = celeba_decoder()
return decoder(x)
celeba_encoder_fun = jit(lambda x: celeba_tencoder.apply(lax.stop_gradient(celeba_state.params),
None,
x.reshape(-1,64,64,3)
)[0].reshape(-1,dim).squeeze())
celeba_decoder_fun = jit(lambda x: celeba_tdecoder.apply(lax.stop_gradient(celeba_state.params),
None,
x.reshape(-1,dim)
).reshape(-1,64*64*3).squeeze())
M = LatentSpaceManifold(dim=dim,
emb_dim=64*64*3,
encoder=celeba_encoder_fun,
decoder=celeba_decoder_fun,
)
z0 = jnp.load(f'data/celeba_{dim}/z0.npy')
zT = jnp.load(f'data/celeba_{dim}/zT.npy')
return z0, zT, M, rho
elif manifold == "svhn":
svhn_state = load_model(''.join(('models/', f'svhn_{dim}/')))
@hk.transform
def svhn_tencoder(x):
encoder = svhn_encoder(latent_dim=dim)
return encoder(x)[0]
@hk.transform
def svhn_tdecoder(x):
decoder = svhn_decoder()
return decoder(x)
svhn_encoder_fun = jit(lambda x: svhn_tencoder.apply(lax.stop_gradient(svhn_state.params),
None,
x.reshape(-1,32,32,3)
)[0].reshape(-1,dim).squeeze())
svhn_decoder_fun = jit(lambda x: svhn_tdecoder.apply(lax.stop_gradient(svhn_state.params),
None,
x.reshape(-1,dim)
).reshape(-1,32*32*3).squeeze())
M = LatentSpaceManifold(dim=dim,
emb_dim=32*32*3,
encoder=svhn_encoder_fun,
decoder=svhn_decoder_fun,
)
z0 = jnp.load(f'data/svhn_{dim}/z0.npy')
zT = jnp.load(f'data/svhn_{dim}/zT.npy')
return z0, zT, M, rho
elif manifold == "mnist":
mnist_state = load_model(''.join(('models/', f'mnist_{dim}/')))
@hk.transform
def mnist_tencoder(x):
encoder = mnist_encoder(latent_dim=dim)
return encoder(x)[0]
@hk.transform
def mnist_tdecoder(x):
decoder = mnist_decoder()
return decoder(x)
mnist_encoder_fun = lambda x: mnist_tencoder.apply(mnist_state.params,
None,
x.reshape(-1,28,28,1)
)[0].reshape(-1,dim).squeeze()
mnist_decoder_fun = lambda x: mnist_tdecoder.apply(mnist_state.params,
None,
x.reshape(-1,dim)
).reshape(-1,28*28).squeeze()
M = LatentSpaceManifold(dim=dim,
emb_dim=28*28,
encoder=mnist_encoder_fun,
decoder=mnist_decoder_fun,
)
z0 = jnp.load(f'data/mnist_{dim}/z0.npy')
zT = jnp.load(f'data/mnist_{dim}/zT.npy')
return z0, zT, M, rho
else:
raise ValueError(f"Manifold, {manifold}, is not defined. Only suported is: \n\t-Euclidean\n\t-Paraboloid\n\t-Sphere")
return z0, zT, M, rho