doevent commited on
Commit
3f9b2ec
1 Parent(s): adbdf8b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import requests
3
+ import torch
4
+ from torchvision import transforms
5
+ from torchvision.transforms.functional import InterpolationMode
6
+
7
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
8
+
9
+
10
+
11
+
12
+
13
+ import gradio as gr
14
+
15
+ from models.blip import blip_decoder
16
+
17
+ image_size = 384
18
+ transform = transforms.Compose([
19
+ transforms.Resize((image_size,image_size),interpolation=InterpolationMode.BICUBIC),
20
+ transforms.ToTensor(),
21
+ transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711))
22
+ ])
23
+
24
+ model_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model_large_caption.pth'
25
+
26
+ model = blip_decoder(pretrained=model_url, image_size=384, vit='large')
27
+ model.eval()
28
+ model = model.to(device)
29
+
30
+
31
+ from models.blip_vqa import blip_vqa
32
+
33
+ image_size_vq = 480
34
+ transform_vq = transforms.Compose([
35
+ transforms.Resize((image_size_vq,image_size_vq),interpolation=InterpolationMode.BICUBIC),
36
+ transforms.ToTensor(),
37
+ transforms.Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711))
38
+ ])
39
+
40
+ model_url_vq = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/models/model*_vqa.pth'
41
+
42
+ model_vq = blip_vqa(pretrained=model_url_vq, image_size=480, vit='base')
43
+ model_vq.eval()
44
+ model_vq = model_vq.to(device)
45
+
46
+
47
+
48
+ def inference(raw_image, model_n, question, strategy):
49
+ if model_n == 'Image Captioning':
50
+ image = transform(raw_image).unsqueeze(0).to(device)
51
+ with torch.no_grad():
52
+ if strategy == "Beam search":
53
+ caption = model.generate(image, sample=False, num_beams=3, max_length=20, min_length=5)
54
+ else:
55
+ caption = model.generate(image, sample=True, top_p=0.9, max_length=20, min_length=5)
56
+ return 'caption: '+caption[0]
57
+
58
+ else:
59
+ image_vq = transform_vq(raw_image).unsqueeze(0).to(device)
60
+ with torch.no_grad():
61
+ answer = model_vq(image_vq, question, train=False, inference='generate')
62
+ return 'answer: '+answer[0]
63
+
64
+ inputs = [gr.inputs.Image(type='pil'),gr.inputs.Radio(choices=['Image Captioning',"Visual Question Answering"], type="value", default="Image Captioning", label="Task"),gr.inputs.Textbox(lines=2, label="Question"),gr.inputs.Radio(choices=['Beam search','Nucleus sampling'], type="value", default="Nucleus sampling", label="Caption Decoding Strategy")]
65
+ outputs = gr.outputs.Textbox(label="Output")
66
+
67
+ title = "BLIP"
68
+
69
+ description = "Gradio demo for BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation (Salesforce Research). To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
70
+
71
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2201.12086' target='_blank'>BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation</a> | <a href='https://github.com/salesforce/BLIP' target='_blank'>Github Repo</a></p>"
72
+
73
+
74
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=[['starrynight.jpeg',"Image Captioning","None","Nucleus sampling"]]).launch(enable_queue=True)