-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCl_denoiseP4.py
More file actions
435 lines (296 loc) · 14.2 KB
/
Copy pathCl_denoiseP4.py
File metadata and controls
435 lines (296 loc) · 14.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
"""
Followed from https://wiseodd.github.io/techblog/2016/12/10/variational-autoencoder/
"""
import numpy as np
from keras.layers import Input, Dense, Lambda
from keras.models import Model
from keras import optimizers
from keras import losses
import matplotlib as mpl
# mpl.use('Agg')
import matplotlib.pyplot as plt
import keras.backend as K
import params
# import Cl_load
# import SetPub
# SetPub.set_pub()
############### Setting same float, random seeds ##############
np.random.seed(42)
from tensorflow import set_random_seed
set_random_seed(42)
K.set_floatx('float32')
###################### PARAMETERS ##############################
original_dim = params.original_dim # 2549
#intermediate_dim3 = params.intermediate_dim3 # 1600
# intermediate_dim2 = params.intermediate_dim2 # 1024
# intermediate_dim1 = params.intermediate_dim1 # 512
intermediate_dim0 = params.intermediate_dim0 # 256
intermediate_dim = params.intermediate_dim # 256
latent_dim = params.latent_dim # 10
# ClID = params.ClID
num_train = params.num_train # 512
num_test = params.num_test # 32
num_para = params.num_para # 5
batch_size = params.batch_size # 8
num_epochs = params.num_epochs # 100
epsilon_mean = params.epsilon_mean # 1.0
epsilon_std = params.epsilon_std # 1.0
learning_rate = params.learning_rate # 1e-3
decay_rate = params.decay_rate # 0.0
noise_factor = params.noise_factor # 0.00
######################## I/O ##################################
DataDir = params.DataDir
PlotsDir = params.PlotsDir
ModelDir = params.ModelDir
fileOut = params.fileOut
# ----------------------------- i/o ------------------------------------------
# Trainfiles = np.loadtxt(DataDir + 'P'+str(num_para)+ClID+'Cl_'+str(num_train)+'.txt')
# Testfiles = np.loadtxt(DataDir + 'P'+str(num_para)+ClID+'Cl_'+str(num_test)+'.txt')
#
# x_train = Trainfiles[:, num_para+2:]
# x_test = Testfiles[:, num_para+2:]
# y_train = Trainfiles[:, 0: num_para]
# y_test = Testfiles[:, 0: num_para]
#
# print(x_train.shape, 'train sequences')
# print(x_test.shape, 'test sequences')
# print(y_train.shape, 'train sequences')
# print(y_test.shape, 'test sequences')
#
# ls = np.loadtxt(DataDir+'P'+str(num_para)+'ls_'+str(num_train)+'.txt')[2:]
datafile = ['DES', 'COSMOS', 'Galacticus'][1]
if datafile == 'DES' :
dirIn = '../data/'
allfiles = ['DES.train.dat', './DES5yr.nfits.dat']
bb_color = ['u', 'g', 'r', 'i', 'z', 'y']
Trainfiles = np.loadtxt(dirIn + allfiles[0])
Testfiles = np.loadtxt(dirIn + allfiles[1])
TrainshuffleOrder = np.arange(Trainfiles.shape[0])
TestshuffleOrder = np.arange(Testfiles.shape[0])
Trainfiles = Trainfiles[TrainshuffleOrder]
Testfiles = Testfiles[TestshuffleOrder]
x_train = Trainfiles[:num_train, 2:10] # color mag
x_test = Testfiles[:num_test, 2:10] # color mag
y_train = Trainfiles[:num_train, 0] # spec z
y_test = Testfiles[:num_test, 0] # spec z
if datafile == 'COSMOS' :
dirIn = '../../Data/fromJonas/'
allfiles = ['catalog_v0.txt', 'catalog_v3.txt'][0]
bb_color = ['u', 'g', 'r', 'i', 'z', 'y']
Trainfiles = np.loadtxt(dirIn + allfiles)
TrainshuffleOrder = np.arange(Trainfiles.shape[0])
Trainfiles = Trainfiles[TrainshuffleOrder]
x_train = Trainfiles[:num_train, 2:8] # color mag
x_test = Trainfiles[num_train + 1:, 2:8] # color mag
y_train = Trainfiles[:num_train, 0] # spec z
y_test = Trainfiles[num_train + 1:, 0] # spec z
print(x_train.shape, 'train sequences')
print(x_test.shape, 'test sequences')
print(y_train.shape, 'train sequences')
print(y_test.shape, 'test sequences')
#----------------------------------------------------------------------------
minVal = np.min( [np.min(x_train), np.min(x_test ) ])
meanFactor = 1.1*minVal if minVal < 0 else 0
# meanFactor = 0.0
print('-------mean factor:', meanFactor)
x_train = x_train - meanFactor #/ 255.
x_test = x_test - meanFactor #/ 255.
# x_train = np.log10(x_train) #x_train[:,2:] #
# x_test = np.log10(x_test) #x_test[:,2:] #
normFactor = np.max( [np.max(x_train), np.max(x_test ) ])
# normFactor = 1
print('-------normalization factor:', normFactor)
x_train = x_train.astype('float32')/normFactor #/ 255.
x_test = x_test.astype('float32')/normFactor #/ 255.
np.savetxt(DataDir+'meanfactorP'+str(num_para)+'_'+ fileOut +'.txt', [meanFactor])
np.savetxt(DataDir+'normfactorP'+str(num_para)+'_'+ fileOut +'.txt', [normFactor])
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
# # Trying to get x_train ~ (-1, 1) -- doesn't work well
# x_mean = np.mean(x_train, axis = 0)
# x_train = x_train - x_mean
# x_test = x_test - x_mean
## ADD noise
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)
# x_train_noisy = np.clip(x_train_noisy, 0., 1.)
# x_test_noisy = np.clip(x_test_noisy, 0., 1.)
# plt.plot(x_test_noisy.T, 'r', alpha = 0.3)
# plt.plot(x_test_noisy.T*(y_test[:,2]**2), 'b', alpha = 0.3)
x_train_noisy = K.cast_to_floatx(x_train_noisy)
x_train = K.cast_to_floatx(x_train)
# ------------------------------------------------------------------------------
################# ARCHITECTURE ###############################
# ----------------------------------------------------------------------------
# Q(z|X) -- encoder
inputs = Input(shape=(original_dim,))
#h_q3 = Dense(intermediate_dim3, activation='relu')(inputs) # ADDED intermediate layer
# h_q2 = Dense(intermediate_dim2, activation='relu')(inputs) # ADDED intermediate layer
# h_q1 = Dense(intermediate_dim1, activation='relu')(inputs) # ADDED intermediate layer
h_q0 = Dense(intermediate_dim0, activation='relu')(inputs) # ADDED intermediate layer
h_q = Dense(intermediate_dim, activation='relu')(h_q0)
mu = Dense(latent_dim, activation='linear')(h_q)
log_sigma = Dense(latent_dim, activation='linear')(h_q)
# ----------------------------------------------------------------------------
def sample_z(args):
mu, log_sigma = args
eps = K.random_normal(shape=(batch_size, latent_dim), mean=epsilon_mean, stddev=epsilon_std)
return mu + K.exp(log_sigma / 2) * eps
# Sample z ~ Q(z|X)
z = Lambda(sample_z)([mu, log_sigma])
# ----------------------------------------------------------------------------
# P(X|z) -- decoder
decoder_hidden = Dense(latent_dim, activation='relu')
decoder_hidden0 = Dense(intermediate_dim, activation='relu') # ADDED intermediate layer
decoder_hidden1 = Dense(intermediate_dim0, activation='relu') # ADDED intermediate layer
# decoder_hidden2 = Dense(intermediate_dim1, activation='relu') # ADDED intermediate layer
# decoder_hidden3 = Dense(intermediate_dim2, activation='relu') # ADDED intermediate layer
#decoder_hidden4 = Dense(intermediate_dim3, activation='relu') # ADDED intermediate layer
decoder_out = Dense(original_dim, activation='sigmoid')
h_p0 = decoder_hidden(z)
h_p1 = decoder_hidden0(h_p0) # ADDED intermediate layer
h_p2 = decoder_hidden1(h_p1) # ADDED intermediate layer
# h_p3 = decoder_hidden2(h_p2) # ADDED intermediate layer
# h_p4 = decoder_hidden3(h_p3) # ADDED intermediate layer
#h_p5 = decoder_hidden4(h_p4) # ADDED intermediate layer
outputs = decoder_out(h_p2)
# ----------------------------------------------------------------------------
# Overall VAE model, for reconstruction and training
vae = Model(inputs, outputs)
# Encoder model, to encode input into latent variable
# We use the mean as the output as it is the center point, the representative of the gaussian
encoder = Model(inputs, mu)
# Generator model, generate new data given latent variable z
# d_in = Input(shape=(latent_dim,))
# d_h = decoder_hidden(d_in)
# d_h1 = decoder_hidden1(d_h)
# d_h2 = decoder_hidden2(d_h1)
# d_out = decoder_out(d_h2)
# decoder = Model(d_in, d_out)
# build a digit generator that can sample from the learned distribution
decoder_input = Input(shape=(latent_dim,))
_h_decoded = decoder_hidden(decoder_input)
_h0_decoded = decoder_hidden0(_h_decoded) ## ADDED layer_1
_h1_decoded = decoder_hidden1(_h0_decoded) ## ADDED layer_1
# _h2_decoded = decoder_hidden2(_h1_decoded) ## ADDED ---
# _h3_decoded = decoder_hidden3(_h2_decoded) ## ADDED --- should replicate decoder arch
#_h4_decoded = decoder_hidden4(_h3_decoded) ## ADDED --- should replicate decoder arch
_x_decoded_mean = decoder_out(_h1_decoded)
decoder = Model(decoder_input, _x_decoded_mean)
# -------------------------------------------------------------
#CUSTOM LOSS
def vae_loss(y_true, y_pred):
""" Calculate loss = reconstruction loss + KL loss for each data in minibatch """
# E[log P(X|z)]
recon = K.sum(K.binary_crossentropy(y_pred, y_true), axis=1)
# recon = K.categorical_crossentropy(y_pred, y_true)
# recon = losses.mean_squared_error(y_pred, y_true)
# D_KL(Q(z|X) || P(z|X)); calculate in closed form as both dist. are Gaussian
kl = 0.5*K.sum(K.exp(log_sigma) + K.square(mu) - 1. - log_sigma, axis=1)
return recon + kl
adam = optimizers.Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=None,
decay=decay_rate)
vae.compile(optimizer='adam', loss=vae_loss)
K.set_value(vae.optimizer.lr, learning_rate)
K.set_value(vae.optimizer.decay, decay_rate)
print(vae.summary())
#TRAIN
vae.fit(x_train_noisy, x_train, shuffle=True, batch_size=batch_size, nb_epoch=num_epochs, verbose=2,
validation_data=(x_test_noisy, x_test))
print('--------learning rate : ', K.eval(vae.optimizer.lr) )
# ----------------------------------------------------------------------------
x_train_encoded = encoder.predict(x_train)
x_train_decoded = decoder.predict(x_train_encoded)
x_test_encoded = encoder.predict(x_test)
x_test_decoded = decoder.predict(x_test_encoded)
np.savetxt(DataDir+'encoded_xtrainP'+str(num_para)+'_'+ fileOut +'.txt', x_train_encoded)
np.savetxt(DataDir+'encoded_xtestP'+str(num_para)+'_'+ fileOut +'.txt', x_test_encoded)
# np.save(DataDir+'para5_'+str(num_train)+'.npy', y_train)
# -------------------- Save model/weights --------------------------
SaveModel = True
if SaveModel:
epochs = np.arange(1, num_epochs+1)
train_loss = vae.history.history['loss']
val_loss = vae.history.history['val_loss']
training_hist = np.vstack([epochs, train_loss, val_loss])
vae.save(ModelDir+'fullAEP'+str(num_para)+'_' + fileOut + '.hdf5')
encoder.save(ModelDir + 'EncoderP'+str(num_para)+'_' + fileOut + '.hdf5')
decoder.save(ModelDir + 'DecoderP'+str(num_para)+'_' + fileOut + '.hdf5')
np.savetxt(ModelDir + 'TrainingHistoryP'+str(num_para)+'_'+fileOut+'.txt', training_hist)
# -------------------- Plotting routines --------------------------
PlotScatter = True
if PlotScatter:
# display a 2D plot of latent space (just 2 dimensions)
plt.figure(figsize=(6, 6))
x_train_encoded = encoder.predict(x_train)
plt.scatter(x_train_encoded[:, 0], x_train_encoded[:, 1], c=y_train, cmap= plt.cm.get_cmap(
'jet', 6) , s = 3, alpha = 0.8 )
plt.colorbar(label = 'redshift')
plt.xlabel('hidden dim (1)')
plt.ylabel('hidden dim (2)')
TestScatter = False
if TestScatter:
x_test_encoded = encoder.predict(x_test)
plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test, cmap=plt.cm.get_cmap(
'copper', 6), s = 2, alpha = 0.5)
plt.colorbar()
plt.title(fileOut)
plt.savefig( PlotsDir + 'Scatter_z'+fileOut+'.png')
PlotSample = True
if PlotSample:
for i in range(10):
plt.figure(91, figsize=(8,6))
# plt.plot(ls, 10**(normFactor*x_train_decoded[i])/10**(normFactor*x_train[i]), 'r-', alpha = 0.8)
# plt.plot(ls, 10**(normFactor*x_test_decoded[i])/10**(normFactor*x_test[i]), 'k-', alpha = 0.8)
plt.plot(x_train_decoded[i], 'r-', markersize = 10, alpha = 0.2)
# plt.plot(x_test_decoded[i], 'k-', markersize = 5, alpha = 0.2)
plt.plot(x_train[i], 'rx--', markersize = 10, alpha = 0.4)
# plt.plot(x_test[i], 'ko-', markersize = 5, alpha = 0.2)
plt.plot(x_train_decoded[i]/x_train[i], 'rx--', markersize = 10, alpha = 0.4)
plt.plot(x_test_decoded[i]/x_test[i], 'ko-', markersize = 5, alpha = 0.2)
# plt.xscale('log')
# plt.yscale('log')
plt.ylabel('reconstructed/real')
plt.title('train(red) and test (black)')
plt.savefig(PlotsDir + 'Ratio_ttP'+str(num_para)+fileOut+'.png')
if (i%2 == 1):
plt.figure(654, figsize=(8,6))
# plt.plot(ls, 10**(normFactor*x_test_decoded[i]), 'r-', alpha = 0.8)
# plt.plot(ls, 10**(normFactor*x_test[i]), 'b--', alpha = 0.8)
plt.plot(( normFactor*x_test_decoded[i] ) + meanFactor, 'rx-', markersize = 10, alpha = 0.4)
plt.plot(( normFactor*x_test[i] ) + meanFactor, 'ko-', markersize = 5, alpha = 0.4)
# plt.xscale('log')
# plt.yscale('log')
plt.title('Testing: reconstructed (red) and real (blue)')
plt.savefig(PlotsDir + 'decoderTestP'+str(num_para)+ fileOut + '.png')
plt.show()
print(fileOut)
print('--------max ratio (train) : ', np.max(x_train_decoded/x_train) )
print('--------max ratio (test) : ', np.max(x_test_decoded/x_test) )
plotLoss = True
if plotLoss:
import matplotlib.pylab as plt
epochs = np.arange(1, num_epochs+1)
train_loss = vae.history.history['loss']
val_loss = vae.history.history['val_loss']
fig, ax = plt.subplots(1,1, sharex= True, figsize = (8,6))
# fig.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace= 0.02)
ax.plot(epochs,train_loss, '-', lw =1.5)
ax.plot(epochs,val_loss, '-', lw = 1.5)
ax.set_ylabel('loss')
ax.set_xlabel('epochs')
# ax[0].set_ylim([0,1])
# ax[0].set_title('Loss')
ax.legend(['train loss','val loss'])
plt.tight_layout()
# plt.savefig(PlotsDir+'Training_loss.png')
PlotModel = False
if PlotModel:
from keras.utils.vis_utils import plot_model
fileOut = PlotsDir + 'ArchitectureFullAE.png'
plot_model(vae, to_file=fileOut, show_shapes=True, show_layer_names=True)
fileOut = PlotsDir + 'ArchitectureEncoder.png'
plot_model(encoder, to_file=fileOut, show_shapes=True, show_layer_names=True)
fileOut = PlotsDir + 'ArchitectureDecoder.png'
plot_model(decoder, to_file=fileOut, show_shapes=True, show_layer_names=True)
plt.show()