Srujan111 commited on
Commit
c3acae8
1 Parent(s): 88b67c5

Upload 9 files

Browse files
Running_code.ipynb ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 38,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stderr",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "c:\\Users\\Srujan Jujare\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
13
+ " from .autonotebook import tqdm as notebook_tqdm\n"
14
+ ]
15
+ }
16
+ ],
17
+ "source": [
18
+ "from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer\n",
19
+ "import torch\n",
20
+ "from PIL import Image"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 39,
26
+ "metadata": {},
27
+ "outputs": [
28
+ {
29
+ "data": {
30
+ "text/plain": [
31
+ "VisionEncoderDecoderModel(\n",
32
+ " (encoder): ViTModel(\n",
33
+ " (embeddings): ViTEmbeddings(\n",
34
+ " (patch_embeddings): ViTPatchEmbeddings(\n",
35
+ " (projection): Conv2d(3, 768, kernel_size=(16, 16), stride=(16, 16))\n",
36
+ " )\n",
37
+ " (dropout): Dropout(p=0.0, inplace=False)\n",
38
+ " )\n",
39
+ " (encoder): ViTEncoder(\n",
40
+ " (layer): ModuleList(\n",
41
+ " (0-11): 12 x ViTLayer(\n",
42
+ " (attention): ViTAttention(\n",
43
+ " (attention): ViTSelfAttention(\n",
44
+ " (query): Linear(in_features=768, out_features=768, bias=True)\n",
45
+ " (key): Linear(in_features=768, out_features=768, bias=True)\n",
46
+ " (value): Linear(in_features=768, out_features=768, bias=True)\n",
47
+ " (dropout): Dropout(p=0.0, inplace=False)\n",
48
+ " )\n",
49
+ " (output): ViTSelfOutput(\n",
50
+ " (dense): Linear(in_features=768, out_features=768, bias=True)\n",
51
+ " (dropout): Dropout(p=0.0, inplace=False)\n",
52
+ " )\n",
53
+ " )\n",
54
+ " (intermediate): ViTIntermediate(\n",
55
+ " (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
56
+ " (intermediate_act_fn): GELUActivation()\n",
57
+ " )\n",
58
+ " (output): ViTOutput(\n",
59
+ " (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
60
+ " (dropout): Dropout(p=0.0, inplace=False)\n",
61
+ " )\n",
62
+ " (layernorm_before): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
63
+ " (layernorm_after): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
64
+ " )\n",
65
+ " )\n",
66
+ " )\n",
67
+ " (layernorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)\n",
68
+ " (pooler): ViTPooler(\n",
69
+ " (dense): Linear(in_features=768, out_features=768, bias=True)\n",
70
+ " (activation): Tanh()\n",
71
+ " )\n",
72
+ " )\n",
73
+ " (decoder): GPT2LMHeadModel(\n",
74
+ " (transformer): GPT2Model(\n",
75
+ " (wte): Embedding(50257, 768)\n",
76
+ " (wpe): Embedding(1024, 768)\n",
77
+ " (drop): Dropout(p=0.1, inplace=False)\n",
78
+ " (h): ModuleList(\n",
79
+ " (0-11): 12 x GPT2Block(\n",
80
+ " (ln_1): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
81
+ " (attn): GPT2Attention(\n",
82
+ " (c_attn): Conv1D()\n",
83
+ " (c_proj): Conv1D()\n",
84
+ " (attn_dropout): Dropout(p=0.1, inplace=False)\n",
85
+ " (resid_dropout): Dropout(p=0.1, inplace=False)\n",
86
+ " )\n",
87
+ " (ln_2): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
88
+ " (crossattention): GPT2Attention(\n",
89
+ " (c_attn): Conv1D()\n",
90
+ " (q_attn): Conv1D()\n",
91
+ " (c_proj): Conv1D()\n",
92
+ " (attn_dropout): Dropout(p=0.1, inplace=False)\n",
93
+ " (resid_dropout): Dropout(p=0.1, inplace=False)\n",
94
+ " )\n",
95
+ " (ln_cross_attn): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
96
+ " (mlp): GPT2MLP(\n",
97
+ " (c_fc): Conv1D()\n",
98
+ " (c_proj): Conv1D()\n",
99
+ " (act): NewGELUActivation()\n",
100
+ " (dropout): Dropout(p=0.1, inplace=False)\n",
101
+ " )\n",
102
+ " )\n",
103
+ " )\n",
104
+ " (ln_f): LayerNorm((768,), eps=1e-05, elementwise_affine=True)\n",
105
+ " )\n",
106
+ " (lm_head): Linear(in_features=768, out_features=50257, bias=False)\n",
107
+ " )\n",
108
+ ")"
109
+ ]
110
+ },
111
+ "execution_count": 39,
112
+ "metadata": {},
113
+ "output_type": "execute_result"
114
+ }
115
+ ],
116
+ "source": [
117
+ "model = VisionEncoderDecoderModel.from_pretrained(\"nlpconnect/vit-gpt2-image-captioning\")\n",
118
+ "feature_extractor = ViTImageProcessor.from_pretrained(\"nlpconnect/vit-gpt2-image-captioning\")\n",
119
+ "tokenizer = AutoTokenizer.from_pretrained(\"nlpconnect/vit-gpt2-image-captioning\")\n",
120
+ "\n",
121
+ "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
122
+ "model.to(device)\n"
123
+ ]
124
+ },
125
+ {
126
+ "cell_type": "code",
127
+ "execution_count": 40,
128
+ "metadata": {},
129
+ "outputs": [],
130
+ "source": [
131
+ "max_length = 16\n",
132
+ "num_beams = 4\n",
133
+ "gen_kwargs = {\"max_length\": max_length, \"num_beams\": num_beams}\n",
134
+ "def predict_step(image_paths):\n",
135
+ " images = []\n",
136
+ " for image_path in image_paths:\n",
137
+ " i_image = Image.open(image_path)\n",
138
+ " if i_image.mode != \"RGB\":\n",
139
+ " i_image = i_image.convert(mode=\"RGB\")\n",
140
+ "\n",
141
+ " images.append(i_image)\n",
142
+ "\n",
143
+ " pixel_values = feature_extractor(images=images, return_tensors=\"pt\").pixel_values\n",
144
+ " pixel_values = pixel_values.to(device)\n",
145
+ "\n",
146
+ " output_ids = model.generate(pixel_values, **gen_kwargs)\n",
147
+ "\n",
148
+ " preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)\n",
149
+ " preds = [pred.strip() for pred in preds]\n",
150
+ " return preds"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": 41,
156
+ "metadata": {},
157
+ "outputs": [
158
+ {
159
+ "name": "stderr",
160
+ "output_type": "stream",
161
+ "text": [
162
+ "We strongly recommend passing in an `attention_mask` since your input_ids may be padded. See https://huggingface.co/docs/transformers/troubleshooting#incorrect-output-when-padding-tokens-arent-masked.\n",
163
+ "You may ignore this warning if your `pad_token_id` (50256) is identical to the `bos_token_id` (50256), `eos_token_id` (50256), or the `sep_token_id` (None), and your input is not padded.\n"
164
+ ]
165
+ },
166
+ {
167
+ "data": {
168
+ "text/plain": [
169
+ "['a clock on a dashboard of a car']"
170
+ ]
171
+ },
172
+ "execution_count": 41,
173
+ "metadata": {},
174
+ "output_type": "execute_result"
175
+ }
176
+ ],
177
+ "source": [
178
+ "predict_step(['D:\\\\Validation\\\\Class 2\\\\i17.jpg'])"
179
+ ]
180
+ }
181
+ ],
182
+ "metadata": {
183
+ "kernelspec": {
184
+ "display_name": "Python 3",
185
+ "language": "python",
186
+ "name": "python3"
187
+ },
188
+ "language_info": {
189
+ "codemirror_mode": {
190
+ "name": "ipython",
191
+ "version": 3
192
+ },
193
+ "file_extension": ".py",
194
+ "mimetype": "text/x-python",
195
+ "name": "python",
196
+ "nbconvert_exporter": "python",
197
+ "pygments_lexer": "ipython3",
198
+ "version": "3.11.5"
199
+ },
200
+ "orig_nbformat": 4
201
+ },
202
+ "nbformat": 4,
203
+ "nbformat_minor": 2
204
+ }
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