← Home

API Reference

Every command. Every flag.

Commands

Install: pip install ghost-hsi

CommandDescription
ghost train_sptFull GHOST pipeline — Spectral Partition Tree + ensemble models
ghost trainFlat model training, no SPT tree (baseline)
ghost predictInference on test split, compute metrics
ghost visualizeGenerate 3-panel segmentation figure
ghost convert_to_matConvert ENVI, TIFF, GeoTIFF, HDF5/NetCDF to .mat — requires pip install ghost-hsi[convert]
ghost demoPrint bundled Indian Pines file paths and example command
ghost versionPrint installed version
ghost flowerEaster egg

ghost train_spt

Primary training command. Builds the Spectral Partition Tree and trains per-node ensemble models.

$ Terminal
ghost train_spt \
  --data /path/to/data.mat \
  --gt   /path/to/labels.mat \
  --loss dice --routing forest \
  --base_filters 32 --num_filters 8 \
  --ensembles 5 --leaf_ensembles 3 \
  --epochs 400 --patience 50 --min_epochs 40 \
  --out-dir runs/my_experiment

Required

FlagDescription
--dataPath to hyperspectral .mat file. Shape: (H, W, Bands)
--gtPath to ground truth .mat file. Shape: (H, W), integer class IDs, 0 = background

Data

FlagDefaultDescription
--train_ratio0.2Fraction of labeled pixels per class for training
--val_ratio0.1Fraction of labeled pixels per class for validation

Model

FlagDefaultDescription
--base_filters32U-Net base filters. Channel progression: f → 2f → 4f → 8f → 16f
--num_filters8Spectral 3D conv filters per layer
--num_blocks3Number of 3D conv blocks

Training

FlagDefaultDescription
--epochs400Base epoch budget for root node. Child nodes receive scaled budgets
--lr1e-4Learning rate (AdamW)
--lossceLoss function: ce, dice, focal, squared_ce
--focal_gamma2.0Gamma for focal loss only
--patience50Early stop after N epochs without improvement
--min_epochs40Never early-stop before this epoch
--warmup_epochs0Linear LR warmup epochs
--val_interval20Validate every N epochs
--seed42Random seed

SPT (Spectral Partition Tree)

FlagDefaultDescription
--depthautoTree depth. auto: stops at depth 3 or SAM < 0.05. full: always recurse. Integer: fixed max depth
--ensembles5Ensemble size per internal node
--leaf_ensembles3Ensemble size per leaf node (≤2 classes)

Routing

FlagDefaultDescription
--routingforestRouting mode: forest (recommended), hybrid, soft. Note: hybrid and soft require the SSSR module which is currently broken — use forest
--d_model64SSM fingerprint dimensionality (SSSR only)
--d_state16SSM filters per branch (SSSR only)
--ssm_epochs300SSM pretraining epochs. Set to 1 when using --routing forest to skip
--ssm_lr1e-3SSM pretraining learning rate
--ssm_savessm_pretrained.ptSSM weights save path (inside --out-dir)
--ssm_loadNoneLoad pre-existing SSM weights, skip pretraining

Output

FlagDefaultDescription
--out-dir.Output directory (created if needed)
--savespt_models.pklModel bundle filename

Output Files

FileDescription
spt_models.pklComplete model bundle: SPT tree structure + all ensemble model weights + SSM state
ssm_pretrained.ptStandalone SSM encoder weights
training_history.csvEpoch-by-epoch metrics for all nodes

ghost train

Flat model training — no SPT tree. Useful as a baseline to compare against the full pipeline, or for datasets with very few classes where the tree structure adds no benefit.

Accepts the same flags as ghost train_spt for: --data, --gt, --train_ratio, --val_ratio, --base_filters, --num_filters, --num_blocks, --epochs, --lr, --seed, --out-dir, --save.

Additional Flags

FlagDefaultDescription
--fp16FalseMixed precision training. Reduces VRAM ~40%
--logtraining_log.csvPer-epoch log filename

Output Files

FileDescription
best_model.pthBest model weights (by val mIoU)
training_log.csvepoch, train_loss, val_loss, val_oa, val_miou, …
test_results.csvFinal test metrics

ghost predict

Run inference on the test split using a trained model. Outputs metric CSVs per routing mode.

$ Terminal
ghost predict \
  --data  /path/to/data.mat \
  --gt    /path/to/labels.mat \
  --model runs/my_experiment/spt_models.pkl \
  --routing forest --out-dir runs/my_experiment

