PureMedia commited on
Commit
4f66d9b
·
verified ·
1 Parent(s): 2f3b0f5

Upload workspace/ComfyUI/models/LLM/Florence-2-large/README.md with huggingface_hub

Browse files
workspace/ComfyUI/models/LLM/Florence-2-large/README.md ADDED
@@ -0,0 +1,302 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ license_link: https://huggingface.co/microsoft/Florence-2-large/resolve/main/LICENSE
4
+ pipeline_tag: image-text-to-text
5
+ tags:
6
+ - vision
7
+ ---
8
+
9
+ # Florence-2: Advancing a Unified Representation for a Variety of Vision Tasks
10
+
11
+ ## Model Summary
12
+
13
+ **This is a continued pretrained version of Florence-2-large model with 4k context length, only 0.1B samples are used for continue pretraining, thus it might not be trained well. In addition, OCR task has been updated with line separator ('\n'). COCO OD AP 39.8**
14
+
15
+ This Hub repository contains a HuggingFace's `transformers` implementation of Florence-2 model from Microsoft.
16
+
17
+ Florence-2 is an advanced vision foundation model that uses a prompt-based approach to handle a wide range of vision and vision-language tasks. Florence-2 can interpret simple text prompts to perform tasks like captioning, object detection, and segmentation. It leverages our FLD-5B dataset, containing 5.4 billion annotations across 126 million images, to master multi-task learning. The model's sequence-to-sequence architecture enables it to excel in both zero-shot and fine-tuned settings, proving to be a competitive vision foundation model.
18
+
19
+ Resources and Technical Documentation:
20
+ + [Florence-2 technical report](https://arxiv.org/abs/2311.06242).
21
+ + [Jupyter Notebook for inference and visualization of Florence-2-large](https://huggingface.co/microsoft/Florence-2-large/blob/main/sample_inference.ipynb)
22
+
23
+ | Model | Model size | Model Description |
24
+ | ------- | ------------- | ------------- |
25
+ | Florence-2-base[[HF]](https://huggingface.co/microsoft/Florence-2-base) | 0.23B | Pretrained model with FLD-5B
26
+ | Florence-2-large[[HF]](https://huggingface.co/microsoft/Florence-2-large) | 0.77B | Pretrained model with FLD-5B
27
+ | Florence-2-base-ft[[HF]](https://huggingface.co/microsoft/Florence-2-base-ft) | 0.23B | Finetuned model on a colletion of downstream tasks
28
+ | Florence-2-large-ft[[HF]](https://huggingface.co/microsoft/Florence-2-large-ft) | 0.77B | Finetuned model on a colletion of downstream tasks
29
+
30
+ ## How to Get Started with the Model
31
+
32
+ Use the code below to get started with the model. All models are trained with float16.
33
+
34
+ ```python
35
+ import requests
36
+
37
+ import torch
38
+ from PIL import Image
39
+ from transformers import AutoProcessor, AutoModelForCausalLM
40
+
41
+
42
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
43
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
44
+
45
+ model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
46
+ processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)
47
+
48
+ prompt = "<OD>"
49
+
50
+ url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
51
+ image = Image.open(requests.get(url, stream=True).raw)
52
+
53
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
54
+
55
+ generated_ids = model.generate(
56
+ input_ids=inputs["input_ids"],
57
+ pixel_values=inputs["pixel_values"],
58
+ max_new_tokens=4096,
59
+ num_beams=3,
60
+ do_sample=False
61
+ )
62
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
63
+
64
+ parsed_answer = processor.post_process_generation(generated_text, task="<OD>", image_size=(image.width, image.height))
65
+
66
+ print(parsed_answer)
67
+
68
+ ```
69
+
70
+
71
+ ## Tasks
72
+
73
+ This model is capable of performing different tasks through changing the prompts.
74
+
75
+ First, let's define a function to run a prompt.
76
+
77
+ <details>
78
+ <summary> Click to expand </summary>
79
+
80
+ ```python
81
+ import requests
82
+
83
+ import torch
84
+ from PIL import Image
85
+ from transformers import AutoProcessor, AutoModelForCausalLM
86
+
87
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
88
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
89
+
90
+ model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
91
+ processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)
92
+
93
+ url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
94
+ image = Image.open(requests.get(url, stream=True).raw)
95
+
96
+ def run_example(task_prompt, text_input=None):
97
+ if text_input is None:
98
+ prompt = task_prompt
99
+ else:
100
+ prompt = task_prompt + text_input
101
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
102
+ generated_ids = model.generate(
103
+ input_ids=inputs["input_ids"],
104
+ pixel_values=inputs["pixel_values"],
105
+ max_new_tokens=1024,
106
+ num_beams=3
107
+ )
108
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
109
+
110
+ parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
111
+
112
+ print(parsed_answer)
113
+ ```
114
+ </details>
115
+
116
+ Here are the tasks `Florence-2` could perform:
117
+
118
+ <details>
119
+ <summary> Click to expand </summary>
120
+
121
+
122
+
123
+ ### Caption
124
+ ```python
125
+ prompt = "<CAPTION>"
126
+ run_example(prompt)
127
+ ```
128
+
129
+ ### Detailed Caption
130
+ ```python
131
+ prompt = "<DETAILED_CAPTION>"
132
+ run_example(prompt)
133
+ ```
134
+
135
+ ### More Detailed Caption
136
+ ```python
137
+ prompt = "<MORE_DETAILED_CAPTION>"
138
+ run_example(prompt)
139
+ ```
140
+
141
+ ### Caption to Phrase Grounding
142
+ caption to phrase grounding task requires additional text input, i.e. caption.
143
+
144
+ Caption to phrase grounding results format:
145
+ {'\<CAPTION_TO_PHRASE_GROUNDING>': {'bboxes': [[x1, y1, x2, y2], ...], 'labels': ['', '', ...]}}
146
+ ```python
147
+ task_prompt = "<CAPTION_TO_PHRASE_GROUNDING>"
148
+ results = run_example(task_prompt, text_input="A green car parked in front of a yellow building.")
149
+ ```
150
+
151
+ ### Object Detection
152
+
153
+ OD results format:
154
+ {'\<OD>': {'bboxes': [[x1, y1, x2, y2], ...],
155
+ 'labels': ['label1', 'label2', ...]} }
156
+
157
+ ```python
158
+ prompt = "<OD>"
159
+ run_example(prompt)
160
+ ```
161
+
162
+ ### Dense Region Caption
163
+ Dense region caption results format:
164
+ {'\<DENSE_REGION_CAPTION>' : {'bboxes': [[x1, y1, x2, y2], ...],
165
+ 'labels': ['label1', 'label2', ...]} }
166
+ ```python
167
+ prompt = "<DENSE_REGION_CAPTION>"
168
+ run_example(prompt)
169
+ ```
170
+
171
+ ### Region proposal
172
+ Dense region caption results format:
173
+ {'\<REGION_PROPOSAL>': {'bboxes': [[x1, y1, x2, y2], ...],
174
+ 'labels': ['', '', ...]}}
175
+ ```python
176
+ prompt = "<REGION_PROPOSAL>"
177
+ run_example(prompt)
178
+ ```
179
+
180
+ ### OCR
181
+
182
+ ```python
183
+ prompt = "<OCR>"
184
+ run_example(prompt)
185
+ ```
186
+
187
+ ### OCR with Region
188
+ OCR with region output format:
189
+ {'\<OCR_WITH_REGION>': {'quad_boxes': [[x1, y1, x2, y2, x3, y3, x4, y4], ...], 'labels': ['text1', ...]}}
190
+ ```python
191
+ prompt = "<OCR_WITH_REGION>"
192
+ run_example(prompt)
193
+ ```
194
+
195
+ ### Output confidence score with Object Detection
196
+ ```python
197
+
198
+ def run_example_with_score(task_prompt, text_input=None):
199
+ if text_input is None:
200
+ prompt = task_prompt
201
+ else:
202
+ prompt = task_prompt + text_input
203
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
204
+ generated_ids = model.generate(
205
+ input_ids=inputs["input_ids"],
206
+ pixel_values=inputs["pixel_values"],
207
+ max_new_tokens=1024,
208
+ num_beams=3,
209
+ return_dict_in_generate=True,
210
+ output_scores=True,
211
+ )
212
+ generated_text = processor.batch_decode(generated_ids.sequences, skip_special_tokens=False)[0]
213
+
214
+ prediction, scores, beam_indices = generated_ids.sequences, generated_ids.scores, generated_ids.beam_indices
215
+ transition_beam_scores = model.compute_transition_scores(
216
+ sequences=prediction,
217
+ scores=scores,
218
+ beam_indices=beam_indices,
219
+ )
220
+
221
+ parsed_answer = processor.post_process_generation(sequence=generated_ids.sequences[0],
222
+ transition_beam_score=transition_beam_scores[0],
223
+ task=task_prompt, image_size=(image.width, image.height)
224
+ )
225
+
226
+ print(parsed_answer)
227
+
228
+ prompt = "<OD>"
229
+ run_example_with_score(prompt)
230
+
231
+ ```
232
+
233
+ for More detailed examples, please refer to [notebook](https://huggingface.co/microsoft/Florence-2-large/blob/main/sample_inference.ipynb)
234
+ </details>
235
+
236
+ # Benchmarks
237
+
238
+ ## Florence-2 Zero-shot performance
239
+
240
+ The following table presents the zero-shot performance of generalist vision foundation models on image captioning and object detection evaluation tasks. These models have not been exposed to the training data of the evaluation tasks during their training phase.
241
+
242
+ | Method | #params | COCO Cap. test CIDEr | NoCaps val CIDEr | TextCaps val CIDEr | COCO Det. val2017 mAP |
243
+ |--------|---------|----------------------|------------------|--------------------|-----------------------|
244
+ | Flamingo | 80B | 84.3 | - | - | - |
245
+ | Florence-2-base| 0.23B | 133.0 | 118.7 | 70.1 | 34.7 |
246
+ | Florence-2-large| 0.77B | 135.6 | 120.8 | 72.8 | 37.5 |
247
+
248
+
249
+ The following table continues the comparison with performance on other vision-language evaluation tasks.
250
+
251
+ | Method | Flickr30k test R@1 | Refcoco val Accuracy | Refcoco test-A Accuracy | Refcoco test-B Accuracy | Refcoco+ val Accuracy | Refcoco+ test-A Accuracy | Refcoco+ test-B Accuracy | Refcocog val Accuracy | Refcocog test Accuracy | Refcoco RES val mIoU |
252
+ |--------|----------------------|----------------------|-------------------------|-------------------------|-----------------------|--------------------------|--------------------------|-----------------------|------------------------|----------------------|
253
+ | Kosmos-2 | 78.7 | 52.3 | 57.4 | 47.3 | 45.5 | 50.7 | 42.2 | 60.6 | 61.7 | - |
254
+ | Florence-2-base | 83.6 | 53.9 | 58.4 | 49.7 | 51.5 | 56.4 | 47.9 | 66.3 | 65.1 | 34.6 |
255
+ | Florence-2-large | 84.4 | 56.3 | 61.6 | 51.4 | 53.6 | 57.9 | 49.9 | 68.0 | 67.0 | 35.8 |
256
+
257
+
258
+
259
+ ## Florence-2 finetuned performance
260
+
261
+ We finetune Florence-2 models with a collection of downstream tasks, resulting two generalist models *Florence-2-base-ft* and *Florence-2-large-ft* that can conduct a wide range of downstream tasks.
262
+
263
+ The table below compares the performance of specialist and generalist models on various captioning and Visual Question Answering (VQA) tasks. Specialist models are fine-tuned specifically for each task, whereas generalist models are fine-tuned in a task-agnostic manner across all tasks. The symbol "▲" indicates the usage of external OCR as input.
264
+
265
+ | Method | # Params | COCO Caption Karpathy test CIDEr | NoCaps val CIDEr | TextCaps val CIDEr | VQAv2 test-dev Acc | TextVQA test-dev Acc | VizWiz VQA test-dev Acc |
266
+ |----------------|----------|-----------------------------------|------------------|--------------------|--------------------|----------------------|-------------------------|
267
+ | **Specialist Models** | | | | | | | |
268
+ | CoCa | 2.1B | 143.6 | 122.4 | - | 82.3 | - | - |
269
+ | BLIP-2 | 7.8B | 144.5 | 121.6 | - | 82.2 | - | - |
270
+ | GIT2 | 5.1B | 145.0 | 126.9 | 148.6 | 81.7 | 67.3 | 71.0 |
271
+ | Flamingo | 80B | 138.1 | - | - | 82.0 | 54.1 | 65.7 |
272
+ | PaLI | 17B | 149.1 | 127.0 | 160.0▲ | 84.3 | 58.8 / 73.1▲ | 71.6 / 74.4▲ |
273
+ | PaLI-X | 55B | 149.2 | 126.3 | 147.0 / 163.7▲ | 86.0 | 71.4 / 80.8▲ | 70.9 / 74.6▲ |
274
+ | **Generalist Models** | | | | | | | |
275
+ | Unified-IO | 2.9B | - | 100.0 | - | 77.9 | - | 57.4 |
276
+ | Florence-2-base-ft | 0.23B | 140.0 | 116.7 | 143.9 | 79.7 | 63.6 | 63.6 |
277
+ | Florence-2-large-ft | 0.77B | 143.3 | 124.9 | 151.1 | 81.7 | 73.5 | 72.6 |
278
+
279
+
280
+ | Method | # Params | COCO Det. val2017 mAP | Flickr30k test R@1 | RefCOCO val Accuracy | RefCOCO test-A Accuracy | RefCOCO test-B Accuracy | RefCOCO+ val Accuracy | RefCOCO+ test-A Accuracy | RefCOCO+ test-B Accuracy | RefCOCOg val Accuracy | RefCOCOg test Accuracy | RefCOCO RES val mIoU |
281
+ |----------------------|----------|-----------------------|--------------------|----------------------|-------------------------|-------------------------|------------------------|---------------------------|---------------------------|------------------------|-----------------------|------------------------|
282
+ | **Specialist Models** | | | | | | | | | | | | |
283
+ | SeqTR | - | - | - | 83.7 | 86.5 | 81.2 | 71.5 | 76.3 | 64.9 | 74.9 | 74.2 | - |
284
+ | PolyFormer | - | - | - | 90.4 | 92.9 | 87.2 | 85.0 | 89.8 | 78.0 | 85.8 | 85.9 | 76.9 |
285
+ | UNINEXT | 0.74B | 60.6 | - | 92.6 | 94.3 | 91.5 | 85.2 | 89.6 | 79.8 | 88.7 | 89.4 | - |
286
+ | Ferret | 13B | - | - | 89.5 | 92.4 | 84.4 | 82.8 | 88.1 | 75.2 | 85.8 | 86.3 | - |
287
+ | **Generalist Models** | | | | | | | | | | | | |
288
+ | UniTAB | - | - | - | 88.6 | 91.1 | 83.8 | 81.0 | 85.4 | 71.6 | 84.6 | 84.7 | - |
289
+ | Florence-2-base-ft | 0.23B | 41.4 | 84.0 | 92.6 | 94.8 | 91.5 | 86.8 | 91.7 | 82.2 | 89.8 | 82.2 | 78.0 |
290
+ | Florence-2-large-ft| 0.77B | 43.4 | 85.2 | 93.4 | 95.3 | 92.0 | 88.3 | 92.9 | 83.6 | 91.2 | 91.7 | 80.5 |
291
+
292
+
293
+ ## BibTex and citation info
294
+
295
+ ```
296
+ @article{xiao2023florence,
297
+ title={Florence-2: Advancing a unified representation for a variety of vision tasks},
298
+ author={Xiao, Bin and Wu, Haiping and Xu, Weijian and Dai, Xiyang and Hu, Houdong and Lu, Yumao and Zeng, Michael and Liu, Ce and Yuan, Lu},
299
+ journal={arXiv preprint arXiv:2311.06242},
300
+ year={2023}
301
+ }
302
+ ```