-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReTrain_PN.py
executable file
·195 lines (164 loc) · 8.15 KB
/
ReTrain_PN.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
#!/usr/bin/env python
import os
import pickle
import argparse
from pprint import pprint
import matplotlib.pyplot as plt
from pathlib import Path
import obspy
import seisbench as sb
import seisbench.models as sbm
import dkpn.train as dktrain
print(" SB version: %s" % sb.__version__)
print("OBS version: %s" % obspy.__version__)
print("")
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
parser = argparse.ArgumentParser(description=(
"Script for training PHASE-NET using full SeisBench APIs and Augmentations. "
"It needs to have the 'dkpn' folder in the working path. "
"Requires Python >= 3.9"))
parser.add_argument('-d', '--dataset_name', type=str, default='ETHZ', help='Dataset name')
parser.add_argument('-s', '--dataset_size', type=str, default='Nano', help='Dataset size')
parser.add_argument('-r', '--random_seed', type=int, default=42, help='Random seed')
parser.add_argument('-o', '--store_folder', type=str, default=None, help='Store folder')
#
parser.add_argument('-e', '--epochs', type=int, default=25, help='Max. Num. Epochs for training')
parser.add_argument('-l', '--learning_rate', type=float, default=1e-3, help='Learning Rate for training')
parser.add_argument('-b', '--batch_size', type=int, default=32, help='Batch-Size for training')
#
parser.add_argument("--early_stop", action="store_true", help="Adopt early-stop regulation for epochs")
parser.add_argument('-x', '--patience', type=int, default=5, help='Num. Epochs to evaluate for early stop')
parser.add_argument('-y', '--delta', type=float, default=0.001, help='Mean dev_loss improvement over the latest patience epochs')
#
args = parser.parse_args()
print("---> Training: PhaseNet")
print("")
print(f"DATASET_NAME: {args.dataset_name}")
print(f"DATASET_SIZE: {args.dataset_size}")
print(f"RANDOM_SEED: {args.random_seed}")
print(f"STORE_FOLDER: {args.store_folder}")
print("")
print(f"MAX. EPOCHS: {args.epochs}")
print(f"LEARNING_RATE: {args.learning_rate}")
print(f"BATCH_SIZE: {args.batch_size}")
print("")
print(f"EARLY STOP: {args.early_stop}")
print(f" PATIENCE: {args.patience}")
print(f" DELTA: {args.delta}")
print("")
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# SELECT DATASET and SIZE
(train, dev, test) = dktrain.select_database_and_size(
args.dataset_name, args.dataset_size,
RANDOM_SEED=args.random_seed)
print("TRAIN samples %s: %d" % (args.dataset_name, len(train)))
print(" DEV samples %s: %d" % (args.dataset_name, len(dev)))
print(" TEST samples %s: %d" % (args.dataset_name, len(test)))
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# INITIALIZE PN
mypn = sbm.PhaseNet()
mypn.cuda();
TRAIN_CLASS = dktrain.TrainHelp_PhaseNet(
mypn, # It will contains the default args for StreamCF calculations!!!
train,
dev,
test,
augmentations_par={
"amp_norm_type": "std",
"window_strategy": "move", # "pad"
"final_windowlength": 3001,
"sigma": 10,
"fp_stabilization": 400,
"phase_dict": {
"trace_p_arrival_sample": "P",
"trace_pP_arrival_sample": "P",
"trace_P_arrival_sample": "P",
"trace_P1_arrival_sample": "P",
"trace_Pg_arrival_sample": "P",
"trace_Pn_arrival_sample": "P",
"trace_PmP_arrival_sample": "P",
"trace_pwP_arrival_sample": "P",
"trace_pwPm_arrival_sample": "P",
"trace_s_arrival_sample": "S",
"trace_S_arrival_sample": "S",
"trace_S1_arrival_sample": "S",
"trace_Sg_arrival_sample": "S",
"trace_SmS_arrival_sample": "S",
"trace_Sn_arrival_sample": "S"
},
},
batch_size=args.batch_size,
num_workers=24,
random_seed=args.random_seed,
)
# ----------------------------------------------------------------------------
# ---------------> ACTUAL TRAINING <---------------
if args.early_stop:
(train_loss_epochs, train_loss_epochs_batches,
dev_loss_epochs, dev_loss_epochs_batches) = TRAIN_CLASS.train_me_early_stop(
epochs=args.epochs, optimizer_type="adam",
learning_rate=args.learning_rate,
patience=args.patience, delta=args.delta)
else:
(train_loss_epochs, train_loss_epochs_batches,
dev_loss_epochs, dev_loss_epochs_batches) = TRAIN_CLASS.train_me(
epochs=args.epochs, optimizer_type="adam",
learning_rate=args.learning_rate)
# ----------------------------------------------------------------------------
# ---------------> STORE MODEL <---------------
_actual_epochs = TRAIN_CLASS.__training_epochs__
MODEL_NAME = "PN_TrainDataset_%s_Size_%s_Rnd_%d_Epochs_%d_LR_%06.4f_Batch_%d" % (
args.dataset_name, args.dataset_size, args.random_seed,
_actual_epochs, args.learning_rate, args.batch_size)
if not args.store_folder:
STORE_DIR_MODEL = Path(MODEL_NAME)
else:
STORE_DIR_MODEL = Path(args.store_folder)
#
if not STORE_DIR_MODEL.is_dir():
STORE_DIR_MODEL.mkdir(parents=True, exist_ok=True)
TRAIN_CLASS.store_weigths(STORE_DIR_MODEL, MODEL_NAME, MODEL_NAME, version="1")
# ----------------------------------------------------------------------------
# ---------------> STORE LOSS TABLE <---------------
with open(str(STORE_DIR_MODEL / "TRAIN_TEST_loss.csv"), "w") as OUT:
OUT.write("EPOCH, TRAIN_LOSS, TEST_LOSS"+os.linesep)
for xx, (trn, tst) in enumerate(zip(train_loss_epochs, dev_loss_epochs)):
OUT.write(("%d, %.4f, %.4f"+os.linesep) % (xx, trn, tst))
# ----------------------------------------------------------------------------
# ---------------> STORE LOSS PICKLE <---------------
TRAIN_LOSSES = []
for xx, (av_loss, batch_loss) in enumerate(zip(train_loss_epochs, train_loss_epochs_batches)):
TRAIN_LOSSES.append((av_loss, batch_loss))
with open(str(STORE_DIR_MODEL / 'TRAIN_loss_batches.pickle'), 'wb') as file:
pickle.dump(TRAIN_LOSSES, file)
DEV_LOSSES = []
for xx, (av_loss, batch_loss) in enumerate(zip(dev_loss_epochs, dev_loss_epochs_batches)):
DEV_LOSSES.append((av_loss, batch_loss))
with open(str(STORE_DIR_MODEL / 'DEV_loss_batches.pickle'), 'wb') as file:
pickle.dump(DEV_LOSSES, file)
# ----------------------------------------------------------------------------
# ---------------> STORE PARAMETERS <---------------
with open(str(STORE_DIR_MODEL / "TRAIN_ARGS.py"), "w") as OUT:
OUT.write("TRAINARGS=%s" % args)
# Store DATABASE INFO
with open(str(STORE_DIR_MODEL / "TRAIN_DATA_INFO.txt"), "w") as OUT:
OUT.write(("TRAIN samples %s: %d"+os.linesep) % (args.dataset_name,
len(train)))
OUT.write((" DEV samples %s: %d"+os.linesep) % (args.dataset_name,
len(dev)))
OUT.write((" TEST samples %s: %d"+os.linesep) % (args.dataset_name,
len(test)))
# ----------------------------------------------------------------------------
fig = plt.figure(figsize=(10, 7))
plt.plot(train_loss_epochs, label="TRAIN_Loss", color="red", lw=2)
plt.plot(dev_loss_epochs, label="DEV_Loss", color="teal", lw=2)
plt.xlabel("epochs")
plt.ylabel("cross-entropy loss")
plt.legend()
plt.tight_layout()
fig.savefig(str(STORE_DIR_MODEL / "TrainTest_LOSS.pdf"))