Views
No views yet
[!IMPORTANT] Transformers Version Compatibility:
- ✅
transformers==4.57.3(Recommended): Works withAutoModel.from_pretrained()- ⚠️
transformers>=5.0.0: Not currently supported. We are actively working on a fix.
Note: This model supports native resolution input. For optimal performance:
- Image: 448×448 resolution (pre-trained)
- Video: 224×224 resolution with 256 tokens per frame (pre-trained)
1from transformers import AutoModel, AutoImageProcessor
2from PIL import Image
3import torch
4
5# Load model and preprocessor
6model = AutoModel.from_pretrained(
7 "lmms-lab-encoder/onevision-encoder-large",
8 trust_remote_code=True,
9 attn_implementation="flash_attention_2"
10).to("cuda").eval()
11
12preprocessor = AutoImageProcessor.from_pretrained(
13 "lmms-lab-encoder/onevision-encoder-large",
14 trust_remote_code=True
15)
16
17# Image inference: [B, C, H, W]
18image = Image.open("path/to/your/image.jpg") # Replace with your image path
19pixel_values = preprocessor(images=image, return_tensors="pt")["pixel_values"].to("cuda")
20with torch.no_grad():
21 outputs = model(pixel_values)
22 # outputs.last_hidden_state: [B, num_patches, hidden_size]
23 # outputs.pooler_output: [B, hidden_size]
24
25# Video inference: [B, C, T, H, W] with patch_positions
26num_frames, target_frames = 16, 64
27patch_size = 14
28# Load video frames and preprocess each frame (replace with your video frame paths)
29frames = [Image.open(f"path/to/frame_{i}.jpg") for i in range(num_frames)]
30video_pixel_values = preprocessor(images=frames, return_tensors="pt")["pixel_values"]
31# Reshape from [T, C, H, W] to [B, C, T, H, W]
32video = video_pixel_values.unsqueeze(0).permute(0, 2, 1, 3, 4).to("cuda")
33
34# Build patch_positions for temporal sampling: [B, num_frames * frame_tokens, 3]
35frame_pos = torch.linspace(0, target_frames - 1, num_frames).long().cuda() # [T]
36grid_h, grid_w = video.shape[-2] // patch_size, video.shape[-1] // patch_size # patch grid
37frame_tokens = grid_h * grid_w
38
39t_positions = frame_pos[:, None].repeat(1, frame_tokens).reshape(-1) # [T * frame_tokens]
40h_positions = torch.arange(grid_h, device="cuda").repeat_interleave(grid_w)
41h_positions = h_positions.repeat(num_frames) # [T * frame_tokens]
42w_positions = torch.arange(grid_w, device="cuda").repeat(grid_h)
43w_positions = w_positions.repeat(num_frames) # [T * frame_tokens]
44
45patch_positions = torch.stack([t_positions, h_positions, w_positions], dim=-1).unsqueeze(0)
46# patch_positions example (256 tokens per frame, 16x16 patch grid):
47# Each row is [t, h, w].
48# First 4 patches of frame 0 (t=0):
49# patch_positions[0, 0:4, :] -> [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3]]
50# First 4 patches of frame 1 (t=4):
51# patch_positions[0, 256:260, :] -> [[4, 0, 0], [4, 0, 1], [4, 0, 2], [4, 0, 3]]
52
53with torch.no_grad():
54 outputs = model(video, patch_positions=patch_positions)1git clone https://github.com/EvolvingLMMs-Lab/OneVision-Encoder.git
2cd OneVision-Encoder
3pip install -e .1from onevision_encoder import OneVisionEncoderModel, OneVisionEncoderConfig
2from transformers import AutoImageProcessor
3model = OneVisionEncoderModel.from_pretrained(
4 "lmms-lab-encoder/onevision-encoder-large",
5 trust_remote_code=True,
6 attn_implementation="flash_attention_2"
7).to("cuda").eval()
8preprocessor = AutoImageProcessor.from_pretrained(
9 "lmms-lab-encoder/onevision-encoder-large",
10 trust_remote_code=True
11)
| Property | Value |
|---|---|
| Model Type | Vision Transformer (ViT) |
| Architecture | HEVC-Style Vision Transformer |
| Hidden Size | 1024 |
| Intermediate Size | 4096 |
| Number of Layers | 24 |
| Number of Attention Heads | 16 |
| Patch Size | 14 |
| Image Resolution | 448×448 (pre-trained) |
| Video Resolution | 224×224 with 256 tokens per frame |
| Positional Encoding | 3D RoPE (4:6:6 split for T:H:W) |
| Normalization | Layer Normalization |
| Activation Function | GELU |
| License | Apache 2.0 |