-
Notifications
You must be signed in to change notification settings - Fork 2
/
DDP_CNN-TL.py
280 lines (217 loc) · 10.9 KB
/
DDP_CNN-TL.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
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
import tensorflow as tf
tf.random.set_seed(10)
import random
random.seed()
#import matplotlib
import os
#import pickle
import numpy as np
#import pandas as pd
##import tensorflow.compat.v1 as tf
#tf.disable_v2_behavior()
#import xarray as xr
#import seaborn as sns
from keras import layers
from keras.backend.tensorflow_backend import clear_session
from keras.layers import Input, Convolution2D, Convolution1D, MaxPooling2D, Dense, Dropout, \
Flatten, concatenate, Activation, Reshape, \
UpSampling2D,ZeroPadding2D
from keras.layers import Dense
from keras import Sequential
import h5py
import keras
#from pylab import plt
#from matplotlib import cm
from scipy.io import loadmat,savemat
# Memory usage
import psutil
process = psutil.Process(os.getpid())
print('Memory used by the process:')
print(process.memory_info().rss) # in bytes
import gc
## Data set has 100k data points
trainN=1500
testN=300
lead=1;
batch_size = 16
num_epochs = 50
pool_size = 2
drop_prob=0.0
conv_activation='relu'
Nlat=512
Nlon=512
n_channels=2
NT = 1800 # Numer of snapshots per file
numDataset = 1 # number of dataset / 2
print('Start....')
input_normalized=np.zeros([trainN+testN,Nlon, Nlat,n_channels],np.float32)
output_normalized=np.zeros([trainN+testN,Nlon,Nlat,1],np.float32)
def reset_keras():
sess = tf.compat.v1.keras.backend.get_session()
tf.compat.v1.keras.backend.clear_session()
sess.close()
sess = tf.compat.v1.keras.backend.get_session()
try:
del classifier # this is from global space - change this as you need
except:
pass
# use the same config as you used to create the session
config = tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 1
config.gpu_options.visible_device_list = "0"
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))
def build_model(conv_depth, kernel_size, hidden_size, n_hidden_layers, lr):
model = keras.Sequential([
## Convolution with dimensionality reduction (similar to Encoder in an autoencoder)
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation, input_shape=(Nlon//2,Nlat//2,n_channels)),
#layers.MaxPooling2D(pool_size=pool_size),
#Dropout(drop_prob),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
# end "encoder"
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
# dense layers (flattening and reshaping happens automatically)
] + [keras.layers.Dense(hidden_size, activation='sigmoid') for i in range(n_hidden_layers)] +
[
# start "Decoder" (mirror of the encoder above)
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
layers.Convolution2D(1, kernel_size, padding='same', activation=None)
]
)
optimizer= keras.optimizers.adam(lr=lr)
model.compile(loss='mean_squared_error', optimizer = optimizer)
return model
def build_model2(conv_depth, kernel_size, hidden_size, n_hidden_layers, lr):
model = keras.Sequential([
## Convolution with dimensionality reduction (similar to Encoder in an autoencoder)
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation, input_shape=(Nlon,Nlat,n_channels)),
layers.MaxPooling2D(pool_size=pool_size),
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
#Dropout(drop_prob),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
# end "encoder"
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.MaxPooling2D(pool_size=pool_size),
#keras.layers.Flatten()
# dense layers (flattening and reshaping happens automatically)
] + [keras.layers.Dense(hidden_size, activation='sigmoid') for i in range(n_hidden_layers)] +
[
#keras.layers.Reshape((8,8,conv_depth)),
# start "Decoder" (mirror of the encoder above)
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
#Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
#layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
layers.UpSampling2D(size=pool_size),
Convolution2D(conv_depth, kernel_size, padding='same', activation=conv_activation),
layers.Convolution2D(1, kernel_size, padding='same', activation=None)
]
)
#optimizer= keras.optimizers.adam(lr=lr)
optimizer= tf.optimizers.Adam(lr=lr)
model.compile(loss='mean_squared_error', optimizer = optimizer)
return model
params = {'conv_depth': 64, 'hidden_size': 5000,
'kernel_size': 5, 'lr': 0.00001, 'n_hidden_layers': 0}
for i in range(0,numDataset):
Filename = '/oasis/scratch/comet/yg62/temp_project/Re64k/gpu training/TL8k_NX512/Data_for_TL_NX512.mat'
with h5py.File(Filename, 'r') as f:
input_normalized[:,:,:,:]=np.array(f['input'],np.float32).T
output_normalized[:,:,:,0]=np.array(f['output'],np.float32).T
f.close()
index=np.random.permutation(trainN+testN)
input_normalized=input_normalized[index,:,:,:]
output_normalized=output_normalized[index,:,:,:]
print('Finish Initialization')
print(np.shape(input_normalized))
print('Memory taken by input:')
print(input_normalized.nbytes)
print('Memory taken by output:')
print(np.shape(output_normalized))
print(output_normalized.nbytes)
# Reset and free GPU memory
#tf.keras.backend.clear_session()
reset_keras()
model = build_model(**params)
model2 = build_model2(**params)
#if i != 0:
model.load_weights('./weights_cnn_KT') # load model weight from last time
# Load the pre-trained model and fix the weights
for layer in range(1,8):
print(layer)
print(model.layers[layer])
print(model2.layers[layer+1])
extracted_weights = model.layers[layer].get_weights()
model2.layers[layer+1].set_weights(extracted_weights)
model2.load_weights('./weights_cnn_KT_ext') # load model weight from last time
for layer in model2.layers[2:-3]:
layer.trainable = False
optimizer= tf.optimizers.Adam(lr=0.00001)
model.compile(loss='mean_squared_error', optimizer = optimizer)
print(model2.summary())
hist = model2.fit(input_normalized[0:trainN,:,:,:], output_normalized[0:trainN,:,:,:],
batch_size = batch_size,shuffle='True',
verbose=1,
epochs = num_epochs,
validation_data=(input_normalized[trainN:,:,:,:],output_normalized[trainN:,:,:,:]))
model2.save_weights('./weights_cnn_KT_ext')
#loss = hist.history['loss']
#val_loss = hist.history['val_loss']
#savemat('loss' + str(i) + '.mat' ,dict([('trainLoss',loss),('valLoss',val_loss)]))
#del input_normalized
#del output_normalized
del hist
#del f
if i != numDataset-1:
del model
gc.collect()
process = psutil.Process(os.getpid())
print('Memory used by the process:')
print(process.memory_info().rss) # in bytes
print('finished training dataset' + str(i+1) + '/' + str(numDataset))
prediction=model2.predict(input_normalized[trainN:,:,:,:])
#print(np.shape(output_normalized[trainN:,:,:,:]))
#input_normalized[trainN:trainN+100,:,:,:]),
savemat('prediction_KT.mat',dict([('test',output_normalized[trainN:trainN+100,:,:,:]),('input',input_normalized[trainN:trainN+100,:,:,:]),('prediction',prediction[0:100,:,:])]))
#savemat('Normalization_parameters.mat',dict([('SDEV_S',SDEV_S),('SDEV_W',SDEV_W),('SDEV_O',SDEV_O)]))