Skip to content

Commit 606aa2f

Browse files
authored
Merge pull request #21 from SCOREC/feature/hyperparameter-tuning
Hyperparameter tuning, FocalDiceLoss, and 5M/10M cross-regime transfer evaluation
2 parents 0e86a49 + e5d8061 commit 606aa2f

6 files changed

Lines changed: 1434 additions & 43 deletions

File tree

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,41 @@ The classifier supports several command line options for training configuration:
9292
- `--epochs`: Number of training epochs (default: 2000)
9393
- `--minTrainingLoss`: Minimum reduction in training loss in orders of magnitude (default: 3, set to 0 to disable check)
9494

95+
### Architecture
96+
- `--baseChannels`: Base number of channels in the UNet encoder (default: 64)
97+
98+
### Loss Function
99+
- `--lossFunction`: Loss function, `dice` (default) or `focal_dice` (combined focal + dice loss for class imbalance)
100+
- `--focalAlpha`: Focal loss alpha, class balance weight (default: 0.75)
101+
- `--focalGamma`: Focal loss gamma, focusing parameter (default: 2.0)
102+
- `--focalDiceWeight`: Weight of the dice component in FocalDiceLoss (default: 0.5)
103+
104+
### Learning Rate Schedule
105+
- `--warmupEpochs`: Number of linear warmup epochs before the main scheduler kicks in (default: 0)
106+
- `--scheduler`: Learning rate scheduler, `cosine` (default) or `plateau`
107+
- `--plateau-factor`: ReduceLROnPlateau factor (default: 0.5)
108+
- `--plateau-patience`: ReduceLROnPlateau patience in epochs (default: 5)
109+
- `--plateau-min-lr`: ReduceLROnPlateau minimum learning rate (default: 1e-6)
110+
111+
### Stochastic Weight Averaging
112+
- `--swa`: Enable Stochastic Weight Averaging for better generalization
113+
- `--swaStart`: Fraction of total epochs after which SWA begins (default: 0.75)
114+
95115
### Data Configuration
96116
- `--trainFrameFirst`: First frame number for training data (default: 1)
97117
- `--trainFrameLast`: Last frame number (exclusive) for training data (default: 140)
98118
- `--validationFrameFirst`: First frame number for validation data (default: 141)
99119
- `--validationFrameLast`: Last frame number (exclusive) for validation data (default: 150)
100120
- `--paramFile`: Path to the parameter txt file containing gkyl input data
101121
- `--xptCacheDir`: Path to directory for caching X-point finder outputs
122+
- `--posRatio`: Target ratio of training patches containing at least one X-point (default: 0.5)
123+
- `--fixed-val-crops`: Use deterministic validation crops each epoch for stable val loss (default: False)
102124

103125
### Training Optimization
104126
- `--use-amp`: Enable automatic mixed precision training for faster training on modern GPUs
105127
- `--amp-dtype`: Data type for mixed precision (`float16` or `bfloat16`, default: `bfloat16`)
106128
- `--patience`: Patience for early stopping (default: 15 epochs)
129+
- `--early-stop-min-delta`: Minimum improvement in validation loss to reset early stopping (default: 0.0)
107130
- `--seed`: Random seed for reproducibility (default: None for non-deterministic)
108131
- `--require-gpu`: Require GPU to be available, exit if not found
109132

@@ -142,6 +165,46 @@ python -u ${rcRoot}/reconClassifier/XPointMLTest.py \
142165
--validationFrameLast 120
143166
```
144167

168+
## Hyperparameter Tuning with Optuna
169+
170+
The `optuna_tuner.py` script automates hyperparameter search over the knobs above (base channels, dropout, weight decay, learning rate, positive ratio, focal/dice weighting, scheduler choice, SWA start). It uses a Tree-structured Parzen Estimator sampler and a Median Pruner that aborts unpromising runs early based on the validation F1 curve.
171+
172+
```
173+
python -u ${rcRoot}/reconClassifier/optuna_tuner.py \
174+
--paramFile=/path/to/params.txt \
175+
--xptCacheDir=/path/to/cache \
176+
--n-trials 50 \
177+
--study-name xpoint-tuning \
178+
--db sqlite:///optuna_xpoint.db
179+
```
180+
181+
The SQLite database is created automatically on first run and reloaded on subsequent runs with the same `--study-name`, so a study can be resumed or extended without re-running completed trials.
182+
183+
## Cross-regime Transfer Evaluation
184+
185+
The PKPM-trained model can be evaluated zero-shot on additional Gkeyll datasets (currently 5-moment "5M" and 10-moment "10M" fluid simulations). Evaluation runs in two steps: first build the X-point cache for the transfer dataset, then run the evaluator.
186+
187+
### Building the X-point cache for 5M/10M
188+
189+
`run_hessian_and_build_cache.py` is the only script that runs the deterministic Hessian X-point classifier; it writes the per-frame results as `.npy` files so the training and evaluation scripts only ever read from cache. Trying to train or evaluate on an uncached frame raises a clear error pointing back to this script.
190+
191+
```
192+
python -u ${rcRoot}/reconClassifier/run_hessian_and_build_cache.py \
193+
--dataset 5M \
194+
--start 1 --end 150 \
195+
--workers 30
196+
```
197+
198+
The `RC_EXTRACT_DIR` and `RC_CACHE_BASE` environment variables override the default raw-data and cache directories. Pointing `RC_EXTRACT_DIR` at a node-local ramdisk (e.g. `/dev/shm/$USER`) significantly accelerates cache construction on machines where the raw data lives on a slow shared filesystem.
199+
200+
### Running transfer evaluation
201+
202+
`test_xpoint_transfer.py` loads the best PKPM-trained checkpoint and evaluates it on each transfer dataset, writing per-dataset and combined metrics to `transfer_eval_results/`. The path to the checkpoint is set by the `BEST_MODEL` constant near the top of the script; update it to point at your trained checkpoint before running. Both transfer caches must exist before this script is run.
203+
204+
```
205+
python -u ${rcRoot}/reconClassifier/test_xpoint_transfer.py
206+
```
207+
145208
## Resuming Development Work
146209

147210
The following commands should be run on `checkers` **every time you create a new shell** to resume work in the existing virtual environment.

0 commit comments

Comments
 (0)