KraftCode commited on
Commit
1fdea64
1 Parent(s): 8fe1f0a

Upload 9 files

Browse files

image to caption

README.md CHANGED
@@ -1,3 +1,90 @@
1
- ---
2
- license: bsd
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - image-to-text
4
+ - image-captioning
5
+ license: apache-2.0
6
+ widget:
7
+ - src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/savanna.jpg
8
+ example_title: Savanna
9
+ - src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/football-match.jpg
10
+ example_title: Football Match
11
+ - src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/airport.jpg
12
+ example_title: Airport
13
+ ---
14
+
15
+ # nlpconnect/vit-gpt2-image-captioning
16
+
17
+ 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).
18
+
19
+
20
+ # The Illustrated Image Captioning using transformers
21
+
22
+ ![](https://ankur3107.github.io/assets/images/vision-encoder-decoder.png)
23
+
24
+ * https://ankur3107.github.io/blogs/the-illustrated-image-captioning-using-transformers/
25
+
26
+
27
+ # Sample running code
28
+
29
+ ```python
30
+
31
+ from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
32
+ import torch
33
+ from PIL import Image
34
+
35
+ model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
36
+ feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
37
+ tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
38
+
39
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
40
+ model.to(device)
41
+
42
+
43
+
44
+ max_length = 16
45
+ num_beams = 4
46
+ gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
47
+ def predict_step(image_paths):
48
+ images = []
49
+ for image_path in image_paths:
50
+ i_image = Image.open(image_path)
51
+ if i_image.mode != "RGB":
52
+ i_image = i_image.convert(mode="RGB")
53
+
54
+ images.append(i_image)
55
+
56
+ pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
57
+ pixel_values = pixel_values.to(device)
58
+
59
+ output_ids = model.generate(pixel_values, **gen_kwargs)
60
+
61
+ preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
62
+ preds = [pred.strip() for pred in preds]
63
+ return preds
64
+
65
+
66
+ predict_step(['doctor.e16ba4e4.jpg']) # ['a woman in a hospital bed with a woman in a hospital bed']
67
+
68
+ ```
69
+
70
+ # Sample running code using transformers pipeline
71
+
72
+ ```python
73
+
74
+ from transformers import pipeline
75
+
76
+ image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
77
+
78
+ image_to_text("https://ankur3107.github.io/assets/images/image-captioning-example.png")
79
+
80
+ # [{'generated_text': 'a soccer game with a player jumping to catch the ball '}]
81
+
82
+
83
+ ```
84
+
85
+
86
+ # Contact for any help
87
+ * https://huggingface.co/ankur310794
88
+ * https://twitter.com/ankur310794
89
+ * http://github.com/ankur3107
90
+ * 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:d3b2e6104fe8d3408c415435b3f01012a718e1aeb1752e534a24b005239238a2
3
+ size 134
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