bhavitvyamalik commited on
Commit
ccc24a7
1 Parent(s): 0ec8765

first commit

Browse files
README.md ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # CLIP-Vision-mBART50 Seq2Seq Encoder-Decoder Model
2
+
3
+ Pretrained CLIP-Vision-mBART50 pre-trained on subset of translated Conceptual-12M image-text pairs using a seq2seq model training objective. 2.5M cleaned English image-text pairs are translated using Marian Model for respective languages to 2.5M examples each in English, French, German and Spanish. We trained CLIP-Vision-mBART50 model during community week hosted by Huggingface 🤗 using JAX/Flax.
4
+
5
+ ## Model description
6
+ CLIP-Vision-mBART50 is a modified mBART50 model which takes in visual embeddings from CLIP-Vision transformer and concatenates them with mBART textual embeddings before passing them to the self-attention layers of mBART. This is done for deep cross-modal interaction between the two modes.
7
+
8
+ ## Intended uses & limitations❗️
9
+ You can use the raw model for encoder decoder network where you want the encoder to encode images and decoder to decode text.
10
+
11
+ Note that this model is primarily aimed at being fine-tuned on tasks like multi-lingual/mono-lingual image captioning.
12
+
13
+ ### How to use❓
14
+ You will need to clone the model from [here](https://github.com/gchhablani/multilingual-image-captioning). An example of usage is shown below:
15
+ ```python
16
+ >>> from torchvision.io import read_image
17
+ >>> import numpy as np
18
+ >>> import os
19
+ >>> from transformers import CLIPProcessor, MBart50TokenizerFast
20
+ >>> from model.flax_clip_vision_mbart.modeling_clip_vision_mbart import FlaxCLIPVisionMBartForConditionalGeneration
21
+ >>> image_path = os.path.join('images/val2014', os.listdir('images/val2014')[0])
22
+ >>> img = read_image(image_path) # reading image
23
+ >>> clip_processor = CLIPProcessor.from_pretrained('openai/clip-vit-base-patch32')
24
+ >>> clip_outputs = clip_processor(images=img)
25
+ >>> clip_outputs['pixel_values'][0] = clip_outputs['pixel_values'][0].transpose(1,2,0) # Need to transpose images as model expected channel last images.
26
+ >>> tokenizer = MBart50TokenizerFast.from_pretrained('facebook/mbart-large-50"')
27
+ >>> model = FlaxCLIPVisionBertForMaskedLM.from_pretrained('flax-community/clip-vit-base-patch32_mbart-large-50')
28
+ >>> output_ids = model.generate(batch["pixel_values"], forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"], num_beams=4, max_length=64).sequences # "en_XX is the language code in which you want the translation
29
+ # en_XX: English, fr_XX: French, es_XX: Spanish, de_DE: Deutsch
30
+ >>> output_string = tokenizer.batch_decode(output_ids.reshape(-1, 64), skip_special_tokens=True, max_length=64)
31
+ >>> output_string # relevant caption
32
+ ```
33
+
34
+ ## Training data 🏋🏻‍♂️
35
+ The Multi-lingual image captioning model was trained on a subset of Conceptual 12M dataset by Google:
36
+ <br>
37
+ <br>
38
+ [Conceptual 12M](https://github.com/google-research-datasets/conceptual-12m), Introduced by Changpinyo et al. in [Conceptual 12M: Pushing Web-Scale Image-Text Pre-Training To Recognize Long-Tail Visual Concepts](https://arxiv.org/abs/2102.08981).
39
+
40
+ The translated dataset can be downloaded from [conceptual-12m-multilingual-marian](https://huggingface.co/datasets/flax-community/conceptual-12m-multilingual-marian). We do not provide images as we do not own any of them. One can download images from the `image_url` section of the original Conceptual 12M dataset.
41
+
42
+ ## Data Cleaning 🧹
43
+ Though the original dataset contains 12M image-text pairs, a lot of the URLs are invalid now, and in some cases, images are corrupt or broken. We remove such examples from our data, which leaves us with approximately 10M image-text pairs.
44
+
45
+ #### **Train set:**
46
+ Total data: <br>
47
+ 10010625 captions <br>
48
+ 2502656 images br><br>
49
+
50
+ Language-wise distribution: <br>
51
+ English: 2502656 captions<br>
52
+ Spanish: 2502656 captions<br>
53
+ Deutsch: 2502656 captions<br>
54
+ French: 2502656 captions<br>
55
+
56
+ #### **Validation set**
57
+ Total data: <br>
58
+ 110592 captions <br>
59
+ 27648 images <br><br>
60
+
61
+ Language-wise distribution: <br>
62
+ English: 27648 captions<br>
63
+ Spanish: 27648 captions<br>
64
+ Deutsch: 27648 captions<br>
65
+ French: 27648 captions<br>
66
+
67
+ ## Training procedure 👨🏻‍💻
68
+ ### Training
69
+ The model was trained on Google Cloud Engine TPUv3-8 machine (with 335 GB of RAM, 1000 GB of hard drive, 96 CPU cores) **8 v3 TPU cores** for 42K steps with a batch size of 128 and a sequence length of 128. The
70
+ optimizer used is Adam with a learning rate of 3e-4, β1 = 0.9, β2 = 0.98 and
71
+ ε = 1e-8, a weight decay of 0.01, learning rate warmup for 1,000 steps and linear decay of the learning
72
+ rate after.
73
+
74
+ We tracked experiments using Tensorboard which can be found in `Training Metrics` tab.
75
+
76
+ #### **Pretraining Results 📊**
77
+
78
+ Our model reached **eval loss of ~2.6** around ~70K steps. Here are the BLEU^ scores for different languages:
79
+
80
+ |Language |BLEU-1|BLEU-2|BLEU-3|BLEU-4|
81
+ |--------------------------|------|------|------|------|
82
+ |English | 0.163| 0.127| 0.10 | 0.081|
83
+ |Spanish | 0.171| 0.133| 0.114| 0.082|
84
+ |German | 0.165| 0.129| 0.103| 0.077|
85
+ |French | 0.162| 0.124| 0.104| 0.073|
86
+
87
+ ^BLEU scores are out of 1
88
+
89
+
90
+ ## **App Demo**
91
+
92
+ You can try out our model on 🤗 Huggingface's spaces 🪐 :
93
+ [Streamlit app of Multi-lingual Image Captioning model on Huggingface Spaces](https://huggingface.co/spaces/flax-community/multilingual-image-captioning)
94
+
95
+
96
+ ## Team Members
97
+ - Bhavitvya Malik [@bhavitvyamalik](https://github.com/bhavitvyamalik)
98
+ - Gunjan Chhablani [@gchhablani](https://github.com/gchhablani)
99
+
100
+ ## Credits
101
+ Thanks to Huggingface 🤗 & Google JAX/FLAX team for such a wonderful community week. Big thanks to [@patrickvonplaten](https://github.com/patrickvonplaten) and [@patil-suraj](https://github.com/patil-suraj) for helping us with our solution during the community week.
102
+
103
+ <img src=https://pbs.twimg.com/media/E443fPjX0AY1BsR.jpg:large>
config.json ADDED
@@ -0,0 +1,316 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "CLIPVisionMBartForConditionalGeneration"
4
+ ],
5
+ "clip_vision_config": {
6
+ "_name_or_path": "",
7
+ "add_cross_attention": false,
8
+ "architectures": [
9
+ "CLIPModel"
10
+ ],
11
+ "attention_dropout": 0.0,
12
+ "bad_words_ids": null,
13
+ "bos_token_id": null,
14
+ "chunk_size_feed_forward": 0,
15
+ "decoder_start_token_id": null,
16
+ "diversity_penalty": 0.0,
17
+ "do_sample": false,
18
+ "dropout": 0.0,
19
+ "early_stopping": false,
20
+ "encoder_no_repeat_ngram_size": 0,
21
+ "eos_token_id": null,
22
+ "finetuning_task": null,
23
+ "forced_bos_token_id": null,
24
+ "forced_eos_token_id": null,
25
+ "gradient_checkpointing": false,
26
+ "hidden_act": "quick_gelu",
27
+ "hidden_size": 768,
28
+ "id2label": {
29
+ "0": "LABEL_0",
30
+ "1": "LABEL_1"
31
+ },
32
+ "image_size": 224,
33
+ "initializer_factor": 1.0,
34
+ "initializer_range": 0.02,
35
+ "intermediate_size": 3072,
36
+ "is_decoder": false,
37
+ "is_encoder_decoder": false,
38
+ "label2id": {
39
+ "LABEL_0": 0,
40
+ "LABEL_1": 1
41
+ },
42
+ "layer_norm_eps": 1e-05,
43
+ "length_penalty": 1.0,
44
+ "max_length": 20,
45
+ "min_length": 0,
46
+ "model_type": "clip_vision_model",
47
+ "no_repeat_ngram_size": 0,
48
+ "num_attention_heads": 12,
49
+ "num_beam_groups": 1,
50
+ "num_beams": 1,
51
+ "num_hidden_layers": 12,
52
+ "num_return_sequences": 1,
53
+ "output_attentions": false,
54
+ "output_hidden_states": false,
55
+ "output_scores": false,
56
+ "pad_token_id": null,
57
+ "patch_size": 32,
58
+ "prefix": null,
59
+ "problem_type": null,
60
+ "projection_dim": 512,
61
+ "pruned_heads": {},
62
+ "remove_invalid_values": false,
63
+ "repetition_penalty": 1.0,
64
+ "return_dict": true,
65
+ "return_dict_in_generate": false,
66
+ "sep_token_id": null,
67
+ "task_specific_params": null,
68
+ "temperature": 1.0,
69
+ "text_config": {
70
+ "_name_or_path": "",
71
+ "add_cross_attention": false,
72
+ "architectures": null,
73
+ "attention_dropout": 0.0,
74
+ "bad_words_ids": null,
75
+ "bos_token_id": 0,
76
+ "chunk_size_feed_forward": 0,
77
+ "decoder_start_token_id": null,
78
+ "diversity_penalty": 0.0,
79
+ "do_sample": false,
80
+ "dropout": 0.0,
81
+ "early_stopping": false,
82
+ "encoder_no_repeat_ngram_size": 0,
83
+ "eos_token_id": 2,
84
+ "finetuning_task": null,
85
+ "forced_bos_token_id": null,
86
+ "forced_eos_token_id": null,
87
+ "gradient_checkpointing": false,
88
+ "hidden_act": "quick_gelu",
89
+ "hidden_size": 512,
90
+ "id2label": {
91
+ "0": "LABEL_0",
92
+ "1": "LABEL_1"
93
+ },
94
+ "initializer_factor": 1.0,
95
+ "initializer_range": 0.02,
96
+ "intermediate_size": 2048,
97
+ "is_decoder": false,
98
+ "is_encoder_decoder": false,
99
+ "label2id": {
100
+ "LABEL_0": 0,
101
+ "LABEL_1": 1
102
+ },
103
+ "layer_norm_eps": 1e-05,
104
+ "length_penalty": 1.0,
105
+ "max_length": 20,
106
+ "max_position_embeddings": 77,
107
+ "min_length": 0,
108
+ "model_type": "clip_text_model",
109
+ "no_repeat_ngram_size": 0,
110
+ "num_attention_heads": 8,
111
+ "num_beam_groups": 1,
112
+ "num_beams": 1,
113
+ "num_hidden_layers": 12,
114
+ "num_return_sequences": 1,
115
+ "output_attentions": false,
116
+ "output_hidden_states": false,
117
+ "output_scores": false,
118
+ "pad_token_id": 1,
119
+ "prefix": null,
120
+ "problem_type": null,
121
+ "pruned_heads": {},
122
+ "remove_invalid_values": false,
123
+ "repetition_penalty": 1.0,
124
+ "return_dict": true,
125
+ "return_dict_in_generate": false,
126
+ "sep_token_id": null,
127
+ "task_specific_params": null,
128
+ "temperature": 1.0,
129
+ "tie_encoder_decoder": false,
130
+ "tie_word_embeddings": true,
131
+ "tokenizer_class": null,
132
+ "top_k": 50,
133
+ "top_p": 1.0,
134
+ "torchscript": false,
135
+ "transformers_version": "4.7.0.dev0",
136
+ "use_bfloat16": false,
137
+ "vocab_size": 49408
138
+ },
139
+ "text_config_dict": null,
140
+ "tie_encoder_decoder": false,
141
+ "tie_word_embeddings": true,
142
+ "tokenizer_class": null,
143
+ "top_k": 50,
144
+ "top_p": 1.0,
145
+ "torch_dtype": null,
146
+ "torchscript": false,
147
+ "transformers_version": "4.9.0.dev0",
148
+ "use_bfloat16": false,
149
+ "vision_config": {
150
+ "_name_or_path": "",
151
+ "add_cross_attention": false,
152
+ "architectures": null,
153
+ "attention_dropout": 0.0,
154
+ "bad_words_ids": null,
155
+ "bos_token_id": null,
156
+ "chunk_size_feed_forward": 0,
157
+ "decoder_start_token_id": null,
158
+ "diversity_penalty": 0.0,
159
+ "do_sample": false,
160
+ "dropout": 0.0,
161
+ "early_stopping": false,
162
+ "encoder_no_repeat_ngram_size": 0,
163
+ "eos_token_id": null,
164
+ "finetuning_task": null,
165
+ "forced_bos_token_id": null,
166
+ "forced_eos_token_id": null,
167
+ "gradient_checkpointing": false,
168
+ "hidden_act": "quick_gelu",
169
+ "hidden_size": 768,
170
+ "id2label": {
171
+ "0": "LABEL_0",
172
+ "1": "LABEL_1"
173
+ },
174
+ "image_size": 224,
175
+ "initializer_factor": 1.0,
176
+ "initializer_range": 0.02,
177
+ "intermediate_size": 3072,
178
+ "is_decoder": false,
179
+ "is_encoder_decoder": false,
180
+ "label2id": {
181
+ "LABEL_0": 0,
182
+ "LABEL_1": 1
183
+ },
184
+ "layer_norm_eps": 1e-05,
185
+ "length_penalty": 1.0,
186
+ "max_length": 20,
187
+ "min_length": 0,
188
+ "model_type": "clip_vision_model",
189
+ "no_repeat_ngram_size": 0,
190
+ "num_attention_heads": 12,
191
+ "num_beam_groups": 1,
192
+ "num_beams": 1,
193
+ "num_hidden_layers": 12,
194
+ "num_return_sequences": 1,
195
+ "output_attentions": false,
196
+ "output_hidden_states": false,
197
+ "output_scores": false,
198
+ "pad_token_id": null,
199
+ "patch_size": 32,
200
+ "prefix": null,
201
+ "problem_type": null,
202
+ "pruned_heads": {},
203
+ "remove_invalid_values": false,
204
+ "repetition_penalty": 1.0,
205
+ "return_dict": true,
206
+ "return_dict_in_generate": false,
207
+ "sep_token_id": null,
208
+ "task_specific_params": null,
209
+ "temperature": 1.0,
210
+ "tie_encoder_decoder": false,
211
+ "tie_word_embeddings": true,
212
+ "tokenizer_class": null,
213
+ "top_k": 50,
214
+ "top_p": 1.0,
215
+ "torchscript": false,
216
+ "transformers_version": "4.7.0.dev0",
217
+ "use_bfloat16": false
218
+ },
219
+ "vision_config_dict": null
220
+ },
221
+ "is_encoder_decoder": true,
222
+ "mbart_config": {
223
+ "_name_or_path": "/home/suraj/projects/mbart-50/hf_models/mbart-50-large",
224
+ "_num_labels": 3,
225
+ "activation_dropout": 0.0,
226
+ "activation_function": "gelu",
227
+ "add_bias_logits": false,
228
+ "add_cross_attention": false,
229
+ "add_final_layer_norm": true,
230
+ "architectures": [
231
+ "MBartForConditionalGeneration"
232
+ ],
233
+ "attention_dropout": 0.0,
234
+ "bad_words_ids": null,
235
+ "bos_token_id": 0,
236
+ "chunk_size_feed_forward": 0,
237
+ "classif_dropout": 0.0,
238
+ "classifier_dropout": 0.0,
239
+ "d_model": 1024,
240
+ "decoder_attention_heads": 16,
241
+ "decoder_ffn_dim": 4096,
242
+ "decoder_layerdrop": 0.0,
243
+ "decoder_layers": 12,
244
+ "decoder_start_token_id": 2,
245
+ "diversity_penalty": 0.0,
246
+ "do_sample": false,
247
+ "dropout": 0.1,
248
+ "early_stopping": true,
249
+ "encoder_attention_heads": 16,
250
+ "encoder_ffn_dim": 4096,
251
+ "encoder_layerdrop": 0.0,
252
+ "encoder_layers": 12,
253
+ "encoder_no_repeat_ngram_size": 0,
254
+ "eos_token_id": 2,
255
+ "finetuning_task": null,
256
+ "forced_bos_token_id": null,
257
+ "forced_eos_token_id": 2,
258
+ "gradient_checkpointing": false,
259
+ "id2label": {
260
+ "0": "LABEL_0",
261
+ "1": "LABEL_1",
262
+ "2": "LABEL_2"
263
+ },
264
+ "init_std": 0.02,
265
+ "is_decoder": false,
266
+ "is_encoder_decoder": true,
267
+ "label2id": {
268
+ "LABEL_0": 0,
269
+ "LABEL_1": 1,
270
+ "LABEL_2": 2
271
+ },
272
+ "length_penalty": 1.0,
273
+ "max_length": 200,
274
+ "max_position_embeddings": 1024,
275
+ "min_length": 0,
276
+ "model_type": "mbart",
277
+ "no_repeat_ngram_size": 0,
278
+ "normalize_before": true,
279
+ "normalize_embedding": true,
280
+ "num_beam_groups": 1,
281
+ "num_beams": 5,
282
+ "num_hidden_layers": 12,
283
+ "num_return_sequences": 1,
284
+ "output_attentions": false,
285
+ "output_hidden_states": false,
286
+ "output_past": true,
287
+ "output_scores": false,
288
+ "pad_token_id": 1,
289
+ "prefix": null,
290
+ "problem_type": null,
291
+ "pruned_heads": {},
292
+ "remove_invalid_values": false,
293
+ "repetition_penalty": 1.0,
294
+ "return_dict": true,
295
+ "return_dict_in_generate": false,
296
+ "scale_embedding": true,
297
+ "sep_token_id": null,
298
+ "static_position_embeddings": false,
299
+ "task_specific_params": null,
300
+ "temperature": 1.0,
301
+ "tie_encoder_decoder": false,
302
+ "tie_word_embeddings": true,
303
+ "tokenizer_class": "MBart50Tokenizer",
304
+ "top_k": 50,
305
+ "top_p": 1.0,
306
+ "torch_dtype": null,
307
+ "torchscript": false,
308
+ "transformers_version": "4.9.0.dev0",
309
+ "use_bfloat16": false,
310
+ "use_cache": true,
311
+ "vocab_size": 250054
312
+ },
313
+ "model_type": "clip-vision-mbart",
314
+ "seed": 42,
315
+ "transformers_version": null
316
+ }
logs/events.out.tfevents.1626777663.t1v-n-e32efd45-w-0.1846718.3.v2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3e6c4ab6e9bb14ec893541f156b36d17bea7c9b8d8ba677bf80d7a0bb6c5853d
3
+ size 13753034