Views
No views yet
| Cider | SacreBLEU | Rouge-L|
|---------|------------|--------|
| 78.04 | 11.71 | 25.76 |pip install git+https://github.com/michelecafagna26/CLIPCap.git1git lfs install # if not installed
2git clone https://huggingface.co/michelecafagna26/clipcap-base-captioning-ft-hl-rationales1from clipcap import ClipCaptionModel
2from transformers import (
3 GPT2Tokenizer,
4 GPT2LMHeadModel,
5)
6import torch
7import clip
8import requests
9from PIL import Image
10
11model_path = "clipcap-base-captioning-ft-hl-rationales/pytorch_model.pt" # change accordingly
12
13# load clip
14device = "cuda" if torch.cuda.is_available() else "cpu"
15clip_model, preprocess = clip.load("ViT-B/32", device=device, jit=False)
16tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
17prefix_length = 10
18
19# load ClipCap
20model = ClipCaptionModel(prefix_length, tokenizer=tokenizer)
21model.from_pretrained(model_path)
22model = model.eval()
23model = model.to(device)
24
25# load the image
26img_url = 'https://datasets-server.huggingface.co/assets/michelecafagna26/hl/--/default/train/0/image/image.jpg'
27raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
28
29
30# extract the prefix
31image = preprocess(raw_image).unsqueeze(0).to(device)
32with torch.no_grad():
33 prefix = clip_model.encode_image(image).to(
34 device, dtype=torch.float32
35 )
36 prefix_embed = model.clip_project(prefix).reshape(1, prefix_length, -1)
37
38# generate the caption
39model.generate_beam(embed=prefix_embed)[0]
40
41
42# >> "she is posing for a photo."1@inproceedings{cafagna2023hl,
2 title={{HL} {D}ataset: {V}isually-grounded {D}escription of {S}cenes, {A}ctions and
3{R}ationales},
4 author={Cafagna, Michele and van Deemter, Kees and Gatt, Albert},
5 booktitle={Proceedings of the 16th International Natural Language Generation Conference (INLG'23)},
6address = {Prague, Czech Republic},
7 year={2023}
8}