JustinLin610 commited on
Commit
4410297
1 Parent(s): 0962f0f
Files changed (2) hide show
  1. EasyOCRLite +1 -1
  2. README.md +26 -600
EasyOCRLite CHANGED
@@ -1 +1 @@
1
- Subproject commit a2f4aa37b5112d4dad7646879ebae799cd606d7b
 
1
+ Subproject commit 416e04103501cf09e1ed5b6dd0bedeeb1cc63021
README.md CHANGED
@@ -1,600 +1,26 @@
1
- <!---
2
- Copyright 2022 The OFA-Sys Team.
3
- All rights reserved.
4
- This source code is licensed under the Apache 2.0 license found in the LICENSE file in the root directory.
5
- -->
6
-
7
- <p align="center">
8
- <br>
9
- <img src="examples/OFA_logo_tp_path.svg" width="150" />
10
- <br>
11
- <p>
12
- <br>
13
-
14
- <p align="center">
15
- <a href="modelscope.md">ModelScope</a>&nbsp | &nbsp<a href="checkpoints.md">Checkpoints</a>&nbsp | &nbsp<a href="colab.md">Colab</a>&nbsp | &nbsp<a href="https://huggingface.co/ofa-sys">Demo</a>&nbsp | &nbsp<a href="http://arxiv.org/abs/2202.03052">Paper </a>&nbsp | &nbspBlog
16
- </p>
17
-
18
- <p align="center">
19
- <br>
20
- <img src="examples/demo.gif" width="800" />
21
- <br>
22
- <p>
23
-
24
- [colab]: <https://colab.research.google.com/assets/colab-badge.svg>
25
-
26
- OFA is a unified sequence-to-sequence pretrained model (support **English** and **Chinese**) that unifies modalities (i.e., cross-modality, vision, language) and tasks (**finetuning** and **prompt tuning** are supported): image captioning (1st at the [MSCOCO Leaderboard](https://competitions.codalab.org/competitions/3221#results)), VQA ([link](https://eval.ai/web/challenges/challenge-page/830/leaderboard/2278)), visual grounding, text-to-image generation, text classification, text generation, image classification, etc. We provide **step-by-step** instructions for pretraining and finetuning and corresponding checkpoints (check official ckpt \[[EN](checkpoints.md)|[CN](checkpoints_cn.md)\] or [huggingface ckpt](https://huggingface.co/OFA-Sys)).
27
-
28
- We sincerely welcome contributions to our project. Feel free to contact us or send us issues / PRs!
29
- <br></br>
30
-
31
-
32
- # Online Demos
33
- We provide online demo via Hugging Face Spaces for you to interact with our pretrained and finetuned models. Below are the links to the demos:
34
- * Image Captioning \[[ModelScope](https://modelscope.cn/#/models/damo/ofa_image-caption_coco_large_en/summary) | [Spaces](https://huggingface.co/spaces/OFA-Sys/OFA-Image_Caption)\]
35
- * Visual Grounding \[[ModelScope](https://modelscope.cn/#/models/damo/ofa_visual-grounding_refcoco_large_en/summary) | [Spaces](https://huggingface.co/spaces/OFA-Sys/OFA-Visual_Grounding)\]
36
- * Visual Question Answering \[[ModelScope](https://modelscope.cn/#/models/damo/ofa_visual-question-answering_pretrain_large_en/summary) | [Spaces](https://huggingface.co/spaces/OFA-Sys/OFA-Visual_Question_Answering)\]
37
- * Text-to-Image Generation \[[ModelScope](https://modelscope.cn/#/models/damo/ofa_text-to-image-synthesis_coco_large_en/summary) | [Spaces](https://huggingface.co/spaces/OFA-Sys/OFA-Text2Image_Generation)\]
38
- * Generic Interface \[[Spaces](https://huggingface.co/spaces/OFA-Sys/OFA-Generic_Interface)\]
39
-
40
- Also we provide Colab notebooks for you to better perceive the procedures. Click [here](colab.md) to check them out!
41
- <br></br>
42
-
43
- # Use in Huggingface Transformers
44
- We support the inference of OFA in Huggingface Transformers. Check the [README](transformers.md) and [Colab Notebook](https://colab.research.google.com/drive/1Ho81RBV8jysZ7e0FhsSCk_v938QeDuy3?usp=sharing) for more information. Codes are released in this branch https://github.com/OFA-Sys/OFA/tree/feature/add_transformers
45
- <br><br>
46
-
47
-
48
- # News
49
- * 2022.8.16: Released the **Chinese** version of OFA. **OFA-CN** needs only switching to `bpe_dir=../../utils/BERT_CN_dict` and `bpe=bert` and using our provided Chinese checkpoints in [checkpoints_cn.md](checkpoints_cn.md). Temporarily, we only provide base-size and large-size pretrained checkpoints and finetuned checkpoints on [MUGE Caption](https://tianchi.aliyun.com/muge) and the Chinese version of RefCOCO(-/+/g) (to release soon).
50
- * 2022.8.5: Released support of **prompt tuning** for OFA. Check our paper [here](https://arxiv.org/abs/2208.02532)! Please see the [prompt_tuning.md](prompt_tuning.md) for further details.
51
- * 2022.7.7: Updated support of OFA on **huggingface transformers** (fixed bugs in forward, add sequence generator from Fairseq to ensure performance, etc.). Refer to the doc [transformers.md](transformers.md) and the branch `feature/add_transformers`.
52
- * 2022.6.17: Released the pretrained checkpoint of **OFA-Huge**. To use it, set `--arch=ofa_huge` in the script.
53
- * 2022.5.15: OFA was accepted by **ICML 2022**
54
- * 2022.4.28: Add support of inference on **huggingface transformers**. For how to use it, please refer to the doc [transformers.md](transformers.md) and our [huggingface models](https://huggingface.co/OFA-Sys).
55
- * 2022.4.16: Released lightweight pretrained models **OFA-Medium** (~93M params) and **OFA-Tiny** (~33M params) in [checkpoints.md](checkpoints.md). To use them, you just need to load the corresponding checkpoint and set `--arch=ofa_medium` or `--arch=ofa_tiny` in the scripts.
56
-
57
- <details>
58
- <summary><b>More News</b></summary>
59
- <p>
60
- <ul>
61
- <li>2022.3.23: Added [Encouraging Loss](https://arxiv.org/pdf/2110.06537.pdf) as a feature. See [README_EncouragingLoss.md](README_EncouragingLoss.md). Leveraging this feature, OFA-Large has achieved improved results in both VQA (**test-std acc: 80.67**) and Image Classification (**test acc: 85.6**) recently.</li>
62
- <li>2022.3.21: Released codes for pretraining OFA.</li>
63
- <li>2022.3.18: Released the finetuned <b>OFA-Base</b> (~180M parameters) checkpoints and running scripts for vision & language tasks, including: <b>Caption (146.4 CIDEr), VQA (78.07 on test-std), SNLI-VE (89.3 on dev), RefCOCO (90.67 on testA), RefCOCO+ (87.15 on testA) and RefCOCOg (82.31 on test-u)</b>.</li>
64
- <li>2022.3.11: Released the finetuning & inference code/checkpoints for <b>Gigaword</b>.</li>
65
- <li>2022.3.08: Released the pretrained checkpoint of <b>OFA-Base</b> in <a href="https://github.com/OFA-Sys/OFA/blob/main/checkpoints.md">checkpoints.md</a>. To use OFA-Base, you just need to load <code>ofa_base.pt</code> and change <code>--arch=ofa_large</code> to <code>--arch=ofa_base</code> in the training scripts.</li>
66
- <li>2022.3.07: Released the finetuning & inference code/checkpoints for <b>Image Classification</b>, which achieves <b>85.0</b> accuracy on ImageNet-1K, slightly better than reported in OFA paper.</li>
67
- <li>2022.3.04: Released the finetuning & inference code/checkpoints for <b>Text-to-Image Generation</b>.</li>
68
- <li>2022.3.03: Released the finetuning & inference code/checkpoints for <b>SNLI-VE</b> and <b>GLUE</b>.</li>
69
- <li>2022.2.22: Released the finetuning & inference code/checkpoints for <b>Visual Question Answering</b>, which can reproduce <b>the reported VQA accuracy in OFA paper (80.02 on test-std)</b>. Check our results on the <a href="https://eval.ai/web/challenges/challenge-page/830/leaderboard/2278">VQA Challenge</a>.</li>
70
- <li>2022.2.15: Released finetuning & inference code/checkpoints for <b>Referring Expression Comprehension</b></li>
71
- <li>2022.2.10: Released the inference code & finetuned checkpoint for <b>Image captioning</b>, which can reproduce <b>the results on COCO Karparthy test split (149.6 CIDEr)</b>. OFA also achieves No.1 on the COCO image captioning online leaderboard <a href='https://competitions.codalab.org/competitions/3221#results'>Link</a> (marked as M6-Team).</li>
72
- </ul>
73
- </p>
74
- </details>
75
- <br></br>
76
-
77
-
78
- # Model Card
79
- We list the parameters and pretrained checkpoints of OFAs below. For finetuned checkpoints, please refer to [checkpoints.md](checkpoints.md).
80
-
81
- <table border="1" width="100%">
82
- <tr align="center">
83
- <th>Model</th><th>Ckpt</th><th>Params</th><th>Backbone</th><th>Hidden size</th><th>Intermediate size</th><th>Num. of heads</th><th>Enc layers</th><th>Dec layers</th>
84
- </tr>
85
- <tr align="center">
86
- <td>OFA<sub>Tiny</sub></td><td><a href="https://ofa-beijing.oss-cn-beijing.aliyuncs.com/checkpoints/ofa_tiny.pt">Download</a></td><td>33M</td><td>ResNet50</td><td>256</td><td>1024</td><td>4</td><td>4</td><td>4</td>
87
- </tr>
88
- <tr align="center">
89
- <td>OFA<sub>Medium</sub></td><td><a href="https://ofa-beijing.oss-cn-beijing.aliyuncs.com/checkpoints/ofa_medium.pt">Download</a></td><td>93M</td><td>ResNet101</td><td>512</td></td><td>2048</td><td>8</td><td>4</td><td>4</td>
90
- </tr>
91
- <tr align="center">
92
- <td>OFA<sub>Base</sub></td><td><a href="https://ofa-beijing.oss-cn-beijing.aliyuncs.com/checkpoints/ofa_base.pt">Download</a></td><td>180M</td><td>ResNet101</td><td>768</td></td><td>3072</td><td>12</td><td>6</td><td>6</td>
93
- </tr>
94
- <tr align="center">
95
- <td>OFA<sub>Large</sub></td><td><a href="https://ofa-beijing.oss-cn-beijing.aliyuncs.com/checkpoints/ofa_large.pt">Download</a></td><td>470M</td><td>ResNet152</td><td>1024</td></td><td>4096</td><td>16</td><td>12</td><td>12</td>
96
- </tr>
97
- <tr align="center">
98
- <td>OFA<sub>Huge</sub></td><td><a href="https://ofa-beijing.oss-cn-beijing.aliyuncs.com/checkpoints/ofa_huge.pt">Download</a></td><td>930M</td><td>ResNet152</td><td>1280</td></td><td>5120</td><td>16</td><td>24</td><td>12</td>
99
- </tr>
100
- </table>
101
- <br></br>
102
-
103
- # Results
104
- Below we demonstrate the results of OFAs on cross-modal understanding and generation.
105
-
106
- <table border="1" width="100%">
107
- <tr align="center">
108
- <th>Task</th><th>Image Captioning</th><th>VQA</th><th>Visual Entailment</th><th colspan="3">Referring Expression Comprehension</th>
109
- </tr>
110
- <tr align="center">
111
- <td>Dataset</td><td>COCO</td><td>VQA v2</td><td>SNLI-VE</td><td>RefCOCO</td><td>RefCOCO+</td><td>RefCOCOg</td>
112
- </tr>
113
- <tr align="center">
114
- <td>Split</td><td>Karpathy test (CE/CIDEr)</td><td>test-dev/test-std</td><td>val/test</td><td>val/test-a/test-b</td><td>val/test-a/test-b</td><td>val-u/test-u</td>
115
- </tr>
116
- <tr align="center">
117
- <td>Metric</td><td>CIDEr</td><td>Acc.</td><td>Acc.</td><td colspan="3">Acc.</td>
118
- </tr>
119
- <tr align="center">
120
- <td>OFA<sub>Tiny</sub></td><td>119.0 / 128.7</td><td>70.3 / 70.4</td><td>85.3 / 85.2</td><td>80.20 / 84.07 / 75.00</td><td>68.22 / 75.13 / 57.66</td><td>72.02 / 69.74</td>
121
- </tr>
122
- <tr align="center">
123
- <td>OFA<sub>Medium</sub></td><td>130.4 / 140.3</td><td>75.4 / 75.5</td><td>86.6 / 87.0</td><td>85.34 / 87.68 / 77.92</td><td>76.09 / 83.04 / 66.25</td><td>78.76 / 78.58</td>
124
- </tr>
125
- <tr align="center">
126
- <td>OFA<sub>Base</sub></td><td>138.2 / 146.7</td><td>78.0 / 78.1</td><td>89.3 / 89.2</td><td>88.48 / 90.67 / 83.30</td><td>81.39 / 87.15 / 74.29</td><td>82.29 / 82.31</td>
127
- </tr>
128
- <tr align="center">
129
- <td>OFA<sub>Large</sub></td><td>142.2 / 150.7</td><td>80.4 / 80.7</td><td>90.3 / 90.2</td><td>90.05 / 92.93 / 85.26</td><td>85.80 / 89.87 / 79.22</td><td>85.89 / 86.55</td>
130
- </tr>
131
- <tr align="center">
132
- <td>OFA<sub>Huge</sub></td><td>145.3 / 154.9</td><td>82.0 / 82.0</td><td>91.0 / 91.2</td><td>92.04 / 94.03 / 88.44</td><td>87.86 / 91.70 / 80.71</td><td>88.07 / 88.78</td>
133
- </tr>
134
- </table>
135
- <br></br>
136
-
137
- # Requirements
138
- * python 3.7.4
139
- * pytorch 1.8.1
140
- * torchvision 0.9.1
141
- * JAVA 1.8 (for COCO evaluation)
142
- <br></br>
143
-
144
- # Installation
145
- ```bash
146
- git clone https://github.com/OFA-Sys/OFA
147
- pip install -r requirements.txt
148
- ```
149
- <br></br>
150
-
151
- # Datasets and Checkpoints
152
- See [datasets.md](datasets.md) and [checkpoints.md](checkpoints.md).
153
- <br></br>
154
-
155
- # Training & Inference
156
- Below we provide methods for training and inference on different tasks. We provide both pretrained OFA-Large and OFA-Base in [checkpoints.md](checkpoints.md). The scripts mentioned in this section are prepared for OFA-Large. For reproducing the downstreaming results of OFA-Base, we have also provided the corresponding finetuning and inference scripts for OFA-Base in the `run_scripts/` folder.
157
-
158
- We recommend that your workspace directory should be organized like this:
159
- ```
160
- OFA/
161
- ├── checkpoints/
162
- │   ├── ofa_base.pt
163
- │   ├── ofa_large.pt
164
- │   ├── caption_large_best_clean.pt
165
- │   └── ...
166
- ├── criterions/
167
- ├── data/
168
- ├── dataset/
169
- │   ├── caption_data/
170
- │   ├── gigaword_data/
171
- │   └── ...
172
- ├── fairseq/
173
- ├── models/
174
- ├── run_scripts/
175
- ├── tasks/
176
- ├── train.py
177
- ├── trainer.py
178
- └── utils/
179
- ```
180
-
181
-
182
- ## Image Processing
183
- To ensure the efficiency of processing data, we did not store images with small files, but instead we encode them to base64 strings.
184
- Transforming image files to base64 strings is simple. Run the following code:
185
- ```python
186
- from PIL import Image
187
- from io import BytesIO
188
- import base64
189
-
190
- img = Image.open(file_name) # path to file
191
- img_buffer = BytesIO()
192
- img.save(img_buffer, format=img.format)
193
- byte_data = img_buffer.getvalue()
194
- base64_str = base64.b64encode(byte_data) # bytes
195
- base64_str = base64_str.decode("utf-8") # str
196
- ```
197
-
198
- ## Pretraining
199
- Below we provide methods for pretraining OFA.
200
-
201
- <details>
202
- <summary><b>1. Prepare the Dataset</b></summary>
203
- <p>
204
- To pretrain OFA, you should first download the dataset we provide (<a href="https://ofa-beijing.oss-cn-beijing.aliyuncs.com/datasets/pretrain_data/pretrain_data_examples.zip">pretrain_data_examples.zip</a>, a small subset of the original pretraining data). For your customed pretraining datasets, please prepare your training samples into the same format. <code>pretrain_data_examples.zip</code> contains 4 TSV files: <code>vision_language_examples.tsv</code>, <code>text_examples.tsv</code>, <code>image_examples.tsv</code> and <code>detection_examples.tsv</code>. Details of these files are as follows:
205
- <br />
206
- <ul type="circle">
207
- <li><b>vision_language_examples.tsv</b>:
208
- Each line contains uniq-id, image (base64 string), caption, question, answer, ground-truth objects (objects appearing in the caption or question), dataset name (source of the data) and task type (caption, qa or visual gronunding). Prepared for the pretraining tasks of visual grounding, grounded captioning, image-text matching, image captioning and visual question answering. </li>
209
- <li><b>text_examples.tsv</b>: Each line contains uniq-id and text. Prepared for the pretraining task of text infilling. </li>
210
- <li><b>image_examples.tsv</b>: Each line contains uniq-id, image (base64 string, should be resized to 256*256 resolution) and image-code (generate the sparse codes for the central part of image through VQ-GAN). Prepared for the pretraining task of image infilling. </li>
211
- <li><b>detection_examples.tsv</b>: Each line contains uniq-id, image (base64 string) and bounding box annotations (contains the top-left and bottom-right coordinates of the bounding box, object_id and object_name, seperated by commas). Prepared for the pretraining task of detection. </li>
212
- </ul>
213
- In addition, the folder negative_sample in pretrain_data_examples.zip contains three files <code>all_captions.txt</code>, <code>object.txt</code> and <code>type2ans.json</code>. The data in these files are used as negative samples for the image-text matching (ITM) task.
214
- </p>
215
- </details>
216
- <details>
217
- <summary><b>2. Pretraining</b></summary>
218
- <p>
219
- By default, the pretraining script will attempt to restore the released pretrained checkpoints of OFA-Base or OFA-Large and perform continuous pretraining. Continuous pretraining is more recommended, which achieves much better results compared with pretraining from scratch. For continuous pretraining, please download the pretrained weights in advance (see <a href='checkpoints.md'>checkpoints.md</a>) and put them in the correct directory <code>OFA/checkpoints/</code>. If not, the pretraining will begin from scratch.
220
- </p>
221
- <pre>
222
- cd run_scripts/pretraining
223
- bash pretrain_ofa_large.sh # Pretrain OFA-Large. For OFA-Base, use pretrain_ofa_base.sh
224
- </pre>
225
- <p>
226
- If the pretrained OFA checkpoint is restored successfully, you will see the following information in the log:
227
- </p>
228
- <pre>
229
- INFO: Loaded checkpoint ../../checkpoints/ofa_large.pt
230
- </pre>
231
- </details>
232
-
233
- ## Image Captioning
234
- We provide procedures to reproduce our results of image captioning on our paper below.
235
- <details>
236
- <summary><b>1. Prepare the Dataset & Checkpoints</b></summary>
237
- <p>
238
- Download data (see <a href='datasets.md'>datasets.md</a>) and models (see <a href='checkpoints.md'>checkpoints.md</a>) and put them in the correct directory. The dataset zipfile <code>caption_data.zip</code> contains caption_stage1_train.tsv, caption_stage2_train.tsv, caption_val.tsv and caption_test.tsv. Each image corresponds to only 1 caption in <code>caption_stage1_train.tsv</code> and corresponds to multiple captions in other TSV files (about 5 captions per image). Each line of the dataset represents a caption sample with the following format. The information of uniq-id, image-id, caption, predicted object labels (taken from <a href='https://github.com/pzzhang/VinVL'>VinVL</a>, not used), image base64 string are separated by tabs.
239
- </p>
240
- <pre>
241
- 162365 12455 the sun sets over the trees beyond some docks. sky&&water&&dock&&pole /9j/4AAQSkZJ....UCP/2Q==
242
- </pre>
243
- </details>
244
- <details>
245
- <summary><b>2. Finetuning</b></summary>
246
- <p>
247
- Following previous standard practice, we divide the finetuning process of image captioning into two stages. In stage 1, we finetune OFA with cross-entropy loss on 4 NVIDIA-V100 GPUs with 32GB memory (expected to obtain ~139.5 CIDEr on the validation set at this stage). In stage 2, we select the best checkpoint of stage 1 and train with CIDEr optimization on 8 NVIDIA-V100 GPUs. <b>Note that CIDEr optimization is very unstable and requires careful hyperparameter tuning. If you encounter training errors in the stage2 finetuning, you can increase the batch size or reduce the learning rate. If neither of these works, you can directly set </b><code>--freeze-resnet</code><b> to freeze the inner states of batch normalization.</b>
248
- </p>
249
- <pre>
250
- cd run_scripts/caption
251
- nohup sh train_caption_stage1.sh > train_stage1.out & # stage 1, train with cross-entropy loss
252
- nohup sh train_caption_stage2.sh > train_stage2.out & # stage 2, load the best ckpt of stage1 and train with CIDEr optimization
253
- </pre>
254
- </details>
255
- <details>
256
- <summary><b>3. Inference</b></summary>
257
- <p>
258
- Run the following commands to get your results and evaluate your model.
259
- </p>
260
- <pre>
261
- cd run_scripts/caption ; sh evaluate_caption.sh # inference & evaluate
262
- </pre>
263
- </details>
264
-
265
- ## Text-to-Image Generation
266
- This part provides procedures for the finetuning and inference of text-to-image generation. See below.
267
-
268
- <details>
269
- <summary><b>1. Prepare the Dataset & Checkpoints</b></summary>
270
- <p>
271
- Download data (see <a href="datasets.md">datasets.md</a>) and models (see <a href="checkpoints.md">checkpoints.md</a>) and put them in the correct directory. The dataset zipfile <code>coco_image_gen.zip</code> contains <code>coco_vqgan_train.tsv</code>, <code>coco_vqgan_dev.tsv</code> and <code>coco_vqgan_full_test.tsv</code>. Each line of the dataset represents a sample with the following format. The information of uniq-id, image-code (produced by <a href="https://github.com/CompVis/taming-transformers">vqgan</a>, a list of integers separated by single-whitespaces), lowercased caption are separated by tabs.
272
- </p>
273
- <pre>
274
- 1 6674 4336 4532 5334 3251 5461 3615 2469 ...4965 4190 1846 the people are posing for a group photo.
275
- </pre>
276
- <p>
277
- The checkpoint zipfile <code>image_gen_large_best.zip</code> contains <code>image_gen_large_best.pt</code>, <code>vqgan/last.ckpt</code>, <code>vqgan/model.yaml</code> and <code>clip/Vit-B-16.pt</code>.
278
- </p>
279
- </details>
280
- <details>
281
- <summary><b>2. Shuffle the Training Data</b></summary>
282
- <p>
283
- (Optional, but achieves better result): If the disk storage is sufficient, we recommend to prepare the shuffled training data for each epoch in advance.
284
- </p>
285
- <pre>
286
- cd dataset/image_gen
287
- ln coco_vqgan_train.tsv coco_vqgan_train_1.tsv
288
- for idx in `seq 1 9`;do shuf coco_vqgan_train_${idx}.tsv > coco_vqgan_train_$[${idx}+1].tsv;done # each file is used for an epoch
289
- </pre>
290
- </details>
291
- <details>
292
- <summary><b>3. Finetuning</b></summary>
293
- <p>
294
- Following previous practice, we divide the finetuning process of image generating into two stages. In stage 1, we finetune OFA with cross-entropy loss on 4 8-V100-32G-GPU servers (expected to obtain ~32.5+ CLIP Score on the validation set at this stage). In stage 2, we select the last checkpoint of stage 1 and train with CLIP Score optimization on 4 8-V100-32G-GPU servers (expected to obtain ~34.0+ CLIP Score on the validation set at this stage). During the validation, the generated image will be dumped into <code>_GEN_IMAGE_PATH_</code>.
295
- </p>
296
- <pre>
297
- # run on each worker after the distributed and data configs have been correctly set following the guide in train_image_gen_stage1_distributed.sh
298
- cd run_scripts/image_gen
299
- nohup sh train_image_gen_stage1_distributed.sh # stage 1, train with cross-entropy loss
300
- nohup sh train_image_gen_stage2_distributed.sh # stage 2, load the last ckpt of stage1 and train with CLIP Score optimization
301
- </pre>
302
- </details>
303
- <details>
304
- <summary><b>4. Inference</b></summary>
305
- <p>
306
- Run the command below to generate your images.
307
- </p>
308
- <pre>
309
- cd run_scripts/image_gen ; sh evaluate_image_gen.sh # inference & evaluate (FID, IS and CLIP Score)
310
- </pre>
311
- </details>
312
-
313
- ## Visual Question Answering
314
- Here we provide the finetuning and inference codes to reproduce the VQAv2 result reported in our paper (**test-std 80.02**). We believe much improvement on accuracy can still be achieved based on this codebase :)
315
- <details>
316
- <summary><b>1. Prepare the Dataset & Checkpoints</b></summary>
317
- <p>
318
- Download data (see <a href="datasets.md">datasets.md</a>) and models (see <a href="checkpoints.md">checkpoints.md</a>) and put them in the correct directory. The dataset zipfile <code>vqa_data.zip</code> is around 100G and the decompressed data costs around 135G disk storage, which contains the training, validation and testing samples together with other necessary data resources. (Since <code>vqa_data.zip</code> is large in size, we have also provided chunked parts of the dataset files for more convenient and stable downloading. Please refer to <a href="https://github.com/OFA-Sys/OFA/issues/68#issuecomment-1096837349">issue #68</a>.) Following common practice, VG-QA samples are also included in the training data. To adapt to the seq2seq paradigm of OFA, we transform original VQA training questions with multiple golden answers into multiple training samples. For the original VQA validation set, we keep around 10k samples for our validation and utilize the other samples for training. Each line of the dataset represents a VQA sample with the following format. The information of question-id, image-id, question, answer (with confidence), predicted object labels (taken from <a href="https://github.com/pzzhang/VinVL">VinVL</a>, slightly brings around +0.1 accuracy improvement), image base64 string are separated by tabs.
319
- </p>
320
- <pre>
321
- 79459 79459 is this person wearing shorts? 0.6|!+no house&&short&&...&&sky /9j/4AAQS...tigZ/9k=
322
- </pre>
323
- <p>
324
- For fine-tuning on customed VQA-formulated tasks, please refer to issue <a href="https://github.com/OFA-Sys/OFA/issues/76">#76</a>, <a href="https://github.com/OFA-Sys/OFA/issues/105">#105</a> and <a href="https://github.com/OFA-Sys/OFA/issues/73">#73</a> for more information.
325
- </p>
326
- </details>
327
- <details>
328
- <summary><b>2. Shuffle the Training Data</b></summary>
329
- <p>
330
- (Optional, but achieves better finetuning accuracy): If the disk storage is sufficient, we recommend to prepare the shuffled training data for each epoch in advance. In our experiments, we use shuffling which brings around <b>+0.3</b> improvement on VQA accuracy.
331
- </p>
332
- <pre>
333
- cd dataset/vqa_data
334
- ln vqa_train.tsv vqa_train_1.tsv
335
- for idx in `seq 1 9`;do shuf vqa_train_${idx}.tsv > vqa_train_$[${idx}+1].tsv;done # each file is used for an epoch
336
- </pre>
337
- </details>
338
- <details>
339
- <summary><b>3. Finetuning</b></summary>
340
- <p>
341
- In our experiments, the VQA finetuning is performed on 4 8-A100-GPU servers (<i>with RDMA</i>). Here provides the finetuning script <code>train_vqa_distributed.sh</code>, which supports multi-server distributed training (as well as single-server training). Please refer to the comments in the beginning of the script and set the configs correctly according to your distribution environment. If you have shuffled the training data in the previous step, please correctly specify the training data path following the guide in the script comments. <b>The command should be run on each worker.</b>
342
- </p>
343
- <pre>
344
- # run on each worker after the distributed and data configs have been correctly set following the guide in train_vqa_distributed.sh
345
- cd run_scripts/vqa
346
- bash train_vqa_distributed.sh
347
- </pre>
348
- <p>
349
- In our experiments, the finetuning costs around 36 hours (for 12 epochs). After each epoch, an evaluation on validation set is performed. The best validation accuracy during finetuning will be around 80.8. The log is saved in <code>${log_dir}</code>.
350
- </p>
351
- <p>
352
- <i>(Update on validation time-cost)</i> As will be mentioned in the <i>4. Inference</i> section, we prepare 2 types of inference: beam-search and all-candidate inference. By default, all-candidate inference is used for validation during fine-tuning, which achieves better accuracy but costs much time. Now we have added a new option in the training scripts called <code>--val-inference-type</code> to switch the validation inference type during fine-tuning. If you feel the validation takes too long, you can refer to <a href="https://github.com/OFA-Sys/OFA/pull/79">PR #79</a> to activate beam-search validation, which significantly takes much less time, with around 0.5-0.6 validation score degradation compared with all-candidate validation.
353
- </p>
354
- </details>
355
- <details>
356
- <summary><b>4. Inference</b></summary>
357
- <p>
358
- We provide 2 types of inference, <b>beam-search</b> (much faster but gets sub-optimal accuracy) and <b>all-candidate evaluation</b> (slower but best accuracy). <br></br>
359
- For beam-search inference, use the script <code>evaluate_vqa_beam.sh</code>. Refer to the command below. The inference on test set costs around 16 GPU hours. After inference on test set, the result JSON file will be dumped in the <code>${result_path}</code> defined in the shell script. You can submit the result <code>test_predict.json</code> to <a href="https://eval.ai/web/challenges/challenge-page/830/overview">EvalAI</a>. Using our released finetuned checkpoint, beam-search inference will get 80.15 validation accuracy, 79.36 test-dev accuracy and 79.48 test-std accuracy (around 0.6 lower than all-candidate evaluation).
360
- </p>
361
- <pre>
362
- cd run_scripts/vqa
363
- bash evaluate_vqa_beam.sh val # specify 'val' or 'test'
364
- </pre>
365
- <p>
366
- For all-candidate evaluation, we recommend to use the distributed script <code>evaluate_vqa_allcand_distributed.sh</code>. Please refer to the guide in the script to set the distributed configs before running. The result JSON file will be dumped in the <code>${result_path}</code> defined in the shell script of rank-0 server. All-candidate evaluation computes scores on all the candidate answers in the VQA dataset, which achieves <b>80.82</b> validation accuracy, <b>79.87</b> test-dev accuracy and <b>80.02</b> test-std accuracy, reproducing our reported results in the paper. However, the inference on test set costs around 1k GPU hours, which is much slower.
367
- </p>
368
- <pre>
369
- # run on each worker after the distributed configs have been correctly set following the guide in evaluate_vqa_allcand_distributed.sh
370
- cd run_scripts/vqa
371
- bash evaluate_vqa_allcand_distributed.sh val # specify 'val' or 'test'
372
- </pre>
373
- </details>
374
-
375
- ## Visual Grounding (Referring Expression Comprehension)
376
- Here provides procedures for you to prepare data, train, and evaluate your model on visual grounding.
377
- <details>
378
- <summary><b>1. Prepare the Dataset & Checkpoints</b></summary>
379
- <p>
380
- Download data (see <a href='datasets.md'>datasets.md</a>) and models (see <a href='checkpoints.md'>checkpoints.md</a>) and put them in the correct directory. We provide RefCOCO (split by UNC), RefCOCO+ (split by UNC) and RefCOCOg (split by UMD) datasets. See <a href='https://www.tensorflow.org/datasets/catalog/ref_coco'>RefCOCO</a> and <a href="https://github.com/lichengunc/refer">Refer</a> for more details. Note that in the original dataset, each region-coord (or bounding box) may corresponds to multiple descriptive texts. We split these texts into multiple samples so that the region-coord in each sample corresponds to only one text. Each line of the processed dataset represents a sample with the following format. The information of uniq-id, image-id, text, region-coord (separated by commas), image base64 string are separated by tabs.
381
- </p>
382
- <pre>
383
- 79_1 237367 A woman in a white blouse holding a glass of wine. 230.79,121.75,423.66,463.06 9j/4AAQ...1pAz/9k=
384
- </pre>
385
- </details>
386
- <details>
387
- <summary><b>2. Finetuning</b></summary>
388
- <p>
389
- Unlike the original paper, we finetune OFA with a drop-path rate of 0.2, and found that training with this hyper-parameter achieves better results. We will update the reported results of the paper later.
390
- </p>
391
- <pre>
392
- cd run_scripts/refcoco
393
- nohup sh train_refcoco.sh > train_refcoco.out & # finetune for refcoco
394
- nohup sh train_refcocoplus.sh > train_refcocoplus.out & # finetune for refcoco+
395
- nohup sh train_refcocog.sh > train_refcocog.out & # finetune for refcocog
396
- </pre>
397
- </details>
398
- <details>
399
- <summary><b>3. Inference</b></summary>
400
- <p>
401
- Run the following commands for the evaluation.
402
- </p>
403
- <pre>
404
- cd run_scripts/refcoco ; sh evaluate_refcoco.sh # inference & evaluate for refcoco/refcoco+/refcocog
405
- </pre>
406
- </details>
407
-
408
- ## Visual Entailment
409
- We provide steps for you to reproduce our results in visual entailment. See the details below.
410
-
411
- <details>
412
- <summary><b>1. Prepare the Dataset & Checkpoints</b></summary>
413
- <p>
414
- Download data (see <a href="datasets.md">datasets.md</a>) and models (see <a href="checkpoints.md">checkpoints.md</a>) and put them in the correct directory. Each line of the processed dataset represents a sample with the following format. The information of uniq-id, image-id, image base64 string, hypothesis, caption (or text premise), label are separated by tabs.
415
- </p>
416
- <pre>
417
- 252244149.jpg#1r1n 252244149 /9j/4AAQ...MD/2Q== a man in pink and gold is chewing on a wooden toothpick. a man in pink is chewing a toothpick on the subway. neutral
418
- </pre>
419
- </details>
420
- <details>
421
- <summary><b>2. Finetuning</b></summary>
422
- <p>
423
- In our experiments, the SNLI-VE finetuning is performed on 8 NVIDIA-V100 GPUs with 32GB memory. In this task, we experimented with only a few sets of hyperparameters. We believe that proper hyperparameter tuning can lead to further accuracy improvement.
424
- </p>
425
- <pre>
426
- cd run_scripts/snli_ve
427
- nohup sh train_snli_ve.sh > train_snli_ve.out & # finetune for snli_ve
428
- </pre>
429
- </details>
430
- <details>
431
- <summary><b>3. Inference</b></summary>
432
- <p>
433
- Run the following command to obtain the results.
434
- </p>
435
- <pre>
436
- cd run_scripts/snli_ve ; sh evaluate_snli_ve.sh dev # specify 'dev' or 'test'
437
- </pre>
438
- </details>
439
-
440
- ## GLUE
441
- Here we provide steps for you to finetune and evaluate our model on language understanding tasks. We demonstrate our practice for the GLUE benchmark.
442
-
443
- <details>
444
- <summary><b>1. Prepare the Dataset & Checkpoints</b></summary>
445
- <p>
446
- Download data (see <a href="datasets.md">datasets.md</a>) and models (see <a href="checkpoints.md">checkpoints.md</a>) and put them in the correct directory. we provide 7 language understanding datasets from GLUE benchmark, including COLA, MNLI, MRPC, QNLI, QQP, RTE and SST2. More details about these datasets can be found in this <a href="https://openreview.net/pdf?id=rJ4km2R5t7">link</a>.
447
- </p>
448
- </details>
449
- <details>
450
- <summary><b>2. Finetuning</b></summary>
451
- <p>
452
- For each task, we have tried multiple sets of hyperparameters (including learning rate, batch size, training epochs). The results under different sets of hyperparameters can be found in <code>${log_dir}</code>.
453
- </p>
454
- <pre>
455
- cd run_scripts/glue
456
- nohup sh train_cola.sh > train_cola.out & # finetune for cola
457
- nohup sh train_mnli.sh > train_mnli.out & # finetune for mnli
458
- nohup sh train_mrpc.sh > train_mrpc.out & # finetune for mrpc
459
- nohup sh train_qnli.sh > train_qnli.out & # finetune for qnli
460
- nohup sh train_qqp.sh > train_qqp.out & # finetune for qqp
461
- nohup sh train_rte.sh > train_rte.out & # finetune for rte
462
- nohup sh train_sst2.sh > train_sst2.out & # finetune for sst2
463
- </pre>
464
- </details>
465
-
466
- ## Image Classification on ImageNet-1K
467
- We provide the finetuning and inference codes which reproduce **85.0 ImageNet-1K accuracy**, slightly better than reported in our paper.
468
-
469
- <details>
470
- <summary><b>1. Prepare the Dataset & Checkpoints</b></summary>
471
- <p>
472
- Download data (see <a href="datasets.md">datasets.md</a>) and models (see <a href="checkpoints.md">checkpoints.md</a>) and put them in the correct directory. Our provided data is derived from the original <a href="http://image-net.org/">ImageNet-1K</a> (ILSVRC2012 train & validation) dataset and shares the same data split with it. To formulate the classification task into seq2seq paradigm, we use the <a href="https://github.com/HoldenCaulfieldRye/caffe/blob/master/data/ilsvrc12/synset_words.txt">synset words</a> provided by Caffe as the generation target for each image class. Each line of the processed dataset represents a sample with the following format. The information of image base64 string, classification label (1-indexed, conform to the order in <code>synset_words.txt</code>), synset words of the label are separated by tabs.
473
- </p>
474
- <pre>
475
- _9j_4AAQS...fzX__Z 769 rugby ball
476
- </pre>
477
- </details>
478
- <details>
479
- <summary><b>2. Shuffle the Training Data</b></summary>
480
- <p>
481
- (Optional, but achieves better finetuning accuracy): If the disk storage is sufficient, we recommend to prepare the shuffled training data for each epoch in advance. In our experiments, we use shuffling which brings around <b>+0.2</b> improvement on ImageNet-1K accuracy.
482
- </p>
483
- <pre>
484
- cd dataset/imagenet_1k_data
485
- ln imagenet_1k_train.tsv imagenet_1k_train_1.tsv
486
- for idx in `seq 1 9`;do shuf imagenet_1k_train_${idx}.tsv > imagenet_1k_train_$[${idx}+1].tsv;done # each file is used for an epoch one by one
487
- </pre>
488
- </details>
489
- <details>
490
- <summary><b>3. Finetuning</b></summary>
491
- <p>
492
- In our experiments, the ImageNet-1K finetuning is performed on 2 8-A100-GPU servers (<i>with RDMA</i>). Here provides the finetuning script <code>train_imagenet_distributed.sh</code>, which supports multi-server distributed training (as well as single-server training). Please refer to the comments in the beginning of the script and set the configs correctly according to your distribution environment. If you have shuffled the training data in the previous step, please correctly specify the training data path following the guide in the script comments. <b>The command should be run on each worker.</b> For quick evaluation during finetuning, by default we sample 20% of the original validation split and report accuracy on this subset after each epoch. The accuracy on the validation subset is generally ±0.1 relative to accuracy on the whole validation split.
493
- </p>
494
- <pre>
495
- # run on each worker after the distributed and data configs have been correctly set following the guide in train_imagenet_distributed.sh
496
- cd run_scripts/image_classify
497
- bash train_imagenet_distributed.sh
498
- </pre>
499
- <p>
500
- In our experiments, the finetuning costs around 80 hours (for 32 epochs). The best accuracy on validation subset during finetuning will be around 85.0. The log is saved in <code>${log_dir}</code>.
501
- </p>
502
- </details>
503
- <details>
504
- <summary><b>4. Inference</b></summary>
505
- <p>
506
- To get the validation accuracy on the whole ImageNet-1K validation set, run the following command. The evaluation costs around 10 GPU hours. The accuracy will be reported in the stdout (expected to be around <b>85.0</b>).
507
- </p>
508
- <pre>
509
- cd run_scripts/image_classify ; sh evaluate_imagenet.sh # inference & evaluate for imagenet-1k
510
- </pre>
511
- </details>
512
-
513
- ## Gigaword
514
- We provide steps for you to reproduce our results in Gigaword. See the details below.
515
-
516
- <details>
517
- <summary><b>1. Prepare the Dataset & Checkpoints</b></summary>
518
- <p>
519
- Download data (see <a href="datasets.md">datasets.md</a>) and models (see <a href="checkpoints.md">checkpoints.md</a>) and put them in the correct directory. The original dataset is taken from <a href="https://github.com/microsoft/unilm/">UniLM</a> and we organized the data into the tsv format. Each line of the processed dataset represents a sample with the following format. The information of source and target texts are separated by tabs.
520
- </p>
521
- <pre>
522
- factory orders for manufactured goods rose #.# percent in september... us september factory orders up #.# percent
523
- </pre>
524
- </details>
525
- <details>
526
- <summary><b>2. Finetuning</b></summary>
527
- <p>
528
- Run the following command to train the model.
529
- </p>
530
- <pre>
531
- cd run_scripts/gigaword
532
- nohup sh train_gigaword.sh > train_gigaword.out & # finetune for gigaword
533
- </pre>
534
- </details>
535
- <details>
536
- <summary><b>3. Inference</b></summary>
537
- <p>
538
- Run the following command to obtain the results (~36.43 rougeL).
539
- </p>
540
- <pre>
541
- cd run_scripts/gigaword ; sh evaluate_gigaword.sh # inference & evaluate for gigaword
542
- </pre>
543
- </details>
544
-
545
- <br></br>
546
-
547
- # Gallery
548
- Below we provide examples of OFA in text-to-image generation and open-ended VQA. Also, we demonstrate its performance in unseen task (Grounded QA) as well as unseen domain (Visual Grounding on images from unseen domains).
549
-
550
- ## Text-to-Image Generation
551
-
552
- ![case1](examples/case1.png)
553
-
554
-
555
- ## Open-Ended VQA
556
- ![open_vqa](examples/open_vqa.png)
557
-
558
- ## Grounded QA (unseen task)
559
- ![grounded_qa](examples/grounded_qa.png)
560
-
561
- ## Visual Grounding (unseen domain)
562
- ![vg](examples/viusal_grounding.png)
563
- <br></br>
564
-
565
- # Related Codebase
566
- * [Fairseq](https://github.com/pytorch/fairseq)
567
- * [taming-transformers](https://github.com/CompVis/taming-transformers)
568
- <br></br>
569
-
570
-
571
- # Getting Involved
572
- Feel free to submit Github issues or pull requests. Welcome to contribute to our project!
573
-
574
- To contact us, never hestitate to send an email to `zheluo.wp@alibaba-inc.com` or `junyang.ljy@alibaba-inc.com`!
575
- <br></br>
576
-
577
-
578
- # Citation
579
- Please cite our paper if you find it helpful :)
580
-
581
- ```
582
- @article{wang2022ofa,
583
- author = {Peng Wang and
584
- An Yang and
585
- Rui Men and
586
- Junyang Lin and
587
- Shuai Bai and
588
- Zhikang Li and
589
- Jianxin Ma and
590
- Chang Zhou and
591
- Jingren Zhou and
592
- Hongxia Yang},
593
- title = {OFA: Unifying Architectures, Tasks, and Modalities Through a Simple Sequence-to-Sequence
594
- Learning Framework},
595
- journal = {CoRR},
596
- volume = {abs/2202.03052},
597
- year = {2022}
598
- }
599
- ```
600
- <br></br>
 
1
+ ---
2
+ title: OFA-Image_Caption
3
+ emoji: 🖼
4
+ colorFrom: red
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ app_file: app.py
8
+ pinned: false
9
+ ---
10
+ # Configuration
11
+ `title`: _string_
12
+ OFA Image Caption
13
+ `emoji`: _string_
14
+ 🖼
15
+ `colorFrom`: _string_
16
+ red
17
+ `colorTo`: _string_
18
+ indigo
19
+ `sdk`: _string_
20
+ gradio
21
+ `app_file`: _string_
22
+ app.py
23
+
24
+
25
+ `pinned`: _boolean_
26
+ false