Spaces:
Running
Running
Download workflow/scripts/run_site2_external_validation.sh from lauransotomayor/hyperFVC: direct link, hf CLI and curl.
- Browser
- Download file 4.32 kB
-
https://huggingface.co/spaces/lauransotomayor/hyperFVC/resolve/main/workflow/scripts/run_site2_external_validation.sh
- Command line
-
hf download hf://spaces/lauransotomayor/hyperFVC/workflow/scripts/run_site2_external_validation.sh
-
curl -L -o run_site2_external_validation.sh https://huggingface.co/spaces/lauransotomayor/hyperFVC/resolve/main/workflow/scripts/run_site2_external_validation.sh
4.32 kB
| # ============================================================================= | |
| # Site-2 (SASMDD0013) external validation of cross-site transfer. | |
| # | |
| # Runs the FROZEN site-1 checkpoint on the externally annotated site-2 tiles | |
| # (no fine-tuning, no re-tuning) and computes quantitative per-class | |
| # precision/recall/F1/IoU + MIoU against the independent reference masks. | |
| # | |
| # Prerequisites: | |
| # 1. Site-2 predictor tiles for the selected external tiles (ENVI .dat) | |
| # 2. External reference masks on the SAME grid, named | |
| # mask_tiles_hyperspectral_<id>.tif, ignore value -1 | |
| # 3. A site-1 trained checkpoint (.pth) | |
| # | |
| # Usage: | |
| # IMAGE_DIR=... MASK_DIR=... CHECKPOINT_PATH=... \ | |
| # bash scripts/run_site2_external_validation.sh | |
| # ============================================================================= | |
| set -euo pipefail | |
| ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | |
| ENV_PREFIX="${ENV_PREFIX:-/home/laura/.local/share/mamba/envs/fvc_function}" | |
| ENV_PYTHON="${ENV_PYTHON:-${ENV_PREFIX}/bin/python}" | |
| MODEL_DIR="${ROOT}/phase_3_models/unet_site_models" | |
| IMAGE_DIR="${IMAGE_DIR:?Set IMAGE_DIR to the site-2 predictor tile directory}" | |
| MASK_DIR="${MASK_DIR:?Set MASK_DIR to the external reference mask directory}" | |
| CHECKPOINT_PATH="${CHECKPOINT_PATH:?Set CHECKPOINT_PATH to the frozen site-1 .pth checkpoint}" | |
| PRED_DIR="${PRED_DIR:-${MASK_DIR%/}_predictions_external}" | |
| TILE_LIST="${TILE_LIST:-}" # optional selected_external_tiles.txt to restrict tiles | |
| SENSOR_PROFILE="${SENSOR_PROFILE:-fx10_fx17_215_trim_aug_spec_transformer_weights_s152}" | |
| CHANNELS="${CHANNELS:-296}" | |
| TILE_SIZE="${TILE_SIZE:-152}" | |
| CLASS_LABELS="${CLASS_LABELS:-BE:0,NPV:1,PV:2}" | |
| OUTPUT_CSV="${OUTPUT_CSV:-${PRED_DIR}/external_validation_metrics.csv}" | |
| export LD_PRELOAD="${ENV_PREFIX}/lib/libstdc++.so.6 ${ENV_PREFIX}/lib/libjpeg.so.8${LD_PRELOAD:+ ${LD_PRELOAD}}" | |
| export PYTHONPATH="${MODEL_DIR}:${ROOT}${PYTHONPATH:+:${PYTHONPATH}}" | |
| for d in "${IMAGE_DIR}" "${MASK_DIR}"; do | |
| [[ -d "$d" ]] || { echo "Missing directory: $d" >&2; exit 1; } | |
| done | |
| [[ -f "${CHECKPOINT_PATH}" ]] || { echo "Missing checkpoint: ${CHECKPOINT_PATH}" >&2; exit 1; } | |
| mkdir -p "${PRED_DIR}" | |
| shopt -s nullglob | |
| tiles=("${IMAGE_DIR}"/*.dat) | |
| shopt -u nullglob | |
| [[ ${#tiles[@]} -gt 0 ]] || { echo "No .dat tiles in ${IMAGE_DIR}" >&2; exit 1; } | |
| echo "External validation: ${#tiles[@]} candidate tiles" | |
| echo " checkpoint: ${CHECKPOINT_PATH}" | |
| echo " predictions -> ${PRED_DIR}" | |
| for tile in "${tiles[@]}"; do | |
| base="$(basename "${tile%.*}")" | |
| if [[ -n "${TILE_LIST}" ]] && ! grep -qxF "$(basename "${tile}")" "${TILE_LIST}"; then | |
| continue | |
| fi | |
| out="${PRED_DIR}/${base}_prediction.tif" | |
| if [[ -f "${out}" ]]; then | |
| "${ENV_PYTHON}" - "${out}" "${CHECKPOINT_PATH}" "${tile}" "${TILE_SIZE}" <<'PY' | |
| import sys | |
| from pathlib import Path | |
| import rasterio | |
| output, checkpoint, source, tile_size = sys.argv[1:] | |
| with rasterio.open(output) as dataset: | |
| tags = dataset.tags() | |
| if (Path(tags.get("checkpoint", "")).resolve() != Path(checkpoint).resolve() | |
| or Path(tags.get("source_raster", "")).resolve() != Path(source).resolve() | |
| or tags.get("inference_tile_size") != tile_size): | |
| raise ValueError(f"Existing prediction has different or missing provenance: {output}. Use a fresh PRED_DIR.") | |
| PY | |
| echo " [skip] ${base} (prediction exists)" | |
| continue | |
| fi | |
| echo " [infer] ${base}" | |
| env \ | |
| SENSOR_PROFILE="${SENSOR_PROFILE}" \ | |
| SPECTRAL_EXPERIMENT_MODE="${SPECTRAL_EXPERIMENT_MODE:-enhanced}" \ | |
| USE_SPECTRAL_TRANSFORMER="${USE_SPECTRAL_TRANSFORMER:-1}" \ | |
| FVC_DETECTED_CHANNELS="${CHANNELS}" \ | |
| FVC_OUT_CHANNELS=3 \ | |
| BAND_OPTION=all \ | |
| PATCH_SIZE="${TILE_SIZE}" \ | |
| INFERENCE_TILE_SIZE="${TILE_SIZE}" \ | |
| INFERENCE_BACKEND=pytorch \ | |
| USE_INT16_INPUT_STORAGE="${USE_INT16_INPUT_STORAGE:-1}" \ | |
| "${ENV_PYTHON}" "${MODEL_DIR}/inference_FVCmapping_streaming.py" \ | |
| --input-raster "${tile}" \ | |
| --output-mask "${out}" \ | |
| --checkpoint-path "${CHECKPOINT_PATH}" \ | |
| --no-output-cog \ | |
| --tile-size "${TILE_SIZE}" | |
| done | |
| echo | |
| echo "Running quantitative evaluation..." | |
| env \ | |
| PRED_DIR="${PRED_DIR}" \ | |
| MASK_DIR="${MASK_DIR}" \ | |
| CLASS_LABELS="${CLASS_LABELS}" \ | |
| OUTPUT_CSV="${OUTPUT_CSV}" \ | |
| "${ENV_PYTHON}" "${MODEL_DIR}/metrics/evaluate_external_validation.py" | |