模型总览
SeisPolarity 提供一系列用于地震极性分类的深度学习模型。所有模型均使用 PyTorch 实现,并支持 GPU 加速。
可用模型
模型 |
输入长度 |
类别 |
|---|---|---|
Ross (SCSN) |
400 |
3 (U/D/N) |
Eqpolarity |
600 |
2 (U/D) |
DiTingMotion |
128 |
3 (U/D/N) |
CFM |
160 |
2 (U/D) |
RPNet |
400 |
2 (U/D) |
PolarCAP |
64 |
2 (U/D) |
APP |
400 |
3 (U/D/N) |
加载模型
预训练模型
从 Hugging Face 加载预训练模型:
1from seispolarity.models import PPNet, RossNet, EqpolarityNet
2
3# Ross 模型
4model = RossNet(num_fm_classes=3)
5
6# Eqpolarity 模型
7model = EqpolarityNet()
8
9# 通用 PPNet(用于 SCSN)
10model = PPNet(num_fm_classes=3)
加载自定义权重
1import torch
2from seispolarity.models import PPNet
3
4model = PPNet(num_fm_classes=3)
5
6# 从检查点加载
7checkpoint = torch.load("checkpoints/model.pth")
8model.load_state_dict(checkpoint["model_state_dict"])
9
10# 或直接加载
11model.load_state_dict(torch.load("model_weights.pth"))
模型 API
SeisPolarity 中的所有模型都提供统一的 PyTorch 接口:
1import torch
2from seispolarity.models import PPNet
3
4# 创建模型
5model = PPNet(num_fm_classes=3)
6model.eval()
7
8# 准备输入
9waveforms = torch.randn(10, 1, 400) # (batch, channels, length)
10
11# 前向传播
12with torch.no_grad():
13 logits = model(waveforms)
14 predictions = logits.argmax(dim=1) # 获取预测类别
模型结构
Ross (SCSN)
Ross 模型是针对 SCSN 数据优化的 CNN 结构。
输入:400 个采样点(100 Hz 采样率下 4 秒)
结构:
带批归一化的卷积层
最大池化
全连接层
用于正则化的 Dropout
1from seispolarity.models import PPNet
2
3model = PPNet(num_fm_classes=3)
Eqpolarity
Eqpolarity 是用于极性分类的深度 CNN 模型。
输入:600 个采样点(100 Hz 采样率下 6 秒)
1from seispolarity.models import EqpolarityNet
2
3model = EqpolarityNet()
DiTingMotion
基于运动的极性分类模型。
输入:128 个采样点(100 Hz 采样率下 1.28 秒)
1from seispolarity.models import DiTingMotionNet
2
3model = DiTingMotionNet()
CFM
用于极性检测的自定义结构。
输入:160 个采样点
1from seispolarity.models import CFM
2
3model = CFM()
RPNet
残差极性网络(Residual Polarity Network)。
输入:400 个采样点(100 Hz 采样率下 4 秒)
1from seispolarity.models import RPNet
2
3model = RPNet()
PolarCAP
用于极性分类的轻量级模型。
输入:64 个采样点(100 Hz 采样率下 0.64 秒)
1from seispolarity.models import PolarCAP, PolarCAPLoss
2
3model = PolarCAP()
4loss_fn = PolarCAPLoss()
APP
自适应极性预测器(Adaptive Polarity Predictor)。
1from seispolarity.models import APP
2
3model = APP()
推理
批量推理
1import torch
2from seispolarity.models import PPNet
3from seispolarity import WaveformDataset
4
5# 加载模型
6model = PPNet(num_fm_classes=3)
7model.eval()
8model.to("cuda")
9
10# 加载数据集
11dataset = WaveformDataset(path="data.hdf5", name="SCSN")
12loader = dataset.get_dataloader(batch_size=1024)
13
14# 推理
15all_predictions = []
16with torch.no_grad():
17 for waveforms, _ in loader:
18 waveforms = waveforms.to("cuda")
19 logits = model(waveforms)
20 predictions = logits.argmax(dim=1)
21 all_predictions.append(predictions.cpu())
22
23predictions = torch.cat(all_predictions)
单样本推理
1import numpy as np
2from seispolarity.models import PPNet
3import torch
4
5# 加载模型
6model = PPNet(num_fm_classes=3)
7model.eval()
8
9# 单样本
10waveform = np.random.randn(400) # 单条波形
11waveform = torch.FloatTensor(waveform).unsqueeze(0).unsqueeze(0) # 添加 batch 和 channel 维度
12
13# 推理
14with torch.no_grad():
15 logits = model(waveform)
16 prediction = logits.argmax(dim=1).item()
17
18# 解释结果
19label_map = {0: "Up", 1: "Down", 2: "Unknown"}
20print(f"Predicted polarity: {label_map[prediction]}")
模型输出
模型为每个类别输出 logits:
1# 原始 logits
2logits = model(waveforms) # Shape: (batch_size, num_classes)
3
4# 概率(softmax)
5import torch.nn.functional as F
6probabilities = F.softmax(logits, dim=1)
7
8# 预测类别
9predictions = logits.argmax(dim=1)
模型下载
预训练模型可在 Hugging Face 上获取:
1from huggingface_hub import hf_hub_download
2import torch
3
4# 下载模型权重
5model_path = hf_hub_download(
6 repo_id="HeXingChen/SeisPolarity-Model",
7 filename="ross_scsn.pth"
8)
9
10# 加载权重
11state_dict = torch.load(model_path)
12model.load_state_dict(state_dict)
自定义模型
创建自定义模型:
1import torch.nn as nn
2from seispolarity.models.base import BasePolarityModel
3
4class CustomModel(BasePolarityModel):
5 def __init__(self, num_fm_classes=3):
6 super().__init__(num_fm_classes)
7 # 在此处定义您的模型结构
8 self.conv1 = nn.Conv1d(1, 32, kernel_size=5)
9 self.fc = nn.Linear(32 * 396, num_fm_classes)
10
11 def forward(self, x):
12 # 前向传播
13 x = self.conv1(x)
14 x = x.view(x.size(0), -1)
15 x = self.fc(x)
16 return x
详细的 API 文档请参见 API 参考。