yifeihu commited on
Commit
a5fca40
1 Parent(s): 11a96c6

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +48 -214
README.md CHANGED
@@ -1,51 +1,59 @@
1
  ---
2
- license: mit
3
- license_link: https://huggingface.co/microsoft/Florence-2-large-ft/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 Hub repository contains a HuggingFace's `transformers` implementation of Florence-2 model from Microsoft.
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- 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.
16
 
17
- Resources and Technical Documentation:
18
- + [Florence-2 technical report](https://arxiv.org/abs/2311.06242).
19
- + [Jupyter Notebook for inference and visualization of Florence-2-large model](https://huggingface.co/microsoft/Florence-2-large/blob/main/sample_inference.ipynb)
20
 
21
- | Model | Model size | Model Description |
22
- | ------- | ------------- | ------------- |
23
- | Florence-2-base[[HF]](https://huggingface.co/microsoft/Florence-2-base) | 0.23B | Pretrained model with FLD-5B
24
- | Florence-2-large[[HF]](https://huggingface.co/microsoft/Florence-2-large) | 0.77B | Pretrained model with FLD-5B
25
- | Florence-2-base-ft[[HF]](https://huggingface.co/microsoft/Florence-2-base-ft) | 0.23B | Finetuned model on a colletion of downstream tasks
26
- | Florence-2-large-ft[[HF]](https://huggingface.co/microsoft/Florence-2-large-ft) | 0.77B | Finetuned model on a colletion of downstream tasks
27
 
28
  ## How to Get Started with the Model
29
 
30
  Use the code below to get started with the model.
31
 
 
 
32
  ```python
33
  import requests
34
-
35
  from PIL import Image
36
  from transformers import AutoProcessor, AutoModelForCausalLM
37
-
38
-
39
- model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large-ft", trust_remote_code=True)
40
- processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large-ft", trust_remote_code=True)
41
-
42
  prompt = "<OD>"
43
-
44
- url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
45
  image = Image.open(requests.get(url, stream=True).raw)
46
-
47
  inputs = processor(text=prompt, images=image, return_tensors="pt")
48
-
49
  generated_ids = model.generate(
50
  input_ids=inputs["input_ids"],
51
  pixel_values=inputs["pixel_values"],
@@ -54,203 +62,29 @@ generated_ids = model.generate(
54
  num_beams=3
55
  )
56
  generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
57
-
58
  parsed_answer = processor.post_process_generation(generated_text, task="<OD>", image_size=(image.width, image.height))
59
-
60
  print(parsed_answer)
61
-
62
  ```
63
 
64
-
65
- ## Tasks
66
-
67
- This model is capable of performing different tasks through changing the prompts.
68
-
69
- First, let's define a function to run a prompt.
70
-
71
- <details>
72
- <summary> Click to expand </summary>
73
-
74
- ```python
75
- import requests
76
-
77
- from PIL import Image
78
- from transformers import AutoProcessor, AutoModelForCausalLM
79
-
80
-
81
- model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large-ft", trust_remote_code=True)
82
- processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large-ft", trust_remote_code=True)
83
-
84
- url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
85
- image = Image.open(requests.get(url, stream=True).raw)
86
-
87
- def run_example(task_prompt, text_input=None):
88
- if text_input is None:
89
- prompt = task_prompt
90
- else:
91
- prompt = task_prompt + text_input
92
- inputs = processor(text=prompt, images=image, return_tensors="pt")
93
- generated_ids = model.generate(
94
- input_ids=inputs["input_ids"],
95
- pixel_values=inputs["pixel_values"],
96
- max_new_tokens=1024,
97
- num_beams=3
98
- )
99
- generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
100
-
101
- parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
102
-
103
- print(parsed_answer)
104
- ```
105
- </details>
106
-
107
- Here are the tasks `Florence-2` could perform:
108
-
109
- <details>
110
- <summary> Click to expand </summary>
111
-
112
-
113
-
114
- ### Caption
115
- ```python
116
- prompt = "<CAPTION>"
117
- run_example(prompt)
118
- ```
119
-
120
- ### Detailed Caption
121
- ```python
122
- prompt = "<DETAILED_CAPTION>"
123
- run_example(prompt)
124
- ```
125
-
126
- ### More Detailed Caption
127
- ```python
128
- prompt = "<MORE_DETAILED_CAPTION>"
129
- run_example(prompt)
130
- ```
131
-
132
- ### Caption to Phrase Grounding
133
- caption to phrase grounding task requires additional text input, i.e. caption.
134
-
135
- Caption to phrase grounding results format:
136
- {'\<CAPTION_TO_PHRASE_GROUNDING>': {'bboxes': [[x1, y1, x2, y2], ...], 'labels': ['', '', ...]}}
137
- ```python
138
- task_prompt = "<CAPTION_TO_PHRASE_GROUNDING>"
139
- results = run_example(task_prompt, text_input="A green car parked in front of a yellow building.")
140
- ```
141
-
142
- ### Object Detection
143
-
144
- OD results format:
145
- {'\<OD>': {'bboxes': [[x1, y1, x2, y2], ...],
146
- 'labels': ['label1', 'label2', ...]} }
147
-
148
- ```python
149
- prompt = "<OD>"
150
- run_example(prompt)
151
- ```
152
-
153
- ### Dense Region Caption
154
- Dense region caption results format:
155
- {'\<DENSE_REGION_CAPTION>' : {'bboxes': [[x1, y1, x2, y2], ...],
156
- 'labels': ['label1', 'label2', ...]} }
157
- ```python
158
- prompt = "<DENSE_REGION_CAPTION>"
159
- run_example(prompt)
160
- ```
161
-
162
- ### Region proposal
163
- Dense region caption results format:
164
- {'\<REGION_PROPOSAL>': {'bboxes': [[x1, y1, x2, y2], ...],
165
- 'labels': ['', '', ...]}}
166
- ```python
167
- prompt = "<REGION_PROPOSAL>"
168
- run_example(prompt)
169
- ```
170
-
171
-
172
- ### OCR
173
-
174
- ```python
175
- prompt = "<OCR>"
176
- run_example(prompt)
177
- ```
178
-
179
- ### OCR with Region
180
- OCR with region output format:
181
- {'\<OCR_WITH_REGION>': {'quad_boxes': [[x1, y1, x2, y2, x3, y3, x4, y4], ...], 'labels': ['text1', ...]}}
182
- ```python
183
- prompt = "<OCR_WITH_REGION>"
184
- run_example(prompt)
185
- ```
186
-
187
- for More detailed examples, please refer to [notebook](https://huggingface.co/microsoft/Florence-2-large/blob/main/sample_inference.ipynb)
188
- </details>
189
-
190
- # Benchmarks
191
-
192
- ## Florence-2 Zero-shot performance
193
-
194
- 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.
195
-
196
- | Method | #params | COCO Cap. test CIDEr | NoCaps val CIDEr | TextCaps val CIDEr | COCO Det. val2017 mAP |
197
- |--------|---------|----------------------|------------------|--------------------|-----------------------|
198
- | Flamingo | 80B | 84.3 | - | - | - |
199
- | Florence-2-base| 0.23B | 133.0 | 118.7 | 70.1 | 34.7 |
200
- | Florence-2-large| 0.77B | 135.6 | 120.8 | 72.8 | 37.5 |
201
-
202
-
203
- The following table continues the comparison with performance on other vision-language evaluation tasks.
204
-
205
- | 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 |
206
- |--------|----------------------|----------------------|-------------------------|-------------------------|-----------------------|--------------------------|--------------------------|-----------------------|------------------------|----------------------|
207
- | Kosmos-2 | 78.7 | 52.3 | 57.4 | 47.3 | 45.5 | 50.7 | 42.2 | 60.6 | 61.7 | - |
208
- | Florence-2-base | 83.6 | 53.9 | 58.4 | 49.7 | 51.5 | 56.4 | 47.9 | 66.3 | 65.1 | 34.6 |
209
- | Florence-2-large | 84.4 | 56.3 | 61.6 | 51.4 | 53.6 | 57.9 | 49.9 | 68.0 | 67.0 | 35.8 |
210
-
211
-
212
-
213
- ## Florence-2 finetuned performance
214
-
215
- 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.
216
-
217
- 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.
218
-
219
- | 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 |
220
- |----------------|----------|-----------------------------------|------------------|--------------------|--------------------|----------------------|-------------------------|
221
- | **Specialist Models** | | | | | | | |
222
- | CoCa | 2.1B | 143.6 | 122.4 | - | 82.3 | - | - |
223
- | BLIP-2 | 7.8B | 144.5 | 121.6 | - | 82.2 | - | - |
224
- | GIT2 | 5.1B | 145.0 | 126.9 | 148.6 | 81.7 | 67.3 | 71.0 |
225
- | Flamingo | 80B | 138.1 | - | - | 82.0 | 54.1 | 65.7 |
226
- | PaLI | 17B | 149.1 | 127.0 | 160.0▲ | 84.3 | 58.8 / 73.1▲ | 71.6 / 74.4▲ |
227
- | PaLI-X | 55B | 149.2 | 126.3 | 147.0 / 163.7▲ | 86.0 | 71.4 / 80.8▲ | 70.9 / 74.6▲ |
228
- | **Generalist Models** | | | | | | | |
229
- | Unified-IO | 2.9B | - | 100.0 | - | 77.9 | - | 57.4 |
230
- | Florence-2-base-ft | 0.23B | 140.0 | 116.7 | 143.9 | 79.7 | 63.6 | 63.6 |
231
- | Florence-2-large-ft | 0.77B | 143.3 | 124.9 | 151.1 | 81.7 | 73.5 | 72.6 |
232
-
233
-
234
- | 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 |
235
- |----------------------|----------|-----------------------|--------------------|----------------------|-------------------------|-------------------------|------------------------|---------------------------|---------------------------|------------------------|-----------------------|------------------------|
236
- | **Specialist Models** | | | | | | | | | | | | |
237
- | SeqTR | - | - | - | 83.7 | 86.5 | 81.2 | 71.5 | 76.3 | 64.9 | 74.9 | 74.2 | - |
238
- | PolyFormer | - | - | - | 90.4 | 92.9 | 87.2 | 85.0 | 89.8 | 78.0 | 85.8 | 85.9 | 76.9 |
239
- | UNINEXT | 0.74B | 60.6 | - | 92.6 | 94.3 | 91.5 | 85.2 | 89.6 | 79.8 | 88.7 | 89.4 | - |
240
- | Ferret | 13B | - | - | 89.5 | 92.4 | 84.4 | 82.8 | 88.1 | 75.2 | 85.8 | 86.3 | - |
241
- | **Generalist Models** | | | | | | | | | | | | |
242
- | UniTAB | - | - | - | 88.6 | 91.1 | 83.8 | 81.0 | 85.4 | 71.6 | 84.6 | 84.7 | - |
243
- | 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 |
244
- | 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 |
245
-
246
 
247
  ## BibTex and citation info
248
 
249
  ```
250
- @article{xiao2023florence,
251
- title={Florence-2: Advancing a unified representation for a variety of vision tasks},
252
- 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},
253
- journal={arXiv preprint arXiv:2311.06242},
254
- year={2023}
 
 
 
 
 
 
 
 
 
 
255
  }
256
  ```
 
1
  ---
2
+ license: apache-2.0
 
3
  pipeline_tag: image-text-to-text
4
  tags:
5
  - vision
6
+ - layout-analysis
7
+ - object-detection
8
+ datasets:
9
+ - ds4sd/DocLayNet-v1.1
10
+ base_model:
11
+ - microsoft/Florence-2-large-ft
12
  ---
13
 
14
+ # Florence-2-DocLayNet-Fixed
15
 
16
  ## Model Summary
17
 
18
+ We finetuned the Florence-2-large-ft [[HF]](https://huggingface.co/microsoft/Florence-2-large-ft) model using the [[DocLayNet-v1.1]](https://huggingface.co/datasets/ds4sd/DocLayNet-v1.1) dataset. To prevent the model from generating hallucinated class names, we re-mapped all class names to single tokens:
19
+ | Original Class Names | New Class Names |
20
+ |----------------------|-----------------|
21
+ | Caption | Cap |
22
+ | Footnote | Footnote |
23
+ | Formula | Math |
24
+ | List-item | List |
25
+ | Page-footer | Bottom |
26
+ | Page-header | Header |
27
+ | Picture | Picture |
28
+ | Section-header | Section |
29
+ | Table | Table |
30
+ | Text | Text |
31
+ | Title | Title |
32
 
33
+ By applying this simple change, we observed **7% improvement** of mAP50-95 score on the DocLayNet test set. The training and inference was also faster thanks to fewer tokens used by the class names.
34
 
35
+ From the mAP50-95 score, this model is far from SOTA on the DocLayNet test set (**70%**). Much smaller Yolo models (github.com/ppaanngggg/yolo-doclaynet)[https://github.com/ppaanngggg/yolo-doclaynet] have much better benchmark results (**~79%**). On the subset of scientific articles, this model performed on par with the best Yolo models (**87%**) in terms of mAP50-95.
 
 
36
 
37
+ However, after we performed some qualitative analysis (paper coming soon), we found that Florence-2 is much better at drawing bounding boxes with clean edges. Yolo models sometimes cut text in the middle or draw multiple bounding boxes on the same object. These behaviors are not seriously published in mAP50-95 but are painful to deal with in real-world use cases. When calculating the mAP scores, we had to manually set the confidence score as 1 for all Florence-2 output.
38
+
39
+ We release the finetuned model weights for the community to further investigate related research topics.
 
 
 
40
 
41
  ## How to Get Started with the Model
42
 
43
  Use the code below to get started with the model.
44
 
45
+ For non-CUDA environments, please check out this post for a simple patch: [https://huggingface.co/microsoft/Florence-2-base/discussions/4](https://huggingface.co/microsoft/Florence-2-base/discussions/4)
46
+
47
  ```python
48
  import requests
 
49
  from PIL import Image
50
  from transformers import AutoProcessor, AutoModelForCausalLM
51
+ model = AutoModelForCausalLM.from_pretrained("yifeihu/Florence-2-DocLayNet-Fixed", trust_remote_code=True)
52
+ processor = AutoProcessor.from_pretrained("yifeihu/Florence-2-DocLayNet-Fixed", trust_remote_code=True)
 
 
 
53
  prompt = "<OD>"
54
+ url = "https://huggingface.co/yifeihu/TF-ID-base/resolve/main/arxiv_2305_10853_5.png?download=true"
 
55
  image = Image.open(requests.get(url, stream=True).raw)
 
56
  inputs = processor(text=prompt, images=image, return_tensors="pt")
 
57
  generated_ids = model.generate(
58
  input_ids=inputs["input_ids"],
59
  pixel_values=inputs["pixel_values"],
 
62
  num_beams=3
63
  )
64
  generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
 
65
  parsed_answer = processor.post_process_generation(generated_text, task="<OD>", image_size=(image.width, image.height))
 
66
  print(parsed_answer)
 
67
  ```
68
 
69
+ To visualize the results, see [this tutorial notebook](https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/how-to-finetune-florence-2-on-detection-dataset.ipynb) for more details.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  ## BibTex and citation info
72
 
73
  ```
74
+ @misc{TF-ID,
75
+ author = {Yifei Hu},
76
+ title = {TF-ID: Table/Figure IDentifier for academic papers},
77
+ year = {2024},
78
+ publisher = {GitHub},
79
+ journal = {GitHub repository},
80
+ howpublished = {\url{https://github.com/ai8hyf/TF-ID}},
81
+ }
82
+
83
+ @article{doclaynet2022,
84
+ title = {DocLayNet: A Large Human-Annotated Dataset for Document-Layout Analysis},
85
+ doi = {10.1145/3534678.353904},
86
+ url = {https://arxiv.org/abs/2206.01062},
87
+ author = {Pfitzmann, Birgit and Auer, Christoph and Dolfi, Michele and Nassar, Ahmed S and Staar, Peter W J},
88
+ year = {2022}
89
  }
90
  ```