Views
No views yet
1from transformers import AutoModel, AutoImageProcessor
2from PIL import Image
3import torch
4
5# Load model and processor
6model = AutoModel.from_pretrained("JessicaE/physics-vit-full")
7processor = AutoImageProcessor.from_pretrained("JessicaE/physics-vit-full")
8
9# Load your physics image
10image = Image.open("physics_simulation.png").convert('RGB')
11
12# Apply custom preprocessing
13image = expand_to_square(image, background_color=(128, 128, 128))
14image = image.resize((224, 224), Image.BILINEAR)
15
16# Convert to tensor and add batch dimension
17from torchvision import transforms
18tensor = transforms.ToTensor()(image).unsqueeze(0)
19
20# Extract physics-aware embeddings
21with torch.no_grad():
22 outputs = model(pixel_values=tensor)
23
24 # CLS token embedding (best for classification tasks)
25 cls_embedding = outputs.last_hidden_state[:, 0, :] # Shape: [1, 1280]
26
27 # Average pooled embedding (good for trajectory prediction)
28 pooled_embedding = outputs.last_hidden_state.mean(dim=1) # Shape: [1, 1280]
29
30 # Patch embeddings (for spatial analysis)
31 patch_embeddings = outputs.last_hidden_state[:, 1:, :] # Shape: [1, 196, 1280]
32
33print(f"CLS embedding shape: {cls_embedding.shape}")1from PIL import Image
2
3def expand_to_square(pil_img, background_color):
4 """
5 Pad image to square with background color, keeping image centered.
6
7 REQUIRED for Physics ViT - this preprocessing was used during training.
8 """
9 background_color = tuple(background_color)
10 width, height = pil_img.size
11 if width == height:
12 return pil_img
13 elif width > height:
14 result = Image.new(pil_img.mode, (width, width), background_color)
15 result.paste(pil_img, (0, (width - height) // 2))
16 return result
17 else:
18 result = Image.new(pil_img.mode, (height, height), background_color)
19 result.paste(pil_img, ((height - width) // 2, 0))
20 return resultpip install transformers torch torchvision pillow1@misc{physics-vit-2025,
2 title={PhySiViT : A Physics Simulation Vision Transformer},
3 author={Jessica Ezemba, James Afful, Mei-Yu Wang},
4 year={2025},
5 howpublished={HuggingFace Model Hub},
6 url={https://huggingface.co/JessicaE/physics-vit-full}
7}