|
|
import torch |
|
|
import torch.nn as nn |
|
|
import sys |
|
|
|
|
|
|
|
|
sys.path.append("/gpfs/home/ym621/UniPointMap") |
|
|
import open_clip |
|
|
from transformers import LlavaForConditionalGeneration, AutoProcessor, AutoTokenizer, AutoConfig |
|
|
|
|
|
|
|
|
import torch.nn as nn |
|
|
from transformers.activations import GELUActivation |
|
|
|
|
|
class LlavaMultiModalProjector(nn.Module): |
|
|
def __init__(self, in_features: int, out_features: int): |
|
|
super().__init__() |
|
|
self.linear_1 = nn.Linear(in_features, out_features, bias=True) |
|
|
self.act = GELUActivation() |
|
|
self.linear_2 = nn.Linear(out_features, out_features, bias=True) |
|
|
|
|
|
def forward(self, x): |
|
|
x = self.linear_1(x) |
|
|
x = self.act(x) |
|
|
x = self.linear_2(x) |
|
|
return x |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vision_model, _, preprocess = open_clip.create_model_and_transforms( |
|
|
"ViT-B-32", pretrained="openai" |
|
|
) |
|
|
|
|
|
|
|
|
custom_vision_tower = vision_model.visual |
|
|
custom_hidden_size = custom_vision_tower.output_dim |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_id = "liuhaotian/llava-v1.5-7b" |
|
|
model = LlavaForConditionalGeneration.from_pretrained( |
|
|
model_id, |
|
|
torch_dtype=torch.float16, |
|
|
low_cpu_mem_usage=True, |
|
|
device_map="auto" |
|
|
) |
|
|
breakpoint() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model.vision_tower = custom_vision_tower |
|
|
|
|
|
|
|
|
llm_hidden_size = model.config.hidden_size |
|
|
model.multi_modal_projector = LlavaMultiModalProjector( |
|
|
in_features=512, |
|
|
out_features=4096 |
|
|
) |
|
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
|
config = AutoConfig.from_pretrained("liuhaotian/llava-v1.5-7b") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model.config.vision_config.hidden_size = custom_hidden_size |
|
|
model.config.vision_config.image_size = getattr(custom_vision_tower, "image_size", 224) |
|
|
model.config.vision_config.patch_size = getattr(custom_vision_tower, "patch_size", 32) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
image_processor = preprocess |
|
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("huggyllama/llama-7b", use_fast=False) |
|
|
processor = AutoProcessor.from_pretrained("liuhaotian/llava-v1.5-7b-pretrain") |
|
|
|
|
|
|
|
|
|
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
prompt = "USER: Describe this image in detail. ASSISTANT:" |
|
|
|
|
|
with torch.no_grad(): |
|
|
output_ids = model.generate(**inputs, max_new_tokens=200) |
|
|
print(processor.tokenizer.decode(output_ids[0], skip_special_tokens=True)) |
|
|
|