Required

FlagDescription
--dataHyperspectral data .mat file
--gtGround truth .mat file
--modelPath to spt_models.pkl from ghost train_spt

Optional

FlagDefaultDescription
--routingallforest, hybrid, soft, or all (runs all three). Use forest — others require SSSR which is currently broken
--ssm_loadNoneStandalone SSM weights. Falls back to embedded state in pkl
--train_ratio0.2Must match training value
--val_ratio0.1Must match training value
--seed42Must match training value
--out-dir.Output directory

Output Files

FileDescription
test_results_forest.csvOA, mIoU, Dice, Precision, Recall — forest routing
test_results_hybrid.csvSame for hybrid routing
test_results_soft.csvSame for soft routing

ghost visualize

Generate a 3-panel PNG: false colour composite | ground truth | GHOST prediction.

$ Terminal
ghost visualize \
  --data    /path/to/data.mat \
  --gt      /path/to/labels.mat \
  --model   runs/my_experiment/spt_models.pkl \
  --dataset indian_pines --routing forest \
  --title   "GHOST — Indian Pines" \
  --out-dir runs/my_experiment

Required

FlagDescription
--dataHyperspectral data .mat file
--gtGround truth .mat file
--modelPath to spt_models.pkl

Optional

FlagDefaultDescription
--routingforestRouting mode for the prediction panel
--datasetNoneDataset name for class labels: indian_pines, pavia, salinas
--r_bandBands×0.75Band index for red channel in false colour
--g_bandBands×0.50Band index for green channel
--b_bandBands×0.25Band index for blue channel
--titleGHOST SegmentationFigure title
--ssm_loadNoneStandalone SSM weights
--train_ratio0.2Must match training value
--val_ratio0.1Must match training value
--seed42Must match training value
--out-dir.Output directory

Output Files

FileDescription
segmentation_forest.png3-panel figure, 180 DPI, dark background

ghost convert_to_mat

Convert ENVI, TIFF, GeoTIFF, and HDF5/NetCDF hyperspectral images to .mat format with zero data loss. Requires the optional convert extras: pip install ghost-hsi[convert].

$ Terminal
pip install ghost-hsi[convert]

ghost convert_to_mat \
  --img image.hdr \
  --gt  labels.tif \
  --out-dir converted/

Required

FlagDescription
--imgPath to the hyperspectral image. Supported: ENVI (.hdr), TIFF/GeoTIFF (.tif), HDF5/NetCDF (.h5, .nc). Format is auto-detected from extension.
--out-dirOutput directory (created if needed). Converted .mat and JSON sidecar are written here.

Optional

FlagDefaultDescription
--gtNoneGround truth label file. Accepts .mat, .png, .tif, .hdr.
--cropNoneSpatial crop before conversion: Y X H W (top-left origin, height, width).

HDF5 dataset path syntax

For HDF5 files with multiple datasets, specify the dataset path with a colon separator:

$ Terminal
ghost convert_to_mat \
  --img file.h5:/dataset/path \
  --out-dir converted/

Output Files

FileDescription
<name>.matConverted hyperspectral cube, shape (H, W, Bands)
<name>_meta.jsonMetadata sidecar: CRS, wavelengths, spatial transforms, band names
<name>_gt.matConverted ground truth (only if --gt is provided)

Dependencies

PackageUsed for
spectralENVI (.hdr) reading
rasterioTIFF / GeoTIFF reading, CRS and transform extraction
h5pyHDF5 / NetCDF reading

None of these are installed by pip install ghost-hsi alone. All three are pulled in by pip install ghost-hsi[convert].

Data Format

v0.1.x accepts .mat files (MATLAB / HDF5 format). Keys inside the file are auto-detected by array dimensionality — no configuration needed.

FileShapeDescription
Data file (--data)(H, W, Bands)The hyperspectral cube. Any band count.
Ground truth (--gt)(H, W)Integer class labels. 0 = background (unlabeled).

Indian Pines is bundled with the package — run ghost demo to get the file paths. For other standard datasets (Indian Pines, Pavia University, Salinas Valley), see the GIC group at UPV/EHU.

ENVI, GeoTIFF, and HDF5 files can be converted to .mat using ghost convert_to_mat (v0.1.7+). Native format support without conversion is planned for v0.2.x.