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