# datapipeline — build a training dataset from a raster + glacier shapefile Turns a big multi-band GeoTIFF/VRT and the rock-glacier shapefile into train/val/test **patches** ready for `train.py` / `run_experiments.py`, at any patch size you choose. > This code is **tracked in git** (unlike the old `all_data/` build scripts, > which were git-ignored and got lost). The *data* it produces is git-ignored. ## Inputs - **raster**: `35_band/band_feature/` (a folder — auto-finds `data_10m.vrt`/`data_10m.tif`), or any `.tif`/`.vrt` file. - **shapefile**: `Final_merged01/Final_merged01.shp`. ## Run ```bash # 128 px patches (recommended for the 10 m data), output to all_data/ python datapipeline/run_pipeline.py --patch 128 # 64 px instead, to a separate folder so both sizes coexist python datapipeline/run_pipeline.py --patch 64 --out all_data_64 ``` Resolution is auto-detected from the raster, so the same command works for 10 m or 30 m data. ## What it does (4 steps) ### 1. build_crops (The Algorithm) This step extracts the initial feature+mask windows. It works as follows to guarantee alignment and prevent label noise: - **Master Mask Creation**: A single massive "master mask" is rasterized covering the entire grid. *Every* glacier polygon is burned in as a `1` (`all_touched=True` ensures any pixel touched by a polygon is included). - **Bounding Box**: For each glacier, it finds its exact spatial bounding box. - **Padding**: It adds a context buffer (`padding_m`, default 200m) symmetrically to all four sides. - **Expansion**: It checks the pixel dimensions of this padded box. If it's smaller than `patch_size` (e.g., 128 px), it expands the box *symmetrically* until it reaches the minimum patch size. This guarantees the target glacier remains centered. - **Cropping**: It slices this pixel window out of the feature raster AND the master mask. *Crucially, because it slices from the master mask, any neighboring glaciers that happen to fall within this expanded window are automatically captured in the patch mask, even if they don't touch the target glacier.* - **Backgrounds**: Finally, it randomly samples pure-background crops (touching no glaciers) to form ~10% of the dataset. ### 2. split_dataset 70/15/15 split **by crop** (overlapping patches never leak across splits). 3. **extract_patches** — tiles crops into `patch×patch` `.npy`; imputes NaN/no-data with the per-band **train** mean; writes `norm_stats.json`; feature & mask are padded **identically** (no contradictory labels). 4. **verify_dataset** — checks counts, shapes, no NaN, binary masks, that masks mark a spectrally/morphologically *distinct* region, and saves overlay PNGs. ## Output layout (under `--out`, default `all_data/`) ``` features/ masks/ # per-crop GeoTIFFs (intermediate) dataset/{train,val,test}/{features,masks}/ # split crops patches/{train,val,test}/{features,masks}/ # PxP .npy patches <- training reads these patches/metadata/ norm_stats.json # per-band train mean/std (+ band_names) patch_metadata.csv diagnostics_report.txt verify_report.txt overlays/ ``` ## Then train / sweep ```bash python run_experiments.py --gpu --data-dir all_data/patches ``` `train.py` / `evaluate.py` auto-detect the patch size from the data, so nothing else needs changing between 64 px and 128 px. ```