Views
No views yet
| Property | Value |
|---|---|
| Total parameters | 569.6M |
| Architecture | ViT with 2D RoPE + learned positional embeddings |
| Hidden dimension | 1152 |
| Encoder layers | 27 |
| Attention heads | 16 (72 dim per head) |
| KV heads | 16 (full MHA, no GQA) |
| MLP | Gated (gate_proj + up_proj + down_proj) |
| MLP intermediate | 4304 |
| Activation | GELU (pytorch_tanh variant) |
| Normalization | RMSNorm (eps=1e-6) |
| Patch size | 16×16 |
| Pooling | 3×3 kernel (reduces token count by 9×) |
| Position embeddings | Learned 2D table (2, 10240, 1152) + RoPE (theta=100) |
| Q/K norms | Yes |
| Default output tokens | 280 |
| Configurable token budgets | 70, 140, 280, 560, 1120 |
| Input | Pre-patchified: (batch, num_patches, 768) where 768 = 3×16×16 |
| Output | (num_valid_tokens, 1152) after pooling + standardization |
| Gemma 3 Vision | Gemma 4 Vision (this model) | |
|---|---|---|
| Architecture | SigLIP (ViT-SO400M) | Custom ViT with 2D RoPE |
| Layers | 27 | 27 |
| Hidden dim | 1152 | 1152 |
| Position encoding | Learned 1D | Learned 2D + RoPE |
| Attention | Standard | QK-normed |
| MLP | Standard (fc1 + fc2) | Gated (gate + up + down) |
| Aspect ratio | Fixed square (896×896) | Variable aspect ratio |
| Token budget | Fixed 256 | Configurable (70–1120) |
| Pooling | 4×4 average | 3×3 |
| E2B/E4B | 31B (this extraction) | |
|---|---|---|
| Layers | 16 | 27 |
| Parameters | ~340M | 569.6M |
1import torch
2from transformers import Gemma4VisionModel, Gemma4ImageProcessor
3from PIL import Image
4
5# Load vision encoder directly from this repo
6vision_model = Gemma4VisionModel.from_pretrained(
7 "rnagabh/gemma4-vision-encoder",
8 torch_dtype=torch.bfloat16,
9)
10vision_model.to("cuda")
11vision_model.eval()
12
13# Load image processor (saved in this repo)
14image_processor = Gemma4ImageProcessor.from_pretrained("rnagabh/gemma4-vision-encoder")
15
16# Process an image
17img = Image.open("your_image.jpg")
18processed = image_processor(images=[img], return_tensors="pt")
19
20pixel_values = processed["pixel_values"].to(dtype=torch.bfloat16, device="cuda")
21position_ids = processed["image_position_ids"].to(device="cuda")
22tokens_per_image = processed["num_soft_tokens_per_image"] # for splitting batch output
23
24with torch.no_grad():
25 output = vision_model(pixel_values=pixel_values, pixel_position_ids=position_ids)
26 embeddings = output.last_hidden_state # (num_tokens, 1152)
27
28 # Mean-pool for a single image vector
29 image_embedding = embeddings.float().mean(dim=0) # (1152,)Important: Always use theGemma4ImageProcessorincluded in this repo for preprocessing. It handles resizing, patchification, position ID generation, and pixel normalization. Manual patchification without this processor will produce significantly degraded results.
| Metric | Value |
|---|---|
| Linear probe accuracy | 94.0% |
| Random baseline | 10.0% |
| Improvement over chance | 9.4× |
| Dataset | CIFAR-10 test set (1000 samples, 100 per class) |
| Probe | Logistic regression on L2-normalized mean-pooled embeddings |
| File | Description | Size |
|---|---|---|
config.json | Vision encoder config (Gemma4VisionConfig) | <1 KB |
model.safetensors | Vision encoder weights (569.6M params, BF16) | 1,139 MB |
preprocessor_config.json | Image processor config (Gemma4ImageProcessor) | <1 KB |
embed_vision.safetensors | Vision→text embedding projection (1152→5376) | 12.4 MB |
embed_vision projection maps to the 31B's text hidden space (5376-dim).Gemma4ImageProcessor included in this repo for preprocessing. The model expects pre-patchified (B, num_patches, 768) tensors with explicit 2D position IDs — the processor handles this automatically.(num_valid_tokens, 1152). For batched inference, use num_soft_tokens_per_image from the processor to split the output back into per-image embeddings.google/gemma-4-31B-it by downloading only the shard containing vision tower weights (model-00001-of-00002.safetensors)strict=True — perfect match