-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtest_instructblip.py
44 lines (35 loc) · 1.7 KB
/
test_instructblip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import torch
from transformers import CLIPImageProcessor
from .instruct_blip.models import load_model_and_preprocess
from .instruct_blip.models.eva_vit import convert_weights_to_fp16
from . import get_image
class TestInstructBLIP:
def __init__(self, device=None) -> None:
self.model, self.vis_processors, _ = load_model_and_preprocess(name="blip2_vicuna_instruct", model_type="vicuna7b", is_eval=True, device='cpu')
if device is not None:
self.move_to_device(device)
def move_to_device(self, device=None):
if device is not None and 'cuda' in device.type:
self.dtype = torch.float16
self.device = device
convert_weights_to_fp16(self.model.visual_encoder)
else:
self.dtype = torch.float32
self.device = 'cpu'
self.model.visual_encoder = self.model.visual_encoder.to(self.device, dtype=self.dtype)
self.model = self.model.to(self.device, dtype=self.dtype)
self.model.llm_model = self.model.llm_model.to(self.device, dtype=self.dtype)
@torch.no_grad()
def generate(self, image, question):
image = get_image(image)
image = self.vis_processors["eval"](image).unsqueeze(0).to(self.device)
output = self.model.generate({"image": image, "prompt": question})[0]
return output
@torch.no_grad()
def batch_generate(self, image_list, question_list):
imgs = [get_image(img) for img in image_list]
imgs = [self.vis_processors["eval"](x) for x in imgs]
imgs = torch.stack(imgs, dim=0).to(self.device)
prompts = question_list
output = self.model.generate({"image": imgs, "prompt": prompts})
return output