1import torch
2import sys
3from PIL import Image
4from torchvision import transforms
5from transformers import AutoConfig
6from huggingface_hub import snapshot_download
7
8# Helper function to load the model (handles custom code automatically)
9def load_lse_dinov2(repo_name="ashiq24/dinov2-base-lse"):
10 """Load LSE-DINOv2 model from HuggingFace Hub."""
11 import sys
12 from huggingface_hub import snapshot_download
13
14 # Load config with trust_remote_code to download custom code
15 config = AutoConfig.from_pretrained(repo_name, trust_remote_code=True)
16
17 # Download model files to cache and get the directory
18 cache_dir = snapshot_download(repo_id=repo_name, allow_patterns="*.py")
19
20 # Add cache directory to Python path
21 if cache_dir not in sys.path:
22 sys.path.insert(0, cache_dir)
23
24 # Import model class from downloaded files
25 from modeling_lse_dinov2 import LSEDinoV2ForImageClassification
26 return LSEDinoV2ForImageClassification.from_pretrained(repo_name)
27
28# Load model
29model = load_lse_dinov2("ashiq24/dinov2-base-lse")
30model.eval()
31
32# Move to GPU if available
33device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
34model = model.to(device)
35
36# Prepare image with ImageNet preprocessing
37transform = transforms.Compose([
38 transforms.Resize(256, interpolation=transforms.InterpolationMode.BICUBIC),
39 transforms.CenterCrop(224),
40 transforms.ToTensor(),
41 transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
42])
43
44# Load and preprocess image
45image = Image.open("your_image.jpg").convert("RGB")
46pixel_values = transform(image).unsqueeze(0).to(device)
47
48# Run inference
49with torch.no_grad():
50 outputs = model(pixel_values)
51 predicted_class = outputs.logits.argmax(-1).item()
52
53print(f"Predicted class: {predicted_class}")
1pip install torch torchvision timm transformers safetensors
2pip install torchdeq # Optional but Recommended: for DEQ solver, falls back to simple iteration if not installed.
The model learns content-aware local scaling transformations (canonicalization). Here's a visualization showing original images and their deformed versions at different layers:
The model learns content-aware deformation parameters (phi) that can be obtained:
1# Get phi parameters for visualization
2with torch.no_grad():
3 phi_x_list, phi_y_list = model.get_phi_parameters(pixel_values)
4
5for i, (phi_x, phi_y) in enumerate(zip(phi_x_list, phi_y_list)):
6 print(f"Layer {i}: phi_x shape {phi_x.shape}, phi_y shape {phi_y.shape}")
For detailed information about the model architecture, training procedures, and experimental results, please refer to:
1@inproceedings{rahman2025local,
2 title={Local Scale Equivariance with Latent Deep Equilibrium Canonicalizer},
3 author={Rahman, Md Ashiqur and Yang, Chiao-An and Cheng, Michael N and Hao, Lim Jun and Jiang, Jeremiah and Lim, Teck-Yian and Yeh, Raymond A},
4 booktitle={Proceedings of the IEEE/CVF International Conference on Computer Vision},
5 pages={10527--10537},
6 year={2025}
7}