Image Feature Extraction
Transformers
Safetensors
English
multilingual
gemma4_vision
feature-extraction
vision
vit
gemma4
google
Eval Results (legacy)
Instructions to use rnagabh/gemma4-vision-encoder with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use rnagabh/gemma4-vision-encoder with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="rnagabh/gemma4-vision-encoder")# Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("rnagabh/gemma4-vision-encoder") model = AutoModel.from_pretrained("rnagabh/gemma4-vision-encoder", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Initial upload: Gemma 4 vision encoder (569.6M, 27-layer ViT with 2D RoPE)
Browse files
README.md
CHANGED
|
@@ -86,16 +86,15 @@ Unlike the audio encoder (which is identical across E2B and E4B), the vision enc
|
|
| 86 |
|
| 87 |
```python
|
| 88 |
import torch
|
| 89 |
-
from transformers import Gemma4VisionModel,
|
| 90 |
-
from safetensors.torch import load_file
|
| 91 |
from PIL import Image
|
| 92 |
|
| 93 |
-
# Load vision encoder
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
vision_model
|
| 99 |
vision_model.eval()
|
| 100 |
|
| 101 |
# Use the parent model's image processor for correct preprocessing
|
|
@@ -113,14 +112,14 @@ tokens_per_image = processed["num_soft_tokens_per_image"] # for splitting batch
|
|
| 113 |
with torch.no_grad():
|
| 114 |
output = vision_model(pixel_values=pixel_values, pixel_position_ids=position_ids)
|
| 115 |
embeddings = output.last_hidden_state # (num_tokens, 1152)
|
| 116 |
-
|
| 117 |
# Mean-pool for a single image vector
|
| 118 |
image_embedding = embeddings.float().mean(dim=0) # (1152,)
|
| 119 |
```
|
| 120 |
|
| 121 |
> **Important:** Always use `Gemma4ImageProcessor` from the parent model for preprocessing.
|
| 122 |
> It handles resizing, patchification, position ID generation, and pixel normalization.
|
| 123 |
-
> Manual patchification without this processor will produce degraded results.
|
| 124 |
|
| 125 |
## Benchmark Results (frozen 1152-dim embeddings, linear probe)
|
| 126 |
|
|
@@ -136,10 +135,6 @@ with torch.no_grad():
|
|
| 136 |
|
| 137 |
Strong performance across all classes: airplane (0.98 F1), ship (0.98 F1), truck (0.97 F1), automobile (0.97 F1). Weakest class is cat (0.86 F1) — a fine-grained category that is inherently harder.
|
| 138 |
|
| 139 |
-
> **Important:** Use the parent model's image processor (`Gemma4ImageProcessor` from `google/gemma-4-31B-it`)
|
| 140 |
-
> for correct preprocessing. Manual patchification without proper resizing and position ID generation
|
| 141 |
-
> will produce significantly degraded results.
|
| 142 |
-
|
| 143 |
## Files in This Repo
|
| 144 |
|
| 145 |
| File | Description | Size |
|
|
@@ -151,9 +146,9 @@ Strong performance across all classes: airplane (0.98 F1), ship (0.98 F1), truck
|
|
| 151 |
## Limitations
|
| 152 |
|
| 153 |
- **End-to-end trained for LLM decoding:** The encoder was trained to produce features for Gemma 4's text decoder. The 1152-dim output is the pure vision representation; the `embed_vision` projection maps to the 31B's text hidden space (5376-dim).
|
| 154 |
-
- **Requires
|
| 155 |
-
- **Variable aspect ratio support:** The 2D position embeddings enable non-square images
|
| 156 |
-
- **
|
| 157 |
|
| 158 |
## Extraction Details
|
| 159 |
|
|
|
|
| 86 |
|
| 87 |
```python
|
| 88 |
import torch
|
| 89 |
+
from transformers import Gemma4VisionModel, AutoProcessor
|
|
|
|
| 90 |
from PIL import Image
|
| 91 |
|
| 92 |
+
# Load vision encoder directly from this repo
|
| 93 |
+
vision_model = Gemma4VisionModel.from_pretrained(
|
| 94 |
+
"rnagabh/gemma4-vision-encoder",
|
| 95 |
+
torch_dtype=torch.bfloat16,
|
| 96 |
+
)
|
| 97 |
+
vision_model.to("cuda")
|
| 98 |
vision_model.eval()
|
| 99 |
|
| 100 |
# Use the parent model's image processor for correct preprocessing
|
|
|
|
| 112 |
with torch.no_grad():
|
| 113 |
output = vision_model(pixel_values=pixel_values, pixel_position_ids=position_ids)
|
| 114 |
embeddings = output.last_hidden_state # (num_tokens, 1152)
|
| 115 |
+
|
| 116 |
# Mean-pool for a single image vector
|
| 117 |
image_embedding = embeddings.float().mean(dim=0) # (1152,)
|
| 118 |
```
|
| 119 |
|
| 120 |
> **Important:** Always use `Gemma4ImageProcessor` from the parent model for preprocessing.
|
| 121 |
> It handles resizing, patchification, position ID generation, and pixel normalization.
|
| 122 |
+
> Manual patchification without this processor will produce significantly degraded results.
|
| 123 |
|
| 124 |
## Benchmark Results (frozen 1152-dim embeddings, linear probe)
|
| 125 |
|
|
|
|
| 135 |
|
| 136 |
Strong performance across all classes: airplane (0.98 F1), ship (0.98 F1), truck (0.97 F1), automobile (0.97 F1). Weakest class is cat (0.86 F1) — a fine-grained category that is inherently harder.
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
## Files in This Repo
|
| 139 |
|
| 140 |
| File | Description | Size |
|
|
|
|
| 146 |
## Limitations
|
| 147 |
|
| 148 |
- **End-to-end trained for LLM decoding:** The encoder was trained to produce features for Gemma 4's text decoder. The 1152-dim output is the pure vision representation; the `embed_vision` projection maps to the 31B's text hidden space (5376-dim).
|
| 149 |
+
- **Requires parent model's image processor:** Use `Gemma4ImageProcessor` from `google/gemma-4-31B-it` for preprocessing. The model expects pre-patchified `(B, num_patches, 768)` tensors with explicit 2D position IDs — the processor handles this automatically.
|
| 150 |
+
- **Variable aspect ratio support:** The 2D position embeddings enable non-square images. The processor generates correct position IDs for any aspect ratio.
|
| 151 |
+
- **Output shape note:** The pooler strips padding and collapses the batch dimension, returning `(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.
|
| 152 |
|
| 153 |
## Extraction Details
|
| 154 |
|