Image Feature Extraction
Transformers
Safetensors
pixtral_large4_vision
feature-extraction
vision
custom_code
Instructions to use immanuelpeter/PixtralViT-Large-4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use immanuelpeter/PixtralViT-Large-4 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="immanuelpeter/PixtralViT-Large-4", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("immanuelpeter/PixtralViT-Large-4", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
House-template card, fixed 448 px example, normalized manifest with file SHA256; weights unchanged
0f0835a verified Download examples/inference.py from immanuelpeter/PixtralViT-Large-4: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/immanuelpeter/PixtralViT-Large-4/resolve/main/examples/inference.py
- Command line
-
hf download hf://immanuelpeter/PixtralViT-Large-4/examples/inference.py
-
curl -L -o inference.py https://huggingface.co/immanuelpeter/PixtralViT-Large-4/resolve/main/examples/inference.py
2.21 kB
| import argparse | |
| from pathlib import Path | |
| import sys | |
| from PIL import Image | |
| import torch | |
| from transformers import AutoImageProcessor, AutoModel | |
| sys.path.insert(0, str(Path(__file__).absolute().parents[1])) | |
| from projector import load_projector, merged_and_projected | |
| MODEL_ID = "immanuelpeter/PixtralViT-Large-4" | |
| def square(image: Image.Image, side: int) -> Image.Image: | |
| edge = min(image.size) | |
| left, top = (image.width - edge) // 2, (image.height - edge) // 2 | |
| return image.crop((left, top, left + edge, top + edge)).resize((side, side), Image.BICUBIC) | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Extract Mistral Large 4 visual features.") | |
| parser.add_argument("image", type=Path) | |
| parser.add_argument("--model", default=MODEL_ID) | |
| parser.add_argument("--size", type=int, default=448, help="square side in pixels, a multiple of 28") | |
| args = parser.parse_args() | |
| if args.size % 28: | |
| raise SystemExit("--size must be a multiple of 28") | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| dtype = torch.bfloat16 if device.type == "cuda" else torch.float32 | |
| # trust_remote_code runs modeling_pixtral_large4.py, the parent's BF16 numeric order. | |
| tower = AutoModel.from_pretrained(args.model, dtype=dtype, trust_remote_code=True).to(device).eval() | |
| processor = AutoImageProcessor.from_pretrained(args.model) | |
| projector = load_projector(args.model).to(device=device, dtype=dtype).eval() | |
| image = square(Image.open(args.image).convert("RGB"), args.size) | |
| inputs = processor(images=image, size={"longest_edge": args.size}, return_tensors="pt") | |
| image_sizes = inputs["image_sizes"].tolist() | |
| with torch.inference_mode(): | |
| pixels = inputs["pixel_values"].to(device, dtype) | |
| tower_tokens = tower(pixels, image_sizes=image_sizes).last_hidden_state.squeeze(0) | |
| merged, projected = merged_and_projected(projector, tower_tokens, image_sizes) | |
| print("device", device, "dtype", dtype, "image_sizes", image_sizes) | |
| print("tower", tuple(tower_tokens.shape)) | |
| print("merged", tuple(merged.shape)) | |
| print("projected", tuple(projected.shape)) | |
| if __name__ == "__main__": | |
| main() | |