Views
No views yet
d_model-dim image token, which
is prepended to the Transformer encoder's input sequence. The Transformer
decoder then forecasts daily streamflow over the next 30 days.kdahal/vitg-streamflow-arizona,
which uses pre-computed ViT embeddings instead of learning visual features
from scratch.1from huggingface_hub import snapshot_download
2import sys, json, torch
3from safetensors.torch import load_file
4
5local = snapshot_download(repo_id="kdahal/cnn-streamflow-arizona")
6sys.path.insert(0, local)
7
8from model_code import Model
9
10cfg = json.load(open(f"{local}/config.json"))
11model = Model(
12 enc_in=cfg["enc_in"], dec_in=cfg["dec_in"], c_out=cfg["c_out"], out_len=cfg["out_len"],
13 d_model=cfg["d_model"], n_heads=cfg["n_heads"],
14 e_layers=cfg["e_layers"], d_layers=cfg["d_layers"], d_ff=cfg["d_ff"],
15 dropout=cfg["dropout"], embed=cfg["embed"], freq=cfg["freq"],
16 activation=cfg["activation"], output_attention=cfg["output_attention"],
17 mix=cfg["mix"], backbone=cfg["backbone"],
18)
19model.load_state_dict(load_file(f"{local}/model.safetensors"))
20model.eval()1pip install -r requirements.txt
2python inference.py| Name | Shape | Dtype | Meaning |
|---|---|---|---|
x_enc | (B, 365, 250) | float32 | Past 365 days of normalized meteorological + static features |
x_mark_enc | (B, 365, 4) | float32 | Time marks (unused; pass zeros) |
x_dec | (B, 180+30, 1) | float32 | Decoder seed |
x_mark_dec | (B, 180+30, 4) | float32 | Decoder time marks |
x_img | (B, 4, 224, 224) | float32 | Channels: (NDVI, EVI, NDSI, SRTM) catchment-centered tile |
(B, 30, 1) — 30-day-ahead streamflow (normalized units).x_img ──► ResNet18 (in_chans=4, num_classes=0) ─► Linear(512→d_model) ─► img_token ┐
▼
x_enc ──► enc_embedding ─► [img_token | enc_seq] ──► encoder ─► decoder ─► predd_model=d_ff=256, GELUlocked_stats.json and identical to the file used
for the TNFR baseline (the CNN consumes the same 250-dim meteorology +
static-attribute tensor as TNFR, plus the raw raster x_img).1import json, numpy as np
2stats = json.load(open("locked_stats.json"))
3site = "09379025"
4means = stats["basin_dynamic_stats"][site]["means"]
5stds = stats["basin_dynamic_stats"][site]["stds"]
6
7# apply to a daily dataframe `df` with columns matching feature names
8for col in means:
9 df[col] = (df[col] - means[col]) / (stds[col] + 1e-6)
10
11# un-normalize streamflow output
12mean_q, std_q = means["streamflow"], stds["streamflow"]
13y_real = np.maximum(y_pred.numpy() * std_q + mean_q, 0.0)locked_stats.json:basin_dynamic_stats: per-basin means/stds for 40 dynamic featuresglobal_static_stats: static catchment-attribute means/stdsstatic_data_lookup: per-basin raw static attribute valuesx_img: the raster stack (NDVI, EVI, NDSI, SRTM) is not
normalized through locked_stats.json — each channel was pre-scaled in the
tile-preparation pipeline. Pass the raster in the same convention you produce
your tiles; the ResNet18 is trained to be robust to the float ranges used.model.safetensors — weights (state_dict)config.json — constructor kwargs (includes img_channels, img_size, backbone)locked_stats.json — frozen per-basin normalization statistics (315 Arizona basins, 40 dynamic features)model_code/ — self-contained PyTorch source (+ timm required for ResNet18)inference.py — end-to-end demokdahal/tnfr-streamflow-arizona — time-series-only baselinekdahal/vitg-streamflow-arizona — ViT-gated variant using pre-computed satellite embeddings