Views
No views yet
pip install transformers==4.45.2 bitsandbytes==0.45.2 peft==0.13.21import requests
2import torch
3from PIL import Image
4
5from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration, BitsAndBytesConfig
6from huggingface_hub import login
7
8# Use your HuggingFace API key, since Paligemma is available through user form submission
9login('hf_...')
10
11# load a fine-tuned image captioning model, and corresponding tokenizer and image processor
12model = PaliGemmaForConditionalGeneration.from_pretrained(
13 'google/paligemma-3b-pt-224',
14 quantization_config=BitsAndBytesConfig(
15 load_in_4bit=True,
16 bnb_4bit_quant_type='nf4',
17 bnb_4bit_compute_dtype=torch.bfloat16,
18 ),
19 device_map={'':0}
20)
21model.load_adapter('laicsiifes/paligemma-flickr30k_pt')
22processor = PaliGemmaProcessor.from_pretrained('laicsiifes/paligemma-flickr30k_pt', trust_remote_code=True)
23
24# preprocess an image
25image = Image.open(requests.get("http://images.cocodataset.org/val2014/COCO_val2014_000000458153.jpg", stream=True).raw)
26inputs = processor(
27 text='caption pt\n',
28 images=image,
29 return_tensors='pt'
30).to('cuda:0')
31
32# generate caption
33generated_ids = model.generate(**inputs, max_new_tokens=25)
34prediction = generated_ids[:, inputs['input_ids'].shape[1]:].tolist()
35generated_text = processor.batch_decode(prediction, skip_special_tokens=True)[0]1import matplotlib.pyplot as plt
2
3# plot image with caption
4plt.imshow(image)
5plt.axis("off")
6plt.title(generated_text)
7plt.show()
| Model | #Params | CIDEr | BLEU-4 | ROUGE-L | METEOR | BERTScore | CLIP-Score |
|---|---|---|---|---|---|---|---|
| ViTucano 1B | 1.53B | 78.78 | 26.29 | 46.67 | 50.25 | 73.44 | 55.48 |
| ViTucano 2B | 2.88B | 80.03 | 27.32 | 47.31 | 50.90 | 73.69 | 56.16 |
| PaliGemma | 2.92B | 47.25 | 18.63 | 39.69 | 48.68 | 69.87 | 60.13 |
| Phi-3 V | 4.15B | 75.47 | 27.26 | 47.24 | 48.24 | 73.20 | 55.44 |
| LLaMa 3.2 V | 11.70B | 70.94 | 23.97 | 45.05 | 47.13 | 72.71 | 55.65 |