-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_cnn.py
268 lines (193 loc) · 8.7 KB
/
main_cnn.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
#%%
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import h5py
import random
import tensorflow as tf
from tensorflow.keras.callbacks import *
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import regularizers
from tensorflow.keras import optimizers
from tensorflow.keras import initializers
from tensorflow.keras.layers import *
from sklearn.model_selection import train_test_split
import datetime
import sys
network_choice = sys.argv[1]
model_chosen = sys.argv[2]
random_state_here = int(sys.argv[3])
def print_time():
parser = datetime.datetime.now()
return parser.strftime("%d-%m-%Y %H:%M:%S")
def normalize(inputs):
normalized = []
for eq in inputs:
maks = np.max(np.abs(eq))
if maks != 0:
normalized.append(eq/maks)
else:
normalized.append(eq)
return np.array(normalized)
def targets_to_list(targets):
targets = targets.transpose(2,0,1)
targetList = []
for i in range(0, len(targets)):
targetList.append(targets[i,:,:])
return targetList
seed = 1
import tensorflow
def k_fold_split(inputs, targets):
# make sure everything is seeded
import os
os.environ['PYTHONHASHSEED']=str(seed)
import random
random.seed(seed)
np.random.seed(seed)
np.random.permutation(seed)
tensorflow.random.set_seed(seed)
p = np.random.permutation(len(targets))
print('min of p = ',np.array(p)[50:100].min())
print('max of p = ',np.array(p)[50:100].max())
print('mean of p = ',np.array(p)[50:100].mean())
inputs = inputs[p]
targets = targets[p]
ind = int(len(inputs)/5)
inputsK = []
targetsK = []
for i in range(0,5-1):
inputsK.append(inputs[i*ind:(i+1)*ind])
targetsK.append(targets[i*ind:(i+1)*ind])
inputsK.append(inputs[(i+1)*ind:])
targetsK.append(targets[(i+1)*ind:])
return inputsK, targetsK
def merge_splits(inputs, targets, k):
if k != 0:
z=0
inputsTrain = inputs[z]
targetsTrain = targets[z]
else:
z=1
inputsTrain = inputs[z]
targetsTrain = targets[z]
for i in range(z+1, 5):
if i != k:
inputsTrain = np.concatenate((inputsTrain, inputs[i]))
targetsTrain = np.concatenate((targetsTrain, targets[i]))
return inputsTrain, targetsTrain, inputs[k], targets[k]
def build_model(input_shape):
reg_const = 0.0001
activation_func = 'relu'
wav_input = layers.Input(shape=input_shape, name='wav_input')
conv1 = layers.Conv2D(32, (1, 125), strides=(1, 2), activation=activation_func, kernel_regularizer=regularizers.l2(reg_const))(wav_input)
conv1 = layers.Conv2D(64, (1, 125), strides=(1, 2), activation=activation_func, kernel_regularizer=regularizers.l2(reg_const))(conv1)
conv1 = layers.Conv2D(64, (39, 5), strides=(39, 5), activation=activation_func, padding = 'same', kernel_regularizer=regularizers.l2(reg_const))(conv1)
conv1 = layers.Flatten()(conv1)
conv1 = layers.Dropout(0.4, seed=seed)(conv1)
graph_features = layers.Input(shape=(39,2), name='graph_features')
graph_features_flattened = layers.Flatten()(graph_features)
if model_chosen == 'nofeatures':
merged = layers.Dense(128)(conv1)
if model_chosen == 'main':
merged = layers.concatenate(inputs=[conv1, graph_features_flattened])
merged = layers.Dense(128)(merged)
pga = layers.Dense(39)(merged)
pgv = layers.Dense(39)(merged)
sa03 = layers.Dense(39)(merged)
sa10 = layers.Dense(39)(merged)
sa30 = layers.Dense(39)(merged)
final_model = models.Model(inputs=[wav_input, graph_features], outputs=[pga, pgv, sa03, sa10, sa30]) #, pgv, sa03, sa10, sa30
# final_model = models.Model(inputs=[wav_input,graph_features], outputs=[pga, pgv, sa03, sa10, sa30]) #, pgv, sa03, sa10, sa30
rmsprop = optimizers.RMSprop(learning_rate=0.0001, rho=0.9, epsilon=None, decay=0.)
# final_model.compile(optimizer=rmsprop, loss='mse', metrics=['mse'])
final_model.compile(optimizer=rmsprop, loss='mse')#, metrics=['mse'])
return final_model
from tensorflow import keras
es = keras.callbacks.EarlyStopping(patience=10, verbose=0, min_delta=0.001, monitor='val_loss', mode='min',baseline=None, restore_best_weights=True)
import tensorflow as tf
import sys
#%%
if network_choice == 'network1':
test_set_size = 0.2
inputs = np.load('data/inputs_ci.npy', allow_pickle = True)
targets = np.load('data/targets.npy', allow_pickle = True)
graph_features = np.load('data/station_coords.npy', allow_pickle=True)
graph_features = np.array([graph_features] * inputs.shape[0])
if network_choice == 'network2':
test_set_size = 0.2
inputs = np.load('data/othernetwork/inputs_cw.npy', allow_pickle = True)
targets = np.load('data/othernetwork/targets.npy', allow_pickle = True)
graph_features = np.load('data/othernetwork/station_coords.npy', allow_pickle=True)
graph_features = np.array([graph_features] * inputs.shape[0])
import random
train_inputs, test_inputs, train_graphfeature, test_graphfeature, train_targets, testTargets = train_test_split(inputs, graph_features, targets, test_size=test_set_size, random_state=random_state_here)
testInputs = normalize(test_inputs[:, :, :1000, :])
import math
inputsK, targetsK = k_fold_split(train_inputs, train_targets)
mse_list = []
rmse_list = []
mae_list = []
for k in range(0,5):
keras.backend.clear_session()
tf.keras.backend.clear_session()
trainInputsAll, trainTargets, valInputsAll, valTargets = merge_splits(inputsK, targetsK, k)
trainInputs = normalize(trainInputsAll[:, :, :1000, :]) # 100 samples per second
valInputs = normalize(valInputsAll[:, :, :1000, :])
train_graphfeatureinput = train_graphfeature[0:trainInputsAll.shape[0],:,:]
val_graphfeatureinput = train_graphfeature[0:valInputsAll.shape[0],:,:]
model = build_model(valInputs[0].shape)
iteration_checkpoint = keras.callbacks.ModelCheckpoint(
f'models/cnn_model_{network_choice}_iteration_{k}.h5',
monitor='val_loss',
verbose=0,
save_best_only=True
)
print(model.summary())
history = model.fit(x=[trainInputs, train_graphfeatureinput],
y=targets_to_list(trainTargets),
epochs=100, batch_size=20,
validation_data=([valInputs,val_graphfeatureinput], targets_to_list(valTargets)),verbose=0, callbacks=[es,iteration_checkpoint])#
print()
print('total number of epochs ran = ',len(history.history['loss']))
print('Fold number:' + str(k))
predictions = model.predict([testInputs,test_graphfeature])
new_predictions = np.array(predictions)
new_predictions = np.swapaxes(new_predictions,0,2)
new_predictions = np.swapaxes(new_predictions,0,1)
from sklearn.metrics import mean_squared_error, mean_absolute_error
MSE = []
for i in range(0,5):
MSE.append(mean_squared_error(testTargets[:,:,i], new_predictions[:,:,i]))
print('mse = ',np.array(MSE).mean())
MSE = np.array(MSE).mean()
RMSE = []
for i in range(0,5):
RMSE.append(mean_squared_error(testTargets[:,:,i], new_predictions[:,:,i], squared=False))
print('rmse = ',np.array(RMSE).mean())
RMSE = np.array(RMSE).mean()
MAE = []
for i in range(0,5):
MAE.append(mean_absolute_error(testTargets[:,:,i], new_predictions[:,:,i]))
print('MAE = ',np.array(MAE).mean())
MAE = np.array(MAE).mean()
mse_list.append(MSE)
rmse_list.append(RMSE)
mae_list.append(MAE)
keras.backend.clear_session()
tf.keras.backend.clear_session()
print('-')
print('-')
print('-')
print('-')
print('all averages = ')
print('mse score = ',np.array(mse_list).mean())
print('rmse score = ',np.array(rmse_list).mean())
print('mae score = ',np.array(mae_list).mean())
with open("githubresults.csv", "a") as text_file:
print(f'{print_time()},{sys.argv[0]},PGV,{network_choice},{model_chosen},{mse_list[0]},{rmse_list[0]},{mae_list[0]},{random_state_here}', file=text_file)
print(f'{print_time()},{sys.argv[0]},PGA,{network_choice},{model_chosen},{mse_list[1]},{rmse_list[1]},{mae_list[1]},{random_state_here}', file=text_file)
print(f'{print_time()},{sys.argv[0]},PSA03,{network_choice},{model_chosen},{mse_list[2]},{rmse_list[2]},{mae_list[2]},{random_state_here}', file=text_file)
print(f'{print_time()},{sys.argv[0]},PSA1,{network_choice},{model_chosen},{mse_list[3]},{rmse_list[3]},{mae_list[3]},{random_state_here}', file=text_file)
print(f'{print_time()},{sys.argv[0]},PSA3,{network_choice},{model_chosen},{mse_list[4]},{rmse_list[4]},{mae_list[4]},{random_state_here}', file=text_file)