Views
No views yet
uv:uv add canvit-pytorchpip:pip install canvit-pytorch1from canvit_pytorch import CanViTForPretrainingHFHub, Viewpoint, sample_at_viewpoint
2from canvit_pytorch.preprocess import preprocess
3from PIL import Image
4import torch
5
6# CanViT is integrated with the HuggingFace Hub.
7model = CanViTForPretrainingHFHub.from_pretrained(
8 "canvit/canvitb16-add-vpe-pretrain-g128px-s512px-in21k-dv3b16-2026-02-02"
9).eval()
10
11# Replace with the image of your choice
12image = Image.open("test_data/Cat03.jpg").convert("RGB")
13image = preprocess(512)(image)
14image = image.unsqueeze(0) # [1, 3, 512, 512]
15
16# CanViT is a recurrent model.
17state = model.init_state(batch_size=1, canvas_grid_size=32)
18
19# Let's process a first glimpse: centered, zoomed-out.
20with torch.inference_mode():
21 vp = Viewpoint.full_scene(batch_size=1, device=image.device)
22 glimpse = sample_at_viewpoint(spatial=image, viewpoint=vp, glimpse_size_px=128)
23 out = model(glimpse=glimpse, state=state, viewpoint=vp)
24
25# Inspect the canvas structure
26# The canvas contains the model's working understanding of the scene
27canvas_spatial = model.get_spatial(out.state.canvas) # [1, 1024, 1024]
28canvas_spatial = canvas_spatial.unflatten(1, (32, 32)) # [1, 32, 32, 1024] — spatial feature map
29print(out.state.recurrent_cls.shape) # [1, 1, 768] — global CLS token1@article{berreby2026canvit,
2 title={CanViT: Toward Active-Vision Foundation Models},
3 author={Berreby, Yoha{\"i}-Eliel and Du, Sabrina and Durand, Audrey and Krishna, B. Suresh},
4 year={2026},
5 eprint={2603.22570},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV},
8 url={https://arxiv.org/abs/2603.22570}
9}