-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEQCCT_P_Test.py
1136 lines (857 loc) · 36.9 KB
/
EQCCT_P_Test.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
from __future__ import division
import tensorflow.compat.v1 as tf1
tf1.disable_v2_behavior()
import os
os.environ['KERAS_BACKEND']='tensorflow'
from tensorflow.keras import backend as K
from tensorflow.keras.models import load_model
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
import matplotlib
import tensorflow
matplotlib.use('agg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import math
import csv
from tensorflow import keras
import time
from os import listdir
import platform
import shutil
from tqdm import tqdm
from datetime import datetime, timedelta
import contextlib
import sys
import warnings
from scipy import signal
from matplotlib.lines import Line2D
from obspy import read
from os.path import join
import json
import pickle
import faulthandler; faulthandler.enable()
import obspy
import logging
from obspy.signal.trigger import trigger_onset
from tensorflow.keras.layers import Activation, Add, Bidirectional, Conv1D, Dense, Dropout, Embedding, Flatten, Reshape, multiply
from tensorflow.keras.layers import concatenate, GRU, Input, LSTM, MaxPooling1D
from tensorflow.keras.layers import GlobalAveragePooling1D, GlobalMaxPooling1D, SpatialDropout1D, Input
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing import text, sequence
from sklearn.metrics import accuracy_score, roc_auc_score, log_loss
from sklearn.model_selection import train_test_split
from tensorflow.keras import initializers, regularizers, constraints, optimizers, layers, callbacks
from tensorflow.keras import backend as K
from tensorflow.keras.models import Model
import numpy as np
from matplotlib import pyplot as plt
from tensorflow.keras import backend as K
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense, Add, Conv2DTranspose
from tensorflow.keras.layers import Conv2D, SeparableConv1D
from tensorflow.keras.layers import Lambda
from tensorflow.keras.layers import Flatten, UpSampling1D
from tensorflow.keras.layers import Reshape
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Conv2DTranspose
from tensorflow.keras import layers, models, optimizers
import os
import sys
import random
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, Callback, ModelCheckpoint, LearningRateScheduler
from tensorflow.keras.layers import GlobalAveragePooling2D, Reshape, Dense, Permute, multiply, GlobalAveragePooling1D
import tensorflow.keras.backend as K
import numpy as np
from tensorflow.keras.layers import Conv2DTranspose, Bidirectional, GRU, LSTM, Input,Dense, SpatialDropout1D, Conv2D, MaxPooling2D, Flatten, Input, UpSampling2D, Dropout,Lambda, Average, concatenate, Activation, Add
import numpy as np
from tensorflow.keras.layers import Input,Dense, Add, UpSampling1D, Conv1D, Conv2D, MaxPooling1D, MaxPooling2D, Flatten, Input, UpSampling2D, Dropout,Lambda, Average, concatenate, Activation
from tensorflow.keras import optimizers, Model
import matplotlib.pyplot as plt
import tensorflow.keras
from tensorflow.keras import backend as K
from sklearn.utils import class_weight
from numpy.random import seed
import math
import h5py
from tensorflow.keras.regularizers import l2
warnings.filterwarnings("ignore")
from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
def picker(args, yh3, yh3_std, spt=None):
"""
Performs detection and picking.
Parameters
----------
args : dic
A dictionary containing all of the input parameters.
yh3 : 1D array
P arrival probabilities.
yh3_std : 1D array
P arrival standard deviations.
spt : {int, None}, default=None
P arrival time in sample.
"""
P_PICKall=[]
Ppickall=[]
Pproball = []
perrorall=[]
sP_arr = _detect_peaks(yh3, mph=args['P_threshold'], mpd=1)
P_PICKS = []
pick_errors = []
if len(sP_arr) > 0:
P_uncertainty = None
for pick in range(len(sP_arr)):
sauto = sP_arr[pick]
if args['estimate_uncertainty'] and sauto:
P_uncertainty = np.round(yh3_std[int(sauto)], 3)
if sauto:
P_prob = np.round(yh3[int(sauto)], 3)
P_PICKS.append([sauto,P_prob, P_uncertainty])
so=[]
si=[]
P_PICKS = np.array(P_PICKS)
P_PICKall.append(P_PICKS)
for ij in P_PICKS:
so.append(ij[1])
si.append(ij[0])
try:
so = np.array(so)
inds = np.argmax(so)
swave = si[inds]
perrorall.append(int(spt- swave))
Ppickall.append(int(swave))
Pproball.append(int(np.max(so)))
except:
perrorall.append(None)
Ppickall.append(None)
Pproball.append(None)
#Ppickall = np.array(Ppickall)
#perrorall = np.array(perrorall)
#Pproball = np.array(Pproball)
return Ppickall, perrorall, Pproball
def _detect_peaks(x, mph=None, mpd=1, threshold=0, edge='rising', kpsh=False, valley=False):
"""
Detect peaks in data based on their amplitude and other features.
Parameters
----------
x : 1D array_like
data.
mph : {None, number}, default=None
detect peaks that are greater than minimum peak height.
mpd : int, default=1
detect peaks that are at least separated by minimum peak distance (in number of data).
threshold : int, default=0
detect peaks (valleys) that are greater (smaller) than `threshold in relation to their immediate neighbors.
edge : str, default=rising
for a flat peak, keep only the rising edge ('rising'), only the falling edge ('falling'), both edges ('both'), or don't detect a flat peak (None).
kpsh : bool, default=False
keep peaks with same height even if they are closer than `mpd`.
valley : bool, default=False
if True (1), detect valleys (local minima) instead of peaks.
Returns
---------
ind : 1D array_like
indeces of the peaks in `x`.
Modified from
----------------
.. [1] http://nbviewer.ipython.org/github/demotu/BMC/blob/master/notebooks/DetectPeaks.ipynb
"""
x = np.atleast_1d(x).astype('float64')
if x.size < 3:
return np.array([], dtype=int)
if valley:
x = -x
# find indices of all peaks
dx = x[1:] - x[:-1]
# handle NaN's
indnan = np.where(np.isnan(x))[0]
if indnan.size:
x[indnan] = np.inf
dx[np.where(np.isnan(dx))[0]] = np.inf
ine, ire, ife = np.array([[], [], []], dtype=int)
if not edge:
ine = np.where((np.hstack((dx, 0)) < 0) & (np.hstack((0, dx)) > 0))[0]
else:
if edge.lower() in ['rising', 'both']:
ire = np.where((np.hstack((dx, 0)) <= 0) & (np.hstack((0, dx)) > 0))[0]
if edge.lower() in ['falling', 'both']:
ife = np.where((np.hstack((dx, 0)) < 0) & (np.hstack((0, dx)) >= 0))[0]
ind = np.unique(np.hstack((ine, ire, ife)))
# handle NaN's
if ind.size and indnan.size:
# NaN's and values close to NaN's cannot be peaks
ind = ind[np.in1d(ind, np.unique(np.hstack((indnan, indnan-1, indnan+1))), invert=True)]
# first and last values of x cannot be peaks
if ind.size and ind[0] == 0:
ind = ind[1:]
if ind.size and ind[-1] == x.size-1:
ind = ind[:-1]
# remove peaks < minimum peak height
if ind.size and mph is not None:
ind = ind[x[ind] >= mph]
# remove peaks - neighbors < threshold
if ind.size and threshold > 0:
dx = np.min(np.vstack([x[ind]-x[ind-1], x[ind]-x[ind+1]]), axis=0)
ind = np.delete(ind, np.where(dx < threshold)[0])
# detect small peaks closer than minimum peak distance
if ind.size and mpd > 1:
ind = ind[np.argsort(x[ind])][::-1] # sort ind by peak height
idel = np.zeros(ind.size, dtype=bool)
for i in range(ind.size):
if not idel[i]:
# keep peaks with the same height if kpsh is True
idel = idel | (ind >= ind[i] - mpd) & (ind <= ind[i] + mpd) \
& (x[ind[i]] > x[ind] if kpsh else True)
idel[i] = 0 # Keep current peak
# remove the small peaks and sort back the indices by their occurrence
ind = np.sort(ind[~idel])
return ind
def generate_arrays_from_file(file_list, step):
"""
Make a generator to generate list of trace names.
Parameters
----------
file_list : str
A list of trace names.
step : int
Batch size.
Returns
--------
chunck : str
A batch of trace names.
"""
n_loops = int(np.ceil(len(file_list) / step))
b = 0
while True:
for i in range(n_loops):
e = i*step + step
if e > len(file_list):
e = len(file_list)
chunck = file_list[b:e]
b=e
yield chunck
class DataGeneratorTest(keras.utils.Sequence):
"""
Keras generator with preprocessing. For testing.
Parameters
----------
list_IDsx: str
List of trace names.
file_name: str
Path to the input hdf5 file.
dim: tuple
Dimension of input traces.
batch_size: int, default=32
Batch size.
n_channels: int, default=3
Number of channels.
norm_mode: str, default=max
The mode of normalization, 'max' or 'std'.
Returns
--------
Batches of two dictionaries: {'input': X}: pre-processed waveform as input {'detector': y1, 'picker_P': y2, 'picker_S': y3}: outputs including three separate numpy arrays as labels for detection, P, and S respectively.
"""
def __init__(self,
list_IDs,
file_name,
dim,
batch_size=32,
n_channels=3,
norm_mode = 'max'):
'Initialization'
self.dim = dim
self.batch_size = batch_size
self.list_IDs = list_IDs
self.file_name = file_name
self.n_channels = n_channels
self.on_epoch_end()
self.norm_mode = norm_mode
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_temp = [self.list_IDs[k] for k in indexes]
X = self.__data_generation(list_IDs_temp)
return ({'input': X})
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
def normalize(self, data, mode = 'max'):
'Normalize waveforms in each batch'
data -= np.mean(data, axis=0, keepdims=True)
if mode == 'max':
max_data = np.max(data, axis=0, keepdims=True)
assert(max_data.shape[-1] == data.shape[-1])
max_data[max_data == 0] = 1
data /= max_data
elif mode == 'std':
std_data = np.std(data, axis=0, keepdims=True)
assert(std_data.shape[-1] == data.shape[-1])
std_data[std_data == 0] = 1
data /= std_data
return data
def __data_generation(self, list_IDs_temp):
'readint the waveforms'
X = np.zeros((self.batch_size, self.dim, self.n_channels))
fl = h5py.File(self.file_name, 'r')
# Generate data
for i, ID in enumerate(list_IDs_temp):
dataset = fl.get(str(ID))
data = np.array(dataset['data'])
if self.norm_mode:
data = self.normalize(data, self.norm_mode)
X[i, :, :] = data
fl.close()
return X
def recall(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def f1(y_true, y_pred):
precisionx = precision(y_true, y_pred)
recallx = recall(y_true, y_pred)
return 2*((precisionx*recallx)/(precisionx+recallx+K.epsilon()))
import tensorflow as tf
def wbceEdit( y_true, y_pred) :
ms = K.mean(K.square(y_true-y_pred))
ssim = 1-tf.reduce_mean(tf.image.ssim(y_true,y_pred,1.0))
return (ssim + ms)
w1 = 6000
w2 = 3
drop_rate = 0.2
stochastic_depth_rate = 0.1
positional_emb = False
conv_layers = 4
num_classes = 1
input_shape = (w1, w2)
num_classes = 1
input_shape = (6000, 3)
image_size = 6000
patch_size = 40 # Size of the patches to be extract from the input images
num_patches = (image_size // patch_size)
projection_dim = 40
num_heads = 4
transformer_units = [
projection_dim,
projection_dim,
] # Size of the transformer layers
transformer_layers = 4
class Patches(layers.Layer):
def __init__(self, patch_size, **kwargs):
super(Patches, self).__init__()
self.patch_size = patch_size
def get_config(self):
config = super().get_config().copy()
config.update({
'patch_size' : self.patch_size,
})
return config
def call(self, images):
batch_size = tf.shape(images)[0]
patches = tf.image.extract_patches(
images=images,
sizes=[1, self.patch_size, 1, 1],
strides=[1, self.patch_size, 1, 1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patch_dims = patches.shape[-1]
patches = tf.reshape(patches, [batch_size, -1, patch_dims])
return patches
class PatchEncoder(layers.Layer):
def __init__(self, num_patches, projection_dim, **kwargs):
super(PatchEncoder, self).__init__()
self.num_patches = num_patches
self.projection = layers.Dense(units=projection_dim)
self.position_embedding = layers.Embedding(
input_dim=num_patches, output_dim=projection_dim
)
def get_config(self):
config = super().get_config().copy()
config.update({
'num_patches' : self.num_patches,
'projection_dim' : projection_dim,
})
return config
def call(self, patch):
positions = tf.range(start=0, limit=self.num_patches, delta=1)
encoded = self.projection(patch) + self.position_embedding(positions)
#print(patch,positions)
#temp = self.position_embedding(positions)
#temp = tf.reshape(temp,(1,int(temp.shape[0]),int(temp.shape[1])))
#encoded = layers.Add()([self.projection(patch), temp])
#print(temp,encoded)
return encoded
# Referred from: github.com:rwightman/pytorch-image-models.
class StochasticDepth(layers.Layer):
def __init__(self, drop_prop, **kwargs):
super(StochasticDepth, self).__init__(**kwargs)
self.drop_prob = drop_prop
def call(self, x, training=None):
if training:
keep_prob = 1 - self.drop_prob
shape = (tf.shape(x)[0],) + (1,) * (len(tf.shape(x)) - 1)
random_tensor = keep_prob + tf.random.uniform(shape, 0, 1)
random_tensor = tf.floor(random_tensor)
return (x / keep_prob) * random_tensor
return x
def convF1(inpt, D1, fil_ord, Dr):
'''
encode = BatchNormalization()(inpt)
encode = Activation(tf.nn.gelu')(encode)
encode = SpatialDropout1D(Dr)(encode, training=True)
encode = Conv1D(D1, fil_ord, strides =(1), padding='same')(encode)
encode = Conv1D(D1, fil_ord, strides =(1), padding='same')(inpt)
encode = BatchNormalization()(encode)
encode = Activation(tf.nn.gelu')(encode)
encode = Dropout(Dr)(encode)
'''
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
#filters = inpt._keras_shape[channel_axis]
filters = int(inpt.shape[-1])
#infx = Activation(tf.nn.gelu')(inpt)
pre = Conv1D(filters, fil_ord, strides =(1), padding='same',kernel_initializer='he_normal')(inpt)
pre = BatchNormalization()(pre)
pre = Activation(tf.nn.gelu)(pre)
#shared_conv = Conv1D(D1, fil_ord, strides =(1), padding='same')
inf = Conv1D(filters, fil_ord, strides =(1), padding='same',kernel_initializer='he_normal')(pre)
inf = BatchNormalization()(inf)
inf = Activation(tf.nn.gelu)(inf)
inf = Add()([inf,inpt])
inf1 = Conv1D(D1, fil_ord, strides =(1), padding='same',kernel_initializer='he_normal')(inf)
inf1 = BatchNormalization()(inf1)
inf1 = Activation(tf.nn.gelu)(inf1)
encode = Dropout(Dr)(inf1)
return encode
def mlp(x, hidden_units, dropout_rate):
for units in hidden_units:
x = layers.Dense(units, activation=tf.nn.gelu)(x)
#x = layers.Dense(units, activation='relu')(x)
x = layers.Dropout(dropout_rate)(x)
return x
class Patches(layers.Layer):
def __init__(self, patch_size, **kwargs):
super(Patches, self).__init__()
self.patch_size = patch_size
def get_config(self):
config = super().get_config().copy()
config.update({
'patch_size' : self.patch_size,
})
return config
def call(self, images):
batch_size = tf.shape(images)[0]
patches = tf.image.extract_patches(
images=images,
sizes=[1, self.patch_size, 1, 1],
strides=[1, self.patch_size, 1, 1],
rates=[1, 1, 1, 1],
padding="VALID",
)
patch_dims = patches.shape[-1]
patches = tf.reshape(patches, [batch_size, -1, patch_dims])
return patches
class PatchEncoder(layers.Layer):
def __init__(self, num_patches, projection_dim, **kwargs):
super(PatchEncoder, self).__init__()
self.num_patches = num_patches
self.projection = layers.Dense(units=projection_dim)
self.position_embedding = layers.Embedding(
input_dim=num_patches, output_dim=projection_dim
)
def get_config(self):
config = super().get_config().copy()
config.update({
'num_patches' : self.num_patches,
'projection_dim' : projection_dim,
})
return config
def call(self, patch):
positions = tf.range(start=0, limit=self.num_patches, delta=1)
encoded = self.projection(patch) + self.position_embedding(positions)
#print(patch,positions)
#temp = self.position_embedding(positions)
#temp = tf.reshape(temp,(1,int(temp.shape[0]),int(temp.shape[1])))
#encoded = layers.Add()([self.projection(patch), temp])
#print(temp,encoded)
return encoded
def create_cct_modelP(inputs):
inputs1 = convF1(inputs, 10, 11, 0.1)
inputs1 = convF1(inputs1, 20, 11, 0.1)
inputs1 = convF1(inputs1, 40, 11, 0.1)
inputreshaped = layers.Reshape((6000,1,40))(inputs1)
# Augment data.
#augmented = data_augmentation(inputs)
# Create patches.
patches = Patches(patch_size)(inputreshaped)
# Encode patches.
encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)
#print('done')
# Calculate Stochastic Depth probabilities.
dpr = [x for x in np.linspace(0, stochastic_depth_rate, transformer_layers)]
# Create multiple layers of the Transformer block.
for i in range(transformer_layers):
#encoded_patches = convF1(encoded_patches, 40,11, 0.1)
# Layer normalization 1.
x1 = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
# Create a multi-head attention layer.
attention_output = layers.MultiHeadAttention(
num_heads=num_heads, key_dim=projection_dim, dropout=0.1
)(x1, x1)
#attention_output = convF1(attention_output, 40,11, 0.1)
# Skip connection 1.
attention_output = StochasticDepth(dpr[i])(attention_output)
x2 = layers.Add()([attention_output, encoded_patches])
# Layer normalization 2.
x3 = layers.LayerNormalization(epsilon=1e-6)(x2)
# MLP.
x3 = mlp(x3, hidden_units=transformer_units, dropout_rate=0.1)
# Skip connection 2.
x3 = StochasticDepth(dpr[i])(x3)
encoded_patches = layers.Add()([x3, x2])
# Apply sequence pooling.
representation = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
#print(representation)
'''
attention_weights = tf.nn.softmax(layers.Dense(1)(representation), axis=1)
weighted_representation = tf.matmul(
attention_weights, representation, transpose_a=True
)
weighted_representation = tf.squeeze(weighted_representation, -2)
return weighted_representation
'''
return representation
def create_cct_modelS(inputs):
inputs1 = convF1(inputs, 10, 11, 0.1)
inputs1 = convF1(inputs1, 20, 11, 0.1)
inputs1 = convF1(inputs1, 40, 11, 0.1)
inputreshaped = layers.Reshape((6000,1,40))(inputs1)
# Augment data.
#augmented = data_augmentation(inputs)
# Create patches.
patches = Patches(patch_size)(inputreshaped)
# Encode patches.
encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)
#print('done')
# Calculate Stochastic Depth probabilities.
dpr = [x for x in np.linspace(0, stochastic_depth_rate, transformer_layers)]
# Create multiple layers of the Transformer block.
for i in range(transformer_layers):
encoded_patches = convF1(encoded_patches, 40,11, 0.1)
# Layer normalization 1.
x1 = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
# Create a multi-head attention layer.
attention_output = layers.MultiHeadAttention(
num_heads=num_heads, key_dim=projection_dim, dropout=0.1
)(x1, x1)
attention_output = convF1(attention_output, 40,11, 0.1)
# Skip connection 1.
attention_output = StochasticDepth(dpr[i])(attention_output)
x2 = layers.Add()([attention_output, encoded_patches])
# Layer normalization 2.
x3 = layers.LayerNormalization(epsilon=1e-6)(x2)
# MLP.
x3 = mlp(x3, hidden_units=transformer_units, dropout_rate=0.1)
# Skip connection 2.
x3 = StochasticDepth(dpr[i])(x3)
encoded_patches = layers.Add()([x3, x2])
# Apply sequence pooling.
representation = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)
#print(representation)
'''
attention_weights = tf.nn.softmax(layers.Dense(1)(representation), axis=1)
weighted_representation = tf.matmul(
attention_weights, representation, transpose_a=True
)
weighted_representation = tf.squeeze(weighted_representation, -2)
return weighted_representation
'''
return representation
def tester1(input_hdf5=None,
input_testset=None,
input_model=None,
output_name=None,
detection_threshold=0.20,
P_threshold=0.1,
S_threshold=0.1,
number_of_plots=100,
estimate_uncertainty=True,
number_of_sampling=5,
loss_weights=[0.05, 0.40, 0.55],
loss_types=[wbceEdit,wbceEdit,wbceEdit],
input_dimention=(6000, 3),
normalization_mode='std',
mode='generator',
batch_size=500,
gpuid=None,
gpu_limit=None):
"""
Applies a trained model to a windowed waveform to perform both detection and picking at the same time.
Parameters
----------
input_hdf5: str, default=None
Path to an hdf5 file containing only one class of "data" with NumPy arrays containing 3 component waveforms each 1 min long.
input_testset: npy, default=None
Path to a NumPy file (automaticaly generated by the trainer) containing a list of trace names.
input_model: str, default=None
Path to a trained model.
output_dir: str, default=None
Output directory that will be generated.
output_probabilities: bool, default=False
If True, it will output probabilities and estimated uncertainties for each trace into an HDF file.
P_threshold: float, default=0.1
A value which the P probabilities above it will be considered as P arrival.
number_of_plots: float, default=10
The number of plots for detected events outputed for each station data.
estimate_uncertainty: bool, default=False
If True uncertainties in the output probabilities will be estimated.
number_of_sampling: int, default=5
Number of sampling for the uncertainty estimation.
input_dimention: tuple, default=(6000, 3)
Loss types for P picking.
normalization_mode: str, default='std'
Mode of normalization for data preprocessing, 'max', maximum amplitude among three components, 'std', standard deviation.
mode: str, default='generator'
Mode of running. 'pre_load_generator' or 'generator'.
batch_size: int, default=500
Batch size. This wont affect the speed much but can affect the performance.
gpuid: int, default=None
Id of GPU used for the prediction. If using CPU set to None.
gpu_limit: int, default=None
Set the maximum percentage of memory usage for the GPU.
"""
args = {
"input_hdf5": input_hdf5,
"input_testset": input_testset,
"input_model": input_model,
"output_name": output_name,
"detection_threshold": detection_threshold,
"P_threshold": P_threshold,
"number_of_plots": number_of_plots,
"estimate_uncertainty": estimate_uncertainty,
"number_of_sampling": number_of_sampling,
"input_dimention": input_dimention,
"normalization_mode": normalization_mode,
"mode": mode,
"batch_size": batch_size,
"gpuid": gpuid,
"gpu_limit": gpu_limit
}
if args['gpuid']:
os.environ['CUDA_VISIBLE_DEVICES'] = '{}'.format(args['gpuid'])
tf.Session(config=tf.ConfigProto(log_device_placement=True))
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = float(args['gpu_limit'])
K.tensorflow_backend.set_session(tf.Session(config=config))
save_dir = os.path.join(os.getcwd(), str(args['output_name'])+'_outputs')
save_figs = os.path.join(save_dir, 'figures')
if os.path.isdir(save_dir):
shutil.rmtree(save_dir)
os.makedirs(save_figs)
test = np.load(args['input_testset'])
print('Loading the model ...', flush=True)
# Model CCT
inputs = layers.Input(shape=input_shape,name='input')
featuresP = create_cct_modelP(inputs)
featuresP = Reshape((6000,1))(featuresP)
logitp = Conv1D(1, 15, strides =(1), padding='same',activation='sigmoid', kernel_initializer='he_normal',name='picker_P')(featuresP)
modelP = Model(inputs=[inputs], outputs=[logitp])
model = Model(inputs=[inputs], outputs=[logitp])
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc',f1,precision, recall])
model.load_weights(args['input_model'])
'''
model = load_model(args['input_model'], custom_objects={'f1': f1, 'precision': precision,
'recall':recall,
'ConvCapsuleLayer':ConvCapsuleLayer},compile=False)
model.compile(loss = args['loss_types'],
loss_weights = args['loss_weights'],
optimizer = Adam(lr = 0.001),
metrics = [f1])
'''
print('Loading is complete!', flush=True)
print('Testing ...', flush=True)
print('Writting results into: " ' + str(args['output_name'])+'_outputs'+' "', flush=True)
start_training = time.time()
csvTst = open(os.path.join(save_dir,'X_test_results.csv'), 'w')
test_writer = csv.writer(csvTst, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
test_writer.writerow([
'p_arrival_sample',
'P_pick',
'P_probability',
'P_error'
])
csvTst.flush()
plt_n = 0
list_generator = generate_arrays_from_file(test, args['batch_size'])
pred_PP_mean_all=[]
pred_PP_std_all=[]
sptall = []
pbar_test = tqdm(total= int(np.ceil(len(test)/args['batch_size'])))
for _ in range(int(np.ceil(len(test) / args['batch_size']))):
pbar_test.update()
new_list = next(list_generator)
if args['mode'].lower() == 'pre_load_generator':
params_test = {'dim': args['input_dimention'][0],
'batch_size': len(new_list),
'n_channels': args['input_dimention'][-1],
'norm_mode': args['normalization_mode']}
test_set={}
else:
params_test = {'file_name': str(args['input_hdf5']),
'dim': args['input_dimention'][0],
'batch_size': len(new_list),
'n_channels': args['input_dimention'][-1],
'norm_mode': args['normalization_mode']}
test_generator = DataGeneratorTest(new_list, **params_test)
if args['estimate_uncertainty']:
pred_PP = []
for mc in range(args['number_of_sampling']):
predP = model.predict_generator(generator=test_generator)
pred_PP.append(predP)
pred_PP = np.array(pred_PP).reshape(args['number_of_sampling'], len(new_list), params_test['dim'])
pred_PP_mean = pred_PP.mean(axis=0)
pred_PP_std = pred_PP.std(axis=0)
else:
pred_PP_mean = model.predict_generator(generator=test_generator)
pred_PP_mean = pred_PP_mean.reshape(pred_PP_mean.shape[0], pred_PP_mean.shape[1])
pred_PP_std = np.zeros((pred_PP_mean.shape))
test_set={}
fl = h5py.File(args['input_hdf5'], 'r')
for ID in new_list:
if ID.split('_')[-1] == 'EV':
dataset = fl.get(str(ID))
elif ID.split('_')[-1] == 'NO':
dataset = fl.get(str(ID))
test_set.update( {str(ID) : dataset})
for ts in range(pred_PP_mean.shape[0]):
evi = new_list[ts]
dataset = test_set[evi]
try:
spt = int(dataset.attrs['p_arrival_sample']);
except Exception:
spt = None
Ppick, perror, Pprob = picker(args, pred_PP_mean[ts], pred_PP_std[ts], spt)
_output_writter_test(args, dataset, evi, test_writer, csvTst, Ppick, perror, Pprob)
pred_PP_mean_all.append(pred_PP_mean[ts])
pred_PP_std_all.append(pred_PP_std[ts])
sptall.append(spt)