-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttsam_realtime.py
1294 lines (1048 loc) · 39.5 KB
/
ttsam_realtime.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
import argparse
import asyncio
import bisect
import json
import multiprocessing
import os
import sys
import threading
import time
from datetime import datetime
import discord
import numpy as np
import paho.mqtt.client as mqtt
import pandas as pd
import PyEW
import torch
import torch.nn as nn
from discord.ext import commands
from flask import Flask, render_template, request
from flask_socketio import SocketIO
from loguru import logger
from scipy.signal import detrend, iirfilter, sosfilt, zpk2sos
from scipy.spatial import cKDTree
app = Flask(__name__)
socketio = SocketIO(app)
# 共享物件
manager = multiprocessing.Manager()
wave_buffer = manager.dict()
wave_queue = manager.Queue()
pick_buffer = manager.dict()
event_queue = manager.Queue()
dataset_queue = manager.Queue()
report_queue = manager.Queue()
discord_queue = manager.Queue()
wave_endt = manager.Value("d", 0)
wave_speed_count = manager.Value("i", 0)
"""
Web Server
"""
@app.route("/", methods=["GET"])
def index():
report_log_dir = "logs/report"
try:
files = []
for f in os.listdir(report_log_dir):
file_path = os.path.join(report_log_dir, f)
if (
f.startswith("report")
and f.endswith(".log")
and os.path.isfile(file_path)
):
files.append(f)
files.sort(
key=lambda x: os.path.getmtime(os.path.join(report_log_dir, x)),
reverse=True,
)
except FileNotFoundError:
files = []
return render_template("index.html", files=files, target=target_dict)
@app.route("/get_file_content")
def get_file_content():
report_log_dir = "logs/report"
file_name = request.args.get("file")
if not file_name.startswith("report"):
return "Invalid file type", 400
if not file_name.endswith(".log"):
return "Invalid file type", 400
# 檢查文件名是否包含相對路徑
if ".." in file_name or "/" in file_name or "\\" in file_name:
return "Invalid file name", 400
try:
file_path = os.path.join(report_log_dir, file_name)
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
return content
except Exception as e:
return str(e), 500
@app.route("/trace")
def trace_page():
return render_template("trace.html")
@app.route("/event")
def event_page():
return render_template("event.html")
@app.route("/dataset")
def dataset_page():
return render_template("dataset.html")
@app.route("/intensityMap")
def map_page():
return render_template("intensityMap.html")
@socketio.on("connect")
def connect_earthworm():
socketio.emit("connect_init")
def wave_emitter():
while True:
wave = wave_queue.get()
wave_id = join_id_from_dict(wave, order="NSLC")
if "Z" not in wave_id:
continue
wave["waveid"] = wave_id
wave_packet = {
"waveid": wave_id,
"data": wave["data"].tolist(),
}
socketio.emit("wave_packet", wave_packet)
def event_emitter():
while True:
event_data = event_queue.get()
if not event_data:
continue
socketio.emit("event_data", event_data)
def dataset_emitter():
while True:
dataset_data = dataset_queue.get()
if not dataset_data:
continue
socketio.emit("dataset_data", dataset_data)
def web_server():
threading.Thread(target=wave_emitter).start()
threading.Thread(target=event_emitter).start()
threading.Thread(target=dataset_emitter).start()
if args.web:
# 開啟 web server
app.run(host=args.host, port=args.port, use_reloader=False)
socketio.run(app, debug=True)
"""
Earthworm Wave Listener
"""
# Load site info
site_info_file = "data/site_info.csv"
try:
logger.info(f"Loading {site_info_file}...")
site_info = pd.read_csv(site_info_file)
constant_dict = site_info.set_index(["Station", "Channel"])["Constant"].to_dict()
logger.info(f"{site_info_file} loaded")
except FileNotFoundError:
logger.warning(f"{site_info_file} not found")
def join_id_from_dict(data, order="NSLC"):
code = {"N": "network", "S": "station", "L": "location", "C": "channel"}
data_id = ".".join(data[code[letter]] for letter in order)
return data_id
def convert_to_tsmip_legacy_naming(wave):
if wave["network"] == "TW":
wave["network"] = "SM"
wave["location"] = "01"
return wave
def get_wave_constant(wave):
# count to cm/s^2
try:
wave_constant = constant_dict[wave["station"], wave["channel"]]
except Exception as e:
logger.debug(f"{wave['station']} not found in site_info.txt, use default 3.2e-6")
wave_constant = 3.2e-6
return wave_constant
def wave_array_init(sample_rate, buffer_time, fill_value):
return np.full(sample_rate * buffer_time, fill_value=fill_value)
def time_array_init(sample_rate, buffer_time, start_time, end_time, data_length):
"""
生成一個時間序列,包含前後兩段
後段從 start_time 內插至 end_time (確定的時間序列)
前段從 start_time 外插至 buffer 開始點 (往前預估的時間序列)
"""
return np.append(
np.linspace(
start_time - (buffer_time - 1),
start_time,
sample_rate * (buffer_time - 1),
),
np.linspace(start_time, end_time, data_length),
)
def slide_array(array, data):
array = np.append(array, data)
return array[data.size :]
def earthworm_wave_listener():
while True:
if not earthworm.mod_sta():
continue
wave = earthworm.get_wave(0)
if not wave:
continue
if wave["endt"] < time.time() - 3:
continue
if wave["endt"] > time.time() + 1:
continue
buffer_time = 30 # 設定緩衝區保留時間
sample_rate = 100 # 設定取樣率
# 得到最新的 wave 結束時間
wave_endt.value = wave["endt"]
try:
wave = convert_to_tsmip_legacy_naming(wave)
wave_id = join_id_from_dict(wave, order="NSLC")
wave["data"] = wave["data"] * get_wave_constant(wave)
# 將 wave_id 加入 wave_queue 給 wave_emitter 發送至前端
if "Z" in wave_id:
wave_queue.put(wave)
# add new trace to buffer
if wave_id not in wave_buffer.keys():
# wave_buffer 初始化時全部填入 wave 的平均值,確保 demean 時不會被斷點影響
wave_buffer[wave_id] = wave_array_init(
sample_rate, buffer_time, fill_value=np.array(wave["data"]).mean()
)
wave_buffer[wave_id] = slide_array(wave_buffer[wave_id], wave["data"])
wave_speed_count.value += 1
except Exception as e:
logger.error("earthworm_wave_process error", e)
"""
Earthworm Pick Listener
"""
def parse_pick_msg(pick_msg):
pick_msg_column = pick_msg.split()
try:
pick = {
"station": pick_msg_column[0],
"channel": pick_msg_column[1],
"network": pick_msg_column[2],
"location": pick_msg_column[3],
"lon": pick_msg_column[4],
"lat": pick_msg_column[5],
"pga": pick_msg_column[6],
"pgv": pick_msg_column[7],
"pd": pick_msg_column[8],
"tc": pick_msg_column[9], # Average period
"pick_time": pick_msg_column[10],
"weight": pick_msg_column[11], # 0:best 5:worst
"instrument": pick_msg_column[12], # 1:Acc 2:Vel
"update_sec": pick_msg_column[13], # sec after pick
}
pick["pickid"] = join_id_from_dict(pick, order="NSLC")
return pick
except IndexError as e:
logger.error(f"pick_msg parsing error: {pick_msg_column}", e)
def earthworm_pick_listener():
"""
監看 pick ring 的訊息,並將 pick 加入 pick_buffer
pick msg 的時間窗為 p 波後 2-10 秒
ref: pick_ew_new/pick_ra_0709.c line 283
"""
event_window = 10
while True:
try:
# 超時移除 pick
for pick_id, buffer_pick in pick_buffer.items():
if float(buffer_pick["sys_time"]) + event_window < time.time():
pick_buffer.__delitem__(pick_id)
logger.debug(f"delete pick: {pick_id}")
except BrokenPipeError:
break
except Exception as e:
logger.error(f"delete pick error: {pick_id}", e)
# 取得 pick msg
pick_msg = earthworm.get_msg(buf_ring=1, msg_type=0)
if not pick_msg:
time.sleep(0.00001)
continue
logger.debug(f"{pick_msg}")
# PickRing trace gap 太大會有 Restarting 的訊息
if "Restarting" in pick_msg:
continue
# PickRing 的未知短訊息,如:1732070774 124547
if len(pick_msg.split()) < 13:
continue
try:
pick_data = parse_pick_msg(pick_msg)
pick_id = join_id_from_dict(pick_data, order="NSLC")
# 跳過程式啟動前殘留在 shared memory 的 Pick
if time.time() > float(pick_data["pick_time"]) + 10:
if args.test_env:
pass # 測試環境使用歷史資料,不跳過
else:
continue
# upsec 為 2 秒時加入 pick
if pick_data["update_sec"] == "2":
print(pick_msg)
sys.stdout.flush()
# 以系統時間作為時間戳記
pick_data["sys_time"] = time.time()
pick_buffer[pick_id] = pick_data
logger.debug(f"add pick: {pick_id}")
except Exception as e:
logger.error("earthworm_pick_listener error:", e)
continue
time.sleep(0.00001)
"""
Model Inference
"""
# Load Vs30 grid
vs30_file = "data/Vs30ofTaiwan.csv"
try:
logger.info(f"Loading {vs30_file}...")
vs30_table = pd.read_csv(vs30_file)
tree = cKDTree(vs30_table[["lat", "lon"]])
logger.info(f"{vs30_file} loaded")
except FileNotFoundError:
logger.error(f"{vs30_file} not found")
# Load target station
target_file = "data/eew_target.csv"
try:
logger.info(f"Loading {target_file}...")
target_df = pd.read_csv(target_file)
target_dict = target_df.to_dict(orient="records")
logger.info(f"{target_file} loaded")
except FileNotFoundError:
logger.error(f"{target_file} not found")
def event_cutter(pick_buffer):
event_data = {}
# pick 只有 Z 軸
for pick_id, pick in pick_buffer.items():
network = pick["network"]
station = pick["station"]
location = pick["location"]
channel = pick["channel"]
data = {}
# 找到 wave_buffer 內的三軸資料
for i, component in enumerate(["Z", "N", "E"]):
try:
wave_id = f"{network}.{station}.{location}.{channel[0:2]}{component}"
data[component.lower()] = wave_buffer[wave_id].tolist()
except KeyError:
logger.debug(f"{wave_id} {component} not found, add zero array")
wave_id = f"{network}.{station}.{location}.{channel[0:2]}Z"
data[component.lower()] = np.zeros(3000).tolist()
continue
trace_dict = {
"traceid": pick_id,
"data": data,
}
event_data[pick_id] = {"pick": pick, "trace": trace_dict}
event_queue.put(event_data)
return event_data
def signal_processing(waveform):
try:
# demean and lowpass filter
data = detrend(waveform, type="constant")
data = lowpass(data, freq=10)
return data
except Exception as e:
logger.error("signal_processing error:", e)
def lowpass(data, freq=10, df=100, corners=4):
"""
Modified form ObsPy Signal Processing
https://docs.obspy.org/_modules/obspy/signal/filter.html#lowpass
"""
fe = 0.5 * df
f = freq / fe
if f > 1:
f = 1.0
z, p, k = iirfilter(corners, f, btype="lowpass", ftype="butter", output="zpk")
sos = zpk2sos(z, p, k)
return sosfilt(sos, data)
def get_vs30(lat, lon):
try:
distance, i = tree.query([float(lat), float(lon)])
vs30 = vs30_table.iloc[i]["Vs30"]
return float(vs30)
except Exception as e:
logger.error("get_vs30 error", e)
def get_station_position(station):
try:
latitude, longitude, elevation = site_info.loc[
(site_info["Station"] == station), ["Latitude", "Longitude", "Elevation"]
].values[0]
return latitude, longitude, elevation
except Exception as e:
logger.error(f"get_station_position error: {station}", e)
return
def get_site_info(pick):
try:
latitude, longitude, elevation = get_station_position(pick["station"])
vs30 = get_vs30(latitude, longitude)
return [latitude, longitude, elevation, vs30]
except Exception as e:
logger.debug(f"{pick['station']} not found in site_info, use pick info")
latitude, longitude, elevation = pick["lat"], pick["lon"], 100
vs30 = get_vs30(latitude, longitude)
return [latitude, longitude, elevation, vs30]
def convert_dataset(event_msg):
try:
waveform_list = []
station_list = []
station_name_list = []
for i, (pick_id, data) in enumerate(event_msg.items()):
trace = []
for j, component in enumerate(["Z", "N", "E"]):
waveform = data["trace"]["data"][component.lower()]
waveform = signal_processing(waveform)
trace.append(waveform.tolist())
waveform_list.append(trace)
station_list.append(get_site_info(data["pick"]))
station_name_list.append(data["pick"]["station"])
dataset = {
"waveform": waveform_list,
"station": station_list,
"station_name": station_name_list,
"target": [],
"target_name": [],
"pga": [],
}
return dataset
except Exception as e:
logger.error("converter error:", e)
def dataset_batch(dataset, batch_size=25):
batch = {}
try:
# 固定前 25 站的 waveform
batch["waveform"] = dataset["waveform"][:batch_size]
batch["station"] = dataset["station"][:batch_size]
batch["station_name"] = dataset["station_name"][:batch_size]
for i in range(0, len(dataset["target"]), batch_size):
# 迭代 25 站的 target
batch["target"] = dataset["target"][i : i + batch_size]
batch["target_name"] = dataset["target_name"][i : i + batch_size]
yield batch
except Exception as e:
logger.error("dataset_batch error:", e)
def get_target_dataset(dataset):
target_list = []
target_name_list = []
for target in target_dict:
latitude = target["latitude"]
longitude = target["longitude"]
elevation = target["elevation"]
target_list.append(
[latitude, longitude, elevation, get_vs30(latitude, longitude)]
)
target_name_list.append(target["station"])
dataset["target"] = target_list
dataset["target_name"] = target_name_list
return dataset
def ttsam_model_predict(tensor):
model_path = f"model/ttsam_trained_model_11.pt"
try:
full_model = get_full_model(model_path)
weight, sigma, mu = full_model(tensor)
pga_list = get_average_pga(weight, sigma, mu)
return pga_list
except FileNotFoundError:
logger.error(f"{model_path} not found")
except Exception as e:
logger.error("ttsam_model_predict error:", e)
def get_average_pga(weight, sigma, mu):
pga_list = torch.sum(weight * mu, dim=2).cpu().detach().numpy().flatten()
return pga_list.tolist()
def calculate_intensity(pga, pgv=None, label=False):
try:
intensity_label = ["0", "1", "2", "3", "4", "5-", "5+", "6-", "6+", "7"]
pga_level = np.log10(
[1e-5, 0.008, 0.025, 0.080, 0.250, 0.80, 1.4, 2.5, 4.4, 8.0]
) # log10(m/s^2)
pgv_level = np.log10(
[1e-5, 0.002, 0.007, 0.019, 0.057, 0.15, 0.3, 0.5, 0.8, 1.4]
) # log10(m/s)
pga_intensity = bisect.bisect(pga_level, pga) - 1
intensity = pga_intensity
if pga > pga_level[5] and pgv is not None:
pgv_intensity = bisect.bisect(pgv_level, pgv) - 1
if pgv_intensity > pga_intensity:
intensity = pgv_intensity
if label:
return intensity_label[intensity]
else:
return intensity
except Exception as e:
logger.error("calculate_intensity error:", e)
def prepare_tensor(data, shape, limit):
# 輸出固定的 tensor shape, 並將資料填入
tensor_data = np.zeros(shape)
tensor_limit = min(len(data), limit)
tensor_data[:tensor_limit] = data[:tensor_limit]
return torch.tensor(tensor_data).to(torch.double).unsqueeze(0)
def loading_animation(pick_threshold):
pick_counts = len(pick_buffer)
loading_chars = ["-", "\\", "|", "/"]
# 無限循環顯示 loading 動畫
wave_speed_count.value = 0
start_time = time.time()
for char in loading_chars:
# 清除上一個字符
sys.stdout.write("\r" + " " * 30 + "\r")
sys.stdout.flush()
wave_count = len(wave_buffer)
wave_timestring = datetime.fromtimestamp(float(wave_endt.value)).strftime(
"%Y-%m-%d %H:%M:%S.%f"
)
delay = time.time() - wave_endt.value
delta = time.time() - start_time
wave_process_rate = wave_speed_count.value / delta
# 顯示目前的 loading 字符
sys.stdout.write(
f"{wave_count} waves: {wave_timestring[:-3]} rate: {wave_process_rate:.3f} lag:{delay:.3f}s picks:{pick_counts}/{pick_threshold} {char} "
)
sys.stdout.flush()
time.sleep(0.1)
def model_inference():
"""
進行模型預測
"""
pick_threshold = 5
log_folder = "logs"
report_log_file = None
while True:
# 小於 3 個測站不觸發模型預測
if len(pick_buffer) < pick_threshold:
if report_log_file:
report_log_file.close()
# 重置 report_log_file
report_log_file = None
loading_animation(pick_threshold)
continue
if len(pick_buffer) >= pick_threshold:
if not report_log_file:
# 當觸發模型預測時,開始記錄 log
# 取得第一個 pick 的時間
event_first_pick = list(pick_buffer.values())[0]
first_pick_timestring = datetime.fromtimestamp(
float(event_first_pick["pick_time"]),
).strftime("%Y%m%d_%H%M%S")
# 以第一個 pick 的時間為 report log 檔案名稱
report_log_file = (
f"{log_folder}/report/report_{first_pick_timestring}.log"
)
report_log_file = open(report_log_file, "w+")
pick_log_file = f"{log_folder}/pick/pick_{first_pick_timestring}.log"
pick_log_file = open(pick_log_file, "w+")
try:
pick_count = len(pick_buffer)
print(f"{pick_count} picks in window, model inference start")
wave_endtime = wave_endt.value # 獲得最新的 wave 結束時間
inference_start_time = time.time()
event_data = event_cutter(pick_buffer)
dataset = convert_dataset(event_data)
dataset = get_target_dataset(dataset)
# 模型預測所有 target
for batch in dataset_batch(dataset):
wave = np.array(batch["waveform"])
wave_transposed = wave.transpose(0, 2, 1)
batch_waveform = prepare_tensor(wave_transposed, (25, 3000, 3), 25)
batch_station = prepare_tensor(batch["station"], (25, 4), 25)
batch_target = prepare_tensor(batch["target"], (25, 4), 25)
tensor = {
"waveform": batch_waveform,
"station": batch_station,
"station_name": batch["station_name"],
"target": batch_target,
"target_name": batch["target_name"],
}
# 模型預測
pga_list = ttsam_model_predict(tensor)
dataset["pga"].extend(pga_list)
dataset["intensity"] = [
calculate_intensity(pga, label=True) for pga in dataset["pga"]
]
# 產生報告
report = {"picks": len(pick_buffer), "log_time": "", "alarm": []}
for i, target_name in enumerate(dataset["target_name"]):
intensity = dataset["intensity"][i]
report[f"{target_name}"] = intensity
if intensity in ["4", "5-", "5+", "6-", "6+", "7"]:
# 過預警門檻值的測站
report["alarm"].append(target_name)
inference_end_time = time.time()
report["report_time"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
report["wave_time"] = wave_endtime - float(event_first_pick["pick_time"])
report["wave_endt"] = datetime.fromtimestamp(float(wave_endtime)).strftime(
"%Y-%m-%d %H:%M:%S.%f"
)
report["run_time"] = inference_end_time - inference_start_time
# log_time 加上 2 秒為 pick msg 的 upsec 2 秒
report["log_time"] = (
f"{inference_end_time - event_first_pick['sys_time'] + 2:.4f}" # upsec 2 sec
)
# 報告傳至 MQTT
mqtt_client.publish(topic, json.dumps(report))
# 報告傳至 Discord Bot
discord_queue.put(report)
print(report)
sys.stdout.flush()
report_log_file.write(json.dumps(report) + "\n")
pick_log = {
"log_time": report["log_time"],
"picks": list(pick_buffer.values()),
}
pick_log_file.write(json.dumps(pick_log) + "\n")
# 資料傳至前端
dataset_queue.put(dataset)
except Exception as e:
logger.error("model_inference error:", e)
"""
PyTorch Model
"""
if torch.cuda.is_available():
device = torch.device("cuda")
logger.info("Cuda detected, torch using gpu")
else:
device = torch.device("cpu")
logger.info("Cuda not detected, torch using cpu")
class LambdaLayer(nn.Module):
def __init__(self, lambd, eps=1e-4):
super(LambdaLayer, self).__init__()
self.lambd = lambd
self.eps = eps
def forward(self, x):
return self.lambd(x) + self.eps
class MLP(nn.Module):
def __init__(
self,
input_shape,
dims=(500, 300, 200, 150),
activation=nn.ReLU(),
last_activation=None,
):
super(MLP, self).__init__()
if last_activation is None:
last_activation = activation
self.dims = dims
self.first_fc = nn.Linear(input_shape[0], dims[0])
self.first_activation = activation
more_hidden = []
if len(self.dims) > 2:
for i in range(1, len(self.dims) - 1):
more_hidden.append(nn.Linear(self.dims[i - 1], self.dims[i]))
more_hidden.append(nn.ReLU())
self.more_hidden = nn.ModuleList(more_hidden)
self.last_fc = nn.Linear(dims[-2], dims[-1])
self.last_activation = last_activation
def forward(self, x):
output = self.first_fc(x)
output = self.first_activation(output)
if self.more_hidden:
for layer in self.more_hidden:
output = layer(output)
output = self.last_fc(output)
output = self.last_activation(output)
return output
class CNN(nn.Module):
"""
input_shape -> BatchSize, Channels, Height, Width
"""
def __init__(
self,
input_shape=(-1, 6000, 3),
activation=nn.ReLU(),
downsample=1,
mlp_input=11665,
mlp_dims=(500, 300, 200, 150),
eps=1e-8,
):
super(CNN, self).__init__()
self.input_shape = input_shape
self.activation = activation
self.downsample = downsample
self.mlp_input = mlp_input
self.mlp_dims = mlp_dims
self.eps = eps
self.lambda_layer_1 = LambdaLayer(
lambda t: t
/ (
torch.max(
torch.max(torch.abs(t), dim=1, keepdim=True).values,
dim=2,
keepdim=True,
).values
+ self.eps
)
)
self.unsqueeze_layer1 = LambdaLayer(lambda t: torch.unsqueeze(t, dim=1))
self.lambda_layer_2 = LambdaLayer(
lambda t: torch.log(
torch.max(torch.max(torch.abs(t), dim=1).values, dim=1).values
+ self.eps
)
/ 100
)
self.unsqueeze_layer2 = LambdaLayer(lambda t: torch.unsqueeze(t, dim=1))
self.conv2d1 = nn.Sequential(
nn.Conv2d(1, 8, kernel_size=(1, downsample), stride=(1, downsample)),
nn.ReLU(), # 用self.activation會有兩個ReLU
)
self.conv2d2 = nn.Sequential(
nn.Conv2d(8, 32, kernel_size=(16, 3), stride=(1, 3)), nn.ReLU()
)
self.conv1d1 = nn.Sequential(nn.Conv1d(32, 64, kernel_size=16), nn.ReLU())
self.maxpooling = nn.MaxPool1d(2)
self.conv1d2 = nn.Sequential(nn.Conv1d(64, 128, kernel_size=16), nn.ReLU())
self.conv1d3 = nn.Sequential(nn.Conv1d(128, 32, kernel_size=8), nn.ReLU())
self.conv1d4 = nn.Sequential(nn.Conv1d(32, 32, kernel_size=8), nn.ReLU())
self.conv1d5 = nn.Sequential(nn.Conv1d(32, 16, kernel_size=4), nn.ReLU())
self.mlp = MLP((self.mlp_input,), dims=self.mlp_dims)
def forward(self, x):
output = self.lambda_layer_1(x)
output = self.unsqueeze_layer1(output)
scale = self.lambda_layer_2(x)
scale = self.unsqueeze_layer2(scale)
output = self.conv2d1(output)
output = self.conv2d2(output)
output = torch.squeeze(output, dim=-1)
output = self.conv1d1(output)
output = self.maxpooling(output)
output = self.conv1d2(output)
output = self.maxpooling(output)
output = self.conv1d3(output)
output = self.maxpooling(output)
output = self.conv1d4(output)
output = self.conv1d5(output)
output = torch.flatten(output, start_dim=1)
output = torch.cat((output, scale), dim=1)
output = self.mlp(output)
return output
class PositionEmbeddingVs30(nn.Module):
"""
# embed station location (latitude, longitude, elevation, Vs30) to vector
"""
def __init__(
self,
wavelengths=((5, 30), (110, 123), (0.01, 5000), (100, 1600)),
emb_dim=500,
**kwargs,
):
super(PositionEmbeddingVs30, self).__init__(**kwargs)
# Format: [(min_lat, max_lat), (min_lon, max_lon), (min_depth, max_depth)]
self.wavelengths = wavelengths
self.emb_dim = emb_dim
min_lat, max_lat = wavelengths[0]
min_lon, max_lon = wavelengths[1]
min_depth, max_depth = wavelengths[2]
min_vs30, max_vs30 = wavelengths[3]
assert emb_dim % 10 == 0
lat_dim = emb_dim // 5
lon_dim = emb_dim // 5
depth_dim = emb_dim // 10
vs30_dim = emb_dim // 10
self.lat_coeff = (
2
* np.pi
* 1.0
/ min_lat
* ((min_lat / max_lat) ** (np.arange(lat_dim) / lat_dim))
)
self.lon_coeff = (
2
* np.pi
* 1.0
/ min_lon
* ((min_lon / max_lon) ** (np.arange(lon_dim) / lon_dim))
)
self.depth_coeff = (
2
* np.pi
* 1.0
/ min_depth
* ((min_depth / max_depth) ** (np.arange(depth_dim) / depth_dim))
)
self.vs30_coeff = (
2
* np.pi
* 1.0
/ min_vs30
* ((min_vs30 / max_vs30) ** (np.arange(vs30_dim) / vs30_dim))
)
lat_sin_mask = np.arange(emb_dim) % 5 == 0
# 0~emb_dim % 5==0 -> True --> 一堆 True False 的矩陣
# 共 500 個T F
lat_cos_mask = np.arange(emb_dim) % 5 == 1
lon_sin_mask = np.arange(emb_dim) % 5 == 2
lon_cos_mask = np.arange(emb_dim) % 5 == 3
depth_sin_mask = np.arange(emb_dim) % 10 == 4
depth_cos_mask = np.arange(emb_dim) % 10 == 9
vs30_sin_mask = np.arange(emb_dim) % 10 == 5
vs30_cos_mask = np.arange(emb_dim) % 10 == 8
self.mask = np.zeros(emb_dim)
self.mask[lat_sin_mask] = np.arange(lat_dim)
# mask 範圍共 1000 個,lat_sin_mask 裡面有 200 個 True
# 若是 True 就按照順序把 np.arange(lat_dim) 塞進去
self.mask[lat_cos_mask] = lat_dim + np.arange(lat_dim)
self.mask[lon_sin_mask] = 2 * lat_dim + np.arange(lon_dim)
self.mask[lon_cos_mask] = 2 * lat_dim + lon_dim + np.arange(lon_dim)
self.mask[depth_sin_mask] = 2 * lat_dim + 2 * lon_dim + np.arange(depth_dim)
self.mask[depth_cos_mask] = (
2 * lat_dim + 2 * lon_dim + depth_dim + np.arange(depth_dim)
)
self.mask[vs30_sin_mask] = (
2 * lat_dim + 2 * lon_dim + 2 * depth_dim + np.arange(vs30_dim)
)
self.mask[vs30_cos_mask] = (
2 * lat_dim + 2 * lon_dim + 2 * depth_dim + vs30_dim + np.arange(vs30_dim)
)
self.mask = self.mask.astype("int32")
def forward(self, x):
lat_base = x[:, :, 0:1].to(device) * torch.Tensor(self.lat_coeff).to(device)
lon_base = x[:, :, 1:2].to(device) * torch.Tensor(self.lon_coeff).to(device)
depth_base = x[:, :, 2:3].to(device) * torch.Tensor(self.depth_coeff).to(device)
vs30_base = x[:, :, 3:4] * torch.Tensor(self.vs30_coeff).to(device)
output = torch.cat(