AbVishwas
/
Towards-extraction-of-orthogonal-and-parsimonious-non-linear-modes-from-turbulent-flows
Public
forked from KTH-FlowAI/Towards-extraction-of-orthogonal-and-parsimonious-non-linear-modes-from-turbulent-flows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction.py
71 lines (64 loc) · 1.93 KB
/
prediction.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
#%%
import numpy as np
from matplotlib import pyplot as plt
from tensorflow.keras import models
import keras.backend as K
from postprocessing.error import err
from config.train_config import config
"""
Generate prediction data by model.
Return:
.npz file contains:
'u_p' : prediction of velocity fields
'c' : latent vectors used for correlation matrix
'modes' : the reconstruction by only using each single mode
"""
model_name = f"VAE_ld{config.latent_dim}_b{config.beta}"
model_save_name = f"VAE_ld{config.latent_dim}_b{int(1000*config.beta)}e-3"
model_decoder_dir = "../models/de_{}.h5".format(model_name)
model_encoder_dir = "../models/en_{}.h5".format(model_name)
#%%
latent_dim = config.latent_dim
def sampling(args):
z_mean, z_log_sigma = args
epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim),
mean=0., stddev=1.0)
return z_mean + K.exp(z_log_sigma) * epsilon
encoder = models.load_model(model_encoder_dir)
print(encoder.summary())
decoder = models.load_model(model_decoder_dir)
print(decoder.summary())
inp = encoder.layers[0].input
out_d = decoder(encoder(inp))
model = models.Model(inp, out_d,name=model_name)
print(model.summary())
#%%
d = np.load("../data/U_train.npz")
u = d["U"][:,:96,4:196]
u = u -u.mean(0)
print(u.shape)
u = u.reshape(-1,96,192)
u = np.expand_dims(u,-1)
print(u.shape)
print(f"INFO: Going to predict , the data has shape of{u.shape}")
if model_name[0] == "A":
z = encoder.predict(u)
u_p = model.predict(u)
elif model_name[0] == "V":
z = encoder.predict(u)
z = sampling([z[0],z[1]])
u_p = decoder(z)
#%%
modes = decoder.predict(np.diag(np.ones(latent_dim))).squeeze()
e = err(u, u_p)
print(e)
#%%
plt.imshow(u[0, :, :])
plt.figure()
plt.imshow(u_p[0, :, :])
#%%
plt.figure()
plt.imshow(modes[0, :, :])
np.savez_compressed("/postprocessing/pred_data/"+model_save_name+".npz", u_p = u_p, modes = modes, c = z)
plt.show()
# %%