andrewluo ankur310794 commited on
Commit
a31db03
0 Parent(s):

Duplicate from nlpconnect/vit-gpt2-image-captioning

Browse files

Co-authored-by: Ankur Singh <ankur310794@users.noreply.huggingface.co>

.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.model filter=lfs diff=lfs merge=lfs -text
12
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
13
+ *.onnx filter=lfs diff=lfs merge=lfs -text
14
+ *.ot filter=lfs diff=lfs merge=lfs -text
15
+ *.parquet filter=lfs diff=lfs merge=lfs -text
16
+ *.pb filter=lfs diff=lfs merge=lfs -text
17
+ *.pt filter=lfs diff=lfs merge=lfs -text
18
+ *.pth filter=lfs diff=lfs merge=lfs -text
19
+ *.rar filter=lfs diff=lfs merge=lfs -text
20
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
22
+ *.tflite filter=lfs diff=lfs merge=lfs -text
23
+ *.tgz filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - image-to-text
4
+ - image-captioning
5
+ license: apache-2.0
6
+ widget:
7
+ - src: >-
8
+ https://huggingface.co/datasets/mishig/sample_images/resolve/main/savanna.jpg
9
+ example_title: Savanna
10
+ - src: >-
11
+ https://huggingface.co/datasets/mishig/sample_images/resolve/main/football-match.jpg
12
+ example_title: Football Match
13
+ - src: >-
14
+ https://huggingface.co/datasets/mishig/sample_images/resolve/main/airport.jpg
15
+ example_title: Airport
16
+ duplicated_from: nlpconnect/vit-gpt2-image-captioning
17
+ ---
18
+
19
+ # nlpconnect/vit-gpt2-image-captioning
20
+
21
+ This is an image captioning model trained by @ydshieh in [flax ](https://github.com/huggingface/transformers/tree/main/examples/flax/image-captioning) this is pytorch version of [this](https://huggingface.co/ydshieh/vit-gpt2-coco-en-ckpts).
22
+
23
+
24
+ # The Illustrated Image Captioning using transformers
25
+
26
+ ![](https://ankur3107.github.io/assets/images/vision-encoder-decoder.png)
27
+
28
+ * https://ankur3107.github.io/blogs/the-illustrated-image-captioning-using-transformers/
29
+
30
+
31
+ # Sample running code
32
+
33
+ ```python
34
+
35
+ from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
36
+ import torch
37
+ from PIL import Image
38
+
39
+ model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
40
+ feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
41
+ tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
42
+
43
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
44
+ model.to(device)
45
+
46
+
47
+
48
+ max_length = 16
49
+ num_beams = 4
50
+ gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
51
+ def predict_step(image_paths):
52
+ images = []
53
+ for image_path in image_paths:
54
+ i_image = Image.open(image_path)
55
+ if i_image.mode != "RGB":
56
+ i_image = i_image.convert(mode="RGB")
57
+
58
+ images.append(i_image)
59
+
60
+ pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
61
+ pixel_values = pixel_values.to(device)
62
+
63
+ output_ids = model.generate(pixel_values, **gen_kwargs)
64
+
65
+ preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
66
+ preds = [pred.strip() for pred in preds]
67
+ return preds
68
+
69
+
70
+ predict_step(['doctor.e16ba4e4.jpg']) # ['a woman in a hospital bed with a woman in a hospital bed']
71
+
72
+ ```
73
+
74
+ # Sample running code using transformers pipeline
75
+
76
+ ```python
77
+
78
+ from transformers import pipeline
79
+
80
+ image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
81
+
82
+ image_to_text("https://ankur3107.github.io/assets/images/image-captioning-example.png")
83
+
84
+ # [{'generated_text': 'a soccer game with a player jumping to catch the ball '}]
85
+
86
+
87
+ ```
88
+
89
+
90
+ # Contact for any help
91
+ * https://huggingface.co/ankur310794
92
+ * https://twitter.com/ankur310794
93
+ * http://github.com/ankur3107
94
+ * https://www.linkedin.com/in/ankur310794
config.json ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "vit-gpt-pt",
3
+ "architectures": [
4
+ "VisionEncoderDecoderModel"
5
+ ],
6
+ "bos_token_id": 50256,
7
+ "decoder": {
8
+ "_name_or_path": "",
9
+ "activation_function": "gelu_new",
10
+ "add_cross_attention": true,
11
+ "architectures": [
12
+ "GPT2LMHeadModel"
13
+ ],
14
+ "attn_pdrop": 0.1,
15
+ "bad_words_ids": null,
16
+ "bos_token_id": 50256,
17
+ "chunk_size_feed_forward": 0,
18
+ "cross_attention_hidden_size": null,
19
+ "decoder_start_token_id": 50256,
20
+ "diversity_penalty": 0.0,
21
+ "do_sample": false,
22
+ "early_stopping": false,
23
+ "embd_pdrop": 0.1,
24
+ "encoder_no_repeat_ngram_size": 0,
25
+ "eos_token_id": 50256,
26
+ "finetuning_task": null,
27
+ "forced_bos_token_id": null,
28
+ "forced_eos_token_id": null,
29
+ "id2label": {
30
+ "0": "LABEL_0",
31
+ "1": "LABEL_1"
32
+ },
33
+ "initializer_range": 0.02,
34
+ "is_decoder": true,
35
+ "is_encoder_decoder": false,
36
+ "label2id": {
37
+ "LABEL_0": 0,
38
+ "LABEL_1": 1
39
+ },
40
+ "layer_norm_epsilon": 1e-05,
41
+ "length_penalty": 1.0,
42
+ "max_length": 20,
43
+ "min_length": 0,
44
+ "model_type": "gpt2",
45
+ "n_ctx": 1024,
46
+ "n_embd": 768,
47
+ "n_head": 12,
48
+ "n_inner": null,
49
+ "n_layer": 12,
50
+ "n_positions": 1024,
51
+ "no_repeat_ngram_size": 0,
52
+ "num_beam_groups": 1,
53
+ "num_beams": 1,
54
+ "num_return_sequences": 1,
55
+ "output_attentions": false,
56
+ "output_hidden_states": false,
57
+ "output_scores": false,
58
+ "pad_token_id": 50256,
59
+ "prefix": null,
60
+ "problem_type": null,
61
+ "pruned_heads": {},
62
+ "remove_invalid_values": false,
63
+ "reorder_and_upcast_attn": false,
64
+ "repetition_penalty": 1.0,
65
+ "resid_pdrop": 0.1,
66
+ "return_dict": true,
67
+ "return_dict_in_generate": false,
68
+ "scale_attn_by_inverse_layer_idx": false,
69
+ "scale_attn_weights": true,
70
+ "sep_token_id": null,
71
+ "summary_activation": null,
72
+ "summary_first_dropout": 0.1,
73
+ "summary_proj_to_labels": true,
74
+ "summary_type": "cls_index",
75
+ "summary_use_proj": true,
76
+ "task_specific_params": {
77
+ "text-generation": {
78
+ "do_sample": true,
79
+ "max_length": 50
80
+ }
81
+ },
82
+ "temperature": 1.0,
83
+ "tie_encoder_decoder": false,
84
+ "tie_word_embeddings": true,
85
+ "tokenizer_class": null,
86
+ "top_k": 50,
87
+ "top_p": 1.0,
88
+ "torch_dtype": null,
89
+ "torchscript": false,
90
+ "transformers_version": "4.15.0",
91
+ "use_bfloat16": false,
92
+ "use_cache": true,
93
+ "vocab_size": 50257
94
+ },
95
+ "decoder_start_token_id": 50256,
96
+ "encoder": {
97
+ "_name_or_path": "",
98
+ "add_cross_attention": false,
99
+ "architectures": [
100
+ "ViTModel"
101
+ ],
102
+ "attention_probs_dropout_prob": 0.0,
103
+ "bad_words_ids": null,
104
+ "bos_token_id": null,
105
+ "chunk_size_feed_forward": 0,
106
+ "cross_attention_hidden_size": null,
107
+ "decoder_start_token_id": null,
108
+ "diversity_penalty": 0.0,
109
+ "do_sample": false,
110
+ "early_stopping": false,
111
+ "encoder_no_repeat_ngram_size": 0,
112
+ "eos_token_id": null,
113
+ "finetuning_task": null,
114
+ "forced_bos_token_id": null,
115
+ "forced_eos_token_id": null,
116
+ "hidden_act": "gelu",
117
+ "hidden_dropout_prob": 0.0,
118
+ "hidden_size": 768,
119
+ "id2label": {
120
+ "0": "LABEL_0",
121
+ "1": "LABEL_1"
122
+ },
123
+ "image_size": 224,
124
+ "initializer_range": 0.02,
125
+ "intermediate_size": 3072,
126
+ "is_decoder": false,
127
+ "is_encoder_decoder": false,
128
+ "label2id": {
129
+ "LABEL_0": 0,
130
+ "LABEL_1": 1
131
+ },
132
+ "layer_norm_eps": 1e-12,
133
+ "length_penalty": 1.0,
134
+ "max_length": 20,
135
+ "min_length": 0,
136
+ "model_type": "vit",
137
+ "no_repeat_ngram_size": 0,
138
+ "num_attention_heads": 12,
139
+ "num_beam_groups": 1,
140
+ "num_beams": 1,
141
+ "num_channels": 3,
142
+ "num_hidden_layers": 12,
143
+ "num_return_sequences": 1,
144
+ "output_attentions": false,
145
+ "output_hidden_states": false,
146
+ "output_scores": false,
147
+ "pad_token_id": null,
148
+ "patch_size": 16,
149
+ "prefix": null,
150
+ "problem_type": null,
151
+ "pruned_heads": {},
152
+ "qkv_bias": true,
153
+ "remove_invalid_values": false,
154
+ "repetition_penalty": 1.0,
155
+ "return_dict": true,
156
+ "return_dict_in_generate": false,
157
+ "sep_token_id": null,
158
+ "task_specific_params": null,
159
+ "temperature": 1.0,
160
+ "tie_encoder_decoder": false,
161
+ "tie_word_embeddings": true,
162
+ "tokenizer_class": null,
163
+ "top_k": 50,
164
+ "top_p": 1.0,
165
+ "torch_dtype": null,
166
+ "torchscript": false,
167
+ "transformers_version": "4.15.0",
168
+ "use_bfloat16": false
169
+ },
170
+ "eos_token_id": 50256,
171
+ "is_encoder_decoder": true,
172
+ "model_type": "vision-encoder-decoder",
173
+ "pad_token_id": 50256,
174
+ "tie_word_embeddings": false,
175
+ "torch_dtype": "float32",
176
+ "transformers_version": null
177
+ }
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
preprocessor_config.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_normalize": true,
3
+ "do_resize": true,
4
+ "feature_extractor_type": "ViTFeatureExtractor",
5
+ "image_mean": [
6
+ 0.5,
7
+ 0.5,
8
+ 0.5
9
+ ],
10
+ "image_std": [
11
+ 0.5,
12
+ 0.5,
13
+ 0.5
14
+ ],
15
+ "resample": 2,
16
+ "size": 224
17
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2605a69b760d0b218c3b5d2069ba070d28f279a16ce4bc87bf019b75a91553e6
3
+ size 982141993
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"bos_token": "<|endoftext|>", "eos_token": "<|endoftext|>", "unk_token": "<|endoftext|>", "pad_token": "<|endoftext|>"}
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "<|endoftext|>", "bos_token": "<|endoftext|>", "eos_token": "<|endoftext|>", "add_prefix_space": false, "model_max_length": 1024, "special_tokens_map_file": null, "name_or_path": "./models/", "tokenizer_class": "GPT2Tokenizer"}
vocab.json ADDED
The diff for this file is too large to render. See raw diff