Views
No views yet
vit_large_patch16_384 classifier1import json, torch, timm
2from huggingface_hub import hf_hub_download
3from timm.data.transforms_factory import create_transform
4from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
5from PIL import Image
6
7REPO = "rexologue/vit_large_384_for_trees"
8MODEL_NAME = "vit_large_patch16_384"
9DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
10
11# 1) labels
12labels_path = hf_hub_download(REPO, filename="labels.json")
13with open(labels_path, "r", encoding="utf-8") as f:
14 raw = json.load(f)
15labels = [raw[str(i)] for i in range(len(raw))] if isinstance(raw, dict) else list(raw)
16
17# 2) weights
18ckpt_path = hf_hub_download(REPO, filename="pytorch_model.bin")
19state = torch.load(ckpt_path, map_location="cpu")
20if any(k.startswith("module.") for k in state): # DDP fix
21 state = {k.replace("module.", "", 1): v for k, v in state.items()}
22
23# 3) model
24model = timm.create_model(MODEL_NAME, num_classes=len(labels), pretrained=False)
25model.load_state_dict(state, strict=True)
26model.to(DEVICE).eval()
27
28# 4) preprocessing (ViT-L/16 @ 384 w/ ImageNet mean/std + bicubic)
29transform = create_transform(
30 input_size=(3, 384, 384),
31 interpolation="bicubic",
32 mean=IMAGENET_DEFAULT_MEAN,
33 std=IMAGENET_DEFAULT_STD,
34)
35
36# 5) run
37img = Image.open("your_image.jpg").convert("RGB")
38x = transform(img).unsqueeze(0).to(DEVICE)
39with torch.no_grad():
40 logits = model(x)
41probs = torch.softmax(logits, dim=1)[0].cpu()
42topk = probs.topk(k=min(5, len(labels)))
43print([(labels[i], float(probs[i])) for i in topk.indices])