Instructions to use immanuelpeter/Hy4-Vision with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use immanuelpeter/Hy4-Vision with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="immanuelpeter/Hy4-Vision", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("immanuelpeter/Hy4-Vision", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 1,708 Bytes
a87dfdd | 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 55 56 57 58 59 60 | """Extract Tower, merged, and projected features from a downloaded Hy4 bundle."""
import argparse
from pathlib import Path
import torch
from PIL import Image
from torchvision import transforms
from transformers import AutoModel
BUNDLE = Path(__file__).absolute().parents[1]
RESOLUTION = 512
def preprocess(image: Image.Image) -> torch.Tensor:
crop = 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)),
]
)
return crop(image.convert("RGB"))
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()
device = torch.device(args.device)
dtype = torch.bfloat16 if device.type == "cuda" else torch.float32
model = (
AutoModel.from_pretrained(
BUNDLE, trust_remote_code=True, local_files_only=True, dtype=dtype
)
.to(device)
.eval()
)
with Image.open(args.image) as image:
pixels = preprocess(image).unsqueeze(0).to(device=device, dtype=dtype)
last = model.config.num_hidden_layers
with torch.inference_mode():
hidden, merged, projected = model(pixels, depths=(last,))
print("tower", tuple(hidden[last].shape))
print("merged", tuple(merged.shape))
print("projected", tuple(projected.shape))
if __name__ == "__main__":
main()
|