Download example_inference.py from TMayer77/DenseForestViT: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/TMayer77/DenseForestViT/resolve/main/example_inference.py
- Command line
-
hf download hf://TMayer77/DenseForestViT/example_inference.py
-
curl -L -o example_inference.py https://huggingface.co/TMayer77/DenseForestViT/resolve/main/example_inference.py
1.34 kB
| #!/usr/bin/env python3 | |
| """Minimal inference with the published DenseForestViT: download, rebuild from config.json, predict one chip. | |
| Real inputs must be built with stage2_windows.py (32x32 window per modality at native resolution, NaN -> window | |
| median, per-band 0-1 stretch); random numbers are used here only to show shapes.""" | |
| import json, sys, torch | |
| from huggingface_hub import hf_hub_download | |
| REPO = sys.argv[1] if len(sys.argv) > 1 else 'MayerT1/DenseForestViT' # change to your Hub repo id | |
| files = {f: hf_hub_download(REPO, f) for f in ['config.json', 'best_model.pt', 'model_v13.py']} | |
| sys.path.insert(0, str(__import__('pathlib').Path(files['model_v13.py']).parent)) | |
| from model_v13 import DenseForestViT, SUP_R0, SUP_R1 | |
| cfg = json.load(open(files['config.json'])) | |
| model = DenseForestViT(bands_per_cov=cfg['bands_per_cov'], **cfg['model_kwargs']).eval() | |
| model.load_state_dict(torch.load(files['best_model.pt'], map_location='cpu', weights_only=False), strict=True) | |
| chip = {m: torch.rand(1, c, 32, 32) for m, c in cfg['bands_per_cov'].items()} # replace with stage2_windows.chip(...) | |
| with torch.no_grad(): out = model(chip, return_waveform=False) | |
| for t in ['rh', 'pai', 'fhd']: # the trained output is the centre 3x3 (30 m) under the chip centre | |
| print(t, round(float(out[t][0, SUP_R0:SUP_R1, SUP_R0:SUP_R1].mean()), 3)) | |