ybelkada commited on
Commit
9c56758
1 Parent(s): 4c90791

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +156 -0
README.md ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: other
3
+ tags:
4
+ - image-captioning
5
+ inference: false
6
+ languages:
7
+ - en
8
+ license: bsd-3-clause
9
+ datasets:
10
+ - ybelkada/football-dataset
11
+ ---
12
+
13
+ # BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation
14
+
15
+ Model card for image captioning pretrained on COCO dataset - base architecture (with ViT base backbone) - and fine-tuned on
16
+ [football dataset](https://huggingface.co/datasets/ybelkada/football-dataset).
17
+
18
+ | ![BLIP.gif](https://s3.amazonaws.com/moonup/production/uploads/1670928184033-62441d1d9fdefb55a0b7d12c.gif) |
19
+ |:--:|
20
+ | <b> Pull figure from BLIP official repo | Image source: https://github.com/salesforce/BLIP </b>|
21
+
22
+ ## TL;DR
23
+
24
+ Authors from the [paper](https://arxiv.org/abs/2201.12086) write in the abstract:
25
+
26
+ *Vision-Language Pre-training (VLP) has advanced the performance for many vision-language tasks. However, most existing pre-trained models only excel in either understanding-based tasks or generation-based tasks. Furthermore, performance improvement has been largely achieved by scaling up the dataset with noisy image-text pairs collected from the web, which is a suboptimal source of supervision. In this paper, we propose BLIP, a new VLP framework which transfers flexibly to both vision-language understanding and generation tasks. BLIP effectively utilizes the noisy web data by bootstrapping the captions, where a captioner generates synthetic captions and a filter removes the noisy ones. We achieve state-of-the-art results on a wide range of vision-language tasks, such as image-text retrieval (+2.7% in average recall@1), image captioning (+2.8% in CIDEr), and VQA (+1.6% in VQA score). BLIP also demonstrates strong generalization ability when directly transferred to videolanguage tasks in a zero-shot manner. Code, models, and datasets are released.*
27
+
28
+ ## Usage
29
+
30
+ You can use this model for conditional and un-conditional image captioning
31
+
32
+ ### Using the Pytorch model
33
+
34
+ #### Running the model on CPU
35
+
36
+ <details>
37
+ <summary> Click to expand </summary>
38
+
39
+ ```python
40
+ import requests
41
+ from PIL import Image
42
+ from transformers import BlipProcessor, BlipForConditionalGeneration
43
+
44
+ processor = BlipProcessor.from_pretrained("ybelkada/blip-image-captioning-base")
45
+ model = BlipForConditionalGeneration.from_pretrained("ybelkada/blip-image-captioning-base")
46
+
47
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
48
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
49
+
50
+ # conditional image captioning
51
+ text = "a photography of"
52
+ inputs = processor(raw_image, text, return_tensors="pt")
53
+
54
+ out = model.generate(**inputs)
55
+ print(processor.decode(out[0], skip_special_tokens=True))
56
+ # >>> a photography of a woman and her dog
57
+
58
+ # unconditional image captioning
59
+ inputs = processor(raw_image, return_tensors="pt")
60
+
61
+ out = model.generate(**inputs)
62
+ print(processor.decode(out[0], skip_special_tokens=True))
63
+ >>> a woman sitting on the beach with her dog
64
+ ```
65
+ </details>
66
+
67
+ #### Running the model on GPU
68
+
69
+ ##### In full precision
70
+
71
+ <details>
72
+ <summary> Click to expand </summary>
73
+
74
+ ```python
75
+ import requests
76
+ from PIL import Image
77
+ from transformers import BlipProcessor, BlipForConditionalGeneration
78
+
79
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
80
+ model = BlipForConditionalGeneration.from_pretrained("Salesfoce/blip-image-captioning-base").to("cuda")
81
+
82
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
83
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
84
+
85
+ # conditional image captioning
86
+ text = "a photography of"
87
+ inputs = processor(raw_image, text, return_tensors="pt").to("cuda")
88
+
89
+ out = model.generate(**inputs)
90
+ print(processor.decode(out[0], skip_special_tokens=True))
91
+ # >>> a photography of a woman and her dog
92
+
93
+ # unconditional image captioning
94
+ inputs = processor(raw_image, return_tensors="pt").to("cuda")
95
+
96
+ out = model.generate(**inputs)
97
+ print(processor.decode(out[0], skip_special_tokens=True))
98
+ >>> a woman sitting on the beach with her dog
99
+ ```
100
+ </details>
101
+
102
+ ##### In half precision (`float16`)
103
+
104
+ <details>
105
+ <summary> Click to expand </summary>
106
+
107
+ ```python
108
+ import torch
109
+ import requests
110
+ from PIL import Image
111
+ from transformers import BlipProcessor, BlipForConditionalGeneration
112
+
113
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
114
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base", torch_dtype=torch.float16).to("cuda")
115
+
116
+ img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
117
+ raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
118
+
119
+ # conditional image captioning
120
+ text = "a photography of"
121
+ inputs = processor(raw_image, text, return_tensors="pt").to("cuda", torch.float16)
122
+
123
+ out = model.generate(**inputs)
124
+ print(processor.decode(out[0], skip_special_tokens=True))
125
+ # >>> a photography of a woman and her dog
126
+
127
+ # unconditional image captioning
128
+ inputs = processor(raw_image, return_tensors="pt").to("cuda", torch.float16)
129
+
130
+ out = model.generate(**inputs)
131
+ print(processor.decode(out[0], skip_special_tokens=True))
132
+ >>> a woman sitting on the beach with her dog
133
+ ```
134
+ </details>
135
+
136
+ ## BibTex and citation info
137
+
138
+ ```
139
+ @misc{https://doi.org/10.48550/arxiv.2201.12086,
140
+ doi = {10.48550/ARXIV.2201.12086},
141
+
142
+ url = {https://arxiv.org/abs/2201.12086},
143
+
144
+ author = {Li, Junnan and Li, Dongxu and Xiong, Caiming and Hoi, Steven},
145
+
146
+ keywords = {Computer Vision and Pattern Recognition (cs.CV), FOS: Computer and information sciences, FOS: Computer and information sciences},
147
+
148
+ title = {BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation},
149
+
150
+ publisher = {arXiv},
151
+
152
+ year = {2022},
153
+
154
+ copyright = {Creative Commons Attribution 4.0 International}
155
+ }
156
+ ```