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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Script to upload PhaseNet-TF model to Hugging Face Hub
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from huggingface_hub import HfApi, create_repo, upload_file
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
+
import torch
|
| 11 |
+
import yaml
|
| 12 |
+
|
| 13 |
+
def create_model_card():
|
| 14 |
+
"""Create a comprehensive model card for PhaseNet-TF"""
|
| 15 |
+
return """---
|
| 16 |
+
language: en
|
| 17 |
+
tags:
|
| 18 |
+
- seismic
|
| 19 |
+
- earthquake
|
| 20 |
+
- phase-picking
|
| 21 |
+
- deep-learning
|
| 22 |
+
- pytorch
|
| 23 |
+
license: mit
|
| 24 |
+
datasets:
|
| 25 |
+
- PS_Alaska
|
| 26 |
+
metrics:
|
| 27 |
+
- f1-score
|
| 28 |
+
- precision
|
| 29 |
+
- recall
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
# PhaseNet-TF: Advanced Seismic Arrival Time Detection
|
| 33 |
+
|
| 34 |
+
## Model Description
|
| 35 |
+
|
| 36 |
+
PhaseNet-TF is an advanced deep learning model for automatic seismic phase picking (P-wave, S-wave, and PS-wave detection) using spectrogram-based image segmentation approaches. The model leverages DeepLabV3Plus architecture to detect seismic arrivals with high accuracy, especially for weak and noisy signals from ocean-bottom seismometers and weak phases such as slab interface refracted PS and SP waves. This Alaska version is specifically trained on the PS_Alaska dataset for P and S phases. For more details, please refer to the paper and the [PhaseNet-TF](https://github.com/swei-seismo/PhaseNet-TF) repository.
|
| 37 |
+
|
| 38 |
+
## Model Architecture
|
| 39 |
+
|
| 40 |
+
- **Backbone**: DeepLabV3Plus with ResNet34 encoder
|
| 41 |
+
- **Input**: 3-component seismic waveforms converted to 6-channel spectrograms (real + imaginary)
|
| 42 |
+
- **Output**: Probability maps for P, S, PS phases and noise
|
| 43 |
+
- **Sampling Rate**: 40 Hz (dt_s = 0.025s)
|
| 44 |
+
- **Window Length**: 4800 points (120 seconds)
|
| 45 |
+
- **Spectrogram Size**: 64 × 4800 (frequency × time)
|
| 46 |
+
- **Input Channels**: 6 (3 real + 3 imaginary spectrogram channels)
|
| 47 |
+
- **Output Classes**: 4 (noise, P, S, PS)
|
| 48 |
+
|
| 49 |
+
## Load the checkpoint
|
| 50 |
+
checkpoint = torch.load("alaska_iter2.ckpt", map_location="cpu")
|
| 51 |
+
|
| 52 |
+
## Citation
|
| 53 |
+
|
| 54 |
+
If you use this model in your research, please cite:
|
| 55 |
+
|
| 56 |
+
```bibtex
|
| 57 |
+
@article{jie2025background,
|
| 58 |
+
title={Background Seismicity and Aftershocks of the 2020-2021 Large Earthquakes at the Alaska Peninsula Revealed by a Deep-learning-based Catalog},
|
| 59 |
+
author={Jie, Yaqi and Wei, Songqiao Shawn and Zhu, Weiqiang and Freymueller, Jeffrey Todd and Elliott, Julie},
|
| 60 |
+
journal={Authorea Preprints},
|
| 61 |
+
year={2025},
|
| 62 |
+
publisher={Authorea}
|
| 63 |
+
}
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
## License
|
| 67 |
+
|
| 68 |
+
This model is licensed under the MIT License.
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
def create_config_json(model_path):
|
| 72 |
+
"""Create config.json with model metadata"""
|
| 73 |
+
config = {
|
| 74 |
+
"model_type": "phasenet-tf",
|
| 75 |
+
"architecture": "DeepLabV3Plus with ResNet34 encoder",
|
| 76 |
+
"input_channels": 6, # 3-component real + 3-component imaginary spectrograms
|
| 77 |
+
"output_classes": 4, # noise, P, S, PS
|
| 78 |
+
"sampling_rate": 40, # 1/0.025 = 40 Hz
|
| 79 |
+
"window_length": 4800, # 120 seconds at 40 Hz
|
| 80 |
+
"phases": ["P", "S", "PS"],
|
| 81 |
+
"framework": "pytorch",
|
| 82 |
+
"license": "mit",
|
| 83 |
+
"tags": ["seismic", "earthquake", "phase-picking", "deep-learning", "deeplabv3plus"]
|
| 84 |
+
}
|
| 85 |
+
return config
|
| 86 |
+
|
| 87 |
+
def upload_model_to_hf(
|
| 88 |
+
checkpoint_path: str,
|
| 89 |
+
config_path: str = None,
|
| 90 |
+
repo_name: str = "PhaseNet-TF_Alaska",
|
| 91 |
+
username: str = None,
|
| 92 |
+
token: str = None
|
| 93 |
+
):
|
| 94 |
+
"""Upload model to Hugging Face Hub"""
|
| 95 |
+
|
| 96 |
+
# Initialize API
|
| 97 |
+
if token:
|
| 98 |
+
api = HfApi(token=token)
|
| 99 |
+
else:
|
| 100 |
+
api = HfApi()
|
| 101 |
+
|
| 102 |
+
# Get username if not provided
|
| 103 |
+
if username is None:
|
| 104 |
+
try:
|
| 105 |
+
username = api.whoami()["name"]
|
| 106 |
+
print(f"Using logged-in username: {username}")
|
| 107 |
+
except Exception as e:
|
| 108 |
+
print(f"Error getting username: {e}")
|
| 109 |
+
print("Please provide username with --username or login with huggingface-cli login")
|
| 110 |
+
return
|
| 111 |
+
|
| 112 |
+
# Create repository
|
| 113 |
+
repo_id = f"{username}/{repo_name}"
|
| 114 |
+
try:
|
| 115 |
+
if token:
|
| 116 |
+
create_repo(repo_id, token=token, exist_ok=True)
|
| 117 |
+
else:
|
| 118 |
+
create_repo(repo_id, exist_ok=True)
|
| 119 |
+
print(f"Repository {repo_id} created/accessed successfully")
|
| 120 |
+
except Exception as e:
|
| 121 |
+
print(f"Error creating repository: {e}")
|
| 122 |
+
return
|
| 123 |
+
|
| 124 |
+
# Upload checkpoint
|
| 125 |
+
print("Uploading model checkpoint...")
|
| 126 |
+
upload_file(
|
| 127 |
+
path_or_fileobj=checkpoint_path,
|
| 128 |
+
path_in_repo="pytorch_model.bin",
|
| 129 |
+
repo_id=repo_id,
|
| 130 |
+
token=token
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# Upload config
|
| 134 |
+
if config_path and os.path.exists(config_path):
|
| 135 |
+
print("Uploading config file...")
|
| 136 |
+
upload_file(
|
| 137 |
+
path_or_fileobj=config_path,
|
| 138 |
+
path_in_repo="config.yaml",
|
| 139 |
+
repo_id=repo_id,
|
| 140 |
+
token=token
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# Create and upload config.json
|
| 144 |
+
config_json = create_config_json(checkpoint_path)
|
| 145 |
+
config_json_path = "config.json"
|
| 146 |
+
with open(config_json_path, 'w') as f:
|
| 147 |
+
json.dump(config_json, f, indent=2)
|
| 148 |
+
|
| 149 |
+
upload_file(
|
| 150 |
+
path_or_fileobj=config_json_path,
|
| 151 |
+
path_in_repo="config.json",
|
| 152 |
+
repo_id=repo_id,
|
| 153 |
+
token=token
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
# Create and upload README.md
|
| 157 |
+
model_card = create_model_card()
|
| 158 |
+
readme_path = "README.md"
|
| 159 |
+
with open(readme_path, 'w') as f:
|
| 160 |
+
f.write(model_card)
|
| 161 |
+
|
| 162 |
+
upload_file(
|
| 163 |
+
path_or_fileobj=readme_path,
|
| 164 |
+
path_in_repo="README.md",
|
| 165 |
+
repo_id=repo_id,
|
| 166 |
+
token=token
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# Clean up temporary files
|
| 170 |
+
os.remove(config_json_path)
|
| 171 |
+
os.remove(readme_path)
|
| 172 |
+
|
| 173 |
+
print(f"Model uploaded successfully to https://huggingface.co/{repo_id}")
|
| 174 |
+
|
| 175 |
+
if __name__ == "__main__":
|
| 176 |
+
import argparse
|
| 177 |
+
|
| 178 |
+
parser = argparse.ArgumentParser(description="Upload PhaseNet-TF model to Hugging Face")
|
| 179 |
+
parser.add_argument("--checkpoint", required=True, help="Path to model checkpoint (.ckpt)")
|
| 180 |
+
parser.add_argument("--config", help="Path to config file (.yaml)")
|
| 181 |
+
parser.add_argument("--repo-name", default="PhaseNet-TF_Alaska", help="Repository name")
|
| 182 |
+
parser.add_argument("--username", help="Hugging Face username (optional if already logged in)")
|
| 183 |
+
parser.add_argument("--token", help="Hugging Face token (optional if already logged in)")
|
| 184 |
+
|
| 185 |
+
args = parser.parse_args()
|
| 186 |
+
|
| 187 |
+
upload_model_to_hf(
|
| 188 |
+
checkpoint_path=args.checkpoint,
|
| 189 |
+
config_path=args.config,
|
| 190 |
+
repo_name=args.repo_name,
|
| 191 |
+
username=args.username,
|
| 192 |
+
token=args.token
|
| 193 |
+
)
|