Views
No views yet


1import torch
2from PIL import Image
3from transformers import AutoProcessor, AutoModelForVision2Seq
4from transformers.image_utils import load_image
5
6DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
7
8# Load images
9image = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
10
11# Initialize processor and model
12processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM-500M-Instruct")
13model = AutoModelForVision2Seq.from_pretrained(
14 "HuggingFaceTB/SmolVLM-500M-Instruct",
15 torch_dtype=torch.bfloat16,
16 _attn_implementation="flash_attention_2" if DEVICE == "cuda" else "eager",
17).to(DEVICE)
18
19# Create input messages
20messages = [
21 {
22 "role": "user",
23 "content": [
24 {"type": "image"},
25 {"type": "text", "text": "Can you describe this image?"}
26 ]
27 },
28]
29
30# Prepare inputs
31prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
32inputs = processor(text=prompt, images=[image], return_tensors="pt")
33inputs = inputs.to(DEVICE)
34
35# Generate outputs
36generated_ids = model.generate(**inputs, max_new_tokens=500)
37generated_texts = processor.batch_decode(
38 generated_ids,
39 skip_special_tokens=True,
40)
41
42print(generated_texts[0])
43"""
44Assistant: The image depicts a cityscape featuring a prominent landmark, the Statue of Liberty, prominently positioned on Liberty Island. The statue is a green, humanoid figure with a crown atop its head and is situated on a small island surrounded by water. The statue is characterized by its large, detailed structure, with a statue of a woman holding a torch above her head and a tablet in her left hand. The statue is surrounded by a small, rocky island, which is partially visible in the foreground.
45In the background, the cityscape is dominated by numerous high-rise buildings, which are densely packed and vary in height. The buildings are primarily made of glass and steel, reflecting the sunlight and creating a bright, urban skyline. The skyline is filled with various architectural styles, including modern skyscrapers and older, more traditional buildings.
46The water surrounding the island is calm, with a few small boats visible, indicating that the area is likely a popular tourist destination. The water is a deep blue, suggesting that it is a large body of water, possibly a river or a large lake.
47In the foreground, there is a small strip of land with trees and grass, which adds a touch of natural beauty to the urban landscape. The trees are green, indicating that it is likely spring or summer.
48The image captures a moment of tranquility and reflection, as the statue and the cityscape come together to create a harmonious and picturesque scene. The statue's presence in the foreground draws attention to the city's grandeur, while the calm water and natural elements in the background provide a sense of peace and serenity.
49In summary, the image showcases the Statue of Liberty, a symbol of freedom and democracy, set against a backdrop of a bustling cityscape. The statue is a prominent and iconic representation of human achievement, while the cityscape is a testament to human ingenuity and progress. The image captures the beauty and complexity of urban life, with the statue serving as a symbol of hope and freedom, while the cityscape provides a glimpse into the modern world.
50"""torch.bfloat16) if your hardware supports it.1from transformers import AutoModelForVision2Seq
2import torch
3
4model = AutoModelForVision2Seq.from_pretrained(
5 "HuggingFaceTB/SmolVLM-Instruct",
6 torch_dtype=torch.bfloat16
7).to("cuda")1from transformers import AutoModelForVision2Seq, BitsAndBytesConfig
2import torch
3
4quantization_config = BitsAndBytesConfig(load_in_8bit=True)
5model = AutoModelForVision2Seq.from_pretrained(
6 "HuggingFaceTB/SmolVLM-Instruct",
7 quantization_config=quantization_config,
8)size={"longest_edge": N*512} when initializing the processor, where N is your desired value. The default N=4 works well, which results in input images of
size 2048×2048. Decreasing N can save GPU memory and is appropriate for lower-resolution images. This is also useful if you want to fine-tune on videos.
1@article{marafioti2025smolvlm,
2 title={SmolVLM: Redefining small and efficient multimodal models},
3 author={Andrés Marafioti and Orr Zohar and Miquel Farré and Merve Noyan and Elie Bakouch and Pedro Cuenca and Cyril Zakka and Loubna Ben Allal and Anton Lozhkov and Nouamane Tazi and Vaibhav Srivastav and Joshua Lochner and Hugo Larcher and Mathieu Morlon and Lewis Tunstall and Leandro von Werra and Thomas Wolf},
4 journal={arXiv preprint arXiv:2504.05299},
5 year={2025}
6}