Views
No views yet
pip install torch transformers safetensors huggingface_hub datasets pillow1from pathlib import Path
2import importlib.util
3import torch
4from datasets import load_dataset
5from huggingface_hub import snapshot_download
6from PIL import Image
7REPO_ID = "EPFL-ECEO/coralscapes-vit-b-dpt" # replace if needed
8
9# 1) Download model repo snapshot
10root = Path(snapshot_download(REPO_ID))
11
12# 2) Load self-contained model code from the repo
13spec = importlib.util.spec_from_file_location("coralscapes_hub_model", root / "coralscapes_hub_model.py")
14mod = importlib.util.module_from_spec(spec)
15spec.loader.exec_module(mod)
16
17# 3) Build model + load weights
18device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19model = mod.Dinov3DPTSegmenter.from_pretrained(root, map_location=device).eval()
20
21# 4) Load one test image from HF dataset
22ds = load_dataset("EPFL-ECEO/coralscapes", split="test")
23image = ds[42]["image"].convert("RGB") # PIL image
24image = image.resize((1376, 768), resample=Image.BILINEAR) # (W, H), divisible by 16, agnostic to aspect ratio
25
26# 5) Preprocess + inference
27batch = model.processor(images=image, return_tensors="pt", do_resize=False)["pixel_values"].to(device)
28with torch.no_grad():
29 logits = model(batch) # shape [1, C, H, W]
30pred = logits.argmax(dim=1)[0].cpu() # shape [H, W], class IDs1@inproceedings{sauder2025coralscapes,
2 title={The Coralscapes Dataset: Semantic Scene Understanding in Coral Reefs},
3 author={Sauder, Jonathan and Domazetoski, Viktor and Banc-Prandi, Guilhem and Perna, Gabriela and Meibom, Anders and Tuia, Devis},
4 booktitle={Proceedings of the IEEE/CVF International Conference on Computer Vision: Joint Workshop on Marine Vision},
5 pages={2115--2122},
6 year={2025}
7}