Image Feature Extraction
Transformers
Safetensors
hy4_flash_vision
feature-extraction
vision
siglip2
custom_code
Instructions to use immanuelpeter/Hy4-Flash-Vision with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use immanuelpeter/Hy4-Flash-Vision with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="immanuelpeter/Hy4-Flash-Vision", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("immanuelpeter/Hy4-Flash-Vision", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 2,083 Bytes
80c7633 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | """Extract Hy4 flash Tower, merged and projected features from this downloaded bundle."""
import argparse
from pathlib import Path
import runpy
from PIL import Image
import torch
from torchvision import transforms
BUNDLE = Path(__file__).absolute().parents[1]
RESOLUTION, PATCH_SIZE = 512, 16
def preprocess(image: Image.Image) -> torch.Tensor:
"""Short-side bicubic resize and centre crop to 512 px, then mean and std 0.5."""
return transforms.Compose([
transforms.Resize(RESOLUTION, interpolation=transforms.InterpolationMode.BICUBIC),
transforms.CenterCrop(RESOLUTION),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])(image.convert("RGB"))
def features(model, pixels: torch.Tensor) -> dict[str, torch.Tensor]:
"""The last block (no final norm), the RMSNorm output grouped 2x2, and the Projector without newlines."""
grid = (pixels.shape[-2] // PATCH_SIZE, pixels.shape[-1] // PATCH_SIZE)
tower = model(pixels).last_hidden_state
normed = model.perceive.norm(tower)
return {
"tower": tower,
"merged": model.perceive.merge(normed, grid),
"projected": model.perceive.project(normed, grid).flatten(2).transpose(1, 2),
}
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("image", type=Path)
parser.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")
args = parser.parse_args()
dtype = torch.bfloat16 if args.device.startswith("cuda") else torch.float32
modeling = runpy.run_path(str(BUNDLE / "modeling_hy4_flash_vision.py"))
model = modeling["Hy4FlashVisionModel"].from_pretrained(BUNDLE, dtype=dtype, local_files_only=True)
model = model.to(args.device).eval()
with Image.open(args.image) as image:
pixels = preprocess(image)[None].to(args.device, dtype)
with torch.inference_mode():
for name, tokens in features(model, pixels).items():
print(name, tuple(tokens.shape))
if __name__ == "__main__":
main()
|