Spaces:
Building
Building
Delete .ipynb_checkpoints
Browse files
.ipynb_checkpoints/gradio_demo-checkpoint.ipynb
DELETED
@@ -1,294 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "code",
|
5 |
-
"execution_count": 2,
|
6 |
-
"id": "35d8939e-909d-45d8-bcf9-0ff1dccacfdf",
|
7 |
-
"metadata": {},
|
8 |
-
"outputs": [
|
9 |
-
{
|
10 |
-
"name": "stderr",
|
11 |
-
"output_type": "stream",
|
12 |
-
"text": [
|
13 |
-
"Some weights of the model checkpoint at bert-base-uncased were not used when initializing BertModel: ['bert.encoder.layer.6.output.LayerNorm.weight', 'bert.encoder.layer.6.attention.self.query.weight', 'bert.encoder.layer.3.attention.output.LayerNorm.bias', 'bert.encoder.layer.4.attention.self.value.bias', 'bert.encoder.layer.2.attention.self.value.bias', 'bert.encoder.layer.10.intermediate.dense.bias', 'bert.encoder.layer.3.intermediate.dense.bias', 'bert.encoder.layer.6.attention.self.value.weight', 'bert.encoder.layer.11.output.dense.bias', 'bert.encoder.layer.3.attention.self.value.bias', 'bert.encoder.layer.7.attention.self.value.bias', 'bert.encoder.layer.2.attention.output.dense.weight', 'bert.encoder.layer.11.attention.output.dense.weight', 'bert.encoder.layer.6.output.dense.bias', 'bert.encoder.layer.6.attention.output.dense.bias', 'bert.encoder.layer.4.output.LayerNorm.weight', 'bert.encoder.layer.9.output.dense.weight', 'bert.encoder.layer.9.attention.self.key.bias', 'bert.encoder.layer.3.attention.self.key.weight', 'bert.encoder.layer.3.intermediate.dense.weight', 'bert.encoder.layer.8.output.LayerNorm.weight', 'cls.seq_relationship.bias', 'bert.encoder.layer.6.attention.self.value.bias', 'bert.encoder.layer.10.output.LayerNorm.bias', 'bert.encoder.layer.10.attention.output.LayerNorm.bias', 'bert.encoder.layer.8.attention.self.key.bias', 'bert.encoder.layer.3.attention.self.query.weight', 'bert.encoder.layer.8.intermediate.dense.weight', 'bert.encoder.layer.8.attention.output.LayerNorm.bias', 'bert.encoder.layer.7.attention.output.dense.weight', 'bert.encoder.layer.9.attention.self.query.bias', 'bert.encoder.layer.2.output.dense.bias', 'bert.encoder.layer.6.attention.self.key.bias', 'bert.encoder.layer.4.attention.self.query.weight', 'bert.encoder.layer.2.attention.self.query.weight', 'bert.encoder.layer.11.attention.self.query.weight', 'bert.encoder.layer.3.attention.output.dense.weight', 'bert.encoder.layer.11.attention.output.LayerNorm.bias', 'bert.encoder.layer.10.attention.self.key.weight', 'bert.encoder.layer.3.attention.self.value.weight', 'bert.encoder.layer.5.attention.self.key.bias', 'bert.encoder.layer.5.intermediate.dense.bias', 'bert.encoder.layer.7.attention.self.key.weight', 'bert.encoder.layer.5.attention.self.value.weight', 'bert.encoder.layer.2.attention.output.dense.bias', 'bert.encoder.layer.2.output.dense.weight', 'bert.encoder.layer.6.attention.output.dense.weight', 'bert.encoder.layer.2.intermediate.dense.bias', 'bert.encoder.layer.9.attention.self.value.bias', 'bert.encoder.layer.6.intermediate.dense.bias', 'bert.encoder.layer.9.attention.output.dense.bias', 'bert.encoder.layer.7.attention.self.query.weight', 'bert.encoder.layer.8.attention.self.value.bias', 'bert.encoder.layer.4.attention.self.key.bias', 'bert.pooler.dense.bias', 'bert.encoder.layer.10.attention.output.dense.bias', 'bert.encoder.layer.5.output.LayerNorm.weight', 'cls.seq_relationship.weight', 'bert.encoder.layer.11.intermediate.dense.weight', 'bert.encoder.layer.2.attention.self.key.bias', 'bert.encoder.layer.10.attention.output.LayerNorm.weight', 'bert.encoder.layer.10.output.dense.bias', 'bert.encoder.layer.10.intermediate.dense.weight', 'bert.encoder.layer.4.intermediate.dense.weight', 'bert.encoder.layer.3.attention.self.key.bias', 'bert.encoder.layer.5.attention.self.query.weight', 'bert.encoder.layer.9.intermediate.dense.weight', 'bert.pooler.dense.weight', 'bert.encoder.layer.7.attention.output.LayerNorm.bias', 'bert.encoder.layer.11.output.LayerNorm.weight', 'bert.encoder.layer.5.attention.output.LayerNorm.bias', 'cls.predictions.transform.dense.weight', 'bert.encoder.layer.10.attention.self.value.bias', 'bert.encoder.layer.4.attention.self.query.bias', 'bert.encoder.layer.3.attention.self.query.bias', 'bert.encoder.layer.10.output.LayerNorm.weight', 'bert.encoder.layer.10.attention.self.key.bias', 'bert.encoder.layer.8.attention.self.value.weight', 'bert.encoder.layer.4.output.dense.bias', 'bert.encoder.layer.7.attention.self.key.bias', 'bert.encoder.layer.8.intermediate.dense.bias', 'bert.encoder.layer.7.intermediate.dense.weight', 'bert.encoder.layer.2.attention.self.key.weight', 'bert.encoder.layer.4.attention.output.dense.bias', 'bert.encoder.layer.6.output.dense.weight', 'bert.encoder.layer.8.attention.output.LayerNorm.weight', 'bert.encoder.layer.11.output.LayerNorm.bias', 'bert.encoder.layer.10.output.dense.weight', 'bert.encoder.layer.4.attention.output.LayerNorm.bias', 'bert.encoder.layer.11.output.dense.weight', 'bert.encoder.layer.8.output.dense.weight', 'bert.encoder.layer.5.attention.self.value.bias', 'bert.encoder.layer.4.intermediate.dense.bias', 'bert.encoder.layer.5.attention.self.key.weight', 'bert.encoder.layer.4.attention.self.key.weight', 'bert.encoder.layer.7.attention.self.query.bias', 'bert.encoder.layer.10.attention.self.query.weight', 'bert.encoder.layer.5.output.dense.bias', 'bert.encoder.layer.5.attention.output.dense.weight', 'bert.encoder.layer.7.output.dense.bias', 'bert.embeddings.token_type_embeddings.weight', 'bert.encoder.layer.8.output.dense.bias', 'bert.encoder.layer.7.attention.output.LayerNorm.weight', 'bert.encoder.layer.6.attention.self.key.weight', 'bert.encoder.layer.11.attention.output.LayerNorm.weight', 'bert.encoder.layer.7.output.LayerNorm.bias', 'bert.encoder.layer.9.attention.output.LayerNorm.weight', 'bert.encoder.layer.3.output.dense.bias', 'bert.encoder.layer.8.attention.self.query.bias', 'bert.encoder.layer.6.attention.self.query.bias', 'bert.encoder.layer.4.attention.output.dense.weight', 'bert.encoder.layer.6.intermediate.dense.weight', 'bert.encoder.layer.8.attention.output.dense.bias', 'bert.encoder.layer.10.attention.self.query.bias', 'bert.encoder.layer.8.attention.output.dense.weight', 'bert.encoder.layer.9.attention.output.dense.weight', 'bert.encoder.layer.5.output.dense.weight', 'cls.predictions.bias', 'cls.predictions.transform.LayerNorm.weight', 'bert.encoder.layer.9.attention.self.query.weight', 'bert.encoder.layer.2.attention.output.LayerNorm.bias', 'bert.encoder.layer.4.attention.self.value.weight', 'bert.encoder.layer.6.output.LayerNorm.bias', 'bert.encoder.layer.10.attention.output.dense.weight', 'bert.encoder.layer.5.attention.self.query.bias', 'bert.encoder.layer.3.output.dense.weight', 'bert.encoder.layer.2.output.LayerNorm.weight', 'bert.encoder.layer.4.output.LayerNorm.bias', 'bert.encoder.layer.9.attention.self.value.weight', 'bert.encoder.layer.6.attention.output.LayerNorm.bias', 'bert.encoder.layer.11.attention.output.dense.bias', 'bert.encoder.layer.2.attention.output.LayerNorm.weight', 'bert.encoder.layer.7.output.LayerNorm.weight', 'bert.encoder.layer.2.output.LayerNorm.bias', 'bert.encoder.layer.3.output.LayerNorm.bias', 'cls.predictions.decoder.weight', 'bert.encoder.layer.5.attention.output.LayerNorm.weight', 'bert.encoder.layer.2.intermediate.dense.weight', 'bert.encoder.layer.11.attention.self.key.weight', 'bert.encoder.layer.11.attention.self.value.weight', 'bert.encoder.layer.9.intermediate.dense.bias', 'bert.encoder.layer.11.intermediate.dense.bias', 'bert.encoder.layer.11.attention.self.key.bias', 'bert.encoder.layer.2.attention.self.value.weight', 'bert.encoder.layer.3.output.LayerNorm.weight', 'bert.encoder.layer.9.output.LayerNorm.bias', 'bert.encoder.layer.5.intermediate.dense.weight', 'bert.encoder.layer.8.output.LayerNorm.bias', 'bert.encoder.layer.9.output.LayerNorm.weight', 'bert.encoder.layer.7.attention.self.value.weight', 'bert.encoder.layer.9.output.dense.bias', 'bert.encoder.layer.7.intermediate.dense.bias', 'bert.encoder.layer.6.attention.output.LayerNorm.weight', 'bert.encoder.layer.8.attention.self.query.weight', 'bert.encoder.layer.9.attention.self.key.weight', 'bert.encoder.layer.4.output.dense.weight', 'bert.encoder.layer.2.attention.self.query.bias', 'bert.encoder.layer.9.attention.output.LayerNorm.bias', 'bert.encoder.layer.3.attention.output.dense.bias', 'bert.encoder.layer.7.output.dense.weight', 'bert.encoder.layer.10.attention.self.value.weight', 'bert.encoder.layer.8.attention.self.key.weight', 'bert.encoder.layer.11.attention.self.value.bias', 'cls.predictions.transform.LayerNorm.bias', 'bert.encoder.layer.3.attention.output.LayerNorm.weight', 'bert.encoder.layer.5.attention.output.dense.bias', 'bert.encoder.layer.4.attention.output.LayerNorm.weight', 'bert.encoder.layer.11.attention.self.query.bias', 'cls.predictions.transform.dense.bias', 'bert.encoder.layer.7.attention.output.dense.bias', 'bert.encoder.layer.5.output.LayerNorm.bias']\n",
|
14 |
-
"- This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
|
15 |
-
"- This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n",
|
16 |
-
"Some weights of BertModel were not initialized from the model checkpoint at bert-base-uncased and are newly initialized: ['bert.encoder.layer.1.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.0.crossattention.self.query.bias', 'bert.encoder.layer.0.crossattention.output.dense.bias', 'bert.encoder.layer.1.crossattention.self.query.weight', 'bert.encoder.layer.0.crossattention.output.dense.weight', 'bert.encoder.layer.1.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.0.crossattention.self.key.weight', 'bert.encoder.layer.1.crossattention.output.dense.weight', 'bert.encoder.layer.0.crossattention.self.query.weight', 'bert.encoder.layer.0.crossattention.output.LayerNorm.bias', 'bert.encoder.layer.1.crossattention.self.key.weight', 'bert.encoder.layer.0.crossattention.self.key.bias', 'bert.encoder.layer.1.crossattention.output.dense.bias', 'bert.encoder.layer.0.crossattention.output.LayerNorm.weight', 'bert.encoder.layer.1.crossattention.self.value.weight', 'bert.encoder.layer.1.crossattention.self.value.bias', 'bert.encoder.layer.0.crossattention.self.value.bias', 'bert.encoder.layer.0.crossattention.self.value.weight', 'bert.encoder.layer.1.crossattention.self.query.bias', 'bert.encoder.layer.1.crossattention.self.key.bias']\n",
|
17 |
-
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
|
18 |
-
]
|
19 |
-
},
|
20 |
-
{
|
21 |
-
"name": "stdout",
|
22 |
-
"output_type": "stream",
|
23 |
-
"text": [
|
24 |
-
"/encoder/layer/0/crossattention/self/query is tied\n",
|
25 |
-
"/encoder/layer/0/crossattention/self/key is tied\n",
|
26 |
-
"/encoder/layer/0/crossattention/self/value is tied\n",
|
27 |
-
"/encoder/layer/0/crossattention/output/dense is tied\n",
|
28 |
-
"/encoder/layer/0/crossattention/output/LayerNorm is tied\n",
|
29 |
-
"/encoder/layer/0/intermediate/dense is tied\n",
|
30 |
-
"/encoder/layer/0/output/dense is tied\n",
|
31 |
-
"/encoder/layer/0/output/LayerNorm is tied\n",
|
32 |
-
"/encoder/layer/1/crossattention/self/query is tied\n",
|
33 |
-
"/encoder/layer/1/crossattention/self/key is tied\n",
|
34 |
-
"/encoder/layer/1/crossattention/self/value is tied\n",
|
35 |
-
"/encoder/layer/1/crossattention/output/dense is tied\n",
|
36 |
-
"/encoder/layer/1/crossattention/output/LayerNorm is tied\n",
|
37 |
-
"/encoder/layer/1/intermediate/dense is tied\n",
|
38 |
-
"/encoder/layer/1/output/dense is tied\n",
|
39 |
-
"/encoder/layer/1/output/LayerNorm is tied\n",
|
40 |
-
"--------------\n",
|
41 |
-
"/home/notebook/code/personal/S9049611/BLIP/output/blip_tagtotext_14m/blip_tagtotext_encoderdiv_tar_random_swin/caption_coco_finetune_tagparse_tagfinetune_threshold075_bceloss_tagsingle_5e6_epoch19_negative_1_05_pos_1_10/checkpoint_05.pth\n",
|
42 |
-
"--------------\n",
|
43 |
-
"load checkpoint from /home/notebook/code/personal/S9049611/BLIP/output/blip_tagtotext_14m/blip_tagtotext_encoderdiv_tar_random_swin/caption_coco_finetune_tagparse_tagfinetune_threshold075_bceloss_tagsingle_5e6_epoch19_negative_1_05_pos_1_10/checkpoint_05.pth\n",
|
44 |
-
"vit: swin_b\n",
|
45 |
-
"msg_v2 _IncompatibleKeys(missing_keys=['visual_encoder.layers.0.blocks.0.attn.relative_position_index', 'visual_encoder.layers.0.blocks.1.attn_mask', 'visual_encoder.layers.0.blocks.1.attn.relative_position_index', 'visual_encoder.layers.1.blocks.0.attn.relative_position_index', 'visual_encoder.layers.1.blocks.1.attn_mask', 'visual_encoder.layers.1.blocks.1.attn.relative_position_index', 'visual_encoder.layers.2.blocks.0.attn.relative_position_index', 'visual_encoder.layers.2.blocks.1.attn_mask', 'visual_encoder.layers.2.blocks.1.attn.relative_position_index', 'visual_encoder.layers.2.blocks.2.attn.relative_position_index', 'visual_encoder.layers.2.blocks.3.attn_mask', 'visual_encoder.layers.2.blocks.3.attn.relative_position_index', 'visual_encoder.layers.2.blocks.4.attn.relative_position_index', 'visual_encoder.layers.2.blocks.5.attn_mask', 'visual_encoder.layers.2.blocks.5.attn.relative_position_index', 'visual_encoder.layers.2.blocks.6.attn.relative_position_index', 'visual_encoder.layers.2.blocks.7.attn_mask', 'visual_encoder.layers.2.blocks.7.attn.relative_position_index', 'visual_encoder.layers.2.blocks.8.attn.relative_position_index', 'visual_encoder.layers.2.blocks.9.attn_mask', 'visual_encoder.layers.2.blocks.9.attn.relative_position_index', 'visual_encoder.layers.2.blocks.10.attn.relative_position_index', 'visual_encoder.layers.2.blocks.11.attn_mask', 'visual_encoder.layers.2.blocks.11.attn.relative_position_index', 'visual_encoder.layers.2.blocks.12.attn.relative_position_index', 'visual_encoder.layers.2.blocks.13.attn_mask', 'visual_encoder.layers.2.blocks.13.attn.relative_position_index', 'visual_encoder.layers.2.blocks.14.attn.relative_position_index', 'visual_encoder.layers.2.blocks.15.attn_mask', 'visual_encoder.layers.2.blocks.15.attn.relative_position_index', 'visual_encoder.layers.2.blocks.16.attn.relative_position_index', 'visual_encoder.layers.2.blocks.17.attn_mask', 'visual_encoder.layers.2.blocks.17.attn.relative_position_index', 'visual_encoder.layers.3.blocks.0.attn.relative_position_index', 'visual_encoder.layers.3.blocks.1.attn.relative_position_index'], unexpected_keys=[])\n"
|
46 |
-
]
|
47 |
-
}
|
48 |
-
],
|
49 |
-
"source": [
|
50 |
-
"from PIL import Image\n",
|
51 |
-
"import requests\n",
|
52 |
-
"import torch\n",
|
53 |
-
"from torchvision import transforms\n",
|
54 |
-
"from torchvision.transforms.functional import InterpolationMode\n",
|
55 |
-
"import ruamel_yaml as yaml\n",
|
56 |
-
"from models.tag2text import tag2text_caption\n",
|
57 |
-
"\n",
|
58 |
-
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
|
59 |
-
"\n",
|
60 |
-
"\n",
|
61 |
-
"\n",
|
62 |
-
"import gradio as gr\n",
|
63 |
-
"\n",
|
64 |
-
"image_size = 384\n",
|
65 |
-
"\n",
|
66 |
-
"\n",
|
67 |
-
"normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],\n",
|
68 |
-
" std=[0.229, 0.224, 0.225])\n",
|
69 |
-
"transform = transforms.Compose([transforms.Resize((image_size, image_size)),transforms.ToTensor(),normalize])\n",
|
70 |
-
"\n",
|
71 |
-
"\n",
|
72 |
-
"\n",
|
73 |
-
"#######Swin Version\n",
|
74 |
-
"pretrained = '/home/notebook/code/personal/S9049611/BLIP/output/blip_tagtotext_14m/blip_tagtotext_encoderdiv_tar_random_swin/caption_coco_finetune_tagparse_tagfinetune_threshold075_bceloss_tagsingle_5e6_epoch19_negative_1_05_pos_1_10/checkpoint_05.pth'\n",
|
75 |
-
"\n",
|
76 |
-
"config_file = 'configs/tag2text_caption.yaml'\n",
|
77 |
-
"config = yaml.load(open(config_file, 'r'), Loader=yaml.Loader)\n",
|
78 |
-
"\n",
|
79 |
-
"\n",
|
80 |
-
"model = tag2text_caption(pretrained=pretrained, image_size=image_size, vit=config['vit'], \n",
|
81 |
-
" vit_grad_ckpt=config['vit_grad_ckpt'], vit_ckpt_layer=config['vit_ckpt_layer'],\n",
|
82 |
-
" prompt=config['prompt'],config=config,threshold = 0.75 )\n",
|
83 |
-
"\n",
|
84 |
-
"model.eval()\n",
|
85 |
-
"model = model.to(device)\n",
|
86 |
-
"\n",
|
87 |
-
"\n"
|
88 |
-
]
|
89 |
-
},
|
90 |
-
{
|
91 |
-
"cell_type": "code",
|
92 |
-
"execution_count": 4,
|
93 |
-
"id": "9772dc6f-680d-45a7-b39c-23770eb5258e",
|
94 |
-
"metadata": {},
|
95 |
-
"outputs": [
|
96 |
-
{
|
97 |
-
"name": "stdout",
|
98 |
-
"output_type": "stream",
|
99 |
-
"text": [
|
100 |
-
"Running on local URL: http://127.0.0.1:7860\n",
|
101 |
-
"Running on public URL: https://202e6e6a-b3d9-4c97.gradio.live\n",
|
102 |
-
"\n",
|
103 |
-
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n"
|
104 |
-
]
|
105 |
-
},
|
106 |
-
{
|
107 |
-
"data": {
|
108 |
-
"text/html": [
|
109 |
-
"<div><iframe src=\"https://202e6e6a-b3d9-4c97.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
110 |
-
],
|
111 |
-
"text/plain": [
|
112 |
-
"<IPython.core.display.HTML object>"
|
113 |
-
]
|
114 |
-
},
|
115 |
-
"metadata": {},
|
116 |
-
"output_type": "display_data"
|
117 |
-
},
|
118 |
-
{
|
119 |
-
"data": {
|
120 |
-
"text/plain": []
|
121 |
-
},
|
122 |
-
"execution_count": 4,
|
123 |
-
"metadata": {},
|
124 |
-
"output_type": "execute_result"
|
125 |
-
},
|
126 |
-
{
|
127 |
-
"name": "stdout",
|
128 |
-
"output_type": "stream",
|
129 |
-
"text": [
|
130 |
-
"<class 'PIL.Image.Image'>\n",
|
131 |
-
"<class 'PIL.Image.Image'>\n"
|
132 |
-
]
|
133 |
-
}
|
134 |
-
],
|
135 |
-
"source": [
|
136 |
-
"\n",
|
137 |
-
"def inference(raw_image, model_n, input_tag, strategy):\n",
|
138 |
-
" if model_n == 'Image Captioning':\n",
|
139 |
-
" raw_image = raw_image.resize((image_size, image_size))\n",
|
140 |
-
" print(type(raw_image))\n",
|
141 |
-
" image = transform(raw_image).unsqueeze(0).to(device) \n",
|
142 |
-
" model.threshold = 0.75\n",
|
143 |
-
" if input_tag == '' or input_tag == 'none' or input_tag == 'None':\n",
|
144 |
-
" input_tag_list = None\n",
|
145 |
-
" else:\n",
|
146 |
-
" input_tag_list = []\n",
|
147 |
-
" input_tag_list.append(input_tag.replace(',',' | '))\n",
|
148 |
-
" # print(input_tag_list)\n",
|
149 |
-
" with torch.no_grad():\n",
|
150 |
-
" if strategy == \"Beam search\":\n",
|
151 |
-
" \n",
|
152 |
-
"\n",
|
153 |
-
" caption, tag_predict = model.generate(image,tag_input = input_tag_list, return_tag_predict = True)\n",
|
154 |
-
" if input_tag_list == None:\n",
|
155 |
-
" tag_1 = tag_predict\n",
|
156 |
-
" tag_2 = ['none']\n",
|
157 |
-
" else:\n",
|
158 |
-
" _, tag_1 = model.generate(image,tag_input = None, return_tag_predict = True)\n",
|
159 |
-
" tag_2 = tag_predict\n",
|
160 |
-
"\n",
|
161 |
-
" else:\n",
|
162 |
-
"\n",
|
163 |
-
" caption,tag_predict = model.generate(image, tag_input = input_tag_list,sample=True, top_p=0.9, max_length=20, min_length=5, return_tag_predict = True)\n",
|
164 |
-
" if input_tag_list == None:\n",
|
165 |
-
" tag_1 = tag_predict\n",
|
166 |
-
" tag_2 = ['none']\n",
|
167 |
-
" else:\n",
|
168 |
-
" _, tag_1 = model.generate(image,tag_input = None, return_tag_predict = True)\n",
|
169 |
-
" tag_2 = tag_predict\n",
|
170 |
-
" # return 'Caption: '+caption[0], 'Identified Tags:' + tag_predict[0]\n",
|
171 |
-
" # return tag_predict[0],caption[0]\n",
|
172 |
-
" return tag_1[0],tag_2[0],caption[0]\n",
|
173 |
-
" \n",
|
174 |
-
" # return 'caption: '+caption[0], tag_predict[0]\n",
|
175 |
-
"\n",
|
176 |
-
" else: \n",
|
177 |
-
" image_vq = transform_vq(raw_image).unsqueeze(0).to(device) \n",
|
178 |
-
" with torch.no_grad():\n",
|
179 |
-
" answer = model_vq(image_vq, question, train=False, inference='generate') \n",
|
180 |
-
" return 'answer: '+answer[0]\n",
|
181 |
-
" \n",
|
182 |
-
"inputs = [gr.inputs.Image(type='pil'),gr.inputs.Radio(choices=['Image Captioning'], type=\"value\", default=\"Image Captioning\", label=\"Task\"),gr.inputs.Textbox(lines=2, label=\"User Identified Tags (Optional, Enter with commas)\"),gr.inputs.Radio(choices=['Beam search','Nucleus sampling'], type=\"value\", default=\"Beam search\", label=\"Caption Decoding Strategy\")]\n",
|
183 |
-
"\n",
|
184 |
-
"# outputs = gr.outputs.Textbox(label=\"Output\")\n",
|
185 |
-
"# outputs = [gr.outputs.Textbox(label=\"Image Caption\"),gr.outputs.Textbox(label=\"Identified Tags\")]\n",
|
186 |
-
"outputs = [gr.outputs.Textbox(label=\"Model Identified Tags\"),gr.outputs.Textbox(label=\"User Identified Tags\"), gr.outputs.Textbox(label=\"Image Caption\") ]\n",
|
187 |
-
"\n",
|
188 |
-
"title = \"Tag2Text\"\n",
|
189 |
-
"\n",
|
190 |
-
"description = \"Gradio demo for Tag2Text: Guiding Language-Image Model via Image Tagging (Fudan University, OPPO Research Institute, International Digital Economy Academy).\"\n",
|
191 |
-
"\n",
|
192 |
-
"article = \"<p style='text-align: center'><a href='' target='_blank'>Tag2Text: Guiding Language-Image Model via Image Tagging</a> | <a href='' target='_blank'>Github Repo</a></p>\"\n",
|
193 |
-
"\n",
|
194 |
-
"demo = gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=[['images/COCO_val2014_000000551338.jpg',\"Image Captioning\",\"none\",\"Beam search\"], \n",
|
195 |
-
" ['images/COCO_val2014_000000551338.jpg',\"Image Captioning\",\"fence, sky\",\"Beam search\"],\n",
|
196 |
-
" # ['images/COCO_val2014_000000551338.jpg',\"Image Captioning\",\"grass\",\"Beam search\"],\n",
|
197 |
-
" ['images/COCO_val2014_000000483108.jpg',\"Image Captioning\",\"none\",\"Beam search\"],\n",
|
198 |
-
" ['images/COCO_val2014_000000483108.jpg',\"Image Captioning\",\"electric cable\",\"Beam search\"],\n",
|
199 |
-
" # ['images/COCO_val2014_000000483108.jpg',\"Image Captioning\",\"sky, train\",\"Beam search\"],\n",
|
200 |
-
" ['images/COCO_val2014_000000483108.jpg',\"Image Captioning\",\"track, train\",\"Beam search\"] , \n",
|
201 |
-
" ['images/COCO_val2014_000000483108.jpg',\"Image Captioning\",\"grass\",\"Beam search\"] \n",
|
202 |
-
" ])\n",
|
203 |
-
"\n",
|
204 |
-
"\n",
|
205 |
-
"demo.launch(share=True)"
|
206 |
-
]
|
207 |
-
},
|
208 |
-
{
|
209 |
-
"cell_type": "code",
|
210 |
-
"execution_count": null,
|
211 |
-
"id": "0da1f11b-e737-47a9-9b07-4e00c0835f63",
|
212 |
-
"metadata": {},
|
213 |
-
"outputs": [],
|
214 |
-
"source": []
|
215 |
-
},
|
216 |
-
{
|
217 |
-
"cell_type": "code",
|
218 |
-
"execution_count": null,
|
219 |
-
"id": "73a4bb88-4200-4853-b1ba-34f0d4b6dc34",
|
220 |
-
"metadata": {},
|
221 |
-
"outputs": [],
|
222 |
-
"source": []
|
223 |
-
},
|
224 |
-
{
|
225 |
-
"cell_type": "code",
|
226 |
-
"execution_count": null,
|
227 |
-
"id": "3340a61f-c6bc-4ead-87ea-b26aa97b7a68",
|
228 |
-
"metadata": {},
|
229 |
-
"outputs": [],
|
230 |
-
"source": []
|
231 |
-
},
|
232 |
-
{
|
233 |
-
"cell_type": "code",
|
234 |
-
"execution_count": null,
|
235 |
-
"id": "d49e3de4-c3f7-4835-90eb-d0d013fc0ffb",
|
236 |
-
"metadata": {},
|
237 |
-
"outputs": [],
|
238 |
-
"source": []
|
239 |
-
},
|
240 |
-
{
|
241 |
-
"cell_type": "code",
|
242 |
-
"execution_count": null,
|
243 |
-
"id": "205e0317-1701-4afd-8d67-bedb6959f350",
|
244 |
-
"metadata": {},
|
245 |
-
"outputs": [],
|
246 |
-
"source": []
|
247 |
-
},
|
248 |
-
{
|
249 |
-
"cell_type": "code",
|
250 |
-
"execution_count": null,
|
251 |
-
"id": "bf5301a5-80c5-4e44-835e-0160a97fef66",
|
252 |
-
"metadata": {},
|
253 |
-
"outputs": [],
|
254 |
-
"source": []
|
255 |
-
},
|
256 |
-
{
|
257 |
-
"cell_type": "code",
|
258 |
-
"execution_count": null,
|
259 |
-
"id": "f63d7a06-7625-4e1c-855d-177971217a0d",
|
260 |
-
"metadata": {},
|
261 |
-
"outputs": [],
|
262 |
-
"source": []
|
263 |
-
},
|
264 |
-
{
|
265 |
-
"cell_type": "code",
|
266 |
-
"execution_count": null,
|
267 |
-
"id": "c929e566-1a6e-4280-96eb-c434ef9a35d0",
|
268 |
-
"metadata": {},
|
269 |
-
"outputs": [],
|
270 |
-
"source": []
|
271 |
-
}
|
272 |
-
],
|
273 |
-
"metadata": {
|
274 |
-
"kernelspec": {
|
275 |
-
"display_name": "Python 3 (ipykernel)",
|
276 |
-
"language": "python",
|
277 |
-
"name": "python3"
|
278 |
-
},
|
279 |
-
"language_info": {
|
280 |
-
"codemirror_mode": {
|
281 |
-
"name": "ipython",
|
282 |
-
"version": 3
|
283 |
-
},
|
284 |
-
"file_extension": ".py",
|
285 |
-
"mimetype": "text/x-python",
|
286 |
-
"name": "python",
|
287 |
-
"nbconvert_exporter": "python",
|
288 |
-
"pygments_lexer": "ipython3",
|
289 |
-
"version": "3.7.12"
|
290 |
-
}
|
291 |
-
},
|
292 |
-
"nbformat": 4,
|
293 |
-
"nbformat_minor": 5
|
294 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.ipynb_checkpoints/upload-checkpoint.ipynb
DELETED
@@ -1,6 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [],
|
3 |
-
"metadata": {},
|
4 |
-
"nbformat": 4,
|
5 |
-
"nbformat_minor": 5
|
6 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|