-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
145 lines (119 loc) · 4.38 KB
/
app.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
# %%
import eqnet
from eqnet.utils import detect_peaks, extract_picks
from dataclasses import dataclass
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List, Optional, Union
import torch
import torch.nn.functional as F
import pandas as pd
@dataclass
class Config:
model = "phasenet_das"
backbone = "unet"
phases = ["P", "S"]
device = "cuda"
min_prob = 0.5
amp = True
dtype = torch.float32
location = None
def padding(data, min_nt=1024, min_nx=1024):
nt, nx = data.shape[-2:]
pad_nt = (min_nt - nt % min_nt) % min_nt
pad_nx = (min_nx - nx % min_nx) % min_nx
with torch.no_grad():
data = F.pad(data, (0, pad_nx, 0, pad_nt), mode="constant")
return data
class Data(BaseModel):
id: List[str]
timestamp: List[str]
vec: Union[List[List[List[float]]], List[List[float]]]
dt_s: Optional[float] = 0.01
def load_model(args):
model = eqnet.models.__dict__[args.model].build_model(
backbone=args.backbone,
in_channels=1,
out_channels=(len(args.phases) + 1),
)
if args.model == "phasenet" and (not args.add_polarity):
raise ("No pretrained model for phasenet, please use phasenet_polarity instead")
elif (args.model == "phasenet") and (args.add_polarity):
model_url = "https://github.com/AI4EPS/models/releases/download/PhaseNet-Polarity-v3/model_99.pth"
elif args.model == "phasenet_das":
if args.location is None:
# model_url = "ai4eps/model-registry/PhaseNet-DAS:latest"
model_url = "https://github.com/AI4EPS/models/releases/download/PhaseNet-DAS-v0/PhaseNet-DAS-v0.pth"
# model_url = "https://github.com/AI4EPS/models/releases/download/PhaseNet-DAS-v1/PhaseNet-DAS-v1.pth"
elif args.location == "forge":
model_url = "https://github.com/AI4EPS/models/releases/download/PhaseNet-DAS-ConvertedPhase/model_99.pth"
else:
raise ("Missing pretrained model for this location")
else:
raise
state_dict = torch.hub.load_state_dict_from_url(
model_url, model_dir="./", progress=True, check_hash=True, map_location="cpu"
)
model.load_state_dict(state_dict["model"], strict=True)
return model
###################### FastAPI ######################
app = FastAPI()
args = Config()
model = load_model(args)
model.to(args.device)
model.eval()
@app.post("/predict")
def predict(meta: Data):
with torch.inference_mode():
with torch.cuda.amp.autocast(enabled=args.amp):
print(meta["nt"], meta["nx"])
output = model(meta)["phase"][:, :, : meta["nt"], : meta["nx"]]
scores = torch.softmax(output, dim=1) # [batch, nch, nt, nsta]
topk_scores, topk_inds = detect_peaks(scores, vmin=args.min_prob, kernel=21)
picks = extract_picks(
topk_inds,
topk_scores,
file_name=meta["id"],
begin_time=meta["timestamp"] if "timestamp" in meta else None,
dt=meta["dt_s"] if "dt_s" in meta else 0.01,
vmin=args.min_prob,
phases=args.phases,
)
return {"picks": picks}
@app.get("/healthz")
def healthz():
return {"status": "ok"}
# %%
if __name__ == "__main__":
# %%
import h5py
import numpy as np
import matplotlib.pyplot as plt
h5_file = "ci37238204.h5"
with h5py.File(h5_file, "r") as f:
vec = f["data"][:].T
vec = vec[np.newaxis, :, :]
timestamp = f["data"].attrs["begin_time"]
data_id = f'{f["data"].attrs["event_id"]}'
# %%
data = torch.tensor(vec, dtype=args.dtype).unsqueeze(0) # [batch, nch, nt, nsta]
nt, nx = data.shape[-2:]
data = padding(data)
meta = {"id": [data_id], "timestamp": [timestamp], "data": data, "dt_s": 0.01, "nx": nx, "nt": nt}
picks = predict(meta)["picks"]
data = data[:, :, :nt, :nx]
# %%
picks = picks[0] ## batch size = 1
picks = pd.DataFrame.from_dict(picks, orient="columns")
# %%
plt.figure()
vmax = torch.std(data[0, 0]) * 3
vmin = -vmax
plt.imshow(data[0, 0], vmin=vmin, vmax=vmax, aspect="auto", cmap="seismic", interpolation="none")
color = picks["phase_type"].map({"P": "red", "S": "blue"})
plt.scatter(picks["station_id"], picks["phase_index"], c=color, s=1)
plt.xticks([])
plt.tight_layout()
plt.savefig("test_v2.png")
plt.show()
# %%